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(); |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.