Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

OC + PHPDoc #28

Merged
merged 10 commits into from
Aug 30, 2015
106 changes: 87 additions & 19 deletions src/App.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Expand All @@ -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();
Expand Down
7 changes: 7 additions & 0 deletions src/Config/Loader.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,13 @@

class Loader
{
/**
* Configurations loader.
*
* @param string $pathRole Glob role.
*
* @return array
*/
public static function load($pathRole = './config/{{*}}{{,*.local}}.php')
{
$config = [];
Expand Down
29 changes: 28 additions & 1 deletion src/Dispatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
80 changes: 79 additions & 1 deletion src/Event/HttpFlowEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
5 changes: 5 additions & 0 deletions src/Exception/MethodNotAllowed.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,10 @@

final class MethodNotAllowed extends Exception
{
/**
* Exception code.
*
* @var integer
*/
protected $code = 405;
}
5 changes: 5 additions & 0 deletions src/Exception/RouteNotFound.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,10 @@

final class RouteNotFound extends Exception
{
/**
* Exception code.
*
* @var integer
*/
protected $code = 404;
}