Setup.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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\ClassLoader;
  21. use Doctrine\Common\Cache\Cache;
  22. use Doctrine\Common\Cache\ArrayCache;
  23. use Doctrine\ORM\Configuration;
  24. use Doctrine\ORM\Mapping\Driver\XmlDriver;
  25. use Doctrine\ORM\Mapping\Driver\YamlDriver;
  26. /**
  27. * Convenience class for setting up Doctrine from different installations and configurations.
  28. *
  29. * @author Benjamin Eberlei <kontakt@beberlei.de>
  30. */
  31. class Setup
  32. {
  33. /**
  34. * Use this method to register all autoloads for a downloaded Doctrine library.
  35. * Pick the directory the library was uncompressed into.
  36. *
  37. * @param string $directory
  38. *
  39. * @return void
  40. */
  41. public static function registerAutoloadDirectory($directory)
  42. {
  43. if (!class_exists('Doctrine\Common\ClassLoader', false)) {
  44. require_once $directory . "/Doctrine/Common/ClassLoader.php";
  45. }
  46. $loader = new ClassLoader("Doctrine", $directory);
  47. $loader->register();
  48. $loader = new ClassLoader("Symfony\Component", $directory . "/Doctrine");
  49. $loader->register();
  50. }
  51. /**
  52. * Creates a configuration with an annotation metadata driver.
  53. *
  54. * @param array $paths
  55. * @param boolean $isDevMode
  56. * @param string $proxyDir
  57. * @param Cache $cache
  58. * @param bool $useSimpleAnnotationReader
  59. *
  60. * @return Configuration
  61. */
  62. public static function createAnnotationMetadataConfiguration(array $paths, $isDevMode = false, $proxyDir = null, Cache $cache = null, $useSimpleAnnotationReader = true)
  63. {
  64. $config = self::createConfiguration($isDevMode, $proxyDir, $cache);
  65. $config->setMetadataDriverImpl($config->newDefaultAnnotationDriver($paths, $useSimpleAnnotationReader));
  66. return $config;
  67. }
  68. /**
  69. * Creates a configuration with a xml metadata driver.
  70. *
  71. * @param array $paths
  72. * @param boolean $isDevMode
  73. * @param string $proxyDir
  74. * @param Cache $cache
  75. *
  76. * @return Configuration
  77. */
  78. public static function createXMLMetadataConfiguration(array $paths, $isDevMode = false, $proxyDir = null, Cache $cache = null)
  79. {
  80. $config = self::createConfiguration($isDevMode, $proxyDir, $cache);
  81. $config->setMetadataDriverImpl(new XmlDriver($paths));
  82. return $config;
  83. }
  84. /**
  85. * Creates a configuration with a yaml metadata driver.
  86. *
  87. * @param array $paths
  88. * @param boolean $isDevMode
  89. * @param string $proxyDir
  90. * @param Cache $cache
  91. *
  92. * @return Configuration
  93. */
  94. public static function createYAMLMetadataConfiguration(array $paths, $isDevMode = false, $proxyDir = null, Cache $cache = null)
  95. {
  96. $config = self::createConfiguration($isDevMode, $proxyDir, $cache);
  97. $config->setMetadataDriverImpl(new YamlDriver($paths));
  98. return $config;
  99. }
  100. /**
  101. * Creates a configuration without a metadata driver.
  102. *
  103. * @param bool $isDevMode
  104. * @param string $proxyDir
  105. * @param Cache $cache
  106. *
  107. * @return Configuration
  108. */
  109. public static function createConfiguration($isDevMode = false, $proxyDir = null, Cache $cache = null)
  110. {
  111. $proxyDir = $proxyDir ?: sys_get_temp_dir();
  112. if ($isDevMode === false && $cache === null) {
  113. if (extension_loaded('apc')) {
  114. $cache = new \Doctrine\Common\Cache\ApcCache();
  115. } elseif (extension_loaded('xcache')) {
  116. $cache = new \Doctrine\Common\Cache\XcacheCache();
  117. } elseif (extension_loaded('memcache')) {
  118. $memcache = new \Memcache();
  119. $memcache->connect('127.0.0.1');
  120. $cache = new \Doctrine\Common\Cache\MemcacheCache();
  121. $cache->setMemcache($memcache);
  122. } elseif (extension_loaded('redis')) {
  123. $redis = new \Redis();
  124. $redis->connect('127.0.0.1');
  125. $cache = new \Doctrine\Common\Cache\RedisCache();
  126. $cache->setRedis($redis);
  127. } else {
  128. $cache = new ArrayCache();
  129. }
  130. } elseif ($cache === null) {
  131. $cache = new ArrayCache();
  132. }
  133. $cache->setNamespace("dc2_" . md5($proxyDir) . "_"); // to avoid collisions
  134. $config = new Configuration();
  135. $config->setMetadataCacheImpl($cache);
  136. $config->setQueryCacheImpl($cache);
  137. $config->setResultCacheImpl($cache);
  138. $config->setProxyDir($proxyDir);
  139. $config->setProxyNamespace('DoctrineProxies');
  140. $config->setAutoGenerateProxyClasses($isDevMode);
  141. return $config;
  142. }
  143. }