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

Build all from dependency injection #88

Merged
merged 5 commits into from
Oct 12, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 17 additions & 10 deletions src/App.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -67,7 +66,7 @@ private function getDispatcher()
/**
* Penny HTTP flow event getter.
*
* @return HttpFlowEvent
* @return EventManager
*/
private function getEventManager()
{
Expand All @@ -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();
Expand Down
10 changes: 9 additions & 1 deletion src/Container/PHPDiFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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')),
]
Expand Down
27 changes: 6 additions & 21 deletions src/Event/HttpFlowEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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);
Expand All @@ -50,39 +43,31 @@ public function __construct($name, $request, $response)
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wow, you hate docblocks very much, huh? :D

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a war with @PHP-DI :P

/**
* Response getter.
*
* @return mixed
* {@inheritDoc}
*/
public function getResponse()
{
return $this->response;
}

/**
* Response setter.
*
* @param mixed $response Representation of an outgoing, server-side response.
* {@inheritDoc}
*/
public function setResponse($response)
{
$this->response = $response;
}

/**
* Request setter.
*
* @param mixed $request Representation of an outgoing, client-side request.
* {@inheritDoc}
*/
public function setRequest($request)
{
$this->request = $request;
}

/**
* Request getter.
*
* @return mixed
* {@inheritDoc}
*/
public function getRequest()
{
Expand All @@ -104,7 +89,7 @@ public function getRouteInfo()
*
* @param array $routerInfo Routing information.
*/
public function setRouteInfo(array $routerInfo)
public function setRouteInfo($routerInfo)
{
$this->routeInfo = $routerInfo;
}
Expand Down
18 changes: 18 additions & 0 deletions src/Event/PennyEventInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php
namespace Penny\Event;

use Exception;
use Zend\EventManager\EventInterface;

interface PennyEventInterface extends EventInterface
{
public function __construct($name, $request, $response);
public function setResponse($response);
public function getResponse();
public function setRequest($request);
public function getRequest();
public function getRouteInfo();
public function setRouteInfo($routerInfo);
public function setException(Exception $exception);
public function getException();
}