1
|
|
|
<?php |
2
|
|
|
namespace micro\controllers; |
3
|
|
|
use micro\orm\DAO; |
4
|
|
|
use micro\utils\StrUtils; |
5
|
|
|
use micro\views\engine\TemplateEngine; |
6
|
|
|
use mindplay\annotations\Annotations; |
7
|
|
|
use mindplay\annotations\AnnotationCache; |
8
|
|
|
use mindplay\annotations\AnnotationManager; |
9
|
|
|
|
10
|
|
|
class Startup{ |
11
|
|
|
public static $urlParts; |
12
|
|
|
private static $config; |
13
|
|
|
|
14
|
|
|
public static function run(array &$config,$url){ |
15
|
|
|
@set_exception_handler(array('Startup', 'errorHandler')); |
16
|
|
|
self::$config=$config; |
17
|
|
|
self::startTemplateEngine($config); |
18
|
|
|
session_start(); |
19
|
|
|
|
20
|
|
|
if($config["test"]){ |
21
|
|
|
\micro\log\Logger::init(); |
22
|
|
|
$config["siteUrl"]="http://127.0.0.1:8090/"; |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
$db=$config["database"]; |
26
|
|
|
if($db["dbName"]!==""){ |
27
|
|
|
DAO::connect($db["dbName"],@$db["serverName"],@$db["port"],@$db["user"],@$db["password"]); |
28
|
|
|
self::startAnnotations(); |
29
|
|
|
} |
30
|
|
|
$u=self::parseUrl($config, $url); |
31
|
|
|
|
32
|
|
|
if(class_exists($u[0]) && StrUtils::startswith($u[0],"_")===false){ |
33
|
|
|
//Construction de l'instance de la classe (1er élément du tableau) |
34
|
|
|
try{ |
35
|
|
|
if(isset($config['onStartup'])){ |
36
|
|
|
if(is_callable($config['onStartup'])){ |
37
|
|
|
$config["onStartup"]($u); |
38
|
|
|
} |
39
|
|
|
} |
40
|
|
|
self::runAction($u); |
41
|
|
|
}catch (\Exception $e){ |
42
|
|
|
print "Error!: " . $e->getMessage() . "<br/>"; |
43
|
|
|
} |
44
|
|
|
}else{ |
45
|
|
|
print "Le contrôleur `".$u[0]."` n'existe pas <br/>"; |
46
|
|
|
} |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
private static function startAnnotations(){ |
50
|
|
|
Annotations::$config['cache'] = new AnnotationCache(ROOT.DS.'models/runtime'); |
51
|
|
|
self::register(Annotations::getManager()); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
private static function register(AnnotationManager $annotationManager){ |
55
|
|
|
$annotationManager->registry['id'] = 'micro\annotations\IdAnnotation'; |
56
|
|
|
$annotationManager->registry['manyToOne'] = 'micro\annotations\ManyToOneAnnotation'; |
57
|
|
|
$annotationManager->registry['oneToMany'] = 'micro\annotations\OneToManyAnnotation'; |
58
|
|
|
$annotationManager->registry['joinColumn'] = 'micro\annotations\JoinColumnAnnotation'; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
private static function parseUrl($config,$url){ |
62
|
|
|
if(!$url){ |
63
|
|
|
$url=$config["documentRoot"]; |
64
|
|
|
} |
65
|
|
View Code Duplication |
if(StrUtils::endswith($url, "/")) |
66
|
|
|
$url=substr($url, 0,strlen($url)-1); |
67
|
|
|
self::$urlParts=explode("/", $url); |
68
|
|
|
|
69
|
|
|
return self::$urlParts; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
private static function startTemplateEngine($config){ |
73
|
|
|
try { |
74
|
|
|
$engineOptions=array('cache' => ROOT.DS."views/cache/"); |
75
|
|
|
if(isset($config["templateEngine"])){ |
76
|
|
|
$templateEngine=$config["templateEngine"]; |
77
|
|
|
if(isset($config["templateEngineOptions"])){ |
78
|
|
|
$engineOptions=$config["templateEngineOptions"]; |
79
|
|
|
} |
80
|
|
|
$engine=new $templateEngine($engineOptions); |
81
|
|
|
if ($engine instanceof TemplateEngine){ |
82
|
|
|
$config["templateEngine"]=$engine; |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
} catch (\Exception $e) { |
86
|
|
|
echo $e->getTraceAsString(); |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
public static function runAction($u,$initialize=true,$finalize=true){ |
91
|
|
|
$controller=new $u[0](); |
92
|
|
|
if(!$controller instanceof Controller){ |
93
|
|
|
print "`{$u[0]}` n'est pas une instance de contrôleur.`<br/>"; |
94
|
|
|
return; |
95
|
|
|
} |
96
|
|
|
$config=self::getConfig(); |
97
|
|
|
//Dependency injection |
98
|
|
|
if(\array_key_exists("di", $config)){ |
99
|
|
|
$di=$config["di"]; |
100
|
|
|
if(\is_array($di)){ |
101
|
|
|
foreach ($di as $k=>$v){ |
102
|
|
|
$controller->$k=$v(); |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
if($initialize) |
108
|
|
|
$controller->initialize(); |
109
|
|
|
self::callController($controller,$u); |
110
|
|
|
if($finalize) |
111
|
|
|
$controller->finalize(); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
private static function callController(Controller $controller,$u){ |
115
|
|
|
$urlSize=sizeof($u); |
116
|
|
|
try{ |
117
|
|
|
switch ($urlSize) { |
118
|
|
|
case 1: |
119
|
|
|
$controller->index(); |
120
|
|
|
break; |
121
|
|
|
case 2: |
122
|
|
|
$action=$u[1]; |
123
|
|
|
//Appel de la méthode (2ème élément du tableau) |
124
|
|
|
if(method_exists($controller, $action)){ |
125
|
|
|
$controller->$action(); |
126
|
|
|
}else{ |
127
|
|
|
print "La méthode `{$action}` n'existe pas sur le contrôleur `".$u[0]."`<br/>"; |
128
|
|
|
} |
129
|
|
|
break; |
130
|
|
|
default: |
131
|
|
|
//Appel de la méthode en lui passant en paramètre le reste du tableau |
132
|
|
|
\call_user_func_array(array($controller,$u[1]), array_slice($u, 2)); |
133
|
|
|
break; |
134
|
|
|
} |
135
|
|
|
}catch (\Exception $e){ |
136
|
|
|
print "Error!: " . $e->getMessage() . "<br/>"; |
137
|
|
|
} |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
public static function getConfig(){ |
141
|
|
|
return self::$config; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
public static function errorHandler($severity, $message, $filename, $lineno) { |
145
|
|
|
if (error_reporting() == 0) { |
146
|
|
|
return; |
147
|
|
|
} |
148
|
|
|
if (error_reporting() & $severity) { |
149
|
|
|
throw new \ErrorException($message, 0, $severity, $filename, $lineno); |
150
|
|
|
} |
151
|
|
|
} |
152
|
|
|
} |
153
|
|
|
|