Permalink
Browse files

Moved to listeners classes and services

Moved all listeners to dedicated classes

Dispatch method return the app response.

New test for hydrator strategies

Moved renderer under listener folder

Added tests for RouteListenerFactory
  • Loading branch information...
wdalmut committed Dec 27, 2013
1 parent 237209b commit acd5e6c50de2d68fdac90955ac5305836582798a
View
@@ -1,146 +1,92 @@
<?php
namespace UpCloo;
use Zend\Mvc\Router;
use Zend\Uri\UriInterface;
use Zend\EventManager\Event;
use Zend\Mvc\Router\Http\TreeRouteStack;
use Zend\EventManager\EventManager;
use Zend\EventManager\EventManagerInterface;
use Zend\Http\PhpEnvironment\Request;
use Zend\Http\PhpEnvironment\Response;
use Zend\ServiceManager\ServiceManager;
use Zend\ServiceManager\Config as ServiceManagerConfig;
use Zend\Stdlib\Hydrator\HydratorInterface;
use Zend\EventManager\Event;
use Zend\EventManager\EventManager;
use Zend\EventManager\EventManagerInterface;
class App
{
private $conf;
private $router;
private $events;
private $request;
private $response;
private $events;
private $serviceManager;
public function __construct(array $userConfigs)
{
$baseConfig = $this->getAnEmptyConf();
$baseConfig = $this->getBaseConf();
$this->conf = $this->mergeConfigs($baseConfig, $userConfigs);
}
private function mergeConfigs(array $conf, array $configs)
{
foreach ($configs as $confFile) {
$conf = array_replace_recursive($conf, $confFile);
$conf = $this->mergeConfig($conf, $confFile);
}
return $conf;
}
private function getAnEmptyConf()
public function appendConfig(array $userConf)
{
$this->conf = $this->mergeConfig($this->conf, $userConf);
}
private function mergeConfig($conf, $userConf)
{
return array_replace_recursive($conf, $userConf);
}
private function getBaseConf()
{
return [
"router" => [],
"services" => [
"invokables" => [
"UpCloo\\Renderer\\Json" => "UpCloo\\Renderer\\Json",
"UpCloo\\Renderer\\Jsonp" => "UpCloo\\Renderer\\Jsonp",
"UpCloo\\Listener\\Renderer\\Json" => "UpCloo\\Listener\\Renderer\\Json",
"UpCloo\\Listener\\Renderer\\Jsonp" => "UpCloo\\Listener\\Renderer\\Jsonp",
"Zend\\Stdlib\\Hydrator\\ClassMethods" => "Zend\\Stdlib\\Hydrator\\ClassMethods",
"UpCloo\\Listener\\SendResponseListener" => "UpCloo\\Listener\\SendResponseListener",
],
"factories" => [
"UpCloo\\Listener\\RouteListener" => "UpCloo\\Service\\RouteListenerFactory",
],
"aliases" => [
"renderer" => "UpCloo\\Renderer\\Jsonp",
"renderer.listener" => "UpCloo\\Listener\\Renderer\\Jsonp",
"route.listener" => "UpCloo\\Listener\\RouteListener",
"response.listener" => "UpCloo\\Listener\\SendResponseListener",
"hydrator" => "Zend\\Stdlib\\Hydrator\\ClassMethods",
]
],
"listeners" => []
"listeners" => [
"route" => [
["route.listener", "prepareControllerToBeExecuted"]
],
"pre.fetch" => [],
"execute" => [],
"renderer" => [
["renderer.listener", "render"]
],
"send.response" => [
["response.listener", "sendResponse"]
],
]
];
}
public function bootstrap()
{
$this->registerRouter();
$this->registerServices();
$this->registerListeners();
$this->events()->attach("route", [$this, "prepareControllerToBeExecuted"]);
$renderer = $this->services()->get("renderer");
if ($this->isNotARenderer($renderer)) {
throw new \InvalidArgumentException("The 'renderer' alias must implement Renderizable interface");
}
$this->events()->attach("renderer", [$renderer, "render"]);
$this->events()->attach("send.response", [$this, "sendResponse"]);
$this->registerListenersFromServices();
return $this;
}
public function sendResponse()
{
$this->response()->send();
}
public function prepareControllerToBeExecuted($event)
{
$request = $event->getParam('request');
$match = $this->getRouter()->match($request);
if ($match) {
$controller = $match->getParam("controller");
$action = $match->getParam("action");
$callable = $this->resolveCallableWithServiceManager([$controller, $action]);
if ($this->isHydratable($callable[0])) {
$this->hydrateCallableWithBaseServices($callable[0]);
}
$this->events()->attach("execute", $callable);
}
return $match;
}
private function isHydratable($type) {
return (is_object($type));
}
private function hydrateCallableWithBaseServices($object)
{
$data = [
"request" => $this->request(),
"response" => $this->response(),
"eventManager" => $this->events(),
"serviceManager" => $this->services(),
];
if ($this->hasValidHydrator()) {
$hydrator = $this->services()->get("hydrator");
$hydrator->hydrate($data, $object);
}
}
private function hasValidHydrator()
{
if ($this->services()->get("hydrator") instanceof HydratorInterface) {
return true;
}
return false;
}
private function isNotARenderer($renderer)
{
return (!($renderer instanceof Renderer\Renderizable));
}
private function registerRouter()
{
$this->router = TreeRouteStack::factory($this->conf["router"]);
}
private function registerServices()
{
$serviceManager = $this->services();
@@ -151,7 +97,7 @@ private function registerServices()
$this->services()->setService("Config", $this->conf);
}
private function registerListeners()
private function registerListenersFromServices()
{
foreach ($this->conf["listeners"] as $eventName => $callables) {
$this->registerCallbacks($eventName, $callables);
@@ -166,9 +112,9 @@ private function registerCallbacks($eventName, $callables)
}
}
private function resolveCallableWithServiceManager($callable)
public function resolveCallableWithServiceManager($callable)
{
if (is_array($callable)) {
if (is_array($callable) && is_string($callable[0])) {
if ($this->services()->has($callable[0])) {
$callable[0] = $this->services()->get($callable[0]);
}
@@ -177,28 +123,24 @@ private function resolveCallableWithServiceManager($callable)
return $callable;
}
public function getRouter()
public function event()
{
return $this->router;
$event = new Event();
$event->setTarget($this);
return $event;
}
public function setEventManager(EventManagerInterface $eventManager)
{
$this->events = $eventManager;
}
public function event()
{
$event = new Event();
$event->setTarget($this);
return $event;
}
public function events()
{
if (!$this->events instanceof EventManagerInterface) {
$this->setEventManager(new EventManager());
}
return $this->events;
}
@@ -254,7 +196,6 @@ public function response()
public function run()
{
$this->bootstrap();
$this->trigger("begin");
try {
@@ -279,7 +220,7 @@ public function run()
);
$this->trigger("finish");
$this->trigger("send.response");
$this->trigger("send.response", ['response' => $this->response()]);
}
private function dispatchUserRequest()
@@ -1,9 +1,9 @@
<?php
namespace UpCloo\Renderer;
namespace UpCloo\Listener\Renderer;
use Zend\EventManager\Event;
class Json implements Renderizable
class Json
{
public function render(Event $event)
{
@@ -1,5 +1,5 @@
<?php
namespace UpCloo\Renderer;
namespace UpCloo\Listener\Renderer;
use Zend\Http\Exception\InvalidArgumentException;
@@ -0,0 +1,76 @@
<?php
namespace UpCloo\Listener;
use Zend\Mvc\Router\RouteInterface as Router;
use Zend\Stdlib\Hydrator\HydratorInterface as Hydrator;
class RouteListener
{
private $router;
private $hydrator;
public function __construct(Router $router)
{
$this->router = $router;
}
public function getRouter()
{
return $this->router;
}
public function setHydrator($hydrator)
{
$this->hydrator = $hydrator;
}
public function getHydrator()
{
return $this->hydrator;
}
public function prepareControllerToBeExecuted($event)
{
$target = $event->getTarget();
$request = $event->getParam('request');
$match = $this->getRouter()->match($request);
if ($match) {
$controller = $match->getParam("controller");
$action = $match->getParam("action");
$callable = $target->resolveCallableWithServiceManager([$controller, $action]);
if ($this->isHydratable($callable[0])) {
if ($this->hasValidHydrator()) {
$hydrator = $this->getHydrator();
$data = [
"request" => $target->request(),
"response" => $target->response(),
"eventManager" => $target->events(),
"serviceManager" => $target->services(),
];
$hydrator->hydrate($data, $callable[0]);
}
}
$target->events()->attach("execute", $callable);
}
return $match;
}
private function isHydratable($type) {
return (is_object($type));
}
private function hasValidHydrator()
{
if ($this->getHydrator() instanceof Hydrator) {
return true;
}
return false;
}
}
@@ -0,0 +1,10 @@
<?php
namespace UpCloo\Listener;
class SendResponseListener
{
public function sendResponse($event)
{
$event->getParams()["response"]->send();
}
}
@@ -1,9 +0,0 @@
<?php
namespace UpCloo\Renderer;
use Zend\EventManager\Event;
interface Renderizable
{
public function render(Event $event);
}
@@ -0,0 +1,22 @@
<?php
namespace UpCloo\Service;
use Zend\ServiceManager\FactoryInterface;
use Zend\ServiceManager\ServiceLocatorInterface;
use Zend\Mvc\Router\Http\TreeRouteStack;
use UpCloo\Listener\RouteListener;
class RouteListenerFactory implements FactoryInterface
{
public function createService(ServiceLocatorInterface $serviceLocator)
{
$router = TreeRouteStack::factory($serviceLocator->get("Config")["router"]);
$routeListener = new RouteListener($router);
$hydrator = $serviceLocator->get("hydrator");
$routeListener->setHydrator($hydrator);
return $routeListener;
}
}
Oops, something went wrong.

0 comments on commit acd5e6c

Please sign in to comment.