1: <?php
2: 3: 4: 5: 6: 7:
8:
9: namespace GLFramework\Module;
10:
11:
12: use GLFramework\Controller\ErrorController;
13: use GLFramework\Controller\ExceptionController;
14: use GLFramework\Events;
15: use GLFramework\Log;
16: use GLFramework\Request;
17: use Symfony\Component\Yaml\Yaml;
18:
19: class ModuleManager
20: {
21: 22: 23:
24: private $modules = array();
25: private $config;
26: private $directory;
27: private $router;
28: 29: 30:
31: private $mainModule;
32: private static $instance;
33:
34: 35: 36: 37: 38:
39: public function __construct($config, $directory)
40: {
41: self::$instance = $this;
42: $this->config = $config;
43: $this->directory = $directory;
44: $this->router = new \AltoRouter(array(), $this->config['app']['basepath']);
45: }
46:
47: public static function getInstance()
48: {
49: return self::$instance;
50: }
51:
52: public static function getModuleInstanceByName($name)
53: {
54: $instance = self::getInstance();
55: foreach($instance->getModules() as $module)
56: {
57: if($module->title == $name) return $module;
58: }
59: }
60:
61: public static function exists($string)
62: {
63: $instance = self::getInstance();
64: foreach($instance->getModules() as $module)
65: {
66: if($module->title == $string) return true;
67: }
68: return false;
69: }
70:
71: public static function existsController($key)
72: {
73: $instance = self::getInstance();
74: foreach($instance->getModules() as $module)
75: {
76: foreach($module->getControllers() as $controller => $file)
77: {
78: if($key == $controller) return true;
79: }
80: }
81: return false;
82: }
83:
84: 85: 86: 87:
88: public static function instanceController($key)
89: {
90: $instance = self::getInstance();
91: foreach($instance->getModules() as $module)
92: {
93: foreach($module->getControllers() as $controller => $file)
94: {
95: if($key == $controller)
96: {
97: return $module->instanceController($controller);
98: }
99: }
100: }
101: return false;
102: }
103:
104: 105: 106:
107: public function getRouter()
108: {
109: return $this->router;
110: }
111:
112: 113: 114:
115: public function getModules()
116: {
117: return $this->modules;
118: }
119:
120: 121: 122:
123: public function getMainModule()
124: {
125: return $this->mainModule;
126: }
127:
128: 129: 130: 131:
132: public function getViews($module)
133: {
134: $views = $module->getViews();
135: foreach($this->getModules() as $module2)
136: {
137: if($module2 != $module)
138: {
139: foreach($module2->getViews() as $view)
140: {
141: $views[] = $view;
142: }
143: }
144: }
145: return $views;
146: }
147:
148: public function init()
149: {
150: $time = microtime();
151: $module = $this->load($this->directory);
152: if($module)
153: {
154: $this->mainModule = $module;
155: $this->add($module);
156: if(isset($this->config['modules']))
157: {
158: $modules = $this->config['modules'];
159: if(!is_array($modules)) $modules = array($modules);
160: foreach($modules as $subsection => $value)
161: {
162: $dirbase = $this->directory;
163: if((string) $subsection == "internal")
164: {
165: $dirbase = __DIR__ . "/../../modules";
166: }
167: else if(is_numeric($subsection))
168: {
169: $dirbase .= "modules";
170: }
171: else
172: {
173: $dirbase .= "$subsection";
174: }
175: if(!is_array($value)) $value = array($value);
176: foreach($value as $name => $extra)
177: {
178: if(is_integer($name)) $name = $extra;
179: if(is_array($extra))
180: {
181: $name = key($extra);
182: $extra = current($extra);
183: }
184: $module = $this->load($dirbase . "/" . $name, $extra);
185: if($module)
186: $this->add($module);
187:
188: }
189: }
190: }
191:
192: foreach($this->modules as $module)
193: {
194: $module->init();
195: }
196: }
197: else
198: {
199: throw new \Exception("Can't not load the main module!");
200: }
201: }
202:
203: public function load($folder, $extra = null)
204: {
205: $configFile = $folder . "/config.yml";
206: if(file_exists($configFile))
207: {
208: $config = Yaml::parse(file_get_contents($configFile));
209: $config = array_merge($this->config, $config);
210: if(is_array($extra))
211: {
212: $config = array_merge_recursive_ex($config, $extra);
213: }
214: return new Module($config, $folder);
215: }
216: return null;
217: }
218:
219: 220: 221:
222: public function add($module)
223: {
224: if($module != null)
225: $this->modules[] = $module;
226: }
227:
228: public function isEnabled($name)
229: {
230: foreach($this->modules as $module)
231: {
232: if($name == $module->title) return true;
233: }
234: return false;
235: }
236:
237:
238: 239: 240: 241: 242:
243: public function run($url = null, $method = null)
244: {
245: $request = new Request($method, $url);
246: foreach($this->modules as $module)
247: {
248: $module->register_router($this->router);
249: }
250: try {
251:
252: if($match = $this->router->match($url, $method))
253: {
254: $target = $match['target'];
255: $module = $target[0];
256: $controller = $target[1];
257: $request->setParams($match['params']);
258: return $module->run($controller, $request);
259: }
260: else
261: {
262: return $this->mainModule->run(new ErrorController("Controller not found. " . $this->getRoutes()), $request);
263: }
264: } catch (\Exception $ex) {
265: return $this->mainModule->run(new ExceptionController($ex), $request);
266: } catch (\Throwable $ex) {
267: return $this->mainModule->run(new ExceptionController($ex), $request);
268: }
269: return false;
270: }
271:
272: public function getRoutes()
273: {
274: if($this->config['app']['debug'])
275: {
276: $html = "<pre>";
277: $result = array();
278: foreach($this->modules as $module)
279: {
280: $list = $module->getControllersUrlRoutes();
281: $result = array_merge($list, $result);
282: }
283: $html .= implode("\n", $result);
284: $html .= "</pre>";
285: return $html;
286: }
287: }
288:
289:
290:
291: }