Rajout de doctrine/orm
[zf2.biz/galerie.git] / vendor / doctrine / orm / lib / Doctrine / ORM / Tools / Setup.php
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
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      * @param bool $useSimpleAnnotationReader
115      * @return Configuration
116      */
117     static public function createAnnotationMetadataConfiguration(array $paths, $isDevMode = false, $proxyDir = null, Cache $cache = null, $useSimpleAnnotationReader = true)
118     {
119         $config = self::createConfiguration($isDevMode, $proxyDir, $cache);
120         $config->setMetadataDriverImpl($config->newDefaultAnnotationDriver($paths, $useSimpleAnnotationReader));
121         return $config;
122     }
123
124     /**
125      * Create a configuration with a xml metadata driver.
126      *
127      * @param array $paths
128      * @param boolean $isDevMode
129      * @param string $proxyDir
130      * @param Cache $cache
131      * @return Configuration
132      */
133     static public function createXMLMetadataConfiguration(array $paths, $isDevMode = false, $proxyDir = null, Cache $cache = null)
134     {
135         $config = self::createConfiguration($isDevMode, $proxyDir, $cache);
136         $config->setMetadataDriverImpl(new XmlDriver($paths));
137         return $config;
138     }
139
140     /**
141      * Create a configuration with a yaml metadata driver.
142      *
143      * @param array $paths
144      * @param boolean $isDevMode
145      * @param string $proxyDir
146      * @param Cache $cache
147      * @return Configuration
148      */
149     static public function createYAMLMetadataConfiguration(array $paths, $isDevMode = false, $proxyDir = null, Cache $cache = null)
150     {
151         $config = self::createConfiguration($isDevMode, $proxyDir, $cache);
152         $config->setMetadataDriverImpl(new YamlDriver($paths));
153         return $config;
154     }
155
156     /**
157      * Create a configuration without a metadata driver.
158      *
159      * @param bool $isDevMode
160      * @param string $proxyDir
161      * @param Cache $cache
162      * @return Configuration
163      */
164     static public function createConfiguration($isDevMode = false, $proxyDir = null, Cache $cache = null)
165     {
166         $proxyDir = $proxyDir ?: sys_get_temp_dir();
167         if ($isDevMode === false && $cache === null) {
168             if (extension_loaded('apc')) {
169                 $cache = new \Doctrine\Common\Cache\ApcCache();
170             } else if (extension_loaded('xcache')) {
171                 $cache = new \Doctrine\Common\Cache\XcacheCache();
172             } else if (extension_loaded('memcache')) {
173                 $memcache = new \Memcache();
174                 $memcache->connect('127.0.0.1');
175                 $cache = new \Doctrine\Common\Cache\MemcacheCache();
176                 $cache->setMemcache($memcache);
177             } else if (extension_loaded('redis')) {
178                 $redis = new \Redis();
179                 $redis->connect('127.0.0.1');
180                 $cache = new \Doctrine\Common\Cache\RedisCache();
181                 $cache->setRedis($redis);
182             } else {
183                 $cache = new ArrayCache();
184             }
185         } else if ($cache === null) {
186             $cache = new ArrayCache();
187         }
188         $cache->setNamespace("dc2_" . md5($proxyDir) . "_"); // to avoid collisions
189
190         $config = new Configuration();
191         $config->setMetadataCacheImpl($cache);
192         $config->setQueryCacheImpl($cache);
193         $config->setResultCacheImpl($cache);
194         $config->setProxyDir($proxyDir);
195         $config->setProxyNamespace('DoctrineProxies');
196         $config->setAutoGenerateProxyClasses($isDevMode);
197
198         return $config;
199     }
200 }