3 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
4 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
5 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
6 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
7 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
8 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
9 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
10 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
11 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
12 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
13 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
15 * This software consists of voluntary contributions made by many individuals
16 * and is licensed under the MIT license. For more information, see
17 * <http://www.doctrine-project.org>.
20 namespace Doctrine\ORM\Tools;
22 use Doctrine\Common\ClassLoader;
23 use Doctrine\Common\Cache\Cache;
24 use Doctrine\Common\Cache\ArrayCache;
25 use Doctrine\ORM\Configuration;
26 use Doctrine\ORM\Mapping\Driver\XmlDriver;
27 use Doctrine\ORM\Mapping\Driver\YamlDriver;
30 * Convenience class for setting up Doctrine from different installations and configurations.
32 * @author Benjamin Eberlei <kontakt@beberlei.de>
37 * Use this method to register all autoloaders for a setup where Doctrine is checked out from
38 * its github repository at {@link http://github.com/doctrine/doctrine2}
40 * @param string $gitCheckoutRootPath
43 static public function registerAutoloadGit($gitCheckoutRootPath)
45 if (!class_exists('Doctrine\Common\ClassLoader', false)) {
46 require_once $gitCheckoutRootPath . "/lib/vendor/doctrine-common/lib/Doctrine/Common/ClassLoader.php";
49 $loader = new ClassLoader("Doctrine\Common", $gitCheckoutRootPath . "/lib/vendor/doctrine-common/lib");
52 $loader = new ClassLoader("Doctrine\DBAL", $gitCheckoutRootPath . "/lib/vendor/doctrine-dbal/lib");
55 $loader = new ClassLoader("Doctrine\ORM", $gitCheckoutRootPath . "/lib");
58 $loader = new ClassLoader("Symfony\Component", $gitCheckoutRootPath . "/lib/vendor");
63 * Use this method to register all autoloaders for a setup where Doctrine is installed
64 * though {@link http://pear.doctrine-project.org}.
68 static public function registerAutoloadPEAR()
70 if (!class_exists('Doctrine\Common\ClassLoader', false)) {
71 require_once "Doctrine/Common/ClassLoader.php";
74 $loader = new ClassLoader("Doctrine");
77 $parts = explode(PATH_SEPARATOR, get_include_path());
79 foreach ($parts as $includePath) {
80 if ($includePath != "." && file_exists($includePath . "/Doctrine")) {
81 $loader = new ClassLoader("Symfony\Component", $includePath . "/Doctrine");
89 * Use this method to register all autoloads for a downloaded Doctrine library.
90 * Pick the directory the library was uncompressed into.
92 * @param string $directory
94 static public function registerAutoloadDirectory($directory)
96 if (!class_exists('Doctrine\Common\ClassLoader', false)) {
97 require_once $directory . "/Doctrine/Common/ClassLoader.php";
100 $loader = new ClassLoader("Doctrine", $directory);
103 $loader = new ClassLoader("Symfony\Component", $directory . "/Doctrine");
108 * Create a configuration with an annotation metadata driver.
110 * @param array $paths
111 * @param boolean $isDevMode
112 * @param string $proxyDir
113 * @param Cache $cache
114 * @param bool $useSimpleAnnotationReader
115 * @return Configuration
117 static public function createAnnotationMetadataConfiguration(array $paths, $isDevMode = false, $proxyDir = null, Cache $cache = null, $useSimpleAnnotationReader = true)
119 $config = self::createConfiguration($isDevMode, $proxyDir, $cache);
120 $config->setMetadataDriverImpl($config->newDefaultAnnotationDriver($paths, $useSimpleAnnotationReader));
125 * Create a configuration with a xml metadata driver.
127 * @param array $paths
128 * @param boolean $isDevMode
129 * @param string $proxyDir
130 * @param Cache $cache
131 * @return Configuration
133 static public function createXMLMetadataConfiguration(array $paths, $isDevMode = false, $proxyDir = null, Cache $cache = null)
135 $config = self::createConfiguration($isDevMode, $proxyDir, $cache);
136 $config->setMetadataDriverImpl(new XmlDriver($paths));
141 * Create a configuration with a yaml metadata driver.
143 * @param array $paths
144 * @param boolean $isDevMode
145 * @param string $proxyDir
146 * @param Cache $cache
147 * @return Configuration
149 static public function createYAMLMetadataConfiguration(array $paths, $isDevMode = false, $proxyDir = null, Cache $cache = null)
151 $config = self::createConfiguration($isDevMode, $proxyDir, $cache);
152 $config->setMetadataDriverImpl(new YamlDriver($paths));
157 * Create a configuration without a metadata driver.
159 * @param bool $isDevMode
160 * @param string $proxyDir
161 * @param Cache $cache
162 * @return Configuration
164 static public function createConfiguration($isDevMode = false, $proxyDir = null, Cache $cache = null)
166 $proxyDir = $proxyDir ?: sys_get_temp_dir();
167 if ($isDevMode === false && $cache === null) {
168 if (extension_loaded('apc')) {
169 $cache = new \Doctrine\Common\Cache\ApcCache();
170 } else if (extension_loaded('xcache')) {
171 $cache = new \Doctrine\Common\Cache\XcacheCache();
172 } else if (extension_loaded('memcache')) {
173 $memcache = new \Memcache();
174 $memcache->connect('127.0.0.1');
175 $cache = new \Doctrine\Common\Cache\MemcacheCache();
176 $cache->setMemcache($memcache);
177 } else if (extension_loaded('redis')) {
178 $redis = new \Redis();
179 $redis->connect('127.0.0.1');
180 $cache = new \Doctrine\Common\Cache\RedisCache();
181 $cache->setRedis($redis);
183 $cache = new ArrayCache();
185 } else if ($cache === null) {
186 $cache = new ArrayCache();
188 $cache->setNamespace("dc2_" . md5($proxyDir) . "_"); // to avoid collisions
190 $config = new Configuration();
191 $config->setMetadataCacheImpl($cache);
192 $config->setQueryCacheImpl($cache);
193 $config->setResultCacheImpl($cache);
194 $config->setProxyDir($proxyDir);
195 $config->setProxyNamespace('DoctrineProxies');
196 $config->setAutoGenerateProxyClasses($isDevMode);