-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBootstrap.php
144 lines (117 loc) · 3.77 KB
/
Bootstrap.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
<?php
namespace Yo;
use Yo\App\Loader;
use Yo\Event\Manager;
use Yo\FileSystem\FileSystem;
/**
* Class Bootstrap
* @package Yo
*/
class Bootstrap
{
const CONFIG_FILE = 'config.xml';
protected $_framework;
protected $_config;
protected static $_stack;
/**
* @var Environment
*/
protected $_environment;
/**
* @param Environment $env
*/
public function setEnvironment($env)
{
$this->_environment = $env;
}
/**
* @return \SplStack
*/
public static function getStack()
{
if (null == self::$_stack) {
self::$_stack = new \SplStack();
}
return self::$_stack;
}
/**
* @return \SplStack
*/
public static function cloneStack()
{
return clone self::getStack();
}
protected $_listenersBaseDir;
protected $_listenersNamespace;
public function __construct(Framework $yo)
{
$this->_framework = $yo;
// this should be removed
$this->_listenersBaseDir = __DIR__."/Listeners";
$this->_listenersNamespace = "Yo\\Listeners";
// end
$this->setEnvironment($yo->getEnvironment());
$this->initApps();
}
public function initConfig()
{
if (file_exists(__DIR__ . FileSystem::PATH_SEPARATOR . self::CONFIG_FILE)) {
$this->_config = simplexml_load_file(__DIR__ . FileSystem::PATH_SEPARATOR . self::CONFIG_FILE);
} else {
throw new \RuntimeException("Config file is not found");
}
}
public function initApps()
{
$appLoader = new Loader($this->_config->applications);
$appLoader->setEnvironment($this->_environment);
$appLoader->load();
}
public function initListeners($listenersDir = null)
{
if ($listenersDir != null) {
self::getStack()->push($listenersDir);
}
// build path
$stack = self::cloneStack();
$listenersPath = $this->_listenersBaseDir;
$listenerNamespace = $this->_listenersNamespace;
while ($stack->valid()) {
$listenersPath.= "/".$stack->current();
$listenerNamespace.= "\\".$stack->current();
$stack->prev();
}
if (is_dir($listenersPath))
{
$listeners = scandir($listenersPath);
foreach ($listeners as $listener) {
$t = array('.', '..');
if (!in_array($listener, $t)) {
if (is_dir($listenersPath ."/". $listener)) {
$this->initListeners($listener);
} else {
$file = $listenersPath ."/". $listener;
if (file_exists($file)) {
$listenerClass = rtrim($listener, ".php");
$this->_framework->getClassLoader()->loadClass($listenerNamespace."\\". $listenerClass);
if (class_exists($listenerNamespace."\\". $listenerClass)) {
$listenerClass = $listenerNamespace."\\". $listenerClass;
$listenerObject = new $listenerClass;
Manager::getInstance()->addListener($listenerObject);
} else {
throw new \Exception('Class '.$listenerClass.' does not exist');
}
} else {
throw new \Exception("File $file does not exist");
}
}
}
}
if (!self::getStack()->isEmpty()) {
self::getStack()->pop();
}
} else {
throw new \Exception("Listener path is not directory");
}
}
}