Bootstrap::init()   C
last analyzed

Complexity

Conditions 7
Paths 16

Size

Total Lines 41
Code Lines 23

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 41
rs 6.7272
cc 7
eloc 23
nc 16
nop 0
1
<?php
2
// @codingStandardsIgnoreFile
3
namespace SpeckCatalogTest;
4
5
use Zend\Loader\AutoloaderFactory;
6
use Zend\Mvc\Service\ServiceManagerConfig;
7
use Zend\ServiceManager\ServiceManager;
8
use Zend\Stdlib\ArrayUtils;
9
use RuntimeException;
10
11
error_reporting(E_ALL | E_STRICT);
12
chdir(__DIR__);
13
14
class Bootstrap
15
{
16
    protected static $serviceManager;
17
    protected static $config;
18
19
    public static function init()
20
    {
21
        // Load the user-defined test configuration file, if it exists; otherwise, load
22
        if (is_readable(__DIR__ . '/TestConfig.php')) {
23
            $testConfig = include __DIR__ . '/TestConfig.php';
24
        } else {
25
            $testConfig = include __DIR__ . '/TestConfig.php.dist';
26
        }
27
28
        $zf2ModulePaths = array();
29
30
        if (isset($testConfig['module_listener_options']['module_paths'])) {
31
            $modulePaths = $testConfig['module_listener_options']['module_paths'];
32
            foreach ($modulePaths as $modulePath) {
33
                if (($path = static::findParentPath($modulePath))) {
34
                    $zf2ModulePaths[] = $path;
35
                }
36
            }
37
        }
38
39
        $zf2ModulePaths  = implode(PATH_SEPARATOR, $zf2ModulePaths) . PATH_SEPARATOR;
40
        $zf2ModulePaths .= getenv('ZF2_MODULES_TEST_PATHS') ?: (defined('ZF2_MODULES_TEST_PATHS') ? ZF2_MODULES_TEST_PATHS : '');
41
42
        static::initAutoloader();
43
44
        // use ModuleManager to load this module and it's dependencies
45
        $baseConfig = array(
46
            'module_listener_options' => array(
47
                'module_paths' => explode(PATH_SEPARATOR, $zf2ModulePaths),
48
            ),
49
        );
50
51
        $config = ArrayUtils::merge($baseConfig, $testConfig);
52
53
        $serviceManager = new ServiceManager(new ServiceManagerConfig());
54
        $serviceManager->setService('ApplicationConfig', $config);
55
        $serviceManager->get('ModuleManager')->loadModules();
56
57
        static::$serviceManager = $serviceManager;
58
        static::$config = $config;
59
    }
60
61
    public static function getServiceManager()
62
    {
63
        return static::$serviceManager;
64
    }
65
66
    public static function getConfig()
67
    {
68
        return static::$config;
69
    }
70
71
    protected static function initAutoloader()
72
    {
73
        $vendorPath = static::findParentPath('vendor');
74
75
        if (is_readable($vendorPath . '/autoload.php')) {
76
            $loader = include $vendorPath . '/autoload.php';
0 ignored issues
show
Unused Code introduced by
$loader is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
77
        } else {
78
            $zf2Path = getenv('ZF2_PATH') ?: (defined('ZF2_PATH') ? ZF2_PATH : (is_dir($vendorPath . '/ZF2/library') ? $vendorPath . '/ZF2/library' : false));
79
80
            if (!$zf2Path) {
81
                throw new RuntimeException('Unable to load ZF2. Run `php composer.phar install` or define a ZF2_PATH environment variable.');
82
            }
83
84
            include $zf2Path . '/Zend/Loader/AutoloaderFactory.php';
85
86
        }
87
88
        AutoloaderFactory::factory(array(
89
            'Zend\Loader\StandardAutoloader' => array(
90
                'autoregister_zf' => true,
91
                'namespaces' => array(
92
                    __NAMESPACE__ => __DIR__ . '/' . __NAMESPACE__,
93
                ),
94
            ),
95
        ));
96
    }
97
98
    protected static function findParentPath($path)
99
    {
100
        $dir = __DIR__;
101
        $previousDir = '.';
102
        while (!is_dir($dir . '/' . $path)) {
103
            $dir = dirname($dir);
104
            if ($previousDir === $dir) {
105
                return false;
106
            }
107
            $previousDir = $dir;
108
        }
109
        return $dir . '/' . $path;
110
    }
111
}
112
113
Bootstrap::init();
114