| Total Complexity | 103 |
| Total Lines | 795 |
| Duplicated Lines | 0 % |
| Changes | 9 | ||
| Bugs | 3 | Features | 0 |
Complex classes like Dispatcher often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Dispatcher, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 46 | class Dispatcher |
||
| 47 | { |
||
| 48 | /** |
||
| 49 | * Heure de démarrage de l'application. |
||
| 50 | * |
||
| 51 | * @var mixed |
||
| 52 | */ |
||
| 53 | protected $startTime; |
||
| 54 | |||
| 55 | /** |
||
| 56 | * Durée totale d'exécution de l'application |
||
| 57 | * |
||
| 58 | * @var float |
||
| 59 | */ |
||
| 60 | protected $totalTime; |
||
| 61 | |||
| 62 | /** |
||
| 63 | * Main application configuration |
||
| 64 | * |
||
| 65 | * @var stdClass |
||
| 66 | */ |
||
| 67 | protected $config; |
||
| 68 | |||
| 69 | /** |
||
| 70 | * instance Timer. |
||
| 71 | * |
||
| 72 | * @var Timer |
||
| 73 | */ |
||
| 74 | protected $timer; |
||
| 75 | |||
| 76 | /** |
||
| 77 | * requête courrante. |
||
| 78 | * |
||
| 79 | * @var ServerRequest |
||
| 80 | */ |
||
| 81 | protected $request; |
||
| 82 | |||
| 83 | /** |
||
| 84 | * Reponse courrante. |
||
| 85 | * |
||
| 86 | * @var Response |
||
| 87 | */ |
||
| 88 | protected $response; |
||
| 89 | |||
| 90 | /** |
||
| 91 | * Router à utiliser. |
||
| 92 | * |
||
| 93 | * @var Router |
||
| 94 | */ |
||
| 95 | protected $router; |
||
| 96 | |||
| 97 | /** |
||
| 98 | * @var Middleware |
||
| 99 | */ |
||
| 100 | private $middleware; |
||
| 101 | |||
| 102 | /** |
||
| 103 | * Contrôleur à utiliser. |
||
| 104 | * |
||
| 105 | * @var Closure|string |
||
| 106 | */ |
||
| 107 | protected $controller; |
||
| 108 | |||
| 109 | /** |
||
| 110 | * Méthode du ontrôleur à exécuter. |
||
| 111 | * |
||
| 112 | * @var string |
||
| 113 | */ |
||
| 114 | protected $method; |
||
| 115 | |||
| 116 | /** |
||
| 117 | * Gestionnaire de sortie à utiliser. |
||
| 118 | * |
||
| 119 | * @var string |
||
| 120 | */ |
||
| 121 | protected $output; |
||
| 122 | |||
| 123 | /** |
||
| 124 | * Chemin de requête à utiliser. |
||
| 125 | * |
||
| 126 | * @var string |
||
| 127 | * |
||
| 128 | * @deprecated No longer used. |
||
| 129 | */ |
||
| 130 | protected $path; |
||
| 131 | |||
| 132 | /** |
||
| 133 | * Application output buffering level |
||
| 134 | */ |
||
| 135 | protected int $bufferLevel = 0; |
||
| 136 | |||
| 137 | /** |
||
| 138 | * Web Page Caching |
||
| 139 | */ |
||
| 140 | protected ResponseCache $pageCache; |
||
| 141 | |||
| 142 | /** |
||
| 143 | * Constructor. |
||
| 144 | */ |
||
| 145 | public function __construct(protected EventManagerInterface $event) |
||
| 146 | { |
||
| 147 | $this->startTime = microtime(true); |
||
| 148 | $this->config = (object) config('app'); |
||
| 149 | |||
| 150 | $this->pageCache = Services::factory(ResponseCache::class, [ |
||
| 151 | 'cacheQueryString' => config('cache.cache_query_string') |
||
| 152 | ]); |
||
| 153 | } |
||
| 154 | |||
| 155 | /** |
||
| 156 | * Retourne la methode invoquee |
||
| 157 | */ |
||
| 158 | public static function getMethod(): ?string |
||
| 159 | { |
||
| 160 | $method = Services::singleton(self::class)->method; |
||
| 161 | if (empty($method)) { |
||
| 162 | $method = Services::routes()->getDefaultMethod(); |
||
| 163 | } |
||
| 164 | |||
| 165 | return $method; |
||
| 166 | } |
||
| 167 | |||
| 168 | /** |
||
| 169 | * Retourne le contrôleur utilisé |
||
| 170 | * |
||
| 171 | * @return Closure|string |
||
| 172 | */ |
||
| 173 | public static function getController(bool $fullName = true) |
||
| 174 | { |
||
| 175 | $routes = Services::routes(); |
||
| 176 | |||
| 177 | $controller = Services::singleton(self::class)->controller; |
||
| 178 | if (empty($controller)) { |
||
| 179 | $controller = $routes->getDefaultController(); |
||
| 180 | } |
||
| 181 | |||
| 182 | if (! $fullName && is_string($controller)) { |
||
| 183 | $controller = str_replace($routes->getDefaultNamespace(), '', $controller); |
||
| 184 | } |
||
| 185 | |||
| 186 | return $controller; |
||
| 187 | } |
||
| 188 | |||
| 189 | /** |
||
| 190 | * Lancez l'application ! |
||
| 191 | * |
||
| 192 | * C'est "la boucle" si vous voulez. Le principal point d'entrée dans le script |
||
| 193 | * qui obtient les instances de classe requises, déclenche les filtres, |
||
| 194 | * essaie d'acheminer la réponse, charge le contrôleur et généralement |
||
| 195 | * fait fonctionner toutes les pièces ensemble. |
||
| 196 | * |
||
| 197 | * @return bool|mixed|ResponseInterface|ServerRequestInterface |
||
| 198 | * |
||
| 199 | * @throws Exception |
||
| 200 | * @throws RedirectException |
||
| 201 | */ |
||
| 202 | public function run(?RouteCollectionInterface $routes = null, bool $returnResponse = false) |
||
| 203 | { |
||
| 204 | $this->pageCache->setTtl(0); |
||
| 205 | $this->bufferLevel = ob_get_level(); |
||
| 206 | |||
| 207 | $this->startBenchmark(); |
||
| 208 | |||
| 209 | $this->getRequestObject(); |
||
| 210 | $this->getResponseObject(); |
||
| 211 | |||
| 212 | $this->initMiddlewareQueue(); |
||
| 213 | |||
| 214 | try { |
||
| 215 | $this->response = $this->handleRequest($routes, config('cache')); |
||
| 216 | } catch (ResponsableInterface|RedirectException $e) { |
||
| 217 | $this->outputBufferingEnd(); |
||
| 218 | if ($e instanceof RedirectException) { |
||
| 219 | $e = new RedirectException($e->getMessage(), $e->getCode(), $e); |
||
| 220 | } |
||
| 221 | |||
| 222 | $this->response = $e->getResponse(); |
||
| 223 | } catch (PageNotFoundException $e) { |
||
| 224 | $this->response = $this->display404errors($e); |
||
| 225 | } catch (Throwable $e) { |
||
| 226 | $this->outputBufferingEnd(); |
||
| 227 | |||
| 228 | throw $e; |
||
| 229 | } |
||
| 230 | |||
| 231 | if ($returnResponse) { |
||
| 232 | return $this->response; |
||
| 233 | } |
||
| 234 | |||
| 235 | $this->sendResponse(); |
||
| 236 | } |
||
| 237 | |||
| 238 | /** |
||
| 239 | * Gère la logique de requête principale et déclenche le contrôleur. |
||
| 240 | * |
||
| 241 | * @throws PageNotFoundException |
||
| 242 | * @throws RedirectException |
||
| 243 | */ |
||
| 244 | protected function handleRequest(?RouteCollectionInterface $routes = null, ?array $cacheConfig = null): ResponseInterface |
||
| 245 | { |
||
| 246 | $this->forceSecureAccess(); |
||
| 247 | |||
| 248 | /** |
||
| 249 | * Init event manager |
||
| 250 | */ |
||
| 251 | Services::singleton(EventDiscover::class)->discove(); |
||
| 252 | |||
| 253 | $this->event->trigger('pre_system'); |
||
| 254 | |||
| 255 | // Check for a cached page. |
||
| 256 | // Execution will stop if the page has been cached. |
||
| 257 | if (($response = $this->displayCache($cacheConfig)) instanceof ResponseInterface) { |
||
| 258 | return $response; |
||
| 259 | } |
||
| 260 | |||
| 261 | $routeMiddlewares = (array) $this->dispatchRoutes($routes); |
||
| 262 | |||
| 263 | // Le bootstrap dans un middleware |
||
| 264 | $this->middleware->alias('blitz', $this->bootApp()); |
||
| 265 | $this->middleware->append('blitz'); |
||
| 266 | |||
| 267 | /** |
||
| 268 | * Ajouter des middlewares de routes |
||
| 269 | */ |
||
| 270 | foreach ($routeMiddlewares as $middleware) { |
||
| 271 | $this->middleware->prepend($middleware); |
||
| 272 | } |
||
| 273 | |||
| 274 | // Enregistrer notre URI actuel en tant qu'URI précédent dans la session |
||
| 275 | // pour une utilisation plus sûre et plus précise avec la fonction d'assistance `previous_url()`. |
||
| 276 | $this->storePreviousURL(current_url(true)); |
||
| 277 | |||
| 278 | /** |
||
| 279 | * Emission de la reponse |
||
| 280 | */ |
||
| 281 | $this->gatherOutput($this->middleware->handle($this->request)); |
||
| 282 | |||
| 283 | // Y a-t-il un événement post-système ? |
||
| 284 | $this->event->trigger('post_system'); |
||
| 285 | |||
| 286 | return $this->response; |
||
| 287 | } |
||
| 288 | |||
| 289 | /** |
||
| 290 | * Démarrer le benchmark |
||
| 291 | * |
||
| 292 | * La minuterie est utilisée pour afficher l'exécution totale du script à la fois dans la |
||
| 293 | * barre d'outils de débogage, et éventuellement sur la page affichée. |
||
| 294 | */ |
||
| 295 | protected function startBenchmark() |
||
| 296 | { |
||
| 297 | if ($this->startTime === null) { |
||
| 298 | $this->startTime = microtime(true); |
||
| 299 | } |
||
| 300 | |||
| 301 | $this->timer = Services::timer(); |
||
| 302 | $this->timer->start('total_execution', $this->startTime); |
||
| 303 | $this->timer->start('bootstrap'); |
||
| 304 | } |
||
| 305 | |||
| 306 | /** |
||
| 307 | * Définit un objet Request à utiliser pour cette requête. |
||
| 308 | * Utilisé lors de l'exécution de certains tests. |
||
| 309 | */ |
||
| 310 | public function setRequest(ServerRequestInterface $request): self |
||
| 311 | { |
||
| 312 | $this->request = $request; |
||
| 313 | |||
| 314 | return $this; |
||
| 315 | } |
||
| 316 | |||
| 317 | /** |
||
| 318 | * Obtenez notre objet Request et définissez le protocole du serveur en fonction des informations fournies |
||
| 319 | * par le serveur. |
||
| 320 | */ |
||
| 321 | protected function getRequestObject() |
||
| 322 | { |
||
| 323 | if ($this->request instanceof ServerRequestInterface) { |
||
| 324 | return; |
||
| 325 | } |
||
| 326 | |||
| 327 | if (is_cli() && ! on_test()) { |
||
| 328 | // @codeCoverageIgnoreStart |
||
| 329 | // $this->request = Services::clirequest($this->config); |
||
| 330 | // @codeCoverageIgnoreEnd |
||
| 331 | } |
||
| 332 | |||
| 333 | $version = $_SERVER['SERVER_PROTOCOL'] ?? 'HTTP/1.1'; |
||
| 334 | if (! is_numeric($version)) { |
||
| 335 | $version = substr($version, strpos($version, '/') + 1); |
||
| 336 | } |
||
| 337 | |||
| 338 | // Assurez-vous que la version est au bon format |
||
| 339 | $version = number_format((float) $version, 1); |
||
| 340 | |||
| 341 | $this->request = Services::request()->withProtocolVersion($version); |
||
| 342 | } |
||
| 343 | |||
| 344 | /** |
||
| 345 | * Obtenez notre objet Response et définissez des valeurs par défaut, notamment |
||
| 346 | * la version du protocole HTTP et une réponse réussie par défaut. |
||
| 347 | */ |
||
| 348 | protected function getResponseObject() |
||
| 349 | { |
||
| 350 | // Supposons le succès jusqu'à preuve du contraire. |
||
| 351 | $this->response = Services::response()->withStatus(200); |
||
| 352 | |||
| 353 | if (! is_cli() || on_test()) { |
||
| 354 | } |
||
| 355 | |||
| 356 | $this->response = $this->response->withProtocolVersion($this->request->getProtocolVersion()); |
||
| 357 | } |
||
| 358 | |||
| 359 | /** |
||
| 360 | * Forcer l'accès au site sécurisé ? Si la valeur de configuration 'forceGlobalSecureRequests' |
||
| 361 | * est vrai, imposera que toutes les demandes adressées à ce site soient effectuées via |
||
| 362 | * HTTPS. Redirigera également l'utilisateur vers la page actuelle avec HTTPS |
||
| 363 | * comme défini l'en-tête HTTP Strict Transport Security pour ces navigateurs |
||
| 364 | * qui le supportent. |
||
| 365 | * |
||
| 366 | * @param int $duration Combien de temps la sécurité stricte des transports |
||
| 367 | * doit être appliqué pour cette URL. |
||
| 368 | */ |
||
| 369 | protected function forceSecureAccess($duration = 31536000) |
||
| 370 | { |
||
| 371 | if ($this->config->force_global_secure_requests !== true) { |
||
| 372 | return; |
||
| 373 | } |
||
| 374 | |||
| 375 | force_https($duration, $this->request, $this->response); |
||
| 376 | } |
||
| 377 | |||
| 378 | /** |
||
| 379 | * Détermine si une réponse a été mise en cache pour l'URI donné. |
||
| 380 | * |
||
| 381 | * @return bool|ResponseInterface |
||
| 382 | * |
||
| 383 | * @throws FrameworkException |
||
| 384 | */ |
||
| 385 | public function displayCache(?array $config = null) |
||
| 386 | { |
||
| 387 | if ($cachedResponse = $this->pageCache->get($this->request, $this->response)) { |
||
| 388 | $this->response = $cachedResponse; |
||
| 389 | |||
| 390 | $this->totalTime = $this->timer->getElapsedTime('total_execution'); |
||
| 391 | $output = $this->displayPerformanceMetrics($cachedResponse->getBody()); |
||
| 392 | |||
| 393 | return $this->response->withBody(to_stream($output)); |
||
| 394 | } |
||
| 395 | |||
| 396 | return false; |
||
| 397 | } |
||
| 398 | |||
| 399 | /** |
||
| 400 | * Renvoie un tableau avec nos statistiques de performances de base collectées. |
||
| 401 | */ |
||
| 402 | public function getPerformanceStats(): array |
||
| 403 | { |
||
| 404 | return [ |
||
| 405 | 'startTime' => $this->startTime, |
||
| 406 | 'totalTime' => $this->totalTime, |
||
| 407 | ]; |
||
| 408 | } |
||
| 409 | |||
| 410 | /** |
||
| 411 | * Remplace les balises memory_usage et elapsed_time. |
||
| 412 | */ |
||
| 413 | public function displayPerformanceMetrics(string $output): string |
||
| 414 | { |
||
| 415 | $this->totalTime = $this->timer->getElapsedTime('total_execution'); |
||
| 416 | |||
| 417 | return str_replace('{elapsed_time}', (string) $this->totalTime, $output); |
||
| 418 | } |
||
| 419 | |||
| 420 | /** |
||
| 421 | * Fonctionne avec le routeur pour |
||
| 422 | * faire correspondre une route à l'URI actuel. Si la route est une |
||
| 423 | * "route de redirection", gérera également la redirection. |
||
| 424 | * |
||
| 425 | * @param RouteCollectionInterface|null $routes Une interface de collecte à utiliser à la place |
||
| 426 | * du fichier de configuration. |
||
| 427 | * |
||
| 428 | * @return string[] |
||
| 429 | * |
||
| 430 | * @throws RedirectException |
||
| 431 | */ |
||
| 432 | protected function dispatchRoutes(?RouteCollectionInterface $routes = null): array |
||
| 433 | { |
||
| 434 | if ($routes === null) { |
||
| 435 | $routes = Services::routes()->loadRoutes(); |
||
| 436 | } |
||
| 437 | |||
| 438 | $this->router = Services::router($routes, $this->request, false); |
||
| 439 | |||
| 440 | $path = $this->determinePath(); |
||
| 441 | |||
| 442 | $this->timer->stop('bootstrap'); |
||
| 443 | $this->timer->start('routing'); |
||
| 444 | |||
| 445 | $this->outputBufferingStart(); |
||
| 446 | |||
| 447 | $this->controller = $this->router->handle($path ?: '/'); |
||
| 448 | $this->method = $this->router->methodName(); |
||
| 449 | |||
| 450 | // Si un segment {locale} correspondait dans la route finale, |
||
| 451 | // alors nous devons définir les paramètres régionaux corrects sur notre requête. |
||
| 452 | if ($this->router->hasLocale()) { |
||
| 453 | $this->request = $this->request->withLocale($this->router->getLocale()); |
||
| 454 | } |
||
| 455 | |||
| 456 | $this->timer->stop('routing'); |
||
| 457 | |||
| 458 | return $this->router->getMiddlewares(); |
||
| 459 | } |
||
| 460 | |||
| 461 | /** |
||
| 462 | * Détermine le chemin à utiliser pour que nous essayions d'acheminer vers, en fonction |
||
| 463 | * de l'entrée de l'utilisateur (setPath), ou le chemin CLI/IncomingRequest. |
||
| 464 | */ |
||
| 465 | protected function determinePath(): string |
||
| 466 | { |
||
| 467 | if (! empty($this->path)) { |
||
| 468 | return $this->path; |
||
| 469 | } |
||
| 470 | |||
| 471 | $path = method_exists($this->request, 'getPath') |
||
| 472 | ? $this->request->getPath() |
||
| 473 | : $this->request->getUri()->getPath(); |
||
| 474 | |||
| 475 | return $this->path = preg_replace('#^' . App::getUri()->getPath() . '#i', '', $path); |
||
| 476 | } |
||
| 477 | |||
| 478 | /** |
||
| 479 | * Maintenant que tout a été configuré, cette méthode tente d'exécuter le |
||
| 480 | * méthode du contrôleur et lancez le script. S'il n'en est pas capable, le fera |
||
| 481 | * afficher l'erreur Page introuvable appropriée. |
||
| 482 | */ |
||
| 483 | protected function startController(ServerRequest $request, Response $response) |
||
| 484 | { |
||
| 485 | // Aucun contrôleur spécifié - nous ne savons pas quoi faire maintenant. |
||
| 486 | if (empty($this->controller)) { |
||
| 487 | throw PageNotFoundException::emptyController(); |
||
| 488 | } |
||
| 489 | |||
| 490 | $this->timer->start('controller'); |
||
| 491 | $this->timer->start('controller_constructor'); |
||
| 492 | |||
| 493 | // Est-il acheminé vers une Closure ? |
||
| 494 | if (is_object($this->controller) && (get_class($this->controller) === 'Closure')) { |
||
| 495 | $controller = $this->controller; |
||
| 496 | |||
| 497 | $sendParameters = []; |
||
| 498 | |||
| 499 | foreach ($this->router->params() as $parameter) { |
||
| 500 | $sendParameters[] = $parameter; |
||
| 501 | } |
||
| 502 | array_push($sendParameters, $request, $response); |
||
| 503 | |||
| 504 | return Services::injector()->call($controller, $sendParameters); |
||
| 505 | } |
||
| 506 | |||
| 507 | // Essayez de charger automatiquement la classe |
||
| 508 | if (! class_exists($this->controller, true) || ($this->method[0] === '_' && $this->method !== '__invoke')) { |
||
| 509 | throw PageNotFoundException::controllerNotFound($this->controller, $this->method); |
||
| 510 | } |
||
| 511 | |||
| 512 | return null; |
||
| 513 | } |
||
| 514 | |||
| 515 | /** |
||
| 516 | * Instancie la classe contrôleur. |
||
| 517 | * |
||
| 518 | * @return \BlitzPHP\Controllers\BaseController|mixed |
||
| 519 | */ |
||
| 520 | private function createController(ServerRequestInterface $request, ResponseInterface $response) |
||
| 521 | { |
||
| 522 | /** |
||
| 523 | * @var \BlitzPHP\Controllers\BaseController |
||
| 524 | */ |
||
| 525 | $class = Services::injector()->get($this->controller); |
||
| 526 | |||
| 527 | if (method_exists($class, 'initialize')) { |
||
| 528 | $class->initialize($request, $response, Services::logger()); |
||
| 529 | } |
||
| 530 | |||
| 531 | $this->timer->stop('controller_constructor'); |
||
| 532 | |||
| 533 | return $class; |
||
| 534 | } |
||
| 535 | |||
| 536 | /** |
||
| 537 | * Exécute le contrôleur, permettant aux méthodes _remap de fonctionner. |
||
| 538 | * |
||
| 539 | * @param mixed $class |
||
| 540 | * |
||
| 541 | * @return mixed |
||
| 542 | */ |
||
| 543 | protected function runController($class) |
||
| 544 | { |
||
| 545 | // S'il s'agit d'une demande de console, utilisez les segments d'entrée comme paramètres |
||
| 546 | $params = defined('KLINGED') ? $this->request->getSegments() : $this->router->params(); |
||
| 547 | $method = $this->method; |
||
| 548 | |||
| 549 | if (method_exists($class, '_remap')) { |
||
| 550 | $params = [$method, $params]; |
||
| 551 | $method = '_remap'; |
||
| 552 | } |
||
| 553 | |||
| 554 | $output = Services::injector()->call([$class, $method], (array) $params); |
||
| 555 | |||
| 556 | $this->timer->stop('controller'); |
||
| 557 | |||
| 558 | if ($output instanceof View) { |
||
| 559 | $output = $this->response->withBody(to_stream($output->get())); |
||
| 560 | } |
||
| 561 | |||
| 562 | return $output; |
||
| 563 | } |
||
| 564 | |||
| 565 | /** |
||
| 566 | * Affiche une page d'erreur 404 introuvable. S'il est défini, essaiera de |
||
| 567 | * appelez le contrôleur/méthode 404Override qui a été défini dans la configuration de routage. |
||
| 568 | */ |
||
| 569 | protected function display404errors(PageNotFoundException $e) |
||
| 570 | { |
||
| 571 | // Existe-t-il une dérogation 404 disponible ? |
||
| 572 | if ($override = $this->router->get404Override()) { |
||
| 573 | $returned = null; |
||
| 574 | |||
| 575 | if ($override instanceof Closure) { |
||
| 576 | echo $override($e->getMessage()); |
||
| 577 | } elseif (is_array($override)) { |
||
| 578 | $this->timer->start('controller'); |
||
| 579 | $this->timer->start('controller_constructor'); |
||
| 580 | |||
| 581 | $this->controller = $override[0]; |
||
| 582 | $this->method = $override[1]; |
||
| 583 | |||
| 584 | $controller = $this->createController($this->request, $this->response); |
||
| 585 | $returned = $this->runController($controller); |
||
| 586 | } |
||
| 587 | |||
| 588 | unset($override); |
||
| 589 | |||
| 590 | $this->gatherOutput($returned); |
||
| 591 | |||
| 592 | return $this->response; |
||
| 593 | } |
||
| 594 | |||
| 595 | // Affiche l'erreur 404 |
||
| 596 | $this->response = $this->response->withStatus($e->getCode()); |
||
| 597 | |||
| 598 | echo $this->outputBufferingEnd(); |
||
| 599 | flush(); |
||
| 600 | |||
| 601 | throw PageNotFoundException::pageNotFound(! on_prod() || is_cli() ? $e->getMessage() : ''); |
||
| 602 | } |
||
| 603 | |||
| 604 | /** |
||
| 605 | * Rassemble la sortie du script à partir du tampon, remplace certaines balises d'exécutions |
||
| 606 | * d'horodatage dans la sortie et affiche la barre d'outils de débogage, si nécessaire. |
||
| 607 | * |
||
| 608 | * @param mixed|null $returned |
||
| 609 | */ |
||
| 610 | protected function gatherOutput($returned = null) |
||
| 611 | { |
||
| 612 | $this->output = $this->outputBufferingEnd(); |
||
| 613 | |||
| 614 | // Si le contrôleur a renvoyé un objet de réponse, |
||
| 615 | // nous devons en saisir le corps pour qu'il puisse |
||
| 616 | // être ajouté à tout ce qui aurait déjà pu être ajouté avant de faire le écho. |
||
| 617 | // Nous devons également enregistrer l'instance localement |
||
| 618 | // afin que tout changement de code d'état, etc., ait lieu. |
||
| 619 | if ($returned instanceof ResponseInterface) { |
||
| 620 | $this->response = $returned; |
||
| 621 | $returned = $returned->getBody()->getContents(); |
||
| 622 | } |
||
| 623 | |||
| 624 | if (is_string($returned)) { |
||
| 625 | $this->output .= $returned; |
||
| 626 | } |
||
| 627 | |||
| 628 | $this->response = $this->response->withBody(to_stream($this->output)); |
||
| 629 | } |
||
| 630 | |||
| 631 | /** |
||
| 632 | * Si nous avons un objet de session à utiliser, stockez l'URI actuel |
||
| 633 | * comme l'URI précédent. Ceci est appelé juste avant d'envoyer la |
||
| 634 | * réponse au client, et le rendra disponible à la prochaine demande. |
||
| 635 | * |
||
| 636 | * Cela permet au fournisseur une détection plus sûre et plus fiable de la fonction previous_url(). |
||
| 637 | * |
||
| 638 | * @param \BlitzPHP\Http\URI|string $uri |
||
| 639 | */ |
||
| 640 | public function storePreviousURL($uri) |
||
| 641 | { |
||
| 642 | // Ignorer les requêtes CLI |
||
| 643 | if (is_cli() && ! on_test()) { |
||
| 644 | return; // @codeCoverageIgnore |
||
| 645 | } |
||
| 646 | |||
| 647 | // Ignorer les requêtes AJAX |
||
| 648 | if (method_exists($this->request, 'isAJAX') && $this->request->isAJAX()) { |
||
| 649 | return; |
||
| 650 | } |
||
| 651 | |||
| 652 | // Ignorer les reponses non-HTML |
||
| 653 | if (strpos($this->response->getHeaderLine('Content-Type'), 'text/html') === false) { |
||
| 654 | return; |
||
| 655 | } |
||
| 656 | |||
| 657 | // Ceci est principalement nécessaire lors des tests ... |
||
| 658 | if (is_string($uri)) { |
||
| 659 | $uri = Services::uri($uri, false); |
||
| 660 | } |
||
| 661 | |||
| 662 | Services::session()->setPreviousUrl(Uri::createURIString( |
||
| 663 | $uri->getScheme(), |
||
| 664 | $uri->getAuthority(), |
||
| 665 | $uri->getPath(), |
||
| 666 | $uri->getQuery(), |
||
| 667 | $uri->getFragment() |
||
| 668 | )); |
||
| 669 | } |
||
| 670 | |||
| 671 | /** |
||
| 672 | * Renvoie la sortie de cette requête au client. |
||
| 673 | * C'est ce qu'il attendait ! |
||
| 674 | */ |
||
| 675 | protected function sendResponse() |
||
| 676 | { |
||
| 677 | $this->totalTime = $this->timer->getElapsedTime('total_execution'); |
||
| 678 | Services::emitter()->emit( |
||
| 679 | Services::toolbar()->prepare($this->getPerformanceStats(), $this->request, $this->response) |
||
| 680 | ); |
||
| 681 | } |
||
| 682 | |||
| 683 | protected function emitResponse() |
||
| 684 | { |
||
| 685 | $this->gatherOutput(); |
||
| 686 | $this->sendResponse(); |
||
| 687 | } |
||
| 688 | |||
| 689 | /** |
||
| 690 | * Construit une reponse adequate en fonction du retour du controleur |
||
| 691 | * |
||
| 692 | * @param mixed $returned |
||
| 693 | */ |
||
| 694 | protected function formatResponse(ResponseInterface $response, $returned): ResponseInterface |
||
| 695 | { |
||
| 696 | if ($returned instanceof ResponseInterface) { |
||
| 697 | return $returned; |
||
| 698 | } |
||
| 699 | |||
| 700 | if ($returned instanceof Responsable) { |
||
| 701 | return $returned->toResponse($this->request); |
||
| 702 | } |
||
| 703 | |||
| 704 | if (is_object($returned)) { |
||
| 705 | if (method_exists($returned, '__toString')) { |
||
| 706 | $returned = $returned->__toString(); |
||
| 707 | } elseif (method_exists($returned, 'toArray')) { |
||
| 708 | $returned = $returned->toArray(); |
||
| 709 | } elseif (method_exists($returned, 'toJSON')) { |
||
| 710 | $returned = $returned->toJSON(); |
||
| 711 | } else { |
||
| 712 | $returned = (array) $returned; |
||
| 713 | } |
||
| 714 | } |
||
| 715 | |||
| 716 | if (is_array($returned)) { |
||
| 717 | $returned = Helpers::collect($returned); |
||
| 718 | $response = $response->withHeader('Content-Type', 'application/json'); |
||
| 719 | } |
||
| 720 | |||
| 721 | try { |
||
| 722 | $response = $response->withBody(to_stream($returned)); |
||
| 723 | } catch (InvalidArgumentException $e) { |
||
| 724 | } |
||
| 725 | |||
| 726 | return $response; |
||
| 727 | } |
||
| 728 | |||
| 729 | /** |
||
| 730 | * Initialise le gestionnaire de middleware |
||
| 731 | */ |
||
| 732 | protected function initMiddlewareQueue(): void |
||
| 748 | } |
||
| 749 | |||
| 750 | protected function outputBufferingStart(): void |
||
| 751 | { |
||
| 752 | $this->bufferLevel = ob_get_level(); |
||
| 753 | |||
| 754 | ob_start(); |
||
| 755 | } |
||
| 756 | |||
| 757 | protected function outputBufferingEnd(): string |
||
| 758 | { |
||
| 759 | $buffer = ''; |
||
| 760 | |||
| 761 | while (ob_get_level() > $this->bufferLevel) { |
||
| 762 | $buffer .= ob_get_contents(); |
||
| 763 | ob_end_clean(); |
||
| 764 | } |
||
| 765 | |||
| 766 | return $buffer; |
||
| 767 | } |
||
| 768 | |||
| 769 | /** |
||
| 770 | * Modifie l'objet de requête pour utiliser une méthode différente |
||
| 771 | * si une variable POST appelée _method est trouvée. |
||
| 772 | */ |
||
| 773 | private function spoofRequestMethod(): callable |
||
| 787 | }; |
||
| 788 | } |
||
| 789 | |||
| 790 | private function bootApp(): callable |
||
| 791 | { |
||
| 792 | return function (ServerRequestInterface $request, ResponseInterface $response, callable $next): ResponseInterface { |
||
| 793 | try { |
||
| 794 | $returned = $this->startController($request, $response); |
||
| 795 | |||
| 796 | // Closure controller has run in startController(). |
||
| 797 | if (! is_callable($this->controller)) { |
||
| 798 | $controller = $this->createController($request, $response); |
||
| 799 | |||
| 800 | if (! method_exists($controller, '_remap') && ! is_callable([$controller, $this->method], false)) { |
||
| 801 | throw PageNotFoundException::methodNotFound($this->method); |
||
| 802 | } |
||
| 803 | |||
| 804 | // Y'a t-il un evenement "post_controller_constructor" |
||
| 805 | $this->event->trigger('post_controller_constructor'); |
||
| 806 | |||
| 807 | $returned = $this->runController($controller); |
||
| 808 | } else { |
||
| 809 | $this->timer->stop('controller_constructor'); |
||
| 810 | $this->timer->stop('controller'); |
||
| 841 | } |
||
| 842 | }; |
||
| 843 | } |
||
| 845 |
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths