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\CacheProvider
;
25 use Doctrine\Common\Cache\ArrayCache
;
26 use Doctrine\ORM\Configuration
;
27 use Doctrine\ORM\Mapping\Driver\XmlDriver
;
28 use Doctrine\ORM\Mapping\Driver\YamlDriver
;
31 * Convenience class for setting up Doctrine from different installations and configurations.
33 * @author Benjamin Eberlei <kontakt@beberlei.de>
38 * Use this method to register all autoloads for a downloaded Doctrine library.
39 * Pick the directory the library was uncompressed into.
41 * @param string $directory
45 public static function registerAutoloadDirectory($directory)
47 if (!class_exists('Doctrine\Common\ClassLoader', false)) {
48 require_once $directory . "/Doctrine/Common/ClassLoader.php";
51 $loader = new ClassLoader("Doctrine", $directory);
54 $loader = new ClassLoader("Symfony\Component", $directory . "/Doctrine");
59 * Creates a configuration with an annotation metadata driver.
62 * @param boolean $isDevMode
63 * @param string $proxyDir
65 * @param bool $useSimpleAnnotationReader
67 * @return Configuration
69 public static function createAnnotationMetadataConfiguration(array $paths, $isDevMode = false, $proxyDir = null, Cache
$cache = null, $useSimpleAnnotationReader = true)
71 $config = self
::createConfiguration($isDevMode, $proxyDir, $cache);
72 $config->setMetadataDriverImpl($config->newDefaultAnnotationDriver($paths, $useSimpleAnnotationReader));
78 * Creates a configuration with a xml metadata driver.
81 * @param boolean $isDevMode
82 * @param string $proxyDir
85 * @return Configuration
87 public static function createXMLMetadataConfiguration(array $paths, $isDevMode = false, $proxyDir = null, Cache
$cache = null)
89 $config = self
::createConfiguration($isDevMode, $proxyDir, $cache);
90 $config->setMetadataDriverImpl(new XmlDriver($paths));
96 * Creates a configuration with a yaml metadata driver.
99 * @param boolean $isDevMode
100 * @param string $proxyDir
101 * @param Cache $cache
103 * @return Configuration
105 public static function createYAMLMetadataConfiguration(array $paths, $isDevMode = false, $proxyDir = null, Cache
$cache = null)
107 $config = self
::createConfiguration($isDevMode, $proxyDir, $cache);
108 $config->setMetadataDriverImpl(new YamlDriver($paths));
114 * Creates a configuration without a metadata driver.
116 * @param bool $isDevMode
117 * @param string $proxyDir
118 * @param Cache $cache
120 * @return Configuration
122 public static function createConfiguration($isDevMode = false, $proxyDir = null, Cache
$cache = null)
124 $proxyDir = $proxyDir ?
: sys_get_temp_dir();
125 $cache = self
::createCacheConfiguration($isDevMode, $proxyDir, $cache);
127 $config = new Configuration();
128 $config->setMetadataCacheImpl($cache);
129 $config->setQueryCacheImpl($cache);
130 $config->setResultCacheImpl($cache);
131 $config->setProxyDir($proxyDir);
132 $config->setProxyNamespace('DoctrineProxies');
133 $config->setAutoGenerateProxyClasses($isDevMode);
139 * @param bool $isDevMode
140 * @param string $proxyDir
141 * @param Cache|null $cache
145 private static function createCacheConfiguration($isDevMode, $proxyDir, Cache
$cache = null)
147 $cache = self
::createCacheInstance($isDevMode, $cache);
149 if ( ! $cache instanceof CacheProvider
) {
153 $namespace = $cache->getNamespace();
155 if ($namespace !== '') {
159 $cache->setNamespace($namespace . 'dc2_' . md5($proxyDir) . '_'); // to avoid collisions
165 * @param bool $isDevMode
166 * @param Cache|null $cache
170 private static function createCacheInstance($isDevMode, Cache
$cache = null)
172 if ($cache !== null) {
176 if ($isDevMode === true) {
177 return new ArrayCache();
180 if (extension_loaded('apc')) {
181 return new \Doctrine\Common\Cache\
ApcCache();
184 if (extension_loaded('xcache')) {
185 return new \Doctrine\Common\Cache\
XcacheCache();
188 if (extension_loaded('memcache')) {
189 $memcache = new \
Memcache();
190 $memcache->connect('127.0.0.1');
192 $cache = new \Doctrine\Common\Cache\
MemcacheCache();
193 $cache->setMemcache($memcache);
198 if (extension_loaded('redis')) {
199 $redis = new \
Redis();
200 $redis->connect('127.0.0.1');
202 $cache = new \Doctrine\Common\Cache\
RedisCache();
203 $cache->setRedis($redis);
208 return new ArrayCache();