2016-04-12 19:31:39 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace b8;
|
|
|
|
|
|
|
|
use b8\Exception\HttpException\NotFoundException;
|
|
|
|
use b8\Http;
|
2016-04-14 19:10:08 +02:00
|
|
|
use b8\Http\Response;
|
|
|
|
use b8\Http\Request;
|
2016-04-12 19:31:39 +02:00
|
|
|
|
|
|
|
class Application
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
protected $route;
|
|
|
|
|
|
|
|
/**
|
2016-04-14 19:10:08 +02:00
|
|
|
* @var Controller
|
2016-04-12 19:31:39 +02:00
|
|
|
*/
|
|
|
|
protected $controller;
|
|
|
|
|
|
|
|
/**
|
2016-04-14 19:10:08 +02:00
|
|
|
* @var Request
|
2016-04-12 19:31:39 +02:00
|
|
|
*/
|
|
|
|
protected $request;
|
|
|
|
|
|
|
|
/**
|
2016-04-14 19:10:08 +02:00
|
|
|
* @var Response
|
2016-04-12 19:31:39 +02:00
|
|
|
*/
|
|
|
|
protected $response;
|
|
|
|
|
|
|
|
/**
|
2016-04-14 19:10:08 +02:00
|
|
|
* @var Config
|
2016-04-12 19:31:39 +02:00
|
|
|
*/
|
|
|
|
protected $config;
|
|
|
|
|
|
|
|
public function __construct(Config $config, Http\Request $request = null)
|
|
|
|
{
|
|
|
|
$this->config = $config;
|
|
|
|
$this->response = new Http\Response();
|
|
|
|
|
|
|
|
if (!is_null($request)) {
|
|
|
|
$this->request = $request;
|
|
|
|
} else {
|
|
|
|
$this->request = new Http\Request();
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->router = new Http\Router($this, $this->request, $this->config);
|
|
|
|
|
|
|
|
if (method_exists($this, 'init')) {
|
|
|
|
$this->init();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function handleRequest()
|
|
|
|
{
|
|
|
|
$this->route = $this->router->dispatch();
|
|
|
|
|
|
|
|
if (!empty($this->route['callback'])) {
|
|
|
|
$callback = $this->route['callback'];
|
|
|
|
|
|
|
|
if (!$callback($this->route, $this->response)) {
|
|
|
|
return $this->response;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-14 19:10:08 +02:00
|
|
|
if (!$this->controllerExists($this->route)) {
|
|
|
|
throw new NotFoundException('Controller ' . $this->toPhpName($this->route['controller']) . ' does not exist!');
|
|
|
|
}
|
2016-04-12 19:31:39 +02:00
|
|
|
|
2016-04-14 19:10:08 +02:00
|
|
|
$action = lcfirst($this->toPhpName($this->route['action']));
|
2016-04-12 19:31:39 +02:00
|
|
|
if (!$this->getController()->hasAction($action)) {
|
2016-04-14 19:10:08 +02:00
|
|
|
throw new NotFoundException('Controller ' . $this->toPhpName($this->route['controller']) . ' does not have action ' . $action . '!');
|
2016-04-12 19:31:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return $this->getController()->handleAction($action, $this->route['args']);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-04-14 19:10:08 +02:00
|
|
|
* @return Controller
|
2016-04-12 19:31:39 +02:00
|
|
|
*/
|
|
|
|
public function getController()
|
|
|
|
{
|
|
|
|
if (empty($this->controller)) {
|
2016-04-14 19:10:08 +02:00
|
|
|
$controllerClass = $this->getControllerClass($this->route);
|
2016-04-12 19:31:39 +02:00
|
|
|
$this->controller = $this->loadController($controllerClass);
|
|
|
|
}
|
|
|
|
return $this->controller;
|
|
|
|
}
|
|
|
|
|
2016-04-14 19:10:08 +02:00
|
|
|
/**
|
|
|
|
* @param string $class
|
|
|
|
*
|
|
|
|
* @return Controller
|
|
|
|
*/
|
2016-04-12 19:31:39 +02:00
|
|
|
protected function loadController($class)
|
|
|
|
{
|
|
|
|
$controller = new $class($this->config, $this->request, $this->response);
|
|
|
|
$controller->init();
|
|
|
|
return $controller;
|
|
|
|
}
|
|
|
|
|
2016-04-14 19:10:08 +02:00
|
|
|
/**
|
|
|
|
* @param array $route
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
2016-04-12 19:31:39 +02:00
|
|
|
protected function controllerExists($route)
|
2016-04-14 19:10:08 +02:00
|
|
|
{
|
|
|
|
return class_exists($this->getControllerClass($route));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param array $route
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
protected function getControllerClass($route)
|
2016-04-12 19:31:39 +02:00
|
|
|
{
|
|
|
|
$namespace = $this->toPhpName($route['namespace']);
|
|
|
|
$controller = $this->toPhpName($route['controller']);
|
2016-04-14 19:10:08 +02:00
|
|
|
return $this->config->get('b8.app.namespace') . '\\' . $namespace . '\\' . $controller . 'Controller';
|
2016-04-12 19:31:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function isValidRoute($route)
|
|
|
|
{
|
|
|
|
if ($this->controllerExists($route)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function toPhpName($string)
|
|
|
|
{
|
|
|
|
$string = str_replace('-', ' ', $string);
|
|
|
|
$string = ucwords($string);
|
|
|
|
$string = str_replace(' ', '', $string);
|
|
|
|
|
|
|
|
return $string;
|
|
|
|
}
|
|
|
|
}
|