php namespace Evilworlds use Evilworlds Router NotFoundException use E

 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
<?php
namespace Evilworlds;
use \Evilworlds\Router\NotFoundException;
use \Evilworlds\Router\Exception as RouterExecption;
class Router {
private static $instance = NULL;
private $module;
private $module_namespace;
private $modules;
private $action;
private $controller;
public function __construct() {
$this -> modules = &\Evilworlds\Config::get("modules");
}
public function prepare($url_prefix) {
$this -> module = NULL;
foreach ($this -> modules as $namespace => $mod) {
if ($mod['url_prefix'] == $url_prefix) {
$this -> module = $mod;
$this -> module_namespace = $namespace;
break;
}
}
if (!$this -> module)
throw new NotFoundException($url_prefix);
return $this;
}
public function getAction() {
return $this -> action;
}
public function getController() {
return $this -> controller;
}
public function getName() {
return $this -> module_namespace;
}
public static function getUrl() {
$r = self::getInstance();
$url = \Evilworlds\Text\Url::get($r -> getName(), $_GET);
unset($url['__route_url']);
return $url;
}
public function execute($function) {
$parts = explode('.', $function, 2);
$this -> controller = isset($parts[0]) && !empty($parts[0]) ?
Validate::prepareExeName($parts[0]) : $this -> module['controller'];
$this -> action = isset($parts[1]) && !empty($parts[1]) ?
Validate::prepareExeName($parts[1]) : $this -> module['action'];
$controller_class = 'Evilworlds\\'.$this -> module_namespace.'\\Controllers\\'.$this -> controller.'Controller';
$action_method = $this -> action.'Action';
if (!is_exists_class($controller_class))
throw new NotFoundException($this -> module['url_prefix']);
if (!method_exists($controller_class, $action_method) || !is_callable([$controller_class, $action_method]))
throw new RouterExecption("Can't execute ".$action_method."() from ".$controller_class."!", RouterExecption::CANT_EXECUTE);
$app = new $controller_class();
if (method_exists($app, 'accessRules'))
$app -> checkAccess($this -> action);
if (!$app -> isNoAction())
$app -> $action_method();
unset($app);
}
public static function getInstance() {
if (self::$instance === NULL) {
$class = __CLASS__;
self::$instance = new $class;
}
return self::$instance;
}
}