|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* DronePHP (http://www.dronephp.com) |
|
4
|
|
|
* |
|
5
|
|
|
* @link http://github.com/Pleets/DronePHP |
|
6
|
|
|
* @copyright Copyright (c) 2016-2018 Pleets. (http://www.pleets.org) |
|
7
|
|
|
* @license http://www.dronephp.com/license |
|
8
|
|
|
* @author Darío Rivera <[email protected]> |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
namespace Drone\Mvc; |
|
12
|
|
|
|
|
13
|
|
|
use Drone\FileSystem\Shell; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* Application class |
|
17
|
|
|
* |
|
18
|
|
|
* This is the main class for mvc pattern |
|
19
|
|
|
*/ |
|
20
|
|
|
class Application |
|
21
|
|
|
{ |
|
22
|
|
|
/** |
|
23
|
|
|
* List of modules available |
|
24
|
|
|
* |
|
25
|
|
|
* @var array |
|
26
|
|
|
*/ |
|
27
|
|
|
private $modules; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* The router instance |
|
31
|
|
|
* |
|
32
|
|
|
* @var Router |
|
33
|
|
|
*/ |
|
34
|
|
|
private $router; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* Development or production mode |
|
38
|
|
|
* |
|
39
|
|
|
* @var boolean |
|
40
|
|
|
*/ |
|
41
|
|
|
private $devMode; |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* Returns the router instance |
|
45
|
|
|
* |
|
46
|
|
|
* @return Router |
|
47
|
|
|
*/ |
|
48
|
|
|
public function getRouter() |
|
49
|
|
|
{ |
|
50
|
|
|
return $this->router; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* Prepares the app environment |
|
55
|
|
|
* |
|
56
|
|
|
* @return null |
|
57
|
|
|
*/ |
|
58
|
|
|
public function prepare() |
|
59
|
|
|
{ |
|
60
|
|
|
# start sessions |
|
61
|
|
|
if (!isset($_SESSION)) |
|
62
|
|
|
session_start(); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* Constructor |
|
67
|
|
|
* |
|
68
|
|
|
* @param array $init_parameters |
|
69
|
|
|
*/ |
|
70
|
|
|
public function __construct($init_parameters) |
|
71
|
|
|
{ |
|
72
|
|
|
$this->prepare(); |
|
73
|
|
|
|
|
74
|
|
|
$this->devMode = $init_parameters["environment"]["dev_mode"]; |
|
75
|
|
|
$this->modules = $init_parameters["modules"]; |
|
76
|
|
|
|
|
77
|
|
|
/* |
|
78
|
|
|
* DEV MODE: |
|
79
|
|
|
* Set Development or production environment |
|
80
|
|
|
*/ |
|
81
|
|
|
|
|
82
|
|
|
if ($this->devMode) |
|
83
|
|
|
{ |
|
84
|
|
|
ini_set('display_errors', 1); |
|
85
|
|
|
|
|
86
|
|
|
// See errors |
|
87
|
|
|
// error_reporting(E_ERROR | E_WARNING | E_PARSE | E_NOTICE); |
|
88
|
|
|
|
|
89
|
|
|
// PHP 5.4 |
|
90
|
|
|
// error_reporting(E_ALL); |
|
91
|
|
|
|
|
92
|
|
|
// Best way to view all possible errors |
|
93
|
|
|
error_reporting(-1); |
|
94
|
|
|
} |
|
95
|
|
|
else { |
|
96
|
|
|
ini_set('display_errors', 0); |
|
97
|
|
|
error_reporting(-1); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
$this->loadModules($this->modules); |
|
101
|
|
|
|
|
102
|
|
|
$this->router = new Router($init_parameters["router"]["routes"]); |
|
103
|
|
|
$this->router->setBasePath($init_parameters["environment"]["base_path"]); |
|
104
|
|
|
|
|
105
|
|
|
# load routes from application.config.php |
|
106
|
|
|
if (file_exists("config/application.config.php")) |
|
107
|
|
|
{ |
|
108
|
|
|
$app_config_file = require "config/application.config.php"; |
|
109
|
|
|
|
|
110
|
|
|
foreach ($app_config_file["router"]["routes"] as $name => $route) |
|
111
|
|
|
{ |
|
112
|
|
|
if ($route instanceof \Zend\Router\Http\RouteInterface) |
|
113
|
|
|
$this->getRouter()->addZendRoute($name, $route); |
|
114
|
|
|
else |
|
115
|
|
|
$this->getRouter()->addRoute($route); |
|
116
|
|
|
} |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
# load routes from modules |
|
120
|
|
|
foreach ($this->modules as $module) |
|
121
|
|
|
{ |
|
122
|
|
|
if (file_exists("module/$module/config/module.config.php")) |
|
123
|
|
|
{ |
|
124
|
|
|
$module_config_file = require "module/$module/config/module.config.php"; |
|
125
|
|
|
$this->getRouter()->addRoute($module_config_file["router"]["routes"]); |
|
126
|
|
|
} |
|
127
|
|
|
} |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
/** |
|
131
|
|
|
* Loads module classes and autoloading functions |
|
132
|
|
|
* |
|
133
|
|
|
* @param array $modules |
|
134
|
|
|
* |
|
135
|
|
|
* @throws RuntimeException |
|
136
|
|
|
* |
|
137
|
|
|
* @return null |
|
138
|
|
|
*/ |
|
139
|
|
|
private function loadModules($modules) |
|
140
|
|
|
{ |
|
141
|
|
|
if (count($modules)) |
|
142
|
|
|
{ |
|
143
|
|
|
foreach ($modules as $module) |
|
144
|
|
|
{ |
|
145
|
|
|
/* |
|
146
|
|
|
* This instruction include each module declared in application.config.php |
|
147
|
|
|
* Each module has an autoloader to load its classes (controllers and models) |
|
148
|
|
|
*/ |
|
149
|
|
|
if (file_exists("module/".$module."/Module.php")) |
|
150
|
|
|
include("module/".$module."/Module.php"); |
|
151
|
|
|
|
|
152
|
|
|
spl_autoload_register($module . "\Module::loader"); |
|
153
|
|
|
} |
|
154
|
|
|
} |
|
155
|
|
|
else |
|
156
|
|
|
throw new \RuntimeException("The application must have at least one module"); |
|
157
|
|
|
} |
|
158
|
|
|
|
|
159
|
|
|
/** |
|
160
|
|
|
* Runs the application |
|
161
|
|
|
* |
|
162
|
|
|
* @return null |
|
163
|
|
|
*/ |
|
164
|
|
|
public function run() |
|
165
|
|
|
{ |
|
166
|
|
|
$module = isset($_GET["module"]) ? $_GET["module"] : null; |
|
167
|
|
|
$controller = isset($_GET["controller"]) ? $_GET["controller"] : null; |
|
168
|
|
|
$view = isset($_GET["view"]) ? $_GET["view"] : null; |
|
169
|
|
|
|
|
170
|
|
|
$request = new \Zend\Http\Request(); |
|
171
|
|
|
|
|
172
|
|
|
# build URI |
|
173
|
|
|
$uri = ''; |
|
174
|
|
|
$uri .= !empty($module) ? '/' . $module : ""; |
|
175
|
|
|
$uri .= !empty($controller) ? '/' . $controller : ""; |
|
176
|
|
|
$uri .= !empty($view) ? '/' . $view : ""; |
|
177
|
|
|
|
|
178
|
|
|
if (empty($uri)) |
|
179
|
|
|
$uri = "/"; |
|
180
|
|
|
|
|
181
|
|
|
$request->setUri($uri); |
|
182
|
|
|
|
|
183
|
|
|
$match = $this->router->getZendRouter()->match($request); |
|
184
|
|
|
|
|
185
|
|
|
if (!is_null($match)) |
|
186
|
|
|
{ |
|
187
|
|
|
$params = $match->getParams(); |
|
188
|
|
|
$parts = explode("\\", $params["controller"]); |
|
189
|
|
|
$module = $parts[0]; |
|
190
|
|
|
$controller = $parts[2]; |
|
191
|
|
|
$view = $params["action"]; |
|
192
|
|
|
|
|
193
|
|
|
$this->router->setIdentifiers($module, $controller, $view); |
|
194
|
|
|
} |
|
195
|
|
|
else |
|
196
|
|
|
$this->router->setIdentifiers($module, $controller, $view); |
|
197
|
|
|
|
|
198
|
|
|
$this->router->run(); |
|
199
|
|
|
} |
|
200
|
|
|
} |