Setup.php 7.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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 LGPL. 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. * @return void
  59. */
  60. static public function registerAutoloadPEAR()
  61. {
  62. if (!class_exists('Doctrine\Common\ClassLoader', false)) {
  63. require_once "Doctrine/Common/ClassLoader.php";
  64. }
  65. $loader = new ClassLoader("Doctrine");
  66. $loader->register();
  67. $parts = explode(PATH_SEPARATOR, get_include_path());
  68. foreach ($parts AS $includePath) {
  69. if ($includePath != "." && file_exists($includePath . "/Doctrine")) {
  70. $loader = new ClassLoader("Symfony\Component", $includePath . "/Doctrine");
  71. $loader->register();
  72. return;
  73. }
  74. }
  75. }
  76. /**
  77. * Use this method to register all autoloads for a downloaded Doctrine library.
  78. * Pick the directory the library was uncompressed into.
  79. *
  80. * @param string $directory
  81. */
  82. static public function registerAutoloadDirectory($directory)
  83. {
  84. if (!class_exists('Doctrine\Common\ClassLoader', false)) {
  85. require_once $directory . "/Doctrine/Common/ClassLoader.php";
  86. }
  87. $loader = new ClassLoader("Doctrine");
  88. $loader->register();
  89. $loader = new ClassLoader("Symfony\Component", $directory . "/Doctrine");
  90. $loader->register();
  91. }
  92. /**
  93. * Create a configuration with an annotation metadata driver.
  94. *
  95. * @param array $paths
  96. * @param boolean $isDevMode
  97. * @param string $proxyDir
  98. * @param Cache $cache
  99. * @return Configuration
  100. */
  101. static public function createAnnotationMetadataConfiguration(array $paths, $isDevMode = false, $proxyDir = null, Cache $cache = null)
  102. {
  103. $config = self::createConfiguration($isDevMode, $cache, $proxyDir);
  104. $config->setMetadataDriverImpl($config->newDefaultAnnotationDriver($paths));
  105. return $config;
  106. }
  107. /**
  108. * Create a configuration with an annotation metadata driver.
  109. *
  110. * @param array $paths
  111. * @param boolean $isDevMode
  112. * @param string $proxyDir
  113. * @param Cache $cache
  114. * @return Configuration
  115. */
  116. static public function createXMLMetadataConfiguration(array $paths, $isDevMode = false, $proxyDir = null, Cache $cache = null)
  117. {
  118. $config = self::createConfiguration($isDevMode, $cache, $proxyDir);
  119. $config->setMetadataDriverImpl(new XmlDriver($paths));
  120. return $config;
  121. }
  122. /**
  123. * Create a configuration with an annotation metadata driver.
  124. *
  125. * @param array $paths
  126. * @param boolean $isDevMode
  127. * @param string $proxyDir
  128. * @param Cache $cache
  129. * @return Configuration
  130. */
  131. static public function createYAMLMetadataConfiguration(array $paths, $isDevMode = false, $proxyDir = null, Cache $cache = null)
  132. {
  133. $config = self::createConfiguration($isDevMode, $cache, $proxyDir);
  134. $config->setMetadataDriverImpl(new YamlDriver($paths));
  135. return $config;
  136. }
  137. /**
  138. * Create a configuration without a metadata driver.
  139. *
  140. * @param bool $isDevMode
  141. * @param string $proxyDir
  142. * @param Cache $cache
  143. * @return Configuration
  144. */
  145. static public function createConfiguration($isDevMode = false, $proxyDir = null, Cache $cache = null)
  146. {
  147. if ($isDevMode === false && $cache === null) {
  148. if (extension_loaded('apc')) {
  149. $cache = new \Doctrine\Common\Cache\ApcCache;
  150. } else if (extension_loaded('xcache')) {
  151. $cache = new \Doctrine\Common\Cache\XcacheCache;
  152. } else if (extension_loaded('memcache')) {
  153. $memcache = new \Memcache();
  154. $memcache->connect('127.0.0.1');
  155. $cache = new \Doctrine\Common\Cache\MemcacheCache();
  156. $cache->setMemcache($memcache);
  157. } else {
  158. $cache = new ArrayCache;
  159. }
  160. $cache->setNamespace("dc2_"); // to avoid collisions
  161. } else if ($cache === null) {
  162. $cache = new ArrayCache;
  163. }
  164. $config = new Configuration();
  165. $config->setMetadataCacheImpl($cache);
  166. $config->setQueryCacheImpl($cache);
  167. $config->setResultCacheImpl($cache);
  168. $config->setProxyDir( $proxyDir ?: sys_get_temp_dir() );
  169. $config->setProxyNamespace('DoctrineProxies');
  170. $config->setAutoGenerateProxyClasses($isDevMode);
  171. return $config;
  172. }
  173. }