Bootstrap   A
last analyzed

Complexity

Total Complexity 15

Size/Duplication

Total Lines 94
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 15
lcom 1
cbo 3
dl 0
loc 94
rs 10
c 1
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
B init() 0 27 3
A chroot() 0 5 1
A getServiceManager() 0 4 1
C initAutoloader() 0 36 7
A findParentPath() 0 13 3
1
<?php
2
3
namespace JhHubTest;
4
5
use Zend\Loader\AutoloaderFactory;
6
use Zend\Mvc\Service\ServiceManagerConfig;
7
use Zend\ServiceManager\ServiceManager;
8
use RuntimeException;
9
10
ini_set('display_errors', 1);
11
error_reporting(E_ALL | E_STRICT);
12
chdir(__DIR__);
13
14
/**
15
 * Test bootstrap, for setting up autoloading
16
 */
17
class Bootstrap
18
{
19
    protected static $serviceManager;
20
21
    public static function init()
22
    {
23
        $zf2ModulePaths = array(dirname(dirname(__DIR__)));
24
        if (($path = static::findParentPath('vendor'))) {
25
            $zf2ModulePaths[] = $path;
26
        }
27
        if (($path = static::findParentPath('module')) !== $zf2ModulePaths[0]) {
28
            $zf2ModulePaths[] = $path;
29
        }
30
31
        static::initAutoloader();
32
33
        // use ModuleManager to load this module and it's dependencies
34
        $config = array(
35
            'module_listener_options' => array(
36
                'module_paths' => $zf2ModulePaths,
37
            ),
38
            'modules' => array(
39
                'JhHub'
40
            )
41
        );
42
43
        $serviceManager = new ServiceManager(new ServiceManagerConfig());
44
        $serviceManager->setService('ApplicationConfig', $config);
45
        $serviceManager->get('ModuleManager')->loadModules();
46
        static::$serviceManager = $serviceManager;
47
    }
48
49
    public static function chroot()
50
    {
51
        $rootPath = dirname(static::findParentPath('module'));
52
        chdir($rootPath);
53
    }
54
55
    public static function getServiceManager()
56
    {
57
        return static::$serviceManager;
58
    }
59
60
    protected static function initAutoloader()
61
    {
62
        $vendorPath = static::findParentPath('vendor');
63
64
        $zf2Path = getenv('ZF2_PATH');
65
        if (!$zf2Path) {
66
            if (defined('ZF2_PATH')) {
67
                $zf2Path = ZF2_PATH;
68
            } elseif (is_dir($vendorPath . '/ZF2/library')) {
69
                $zf2Path = $vendorPath . '/ZF2/library';
70
            } elseif (is_dir($vendorPath . '/zendframework/zendframework/library')) {
71
                $zf2Path = $vendorPath . '/zendframework/zendframework/library';
72
            }
73
        }
74
75
        if (!$zf2Path) {
76
            throw new RuntimeException(
77
                'Unable to load ZF2. Run `php composer.phar install` or'
78
                . ' define a ZF2_PATH environment variable.'
79
            );
80
        }
81
82
        if (file_exists($vendorPath . '/autoload.php')) {
83
            include $vendorPath . '/autoload.php';
84
        }
85
86
        include $zf2Path . '/Zend/Loader/AutoloaderFactory.php';
87
        AutoloaderFactory::factory(array(
88
            'Zend\Loader\StandardAutoloader' => array(
89
                'autoregister_zf' => true,
90
                'namespaces' => array(
91
                    __NAMESPACE__ => __DIR__ . '/' . __NAMESPACE__,
92
                ),
93
            ),
94
        ));
95
    }
96
97
    protected static function findParentPath($path)
98
    {
99
        $dir = __DIR__;
100
        $previousDir = '.';
101
        while (!is_dir($dir . '/' . $path)) {
102
            $dir = dirname($dir);
103
            if ($previousDir === $dir) {
104
                return false;
105
            }
106
            $previousDir = $dir;
107
        }
108
        return $dir . '/' . $path;
109
    }
110
}
111
112
date_default_timezone_set("Europe/London");
113
Bootstrap::init();
114
Bootstrap::chroot();