composer package updates
[openemr.git] / vendor / doctrine / orm / lib / Doctrine / ORM / Tools / Setup.php
bloba5a04c90eb8a3b0ab585e047a6ecc8e265afface
1 <?php
2 /*
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;
30 /**
31 * Convenience class for setting up Doctrine from different installations and configurations.
33 * @author Benjamin Eberlei <kontakt@beberlei.de>
35 class Setup
37 /**
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
43 * @return void
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);
52 $loader->register();
54 $loader = new ClassLoader("Symfony\Component", $directory . "/Doctrine");
55 $loader->register();
58 /**
59 * Creates a configuration with an annotation metadata driver.
61 * @param array $paths
62 * @param boolean $isDevMode
63 * @param string $proxyDir
64 * @param Cache $cache
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));
74 return $config;
77 /**
78 * Creates a configuration with a xml metadata driver.
80 * @param array $paths
81 * @param boolean $isDevMode
82 * @param string $proxyDir
83 * @param Cache $cache
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));
92 return $config;
95 /**
96 * Creates a configuration with a yaml metadata driver.
98 * @param array $paths
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));
110 return $config;
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);
135 return $config;
139 * @param bool $isDevMode
140 * @param string $proxyDir
141 * @param Cache|null $cache
143 * @return Cache
145 private static function createCacheConfiguration($isDevMode, $proxyDir, Cache $cache = null)
147 $cache = self::createCacheInstance($isDevMode, $cache);
149 if ( ! $cache instanceof CacheProvider) {
150 return $cache;
153 $namespace = $cache->getNamespace();
155 if ($namespace !== '') {
156 $namespace .= ':';
159 $cache->setNamespace($namespace . 'dc2_' . md5($proxyDir) . '_'); // to avoid collisions
161 return $cache;
165 * @param bool $isDevMode
166 * @param Cache|null $cache
168 * @return Cache
170 private static function createCacheInstance($isDevMode, Cache $cache = null)
172 if ($cache !== null) {
173 return $cache;
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);
195 return $cache;
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);
205 return $cache;
208 return new ArrayCache();