Setup.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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 autoloaders for a setup where Doctrine is checked out from
  35. * its github repository at {@link http://github.com/doctrine/doctrine2}
  36. *
  37. * @param string $gitCheckoutRootPath
  38. * @return void
  39. */
  40. static public function registerAutoloadGit($gitCheckoutRootPath)
  41. {
  42. if (!class_exists('Doctrine\Common\ClassLoader', false)) {
  43. require_once $gitCheckoutRootPath . "/lib/vendor/doctrine-common/lib/Doctrine/Common/ClassLoader.php";
  44. }
  45. $loader = new ClassLoader("Doctrine\Common", $gitCheckoutRootPath . "/lib/vendor/doctrine-common/lib");
  46. $loader->register();
  47. $loader = new ClassLoader("Doctrine\DBAL", $gitCheckoutRootPath . "/lib/vendor/doctrine-dbal/lib");
  48. $loader->register();
  49. $loader = new ClassLoader("Doctrine\ORM", $gitCheckoutRootPath . "/lib");
  50. $loader->register();
  51. $loader = new ClassLoader("Symfony\Component", $gitCheckoutRootPath . "/lib/vendor");
  52. $loader->register();
  53. }
  54. /**
  55. * Use this method to register all autoloaders for a setup where Doctrine is installed
  56. * though {@link http://pear.doctrine-project.org}.
  57. *
  58. * This method registers autoloaders for both Doctrine and Symfony top
  59. * level namespaces.
  60. *
  61. * @return void
  62. */
  63. static public function registerAutoloadPEAR()
  64. {
  65. if (!class_exists('Doctrine\Common\ClassLoader', false)) {
  66. require_once "Doctrine/Common/ClassLoader.php";
  67. }
  68. $loader = new ClassLoader("Doctrine");
  69. $loader->register();
  70. $loader = new ClassLoader("Symfony");
  71. $loader->register();
  72. }
  73. /**
  74. * Use this method to register all autoloads for a downloaded Doctrine library.
  75. * Pick the directory the library was uncompressed into.
  76. *
  77. * @param string $directory
  78. */
  79. static public function registerAutoloadDirectory($directory)
  80. {
  81. if (!class_exists('Doctrine\Common\ClassLoader', false)) {
  82. require_once $directory . "/Doctrine/Common/ClassLoader.php";
  83. }
  84. $loader = new ClassLoader("Doctrine", $directory);
  85. $loader->register();
  86. $loader = new ClassLoader("Symfony\Component", $directory . "/Doctrine");
  87. $loader->register();
  88. }
  89. /**
  90. * Create a configuration with an annotation metadata driver.
  91. *
  92. * @param array $paths
  93. * @param boolean $isDevMode
  94. * @param string $proxyDir
  95. * @param Cache $cache
  96. * @param bool $useSimpleAnnotationReader
  97. * @return Configuration
  98. */
  99. static public function createAnnotationMetadataConfiguration(array $paths, $isDevMode = false, $proxyDir = null, Cache $cache = null, $useSimpleAnnotationReader = true)
  100. {
  101. $config = self::createConfiguration($isDevMode, $proxyDir, $cache);
  102. $config->setMetadataDriverImpl($config->newDefaultAnnotationDriver($paths, $useSimpleAnnotationReader));
  103. return $config;
  104. }
  105. /**
  106. * Create a configuration with a xml metadata driver.
  107. *
  108. * @param array $paths
  109. * @param boolean $isDevMode
  110. * @param string $proxyDir
  111. * @param Cache $cache
  112. * @return Configuration
  113. */
  114. static public function createXMLMetadataConfiguration(array $paths, $isDevMode = false, $proxyDir = null, Cache $cache = null)
  115. {
  116. $config = self::createConfiguration($isDevMode, $proxyDir, $cache);
  117. $config->setMetadataDriverImpl(new XmlDriver($paths));
  118. return $config;
  119. }
  120. /**
  121. * Create a configuration with a yaml metadata driver.
  122. *
  123. * @param array $paths
  124. * @param boolean $isDevMode
  125. * @param string $proxyDir
  126. * @param Cache $cache
  127. * @return Configuration
  128. */
  129. static public function createYAMLMetadataConfiguration(array $paths, $isDevMode = false, $proxyDir = null, Cache $cache = null)
  130. {
  131. $config = self::createConfiguration($isDevMode, $proxyDir, $cache);
  132. $config->setMetadataDriverImpl(new YamlDriver($paths));
  133. return $config;
  134. }
  135. /**
  136. * Create a configuration without a metadata driver.
  137. *
  138. * @param bool $isDevMode
  139. * @param string $proxyDir
  140. * @param Cache $cache
  141. * @return Configuration
  142. */
  143. static public function createConfiguration($isDevMode = false, $proxyDir = null, Cache $cache = null)
  144. {
  145. $proxyDir = $proxyDir ?: sys_get_temp_dir();
  146. if ($isDevMode === false && $cache === null) {
  147. if (extension_loaded('apc')) {
  148. $cache = new \Doctrine\Common\Cache\ApcCache();
  149. } else if (extension_loaded('xcache')) {
  150. $cache = new \Doctrine\Common\Cache\XcacheCache();
  151. } else if (extension_loaded('memcache')) {
  152. $memcache = new \Memcache();
  153. $memcache->connect('127.0.0.1');
  154. $cache = new \Doctrine\Common\Cache\MemcacheCache();
  155. $cache->setMemcache($memcache);
  156. } else if (extension_loaded('redis')) {
  157. $redis = new \Redis();
  158. $redis->connect('127.0.0.1');
  159. $cache = new \Doctrine\Common\Cache\RedisCache();
  160. $cache->setRedis($redis);
  161. } else {
  162. $cache = new ArrayCache();
  163. }
  164. } else if ($cache === null) {
  165. $cache = new ArrayCache();
  166. }
  167. $cache->setNamespace("dc2_" . md5($proxyDir) . "_"); // to avoid collisions
  168. $config = new Configuration();
  169. $config->setMetadataCacheImpl($cache);
  170. $config->setQueryCacheImpl($cache);
  171. $config->setResultCacheImpl($cache);
  172. $config->setProxyDir($proxyDir);
  173. $config->setProxyNamespace('DoctrineProxies');
  174. $config->setAutoGenerateProxyClasses($isDevMode);
  175. return $config;
  176. }
  177. }