These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | /** |
||
3 | * Pimf |
||
4 | * |
||
5 | * @copyright Copyright (c) Gjero Krsteski (http://krsteski.de) |
||
6 | * @license http://opensource.org/licenses/MIT MIT License |
||
7 | */ |
||
8 | |||
9 | namespace Pimf; |
||
10 | |||
11 | use Pimf\Util\Character as Str, Pimf\Util\Header, Pimf\Util\Header\ResponseStatus, Pimf\Util\Uuid; |
||
12 | |||
13 | /** |
||
14 | * Provides a facility for applications which provides reusable resources, |
||
15 | * common-based bootstrapping and dependency checking. |
||
16 | * |
||
17 | * @package Pimf |
||
18 | * @author Gjero Krsteski <[email protected]> |
||
19 | * |
||
20 | */ |
||
21 | final class Application |
||
22 | { |
||
23 | const VERSION = '1.9.0'; |
||
24 | |||
25 | /** |
||
26 | * @var Environment |
||
27 | */ |
||
28 | protected static $env; |
||
29 | |||
30 | /** |
||
31 | * @var Logger |
||
32 | */ |
||
33 | protected static $logger; |
||
34 | |||
35 | /** |
||
36 | * @var EntityManager |
||
37 | */ |
||
38 | protected static $em; |
||
39 | |||
40 | /** |
||
41 | * @var Router |
||
42 | */ |
||
43 | protected static $router; |
||
44 | |||
45 | /** |
||
46 | * Mechanism used to do initial setup and edging before a application runs. |
||
47 | * |
||
48 | * @param array $conf The array of configuration options. |
||
49 | * @param array $server Array of information such as headers, paths, and script locations. |
||
50 | * |
||
51 | * @return boolean|null |
||
52 | */ |
||
53 | public static function bootstrap(array $conf, array $server = array()) |
||
54 | { |
||
55 | $problems = array(); |
||
56 | |||
57 | try { |
||
58 | |||
59 | Config::load($conf); |
||
60 | |||
61 | $environment = Config::get('environment'); |
||
62 | |||
63 | date_default_timezone_set(Config::get('timezone')); |
||
64 | |||
65 | self::setupUtils($server, Config::get('bootstrap.local_temp_directory')); |
||
66 | self::loadListeners(BASE_PATH . 'app/' . Config::get('app.name') . '/events.php'); |
||
67 | self::setupErrorHandling($environment); |
||
68 | self::loadPdoDriver($environment, Config::get($environment . '.db'), Config::get('app.name')); |
||
69 | self::loadRoutes( |
||
70 | Config::get('app.routeable'), |
||
71 | BASE_PATH . 'app/' . Config::get('app.name') . '/routes.php' |
||
72 | ); |
||
73 | |||
74 | } catch (\Throwable $throwable) { |
||
75 | $problems[] = $throwable->getMessage(); |
||
76 | } catch (\Exception $exception) { |
||
77 | $problems[] = $exception->getMessage(); |
||
78 | } |
||
79 | |||
80 | self::reportIf($problems, PHP_VERSION); |
||
81 | } |
||
82 | |||
83 | /** |
||
84 | * Please bootstrap first, than run the application! |
||
85 | * Run a application, let application accept a request, route the request, |
||
86 | * dispatch to controller/action, render response and return response to client finally. |
||
87 | * |
||
88 | * @param array $get Array of variables passed to the current script via the URL parameters. |
||
89 | * @param array $post Array of variables passed to the current script via the HTTP POST method. |
||
90 | * @param array $cookie Array of variables passed to the current script via HTTP Cookies. |
||
91 | * @param array $files An associative array FILES of items uploaded to the current script via the HTTP POST method. |
||
92 | * |
||
93 | * @return void |
||
94 | */ |
||
95 | public static function run(array $get, array $post, array $cookie, array $files) |
||
96 | { |
||
97 | $cli = array(); |
||
98 | if (Sapi::isCli()) { |
||
99 | $cli = Cli::parse((array)self::$env->argv); |
||
100 | if (count($cli) < 1 || isset($cli['list'])) { |
||
101 | Cli::absorb(); |
||
102 | exit(0); |
||
103 | } |
||
104 | } |
||
105 | |||
106 | $prefix = Str::ensureTrailing('\\', Config::get('app.name')); |
||
107 | $repository = BASE_PATH . 'app/' . Config::get('app.name') . '/Controller'; |
||
108 | |||
109 | if (isset($cli['controller']) && $cli['controller'] == 'core') { |
||
110 | $prefix = 'Pimf\\'; |
||
111 | $repository = BASE_PATH . 'pimf-framework/core/Pimf/Controller'; |
||
112 | } |
||
113 | |||
114 | $request = new Request($get, $post, $cookie, $cli, $files, self::$env); |
||
115 | $resolver = new Resolver($request, $repository, $prefix, self::$router); |
||
116 | $sessionized = (Sapi::isWeb() && Config::get('session.storage') !== ''); |
||
117 | |||
118 | if ($sessionized) { |
||
119 | Session::load(); |
||
120 | } |
||
121 | |||
122 | $pimf = $resolver->process(self::$env, self::$em, self::$logger); |
||
123 | |||
124 | if ($sessionized) { |
||
125 | Session::save(); |
||
126 | Cookie::send(); |
||
127 | } |
||
128 | |||
129 | $pimf->render(); |
||
130 | } |
||
131 | |||
132 | /** |
||
133 | * @param string $environment |
||
134 | */ |
||
135 | private static function setupErrorHandling($environment) |
||
136 | { |
||
137 | if ($environment == 'testing') { |
||
138 | error_reporting(E_ALL | E_STRICT); |
||
139 | } else { |
||
140 | |||
141 | $logger = self::$logger; |
||
142 | |||
143 | set_exception_handler( |
||
144 | function ($exception) use ($logger) { |
||
145 | Error::exception($exception, $logger); |
||
146 | } |
||
147 | ); |
||
148 | |||
149 | set_error_handler( |
||
150 | function ($code, $error, $file, $line) use ($logger) { |
||
151 | Error::native($code, $error, $file, $line, $logger, error_reporting()); |
||
152 | } |
||
153 | ); |
||
154 | |||
155 | register_shutdown_function( |
||
156 | function () use ($logger) { |
||
157 | Error::shutdown($logger, error_get_last()); |
||
158 | } |
||
159 | ); |
||
160 | |||
161 | error_reporting(-1); |
||
162 | } |
||
163 | } |
||
164 | |||
165 | /** |
||
166 | * @param array $server |
||
167 | * @param string $tmpPath |
||
168 | */ |
||
169 | private static function setupUtils(array $server, $tmpPath) |
||
170 | { |
||
171 | self::$env = new Environment($server); |
||
172 | $envData = self::$env->data(); |
||
173 | |||
174 | Logger::setup( |
||
175 | self::$env->getIp(), |
||
176 | $envData->get('PHP_SELF', $envData->get('SCRIPT_NAME')) |
||
177 | ); |
||
178 | |||
179 | ResponseStatus::setup($envData->get('SERVER_PROTOCOL', 'HTTP/1.0')); |
||
180 | |||
181 | Header::setup( |
||
182 | self::$env->getUserAgent(), |
||
183 | self::$env->HTTP_IF_MODIFIED_SINCE, |
||
0 ignored issues
–
show
|
|||
184 | self::$env->HTTP_IF_NONE_MATCH |
||
185 | ); |
||
186 | |||
187 | Url::setup(self::$env->getUrl(), self::$env->isHttps()); |
||
188 | Uri::setup(self::$env->PATH_INFO, self::$env->REQUEST_URI); |
||
189 | Uuid::setup(self::$env->getIp(), self::$env->getHost()); |
||
190 | |||
191 | self::$logger = new Logger($tmpPath); |
||
192 | self::$logger->init(); |
||
193 | } |
||
194 | |||
195 | /** |
||
196 | * @param string $environment |
||
197 | * @param array $dbConf |
||
198 | * @param string $appName |
||
199 | */ |
||
200 | private static function loadPdoDriver($environment, $dbConf, $appName) |
||
201 | { |
||
202 | if (is_array($dbConf) && $environment != 'testing') { |
||
203 | self::$em = new EntityManager(Pdo\Factory::get($dbConf), $appName); |
||
204 | } |
||
205 | } |
||
206 | |||
207 | /** |
||
208 | * @param boolean $routeable |
||
209 | * @param string $routes Path to routes definition file. |
||
210 | */ |
||
211 | private static function loadRoutes($routeable, $routes) |
||
212 | { |
||
213 | if ($routeable === true && file_exists($routes)) { |
||
214 | |||
215 | self::$router = new Router(); |
||
216 | |||
217 | foreach ((array)(include $routes) as $route) { |
||
218 | |||
219 | self::$router->map($route); |
||
220 | |||
221 | } |
||
222 | } |
||
223 | } |
||
224 | |||
225 | /** |
||
226 | * @param string $events Path to event listeners |
||
227 | */ |
||
228 | private static function loadListeners($events) |
||
229 | { |
||
230 | if (file_exists($events)) { |
||
231 | include_once $events; |
||
232 | } |
||
233 | } |
||
234 | |||
235 | /** |
||
236 | * @param array $problems |
||
237 | * @param float $version |
||
238 | * @param bool $die |
||
239 | * |
||
240 | * @return array|void |
||
241 | */ |
||
242 | private static function reportIf(array $problems, $version, $die = true) |
||
243 | { |
||
244 | if (version_compare($version, 5.3) == -1) { |
||
245 | $problems[] = 'You have PHP ' . $version . ' and you need 5.3 or higher!'; |
||
246 | } |
||
247 | |||
248 | if (!empty($problems)) { |
||
249 | return ($die === true) ? die(implode(PHP_EOL . PHP_EOL, $problems)) : $problems; |
||
250 | } |
||
251 | } |
||
252 | |||
253 | /** |
||
254 | * PIMF Application can not be cloned. |
||
255 | */ |
||
256 | private function __clone() |
||
257 | { |
||
258 | } |
||
259 | |||
260 | /** |
||
261 | * Stopping the PHP process for PHP-FastCGI users to speed up some PHP queries. |
||
262 | */ |
||
263 | public static function finish() |
||
264 | { |
||
265 | if (function_exists('fastcgi_finish_request')) { |
||
266 | fastcgi_finish_request(); |
||
267 | } |
||
268 | } |
||
269 | } |
||
270 |
This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.
If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.
In this case you can add the
@ignore
PhpDoc annotation to the duplicate definition and it will be ignored.