[345] | 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 | |
---|
| 20 | namespace Doctrine\ORM\Tools; |
---|
| 21 | |
---|
| 22 | use Doctrine\Common\ClassLoader; |
---|
| 23 | use Doctrine\Common\Cache\Cache; |
---|
| 24 | use Doctrine\Common\Cache\ArrayCache; |
---|
| 25 | use Doctrine\ORM\Configuration; |
---|
| 26 | use Doctrine\ORM\Mapping\Driver\XmlDriver; |
---|
| 27 | use Doctrine\ORM\Mapping\Driver\YamlDriver; |
---|
| 28 | |
---|
| 29 | /** |
---|
| 30 | * Convenience class for setting up Doctrine from different installations and configurations. |
---|
| 31 | * |
---|
| 32 | * @author Benjamin Eberlei <kontakt@beberlei.de> |
---|
| 33 | */ |
---|
| 34 | class Setup |
---|
| 35 | { |
---|
| 36 | /** |
---|
| 37 | * Use this method to register all autoloaders for a setup where Doctrine is checked out from |
---|
| 38 | * its github repository at {@link http://github.com/doctrine/doctrine2} |
---|
| 39 | * |
---|
| 40 | * @param string $gitCheckoutRootPath |
---|
| 41 | * @return void |
---|
| 42 | */ |
---|
| 43 | static public function registerAutoloadGit($gitCheckoutRootPath) |
---|
| 44 | { |
---|
| 45 | if (!class_exists('Doctrine\Common\ClassLoader', false)) { |
---|
| 46 | require_once $gitCheckoutRootPath . "/lib/vendor/doctrine-common/lib/Doctrine/Common/ClassLoader.php"; |
---|
| 47 | } |
---|
| 48 | |
---|
| 49 | $loader = new ClassLoader("Doctrine\Common", $gitCheckoutRootPath . "/lib/vendor/doctrine-common/lib"); |
---|
| 50 | $loader->register(); |
---|
| 51 | |
---|
| 52 | $loader = new ClassLoader("Doctrine\DBAL", $gitCheckoutRootPath . "/lib/vendor/doctrine-dbal/lib"); |
---|
| 53 | $loader->register(); |
---|
| 54 | |
---|
| 55 | $loader = new ClassLoader("Doctrine\ORM", $gitCheckoutRootPath . "/lib"); |
---|
| 56 | $loader->register(); |
---|
| 57 | |
---|
| 58 | $loader = new ClassLoader("Symfony\Component", $gitCheckoutRootPath . "/lib/vendor"); |
---|
| 59 | $loader->register(); |
---|
| 60 | } |
---|
| 61 | |
---|
| 62 | /** |
---|
| 63 | * Use this method to register all autoloaders for a setup where Doctrine is installed |
---|
| 64 | * though {@link http://pear.doctrine-project.org}. |
---|
| 65 | * |
---|
| 66 | * @return void |
---|
| 67 | */ |
---|
| 68 | static public function registerAutoloadPEAR() |
---|
| 69 | { |
---|
| 70 | if (!class_exists('Doctrine\Common\ClassLoader', false)) { |
---|
| 71 | require_once "Doctrine/Common/ClassLoader.php"; |
---|
| 72 | } |
---|
| 73 | |
---|
| 74 | $loader = new ClassLoader("Doctrine"); |
---|
| 75 | $loader->register(); |
---|
| 76 | |
---|
| 77 | $parts = explode(PATH_SEPARATOR, get_include_path()); |
---|
| 78 | |
---|
| 79 | foreach ($parts AS $includePath) { |
---|
| 80 | if ($includePath != "." && file_exists($includePath . "/Doctrine")) { |
---|
| 81 | $loader = new ClassLoader("Symfony\Component", $includePath . "/Doctrine"); |
---|
| 82 | $loader->register(); |
---|
| 83 | return; |
---|
| 84 | } |
---|
| 85 | } |
---|
| 86 | } |
---|
| 87 | |
---|
| 88 | /** |
---|
| 89 | * Use this method to register all autoloads for a downloaded Doctrine library. |
---|
| 90 | * Pick the directory the library was uncompressed into. |
---|
| 91 | * |
---|
| 92 | * @param string $directory |
---|
| 93 | */ |
---|
| 94 | static public function registerAutoloadDirectory($directory) |
---|
| 95 | { |
---|
| 96 | if (!class_exists('Doctrine\Common\ClassLoader', false)) { |
---|
| 97 | require_once $directory . "/Doctrine/Common/ClassLoader.php"; |
---|
| 98 | } |
---|
| 99 | |
---|
| 100 | $loader = new ClassLoader("Doctrine", $directory); |
---|
| 101 | $loader->register(); |
---|
| 102 | |
---|
| 103 | $loader = new ClassLoader("Symfony\Component", $directory . "/Doctrine"); |
---|
| 104 | $loader->register(); |
---|
| 105 | } |
---|
| 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 createAnnotationMetadataConfiguration(array $paths, $isDevMode = false, $proxyDir = null, Cache $cache = null) |
---|
| 117 | { |
---|
| 118 | $config = self::createConfiguration($isDevMode, $proxyDir, $cache); |
---|
| 119 | $config->setMetadataDriverImpl($config->newDefaultAnnotationDriver($paths)); |
---|
| 120 | return $config; |
---|
| 121 | } |
---|
| 122 | |
---|
| 123 | /** |
---|
| 124 | * Create a configuration with a xml metadata driver. |
---|
| 125 | * |
---|
| 126 | * @param array $paths |
---|
| 127 | * @param boolean $isDevMode |
---|
| 128 | * @param string $proxyDir |
---|
| 129 | * @param Cache $cache |
---|
| 130 | * @return Configuration |
---|
| 131 | */ |
---|
| 132 | static public function createXMLMetadataConfiguration(array $paths, $isDevMode = false, $proxyDir = null, Cache $cache = null) |
---|
| 133 | { |
---|
| 134 | $config = self::createConfiguration($isDevMode, $proxyDir, $cache); |
---|
| 135 | $config->setMetadataDriverImpl(new XmlDriver($paths)); |
---|
| 136 | return $config; |
---|
| 137 | } |
---|
| 138 | |
---|
| 139 | /** |
---|
| 140 | * Create a configuration with a yaml metadata driver. |
---|
| 141 | * |
---|
| 142 | * @param array $paths |
---|
| 143 | * @param boolean $isDevMode |
---|
| 144 | * @param string $proxyDir |
---|
| 145 | * @param Cache $cache |
---|
| 146 | * @return Configuration |
---|
| 147 | */ |
---|
| 148 | static public function createYAMLMetadataConfiguration(array $paths, $isDevMode = false, $proxyDir = null, Cache $cache = null) |
---|
| 149 | { |
---|
| 150 | $config = self::createConfiguration($isDevMode, $proxyDir, $cache); |
---|
| 151 | $config->setMetadataDriverImpl(new YamlDriver($paths)); |
---|
| 152 | return $config; |
---|
| 153 | } |
---|
| 154 | |
---|
| 155 | /** |
---|
| 156 | * Create a configuration without a metadata driver. |
---|
| 157 | * |
---|
| 158 | * @param bool $isDevMode |
---|
| 159 | * @param string $proxyDir |
---|
| 160 | * @param Cache $cache |
---|
| 161 | * @return Configuration |
---|
| 162 | */ |
---|
| 163 | static public function createConfiguration($isDevMode = false, $proxyDir = null, Cache $cache = null) |
---|
| 164 | { |
---|
| 165 | $proxyDir = $proxyDir ?: sys_get_temp_dir(); |
---|
| 166 | if ($isDevMode === false && $cache === null) { |
---|
| 167 | if (extension_loaded('apc')) { |
---|
| 168 | $cache = new \Doctrine\Common\Cache\ApcCache; |
---|
| 169 | } else if (extension_loaded('xcache')) { |
---|
| 170 | $cache = new \Doctrine\Common\Cache\XcacheCache; |
---|
| 171 | } else if (extension_loaded('memcache')) { |
---|
| 172 | $memcache = new \Memcache(); |
---|
| 173 | $memcache->connect('127.0.0.1'); |
---|
| 174 | $cache = new \Doctrine\Common\Cache\MemcacheCache(); |
---|
| 175 | $cache->setMemcache($memcache); |
---|
| 176 | } else { |
---|
| 177 | $cache = new ArrayCache; |
---|
| 178 | } |
---|
| 179 | } else if ($cache === null) { |
---|
| 180 | $cache = new ArrayCache; |
---|
| 181 | } |
---|
| 182 | $cache->setNamespace("dc2_" . md5($proxyDir) . "_"); // to avoid collisions |
---|
| 183 | |
---|
| 184 | $config = new Configuration(); |
---|
| 185 | $config->setMetadataCacheImpl($cache); |
---|
| 186 | $config->setQueryCacheImpl($cache); |
---|
| 187 | $config->setResultCacheImpl($cache); |
---|
| 188 | $config->setProxyDir( $proxyDir ); |
---|
| 189 | $config->setProxyNamespace('DoctrineProxies'); |
---|
| 190 | $config->setAutoGenerateProxyClasses($isDevMode); |
---|
| 191 | |
---|
| 192 | return $config; |
---|
| 193 | } |
---|
| 194 | } |
---|