Created
July 26, 2023 22:40
-
-
Save vihugarcia/a361b3415d91b6deee87c2be5e4ba24a to your computer and use it in GitHub Desktop.
Chapter 09 Application.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace SimpleMVC\core; | |
class Application { | |
public function setReporting() | |
{ | |
if (DEVELOPMENT_ENVIRONMENT) { | |
error_reporting(E_ALL); | |
ini_set('display_error', 'on'); | |
} else { | |
error_reporting(E_ALL); | |
ini_set('display_error', 'off'); | |
ini_set('error_log', ROOT . DS . 'tmp' . DS . 'logs' . DS . 'error.log'); | |
} | |
} | |
public function stripSlashesDeep($value) | |
{ | |
(is_array($value)) ? array_map([$this, 'stripSlashesDeep'], $value) : stripslashes($value); | |
return $value; | |
} | |
public function removeMagicQuotes() | |
{ | |
$_GET = $this->stripSlashesDeep($_GET); | |
} | |
public function run($url) | |
{ | |
$url = explode('/', $url); | |
$controller = array_shift($url); | |
$controllerClass = 'App\\controllers\\' . ucwords($controller) . 'Controller'; | |
$action = array_shift($url); | |
$queryString = $url; | |
try { | |
if (class_exists($controllerClass)) { | |
$parents = class_parents($controllerClass); | |
if (in_array('SimpleMVC\\core\\Controller', $parents)) { | |
if (method_exists($controllerClass, $action)) { | |
DependencyResolver::set('table', $controller . 's'); | |
DependencyResolver::set('controller', $controller . 's'); | |
$dispatch = DependencyResolver::resolveDependencies($controllerClass); | |
call_user_func_array([$dispatch, $action], $queryString); | |
} else { | |
// Bad action | |
throw new ResourceNotFoundException('Not found', 404); | |
} | |
} else { | |
// Bad controller | |
throw new ResourceNotFoundException('Not found', 404); | |
} | |
} else { | |
// Bad controller | |
throw new ResourceNotFoundException('Not found', 404); | |
} | |
} catch (ResourceNotFoundException $e) { | |
$response = new Response(); | |
$response->setResponseHeader(404, 'Not found'); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment