Completed
Pull Request — 2.6 (#7716)
by
unknown
08:25
created

Setup   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 177
Duplicated Lines 0 %

Test Coverage

Coverage 81.97%

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 55
c 3
b 0
f 0
dl 0
loc 177
ccs 50
cts 61
cp 0.8197
rs 10
wmc 17

7 Methods

Rating   Name   Duplication   Size   Complexity  
A createCacheConfiguration() 0 17 3
A createConfiguration() 0 15 2
A createXMLMetadataConfiguration() 0 6 1
A createYAMLMetadataConfiguration() 0 6 1
A createAnnotationMetadataConfiguration() 0 6 1
A registerAutoloadDirectory() 0 11 2
B createCacheInstance() 0 54 7
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 1
            require_once $directory . "/Doctrine/Common/ClassLoader.php";
49
        }
50
51 1
        $loader = new ClassLoader("Doctrine", $directory);
0 ignored issues
show
Deprecated Code introduced by
The class Doctrine\Common\ClassLoader has been deprecated: The ClassLoader is deprecated and will be removed in version 3.0 of doctrine/common. ( Ignorable by Annotation )

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

51
        $loader = /** @scrutinizer ignore-deprecated */ new ClassLoader("Doctrine", $directory);
Loading history...
52 1
        $loader->register();
53
54 1
        $loader = new ClassLoader("Symfony\Component", $directory . "/Doctrine");
0 ignored issues
show
Deprecated Code introduced by
The class Doctrine\Common\ClassLoader has been deprecated: The ClassLoader is deprecated and will be removed in version 3.0 of doctrine/common. ( Ignorable by Annotation )

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

54
        $loader = /** @scrutinizer ignore-deprecated */ new ClassLoader("Symfony\Component", $directory . "/Doctrine");
Loading history...
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
        try {
169
170
            /*
171
            For backwards-compatability, attempt to create a caching provider with
172
            default settings if that providers extension is installed.
173
            */
174
175 1
            if (extension_loaded('apcu')) {
176
                return new \Doctrine\Common\Cache\ApcuCache();
177
            }
178
179 1
            if (extension_loaded('memcached')) {
180
                $memcached = new Memcached();
0 ignored issues
show
Bug introduced by
The type Doctrine\ORM\Tools\Memcached was not found. Did you mean Memcached? If so, make sure to prefix the type with \.
Loading history...
181
                $memcached->addServer('127.0.0.1', 11211);
182
183
                $cache = new \Doctrine\Common\Cache\MemcachedCache();
184
                $cache->setMemcached($memcached);
185
186
                return $cache;
187
            }
188
189 1
            if (extension_loaded('redis')) {
190
                $redis = new Redis();
0 ignored issues
show
Bug introduced by
The type Doctrine\ORM\Tools\Redis was not found. Did you mean Redis? If so, make sure to prefix the type with \.
Loading history...
191
                $redis->connect('127.0.0.1');
192
193
                $cache = new \Doctrine\Common\Cache\RedisCache();
194
                $cache->setRedis($redis);
195
196 1
                return $cache;
197
            }
198
            
199
        } catch (\Throwable $ex) {
200
201
            /*
202
            Deliberate catch of top level \Exception in the event that an extension IS installed, but
203
            an attempt to use it fails (Eg. Redis is installed, but there is no server running on
204
            localhost).
205
206
            This block is empty because the fallback to use ArrayCache() seems as good for an exception
207
            as if there were no matching extensions installed.
208
            */
209
        }
210
211 1
        return new ArrayCache();
212
    }
213
}
214