Bootstrap::initAutoloader()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 0 Features 0
Metric Value
dl 0
loc 8
rs 9.4285
c 4
b 0
f 0
cc 2
eloc 4
nc 2
nop 0
1
<?php
2
/**
3
 * MtMail - e-mail module for Zend Framework
4
 *
5
 * @link      http://github.com/mtymek/MtMail
6
 * @copyright Copyright (c) 2013-2017 Mateusz Tymek
7
 * @license   BSD 2-Clause
8
 */
9
10
namespace MtMailTest;
11
12
use Zend\Mvc\Application;
13
use Zend\ServiceManager\ServiceManager;
14
use Zend\Mvc\Service\ServiceManagerConfig;
15
16
/**
17
 * Test bootstrap, for setting up autoloading
18
 */
19
class Bootstrap
20
{
21
    protected static $serviceManager;
22
23
    public static function init()
24
    {
25
        $zf2ModulePaths = [dirname(dirname(__DIR__))];
26
        if (($path = static::findParentPath('vendor'))) {
27
            $zf2ModulePaths[] = $path;
28
        }
29
        if (($path = static::findParentPath('module')) !== $zf2ModulePaths[0]) {
30
            $zf2ModulePaths[] = $path;
31
        }
32
33
        static::initAutoloader();
34
35
        // use ModuleManager to load this module and it's dependencies
36
        if (file_exists(__DIR__ . '/TestConfiguration.php')) {
37
            $config = require __DIR__ . '/../TestConfiguration.php';
38
        } else {
39
            $config = require __DIR__ . '/../TestConfiguration.php.dist';
40
        }
41
42
        $smConfig = new ServiceManagerConfig();
43
        $serviceManager = new ServiceManager();
44
        $smConfig->configureServiceManager($serviceManager);
45
        $serviceManager->setService('ApplicationConfig', $config);
46
        $serviceManager->get('ModuleManager')->loadModules();
47
48
        $application = new Application($config, $serviceManager);
49
        $application->bootstrap();
50
51
        static::$serviceManager = $serviceManager;
52
    }
53
54
    /**
55
     * @return ServiceManager
56
     */
57
    public static function getServiceManager()
58
    {
59
        return static::$serviceManager;
60
    }
61
62
    protected static function initAutoloader()
63
    {
64
        $vendorPath = static::findParentPath('vendor');
65
66
        if (file_exists($vendorPath . '/autoload.php')) {
67
            include $vendorPath . '/autoload.php';
68
        }
69
    }
70
71
    protected static function findParentPath($path)
72
    {
73
        $dir = __DIR__;
74
        $previousDir = '.';
75
        while (!is_dir($dir . '/' . $path)) {
76
            $dir = dirname($dir);
77
            if ($previousDir === $dir) {
78
                return false;
79
            }
80
            $previousDir = $dir;
81
        }
82
        return $dir . '/' . $path;
83
    }
84
}
85