diff --git a/src/App.php b/src/App.php index 373c119..afe61a2 100644 --- a/src/App.php +++ b/src/App.php @@ -4,11 +4,10 @@ use Exception; use Penny\Config\Loader; -use Penny\Event\HttpFlowEvent; +use Penny\Event\PennyEventInterface; use ReflectionClass; -use Zend\Diactoros\Response; -use Zend\Diactoros\ServerRequestFactory; use Interop\Container\ContainerInterface; +use Zend\EventManager\EventManager; class App { @@ -67,7 +66,7 @@ private function getDispatcher() /** * Penny HTTP flow event getter. * - * @return HttpFlowEvent + * @return EventManager */ private function getEventManager() { @@ -77,17 +76,25 @@ private function getEventManager() /** * Application execution. * - * @param mixed|null $request Representation of an outgoing, client-side request. - * @param mixed|null $response Representation of an incoming, server-side response. + * @param mixed|null $request Representation of an outgoing, + * client-side request. + * @param mixed|null $response Representation of an incoming, + * server-side response. * * @return mixed */ public function run($request = null, $response = null) { - $request = $request ?: ServerRequestFactory::fromGlobals(); - $response = $response ?: new Response(); - - $event = new HttpFlowEvent('bootstrap', $request, $response); + $event = $this->getContainer()->get('http_flow_event'); + if (!($event instanceof PennyEventInterface)) { + throw new \RuntimeException('This event did not supported'); + } + if ($request != null) { + $event->setRequest($request); + } + if ($response != null) { + $event->setResponse($response); + } $dispatcher = $this->getDispatcher(); $httpFlow = $this->getEventManager(); diff --git a/src/Container/PHPDiFactory.php b/src/Container/PHPDiFactory.php index 2fafbbd..3db6968 100644 --- a/src/Container/PHPDiFactory.php +++ b/src/Container/PHPDiFactory.php @@ -21,7 +21,15 @@ public static function buildContainer($config = []) $builder->useAnnotations(true); $builder->addDefinitions( [ - 'event_manager' => DI\object('Zend\EventManager\EventManager'), + 'request' => \Zend\Diactoros\ServerRequestFactory::fromGlobals(), + 'response' => DI\object('Zend\Diactoros\Response'), + 'http_flow_event' => DI\object('Penny\Event\HttpFlowEvent') + ->constructor( + 'bootstrap', + DI\get('request'), + DI\get('response') + ), + 'event_manager' => DI\object('Zend\EventManager\EventManager'), 'dispatcher' => DI\object('Penny\Dispatcher') ->constructor(DI\get('router')), ] diff --git a/src/Event/HttpFlowEvent.php b/src/Event/HttpFlowEvent.php index 9f14ac6..479c9fd 100644 --- a/src/Event/HttpFlowEvent.php +++ b/src/Event/HttpFlowEvent.php @@ -5,7 +5,7 @@ use Exception; use Zend\EventManager\Event; -class HttpFlowEvent extends Event +class HttpFlowEvent extends Event implements PennyEventInterface { /** * Representation of an outgoing, client-side request. @@ -35,13 +35,6 @@ class HttpFlowEvent extends Event */ 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->setName($name); @@ -50,9 +43,7 @@ public function __construct($name, $request, $response) } /** - * Response getter. - * - * @return mixed + * {@inheritDoc} */ public function getResponse() { @@ -60,9 +51,7 @@ public function getResponse() } /** - * Response setter. - * - * @param mixed $response Representation of an outgoing, server-side response. + * {@inheritDoc} */ public function setResponse($response) { @@ -70,9 +59,7 @@ public function setResponse($response) } /** - * Request setter. - * - * @param mixed $request Representation of an outgoing, client-side request. + * {@inheritDoc} */ public function setRequest($request) { @@ -80,9 +67,7 @@ public function setRequest($request) } /** - * Request getter. - * - * @return mixed + * {@inheritDoc} */ public function getRequest() { @@ -104,7 +89,7 @@ public function getRouteInfo() * * @param array $routerInfo Routing information. */ - public function setRouteInfo(array $routerInfo) + public function setRouteInfo($routerInfo) { $this->routeInfo = $routerInfo; } diff --git a/src/Event/PennyEventInterface.php b/src/Event/PennyEventInterface.php new file mode 100644 index 0000000..3a4ea4c --- /dev/null +++ b/src/Event/PennyEventInterface.php @@ -0,0 +1,18 @@ +