Setup.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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.
  14. *
  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>.
  18. */
  19. namespace Doctrine\ORM\Tools;
  20. use Doctrine\Common\Cache\ApcuCache;
  21. use Doctrine\Common\Cache\ArrayCache;
  22. use Doctrine\Common\Cache\Cache;
  23. use Doctrine\Common\Cache\CacheProvider;
  24. use Doctrine\Common\Cache\MemcachedCache;
  25. use Doctrine\Common\Cache\RedisCache;
  26. use Doctrine\Common\ClassLoader;
  27. use Doctrine\ORM\Configuration;
  28. use Doctrine\ORM\Mapping\Driver\XmlDriver;
  29. use Doctrine\ORM\Mapping\Driver\YamlDriver;
  30. use Memcached;
  31. use Redis;
  32. use function class_exists;
  33. use function extension_loaded;
  34. use function md5;
  35. use function sys_get_temp_dir;
  36. /**
  37. * Convenience class for setting up Doctrine from different installations and configurations.
  38. */
  39. class Setup
  40. {
  41. /**
  42. * Use this method to register all autoloads for a downloaded Doctrine library.
  43. * Pick the directory the library was uncompressed into.
  44. *
  45. * @param string $directory
  46. *
  47. * @return void
  48. */
  49. public static function registerAutoloadDirectory($directory)
  50. {
  51. if (! class_exists('Doctrine\Common\ClassLoader', false)) {
  52. require_once $directory . '/Doctrine/Common/ClassLoader.php';
  53. }
  54. $loader = new ClassLoader('Doctrine', $directory);
  55. $loader->register();
  56. $loader = new ClassLoader('Symfony\Component', $directory . '/Doctrine');
  57. $loader->register();
  58. }
  59. /**
  60. * Creates a configuration with an annotation metadata driver.
  61. *
  62. * @param mixed[] $paths
  63. * @param bool $isDevMode
  64. * @param string $proxyDir
  65. * @param bool $useSimpleAnnotationReader
  66. *
  67. * @return Configuration
  68. */
  69. public static function createAnnotationMetadataConfiguration(array $paths, $isDevMode = false, $proxyDir = null, ?Cache $cache = null, $useSimpleAnnotationReader = true)
  70. {
  71. $config = self::createConfiguration($isDevMode, $proxyDir, $cache);
  72. $config->setMetadataDriverImpl($config->newDefaultAnnotationDriver($paths, $useSimpleAnnotationReader));
  73. return $config;
  74. }
  75. /**
  76. * Creates a configuration with a xml metadata driver.
  77. *
  78. * @param mixed[] $paths
  79. * @param bool $isDevMode
  80. * @param string $proxyDir
  81. *
  82. * @return Configuration
  83. */
  84. public static function createXMLMetadataConfiguration(array $paths, $isDevMode = false, $proxyDir = null, ?Cache $cache = null)
  85. {
  86. $config = self::createConfiguration($isDevMode, $proxyDir, $cache);
  87. $config->setMetadataDriverImpl(new XmlDriver($paths));
  88. return $config;
  89. }
  90. /**
  91. * Creates a configuration with a yaml metadata driver.
  92. *
  93. * @param mixed[] $paths
  94. * @param bool $isDevMode
  95. * @param string $proxyDir
  96. *
  97. * @return Configuration
  98. */
  99. public static function createYAMLMetadataConfiguration(array $paths, $isDevMode = false, $proxyDir = null, ?Cache $cache = null)
  100. {
  101. $config = self::createConfiguration($isDevMode, $proxyDir, $cache);
  102. $config->setMetadataDriverImpl(new YamlDriver($paths));
  103. return $config;
  104. }
  105. /**
  106. * Creates a configuration without a metadata driver.
  107. *
  108. * @param bool $isDevMode
  109. * @param string $proxyDir
  110. *
  111. * @return Configuration
  112. */
  113. public static function createConfiguration($isDevMode = false, $proxyDir = null, ?Cache $cache = null)
  114. {
  115. $proxyDir = $proxyDir ?: sys_get_temp_dir();
  116. $cache = self::createCacheConfiguration($isDevMode, $proxyDir, $cache);
  117. $config = new Configuration();
  118. $config->setMetadataCacheImpl($cache);
  119. $config->setQueryCacheImpl($cache);
  120. $config->setResultCacheImpl($cache);
  121. $config->setProxyDir($proxyDir);
  122. $config->setProxyNamespace('DoctrineProxies');
  123. $config->setAutoGenerateProxyClasses($isDevMode);
  124. return $config;
  125. }
  126. private static function createCacheConfiguration(bool $isDevMode, string $proxyDir, ?Cache $cache): Cache
  127. {
  128. $cache = self::createCacheInstance($isDevMode, $cache);
  129. if (! $cache instanceof CacheProvider) {
  130. return $cache;
  131. }
  132. $namespace = $cache->getNamespace();
  133. if ($namespace !== '') {
  134. $namespace .= ':';
  135. }
  136. $cache->setNamespace($namespace . 'dc2_' . md5($proxyDir) . '_'); // to avoid collisions
  137. return $cache;
  138. }
  139. private static function createCacheInstance(bool $isDevMode, ?Cache $cache): Cache
  140. {
  141. if ($cache !== null) {
  142. return $cache;
  143. }
  144. if ($isDevMode === true) {
  145. return new ArrayCache();
  146. }
  147. if (extension_loaded('apcu')) {
  148. return new ApcuCache();
  149. }
  150. if (extension_loaded('memcached')) {
  151. $memcached = new Memcached();
  152. $memcached->addServer('127.0.0.1', 11211);
  153. $cache = new MemcachedCache();
  154. $cache->setMemcached($memcached);
  155. return $cache;
  156. }
  157. if (extension_loaded('redis')) {
  158. $redis = new Redis();
  159. $redis->connect('127.0.0.1');
  160. $cache = new RedisCache();
  161. $cache->setRedis($redis);
  162. return $cache;
  163. }
  164. return new ArrayCache();
  165. }
  166. }