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