Completed
Push — master ( eb6033...4329f6 )
by Gianluca
10s
created

ServiceManagerFactory::getServiceManager()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 20
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 20
rs 9.2
c 1
b 0
f 0
cc 4
eloc 12
nc 4
nop 1
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 DoctrineModuleTest;
21
22
use Zend\Mvc\Application;
23
use Zend\Mvc\Service\ServiceManagerConfig;
24
use Zend\ServiceManager\ServiceManager;
25
26
/**
27
 * Base test case to be used when a service manager instance is required
28
 */
29
class ServiceManagerFactory
30
{
31
    /**
32
     * @return array
33
     */
34
    public static function getConfiguration()
35
    {
36
        $r = new \ReflectionClass(Application::class);
37
        $requiredParams = $r->getConstructor()->getNumberOfRequiredParameters();
38
39
        $configFile = $requiredParams == 1 ? 'TestConfigurationV3.php' : 'TestConfigurationV2.php';
40
41
        return include __DIR__ . '/../' . $configFile;
42
    }
43
44
    /**
45
     * Retrieves a new ServiceManager instance
46
     *
47
     * @param  array|null     $configuration
48
     * @return ServiceManager
49
     */
50
    public static function getServiceManager(array $configuration = null)
51
    {
52
        $configuration        = $configuration ?: static::getConfiguration();
53
        $serviceManager       = new ServiceManager();
54
        $serviceManagerConfig = new ServiceManagerConfig(
55
            isset($configuration['service_manager']) ? $configuration['service_manager'] : array()
56
        );
57
        $serviceManagerConfig->configureServiceManager($serviceManager);
58
59
        $serviceManager->setService('ApplicationConfig', $configuration);
60
        if (!$serviceManager->has('ServiceListener')) {
61
            $serviceManager->setFactory('ServiceListener', 'Zend\Mvc\Service\ServiceListenerFactory');
62
        }
63
64
        /* @var $moduleManager \Zend\ModuleManager\ModuleManagerInterface */
65
        $moduleManager = $serviceManager->get('ModuleManager');
66
        $moduleManager->loadModules();
67
68
        return $serviceManager;
69
    }
70
}
71