Skip to content

Instantly share code, notes, and snippets.

@vihugarcia
Last active September 3, 2023 13:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vihugarcia/ee51012f349ce8889292df044c10c530 to your computer and use it in GitHub Desktop.
Save vihugarcia/ee51012f349ce8889292df044c10c530 to your computer and use it in GitHub Desktop.
Chapter 9: Application.php
<?php
namespace 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 = 'controllers\\' . ucwords($controller) . 'Controller';
$action = array_shift($url);
$queryString = $url;
try {
if (class_exists($controllerClass)) {
$parents = class_parents($controllerClass);
if (in_array('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