diff --git a/src/App.php b/src/App.php index 07d0365..b562440 100644 --- a/src/App.php +++ b/src/App.php @@ -14,10 +14,33 @@ class App { + /** + * Dependency Injection container. + * + * @var mixed + */ private $container; + + /** + * Representation of an outgoing, client-side request. + * + * @var RequestInterface + */ private $request; + + /** + * Representation of an outgoing, server-side response. + * + * @var ResponseInterface + */ private $response; + /** + * Application initialization. + * + * @param mixed $router Routing system. + * @param mixed $container Dependency Injection container. + */ public function __construct($router = null, $container = null) { $this->container = $container; @@ -50,6 +73,15 @@ public function __construct($router = null, $container = null) $this->container = $container; } + /** + * Container compilation. + * + * @param mixed $config Configuration file/array. + * + * @link http://php-di.org/doc/php-definitions.html + * + * @return \DI\Container + */ private function buildContainer($config) { $builder = new ContainerBuilder(); @@ -59,53 +91,89 @@ private function buildContainer($config) return $builder->build(); } + /** + * Container getter. + * + * @return \DI\Container + */ public function getContainer() { return $this->container; } + /** + * Penny dispatcher getter. + * + * @return Dispatcher + */ + private function getDispatcher() + { + $container = $this->container; + + return $container->get('dispatcher'); + } + + /** + * Penny HTTP flow event getter. + * + * @return HttpFlowEvent + */ + private function getHttpFlow() + { + $container = $this->container; + + return $container->get('http.flow'); + } + + /** + * Application execution. + * + * @param RequestInterface|null $request Representation of an outgoing, client-side request. + * @param ResponseInterface|null $response Representation of an outgoing, server-side response. + * + * @return RequestInterface|mixed + */ public function run(RequestInterface $request = null, ResponseInterface $response = null) { ($request != null) ?: $request = $this->request; ($response != null) ?: $response = $this->response; $event = new HttpFlowEvent("bootstrap", $request, $response); + $container = $this->getContainer(); + $dispatcher = $this->getDispatcher(); + $httpFlow = $this->getHttpFlow(); + try { - $routerInfo = $this->getContainer()->get("dispatcher") - ->dispatch($request); - } catch (Exception $e) { + $routerInfo = $dispatcher->dispatch($request); + } catch (Exception $exception) { $event->setName("ERROR_DISPATCH"); - $event->setException($e); - $this->getContainer()->get("http.flow")->trigger($event); + $event->setException($exception); + $httpFlow->trigger($event); return $event->getResponse(); } - $controller = $this->getContainer()->get($routerInfo[1][0]); + $controller = $container->get($routerInfo[1][0]); $method = $routerInfo[1][1]; - $function = new ReflectionClass($controller); - $name = strtolower($function->getShortName()); + $function = (new ReflectionClass($controller))->getShortName(); - $eventName = "{$name}.{$method}"; + $eventName = sprintf('%s.%s', strtolower($function), $method); $event->setName($eventName); $event->setRouteInfo($routerInfo); - $this->getContainer()->get("http.flow")->attach($eventName, function ($event) use ($controller, $method) { - $args = [ - $event->getRequest(), - $event->getResponse(), - ]+$event->getRouteInfo()[2]; - - $response = call_user_func_array([$controller, $method], $args); - $event->setResponse($response); + $httpFlow->attach($eventName, function ($event) use ($controller, $method) { + $event->setResponse(call_user_func_array( + [$controller, $method], + [$event->getRequest(), $event->getResponse()] + $event->getRouteInfo()[2] + )); }, 0); try { - $this->getContainer()->get("http.flow")->trigger($event); + $httpFlow->trigger($event); } catch (Exception $exception) { $event->setName($eventName."_error"); $event->setException($exception); - $this->getContainer()->get("http.flow")->trigger($event); + $httpFlow->trigger($event); } return $event->getResponse(); diff --git a/src/Config/Loader.php b/src/Config/Loader.php index 11c1b8b..92e1a31 100644 --- a/src/Config/Loader.php +++ b/src/Config/Loader.php @@ -7,6 +7,13 @@ class Loader { + /** + * Configurations loader. + * + * @param string $pathRole Glob role. + * + * @return array + */ public static function load($pathRole = './config/{{*}}{{,*.local}}.php') { $config = []; diff --git a/src/Dispatcher.php b/src/Dispatcher.php index 349a399..188672b 100644 --- a/src/Dispatcher.php +++ b/src/Dispatcher.php @@ -11,16 +11,43 @@ class Dispatcher { + /** + * Inner dispatcher. + * + * @var FastRouterDispatcherInterface + */ private $router; + /** + * Class constructor with required FastRoute dispatcher implementation. + * + * @param FastRouterDispatcherInterface $router Inner router (based on Nikic FastRouter). + */ public function __construct(FastRouterDispatcherInterface $router) { $this->router = $router; } + /** + * Dispatching. + * + * @param RequestInterface $request Representation of an outgoing, client-side request. + * + * @throws RouteNotFound If the route is not found. + * @throws MethodNotAllowed If the method is not allowed. + * + * @return array + */ public function dispatch(RequestInterface $request) { - $routeInfo = $this->router->dispatch($request->getMethod(), $request->getUri()->getPath()); + $router = $this->router; + $uri = $request->getUri(); + + $routeInfo = $router->dispatch( + $request->getMethod(), + $uri->getPath() + ); + switch ($routeInfo[0]) { case BaseDispatcher::NOT_FOUND: throw new RouteNotFound(); diff --git a/src/Event/HttpFlowEvent.php b/src/Event/HttpFlowEvent.php index e9651e6..d5c467b 100644 --- a/src/Event/HttpFlowEvent.php +++ b/src/Event/HttpFlowEvent.php @@ -9,53 +9,131 @@ class HttpFlowEvent extends Event { + /** + * Representation of an outgoing, client-side request. + * + * @var RequestInterface|mixed + */ private $request; + + /** + * Representation of an outgoing, server-side response. + * + * @var ResponseInterface|mixed + */ private $response; + + /** + * Exception thrown during execution. + * + * @var Exception + */ private $exception; + + /** + * Routing information. + * + * @var array + */ private $routeInfo = []; + /** + * Class constructor. + * + * @param string $name Event name. + * @param mixed $request Representation of an outgoing, client-side request. + * @param mixed $response Representation of an outgoing, server-side response. + */ public function __construct($name, $request, $response) { - $this->name = $name; + $this->setName($name); $this->response = $response; $this->request = $request; } + /** + * Response getter. + * + * @return ResponseInterface|mixed + */ public function getResponse() { return $this->response; } + /** + * Response setter. + * + * @param ResponseInterface $response Representation of an outgoing, server-side response. + * + * @return void + */ public function setResponse(ResponseInterface $response) { $this->response = $response; } + /** + * Request setter. + * + * @param RequestInterface $request Representation of an outgoing, client-side request. + * + * @return void + */ public function setRequest(RequestInterface $request) { $this->request = $request; } + /** + * Request getter. + * + * @return RequestInterface|mixed + */ public function getRequest() { return $this->request; } + /** + * Route info getter. + * + * @return array + */ public function getRouteInfo() { return $this->routeInfo; } + /** + * Route info setter. + * + * @param array $routerInfo Routing information. + * + * @return void + */ public function setRouteInfo(array $routerInfo) { $this->routeInfo = $routerInfo; } + /** + * Exception setter. + * + * @param Exception $exception Exception thrown during execution. + * + * @return void + */ public function setException(Exception $exception) { $this->exception = $exception; } + /** + * Exception getter. + * + * @return Exception + */ public function getException() { return $this->exception; diff --git a/src/Exception/MethodNotAllowed.php b/src/Exception/MethodNotAllowed.php index e8223c5..57ad085 100644 --- a/src/Exception/MethodNotAllowed.php +++ b/src/Exception/MethodNotAllowed.php @@ -6,5 +6,10 @@ final class MethodNotAllowed extends Exception { + /** + * Exception code. + * + * @var integer + */ protected $code = 405; } diff --git a/src/Exception/RouteNotFound.php b/src/Exception/RouteNotFound.php index c0c1594..74d9931 100644 --- a/src/Exception/RouteNotFound.php +++ b/src/Exception/RouteNotFound.php @@ -6,5 +6,10 @@ final class RouteNotFound extends Exception { + /** + * Exception code. + * + * @var integer + */ protected $code = 404; }