Failed Conditions
Push — master ( a16dc6...b210c1 )
by Luís
09:00
created

lib/Doctrine/ORM/Tools/Setup.php (1 issue)

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\CacheProvider;
25
use Doctrine\Common\Cache\ArrayCache;
26
use Doctrine\ORM\Configuration;
27
use Doctrine\ORM\Mapping\Driver\XmlDriver;
28
use Doctrine\ORM\Mapping\Driver\YamlDriver;
29
30
/**
31
 * Convenience class for setting up Doctrine from different installations and configurations.
32
 *
33
 * @author Benjamin Eberlei <[email protected]>
34
 */
35
class Setup
36
{
37
    /**
38
     * Use this method to register all autoloads for a downloaded Doctrine library.
39
     * Pick the directory the library was uncompressed into.
40
     *
41
     * @param string $directory
42
     *
43
     * @return void
44
     */
45 1
    public static function registerAutoloadDirectory($directory)
46
    {
47 1
        if (!class_exists('Doctrine\Common\ClassLoader', false)) {
48
            require_once $directory . "/Doctrine/Common/ClassLoader.php";
49
        }
50
51 1
        $loader = new ClassLoader("Doctrine", $directory);
52 1
        $loader->register();
53
54 1
        $loader = new ClassLoader("Symfony\Component", $directory . "/Doctrine");
55 1
        $loader->register();
56 1
    }
57
58
    /**
59
     * Creates a configuration with an annotation metadata driver.
60
     *
61
     * @param array   $paths
62
     * @param boolean $isDevMode
63
     * @param string  $proxyDir
64
     * @param Cache   $cache
65
     * @param bool    $useSimpleAnnotationReader
66
     *
67
     * @return Configuration
68
     */
69 3
    public static function createAnnotationMetadataConfiguration(array $paths, $isDevMode = false, $proxyDir = null, Cache $cache = null, $useSimpleAnnotationReader = true)
70
    {
71 3
        $config = self::createConfiguration($isDevMode, $proxyDir, $cache);
72 3
        $config->setMetadataDriverImpl($config->newDefaultAnnotationDriver($paths, $useSimpleAnnotationReader));
73
74 3
        return $config;
75
    }
76
77
    /**
78
     * Creates a configuration with a xml metadata driver.
79
     *
80
     * @param array   $paths
81
     * @param boolean $isDevMode
82
     * @param string  $proxyDir
83
     * @param Cache   $cache
84
     *
85
     * @return Configuration
86
     */
87 1
    public static function createXMLMetadataConfiguration(array $paths, $isDevMode = false, $proxyDir = null, Cache $cache = null)
88
    {
89 1
        $config = self::createConfiguration($isDevMode, $proxyDir, $cache);
90 1
        $config->setMetadataDriverImpl(new XmlDriver($paths));
91
92 1
        return $config;
93
    }
94
95
    /**
96
     * Creates a configuration with a yaml metadata driver.
97
     *
98
     * @param array   $paths
99
     * @param boolean $isDevMode
100
     * @param string  $proxyDir
101
     * @param Cache   $cache
102
     *
103
     * @return Configuration
104
     */
105 1
    public static function createYAMLMetadataConfiguration(array $paths, $isDevMode = false, $proxyDir = null, Cache $cache = null)
106
    {
107 1
        $config = self::createConfiguration($isDevMode, $proxyDir, $cache);
108 1
        $config->setMetadataDriverImpl(new YamlDriver($paths));
109
110 1
        return $config;
111
    }
112
113
    /**
114
     * Creates a configuration without a metadata driver.
115
     *
116
     * @param bool   $isDevMode
117
     * @param string $proxyDir
118
     * @param Cache  $cache
119
     *
120
     * @return Configuration
121
     */
122 9
    public static function createConfiguration($isDevMode = false, $proxyDir = null, Cache $cache = null)
123
    {
124 9
        $proxyDir = $proxyDir ?: sys_get_temp_dir();
125
126 9
        $cache = self::createCacheConfiguration($isDevMode, $proxyDir, $cache);
127
128 9
        $config = new Configuration();
129 9
        $config->setMetadataCacheImpl($cache);
130 9
        $config->setQueryCacheImpl($cache);
131 9
        $config->setResultCacheImpl($cache);
132 9
        $config->setProxyDir($proxyDir);
133 9
        $config->setProxyNamespace('DoctrineProxies');
134 9
        $config->setAutoGenerateProxyClasses($isDevMode);
135
136 9
        return $config;
137
    }
138
139 9
    private static function createCacheConfiguration(bool $isDevMode, string $proxyDir, ?Cache $cache) :  Cache
140
    {
141 9
        $cache = self::createCacheInstance($isDevMode, $cache);
142
143 9
        if ( ! $cache instanceof CacheProvider) {
144 1
            return $cache;
145
        }
146
147 8
        $namespace = $cache->getNamespace();
148
149 8
        if ($namespace !== '') {
150 1
            $namespace .= ':';
151
        }
152
153 8
        $cache->setNamespace($namespace . 'dc2_' . md5($proxyDir) . '_'); // to avoid collisions
154
155 8
        return $cache;
156
    }
157
158 9
    private static function createCacheInstance(bool $isDevMode, ?Cache $cache) : Cache
159
    {
160 9
        if ($cache !== null) {
161 4
            return $cache;
162
        }
163
164 5
        if ($isDevMode === true) {
165 4
            return new ArrayCache();
166
        }
167
168 1
        if (extension_loaded('apc')) {
169
            return new \Doctrine\Common\Cache\ApcCache();
0 ignored issues
show
Deprecated Code introduced by
The class Doctrine\Common\Cache\ApcCache has been deprecated: since version 1.6, use ApcuCache instead ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

169
            return /** @scrutinizer ignore-deprecated */ new \Doctrine\Common\Cache\ApcCache();
Loading history...
170
        }
171
172 1
        if (ini_get('xcache.cacher')) {
173
            return new \Doctrine\Common\Cache\XcacheCache();
174
        }
175
176 1
        if (extension_loaded('memcache')) {
177
            $memcache = new \Memcache();
178
            $memcache->connect('127.0.0.1');
179
180
            $cache = new \Doctrine\Common\Cache\MemcacheCache();
181
            $cache->setMemcache($memcache);
182
183
            return $cache;
184
        }
185
186 1
        if (extension_loaded('redis')) {
187
            $redis = new \Redis();
188
            $redis->connect('127.0.0.1');
189
190
            $cache = new \Doctrine\Common\Cache\RedisCache();
191
            $cache->setRedis($redis);
192
193
            return $cache;
194
        }
195
196 1
        return new ArrayCache();
197
    }
198
}
199