Bootstrap::findParentPath()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 13
Code Lines 9

Duplication

Lines 13
Ratio 100 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 13
loc 13
rs 9.4285
cc 3
eloc 9
nc 3
nop 1
1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 25 and the first side effect is on line 19.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
2
3
/**
4
 * Zend Framework Application (http://mateuszsitek.com/projects/zendframework-application).
5
 *
6
 * @link      http://github.com/ma-si/zendframework-application for the canonical source repository
7
 *
8
 * @copyright Copyright (c) 2006-2016 Aist Internet Technologies (http://aist.pl) All rights reserved.
9
 * @license   http://opensource.org/licenses/BSD-3-Clause BSD-3-Clause
10
 */
11
12
namespace ApplicationTest;
13
14
use Zend\Loader\AutoloaderFactory;
15
use Zend\Mvc\Service\ServiceManagerConfig;
16
use Zend\ServiceManager\ServiceManager;
17
use RuntimeException;
18
19
error_reporting(E_ALL | E_STRICT);
20
chdir(__DIR__);
21
22
/**
23
 * Test bootstrap, for setting up autoloading
24
 */
25 View Code Duplication
class Bootstrap
0 ignored issues
show
Duplication introduced by
This class seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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();
109