Skip to content

Commit 9f1d541

Browse files
committed
Merge pull request #35 from kenjis/codeingiter-class
Refactor 404 logic
2 parents 20f28e3 + febb5cd commit 9f1d541

File tree

2 files changed

+95
-75
lines changed

2 files changed

+95
-75
lines changed

system/CodeIgniter.php

Lines changed: 94 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -157,15 +157,22 @@ public function run()
157157
//--------------------------------------------------------------------
158158
Hooks::trigger('pre_controller');
159159

160-
$this->startController();
160+
try
161+
{
162+
$this->startController();
161163

162-
//--------------------------------------------------------------------
163-
// Is there a "post_controller" hook?
164-
//--------------------------------------------------------------------
165-
Hooks::trigger('post_controller');
164+
//--------------------------------------------------------------------
165+
// Is there a "post_controller" hook?
166+
//--------------------------------------------------------------------
167+
Hooks::trigger('post_controller');
166168

167-
$this->gatherOutput();
168-
$this->sendResponse();
169+
$this->gatherOutput();
170+
$this->sendResponse();
171+
}
172+
catch (PageNotFoundException $e)
173+
{
174+
$this->display404errors($e);
175+
}
169176

170177
//--------------------------------------------------------------------
171178
// Is there a post-system hook?
@@ -300,11 +307,15 @@ protected function loadComposerAutoloader()
300307
*/
301308
protected function getRequestObject()
302309
{
303-
$this->request = is_cli()
304-
? Services::clirequest($this->config, true)
305-
: Services::request($this->config, true);
306-
307-
$this->request->setProtocolVersion($_SERVER['SERVER_PROTOCOL']);
310+
if (is_cli())
311+
{
312+
$this->request = Services::clirequest($this->config, true);
313+
}
314+
else
315+
{
316+
$this->request = Services::request($this->config, true);
317+
$this->request->setProtocolVersion($_SERVER['SERVER_PROTOCOL']);
318+
}
308319
}
309320

310321
//--------------------------------------------------------------------
@@ -316,7 +327,10 @@ protected function getRequestObject()
316327
protected function getResponseObject()
317328
{
318329
$this->response = Services::response($this->config, true);
319-
$this->response->setProtocolVersion($this->request->getProtocolVersion());
330+
if ( ! is_cli())
331+
{
332+
$this->response->setProtocolVersion($this->request->getProtocolVersion());
333+
}
320334

321335
// Assume success until proven otherwise.
322336
$this->response->setStatusCode(200);
@@ -415,8 +429,6 @@ protected function startController()
415429
$this->benchmark->start('controller');
416430
$this->benchmark->start('controller_constructor');
417431

418-
$e404 = false;
419-
420432
// Is it routed to a Closure?
421433
if (is_callable($this->controller))
422434
{
@@ -426,92 +438,100 @@ protected function startController()
426438
{
427439
if (empty($this->controller))
428440
{
429-
$e404 = true;
441+
throw new PageNotFoundException('Controller is empty.');
430442
}
431443
else
432444
{
433445
// Try to autoload the class
434446
if ( ! class_exists($this->controller, true) || $this->method[0] === '_')
435447
{
436-
$e404 = true;
448+
throw new PageNotFoundException('Controller or its method is not found.');
437449
}
438450
else if ( ! method_exists($this->controller, '_remap') &&
439451
! is_callable([$this->controller, $this->method], false)
440452
)
441453
{
442-
$e404 = true;
454+
throw new PageNotFoundException('Controller method is not found.');
443455
}
444456
}
457+
}
445458

446-
// Is there a 404 Override available?
447-
if ($e404 && $override = $this->router->get404Override())
448-
{
449-
if ($override instanceof \Closure)
450-
{
451-
echo $override();
452-
}
453-
else if (is_array($override))
454-
{
455-
$this->controller = $override[0];
456-
$this->method = $override[1];
459+
$this->createControllerAndRun();
460+
}
457461

458-
unset($override);
459-
}
462+
protected function createControllerAndRun()
463+
{
464+
$class = new $this->controller($this->request, $this->response);
460465

461-
$e404 = false;
462-
}
466+
$this->benchmark->stop('controller_constructor');
463467

464-
// Display 404 Errors
465-
if ($e404)
466-
{
467-
$this->response->setStatusCode(404);
468+
//--------------------------------------------------------------------
469+
// Is there a "post_controller_constructor" hook?
470+
//--------------------------------------------------------------------
471+
Hooks::trigger('post_controller_constructor');
468472

469-
if (ob_get_level() > 0)
470-
{
471-
ob_end_flush();
472-
}
473-
ob_start();
473+
if (method_exists($class, '_remap'))
474+
{
475+
$class->_remap($this->method, ...$this->router->params());
476+
}
477+
else
478+
{
479+
$class->{$this->method}(...$this->router->params());
480+
}
474481

475-
// Show the 404 error page
476-
if (is_cli())
477-
{
478-
require APPPATH.'Views/errors/cli/error_404.php';
479-
}
480-
else
481-
{
482-
require APPPATH.'Views/errors/html/error_404.php';
483-
}
482+
$this->benchmark->stop('controller');
483+
}
484484

485-
$buffer = ob_get_contents();
486-
ob_end_clean();
485+
protected function display404errors(PageNotFoundException $e)
486+
{
487+
// Is there a 404 Override available?
488+
if ($override = $this->router->get404Override())
489+
{
490+
if ($override instanceof \Closure)
491+
{
492+
echo $override();
493+
}
494+
else if (is_array($override))
495+
{
496+
$this->controller = $override[0];
497+
$this->method = $override[1];
487498

488-
echo $buffer;
489-
exit(EXIT_UNKNOWN_FILE); // Unknown file
499+
unset($override);
490500
}
491501

492-
if ( ! $e404 && ! isset($override))
493-
{
494-
$class = new $this->controller($this->request, $this->response);
502+
$this->createControllerAndRun();
503+
$this->gatherOutput();
504+
$this->sendResponse();
505+
return;
506+
}
495507

496-
$this->benchmark->stop('controller_constructor');
508+
// Display 404 Errors
509+
$this->response->setStatusCode(404);
497510

498-
//--------------------------------------------------------------------
499-
// Is there a "post_controller_constructor" hook?
500-
//--------------------------------------------------------------------
501-
Hooks::trigger('post_controller_constructor');
511+
if (ob_get_level() > 0)
512+
{
513+
ob_end_flush();
514+
}
515+
ob_start();
502516

503-
if (method_exists($class, '_remap'))
504-
{
505-
$class->_remap($this->method, ...$this->router->params());
506-
}
507-
else
508-
{
509-
$class->{$this->method}(...$this->router->params());
510-
}
511-
}
517+
$heading = 'Page Not Found';
518+
$message = $e->getMessage();
519+
520+
// Show the 404 error page
521+
if (is_cli())
522+
{
523+
require APPPATH.'Views/errors/cli/error_404.php';
524+
}
525+
else
526+
{
527+
require APPPATH.'Views/errors/html/error_404.php';
512528
}
513529

514-
$this->benchmark->stop('controller');
530+
$buffer = ob_get_contents();
531+
ob_end_clean();
532+
533+
echo $buffer;
534+
exit(EXIT_UNKNOWN_FILE); // Unknown file
515535
}
516536

517537
//--------------------------------------------------------------------
@@ -532,7 +552,7 @@ protected function gatherOutput()
532552
//--------------------------------------------------------------------
533553
// Display the Debug Toolbar?
534554
//--------------------------------------------------------------------
535-
if (ENVIRONMENT != 'production' && $this->config->toolbarEnabled)
555+
if ( ! is_cli() && ENVIRONMENT != 'production' && $this->config->toolbarEnabled)
536556
{
537557
$toolbar = Services::toolbar($this->config);
538558
$this->output .= $toolbar->run($this->startTime, $totalTime,

system/HTTP/Message.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -330,7 +330,7 @@ public function prependHeader(string $name, $value): self
330330
*/
331331
public function getProtocolVersion(): string
332332
{
333-
return $this->protocolVersion;
333+
return $this->protocolVersion;
334334
}
335335

336336
//--------------------------------------------------------------------

0 commit comments

Comments
 (0)