Code Duplication    Length = 81-81 lines in 2 locations

module/Application/test/Bootstrap.php 1 location

@@ 25-105 (lines=81) @@
22
/**
23
 * Test bootstrap, for setting up autoloading
24
 */
25
class Bootstrap
26
{
27
    protected static $serviceManager;
28
29
    public static function init()
30
    {
31
        $zf2ModulePaths = array(dirname(dirname(__DIR__)));
32
        if (($path = static::findParentPath('vendor'))) {
33
            $zf2ModulePaths[] = $path;
34
        }
35
        if (($path = static::findParentPath('module')) !== $zf2ModulePaths[0]) {
36
            $zf2ModulePaths[] = $path;
37
        }
38
39
        static::initAutoloader();
40
41
        // use ModuleManager to load this module and it's dependencies
42
        $config = array(
43
            'module_listener_options' => array(
44
                'module_paths' => $zf2ModulePaths,
45
            ),
46
            'modules' => array(
47
                'Application'
48
            )
49
        );
50
51
        $serviceManager = new ServiceManager(new ServiceManagerConfig());
52
        $serviceManager->setService('ApplicationConfig', $config);
53
        $serviceManager->get('ModuleManager')->loadModules();
54
        static::$serviceManager = $serviceManager;
55
    }
56
57
    public static function chroot()
58
    {
59
        $rootPath = dirname(static::findParentPath('module'));
60
        chdir($rootPath);
61
    }
62
63
    public static function getServiceManager()
64
    {
65
        return static::$serviceManager;
66
    }
67
68
    protected static function initAutoloader()
69
    {
70
        $vendorPath = static::findParentPath('vendor');
71
72
        if (file_exists($vendorPath.'/autoload.php')) {
73
            include $vendorPath.'/autoload.php';
74
        }
75
76
        if (! class_exists('Zend\Loader\AutoloaderFactory')) {
77
            throw new RuntimeException(
78
                'Unable to load ZF2. Run `php composer.phar install`'
79
            );
80
        }
81
82
        AutoloaderFactory::factory(array(
83
            'Zend\Loader\StandardAutoloader' => array(
84
                'autoregister_zf' => true,
85
                'namespaces' => array(
86
                    __NAMESPACE__ => __DIR__ . '/' . __NAMESPACE__,
87
                ),
88
            ),
89
        ));
90
    }
91
92
    protected static function findParentPath($path)
93
    {
94
        $dir = __DIR__;
95
        $previousDir = '.';
96
        while (!is_dir($dir . '/' . $path)) {
97
            $dir = dirname($dir);
98
            if ($previousDir === $dir) {
99
                return false;
100
            }
101
            $previousDir = $dir;
102
        }
103
        return $dir . '/' . $path;
104
    }
105
}
106
107
Bootstrap::init();
108
Bootstrap::chroot();

test/Bootstrap.php 1 location

@@ 25-105 (lines=81) @@
22
/**
23
 * Test bootstrap, for setting up autoloading
24
 */
25
class Bootstrap
26
{
27
    protected static $serviceManager;
28
29
    public static function init()
30
    {
31
        $zf2ModulePaths = array(dirname(dirname(__DIR__)));
32
        if (($path = static::findParentPath('vendor'))) {
33
            $zf2ModulePaths[] = $path;
34
        }
35
        if (($path = static::findParentPath('module')) !== $zf2ModulePaths[0]) {
36
            $zf2ModulePaths[] = $path;
37
        }
38
39
        static::initAutoloader();
40
41
        // use ModuleManager to load this module and it's dependencies
42
        $config = array(
43
            'module_listener_options' => array(
44
                'module_paths' => $zf2ModulePaths,
45
            ),
46
            'modules' => array(
47
                'Application'
48
            )
49
        );
50
51
        $serviceManager = new ServiceManager(new ServiceManagerConfig());
52
        $serviceManager->setService('ApplicationConfig', $config);
53
        $serviceManager->get('ModuleManager')->loadModules();
54
        static::$serviceManager = $serviceManager;
55
    }
56
57
    public static function chroot()
58
    {
59
        $rootPath = dirname(static::findParentPath('module'));
60
        chdir($rootPath);
61
    }
62
63
    public static function getServiceManager()
64
    {
65
        return static::$serviceManager;
66
    }
67
68
    protected static function initAutoloader()
69
    {
70
        $vendorPath = static::findParentPath('vendor');
71
72
        if (file_exists($vendorPath.'/autoload.php')) {
73
            include $vendorPath.'/autoload.php';
74
        }
75
76
        if (! class_exists('Zend\Loader\AutoloaderFactory')) {
77
            throw new RuntimeException(
78
                'Unable to load ZF2. Run `php composer.phar install`'
79
            );
80
        }
81
82
        AutoloaderFactory::factory(array(
83
            'Zend\Loader\StandardAutoloader' => array(
84
                'autoregister_zf' => true,
85
                'namespaces' => array(
86
                    __NAMESPACE__ => __DIR__ . '/' . __NAMESPACE__,
87
                ),
88
            ),
89
        ));
90
    }
91
92
    protected static function findParentPath($path)
93
    {
94
        $dir = __DIR__;
95
        $previousDir = '.';
96
        while (!is_dir($dir . '/' . $path)) {
97
            $dir = dirname($dir);
98
            if ($previousDir === $dir) {
99
                return false;
100
            }
101
            $previousDir = $dir;
102
        }
103
        return $dir . '/' . $path;
104
    }
105
}
106
107
Bootstrap::init();
108
Bootstrap::chroot();