Skip to content
This repository has been archived by the owner on Feb 10, 2023. It is now read-only.

Commit

Permalink
Adds more app events
Browse files Browse the repository at this point in the history
  • Loading branch information
stevewest committed May 13, 2016
1 parent 4952c2b commit c989b6d
Show file tree
Hide file tree
Showing 9 changed files with 228 additions and 28 deletions.
32 changes: 25 additions & 7 deletions src/Application.php
Expand Up @@ -15,7 +15,12 @@
use Fuel\Config\Container as ConfigContainer;
use Fuel\Config\Container;
use Fuel\Dependency\Container as DependencyContainer;
use Fuel\Foundation\Event\AppShutdown;
use Fuel\Foundation\Event\AppStarted;
use Fuel\Foundation\Event\RequestFinished;
use Fuel\Foundation\Event\RequestStarted;
use Fuel\Foundation\Event\ResponseFinished;
use Fuel\Foundation\Event\ResponseStarted;
use Fuel\Foundation\Request\RequestInterface;
use Fuel\Foundation\Response\ResponseInterface;
use Fuel\Routing\Router;
Expand Down Expand Up @@ -93,32 +98,45 @@ public function run()
$request = $this->dependencyContainer->get('fuel.application.request');
$response = $this->performRequest($request);

// trigger response started event
$this->dependencyContainer
->get('fuel.application.event')
->emit(new ResponseStarted($this));

http_response_code($response->getStatusCode());
echo $response->getBody();

// TODO: send shutdown event
// send shutdown event
$this->dependencyContainer
->get('fuel.application.event')
->emit(new ResponseFinished($this))
->emit(new AppShutdown($this));
}

public function performRequest(RequestInterface $request) : ResponseInterface
{
$this->dependencyContainer->add('fuel.application.request', $request);

// TODO: trigger request started event
// trigger request started event
$this->dependencyContainer
->get('fuel.application.event')
->emit(new RequestStarted($this));

// TODO: route to and call controller
// route to and call controller
// TODO: Handle 404 and 500?
/** @var Router $router */
$router = $this->dependencyContainer->get('fuel.application.router');
$match = $router->translate($request->getUri()->getPath(), $request->getMethod());

// TODO: Use dependency magic to create the controller instance
$controller = new $match->controller();
// TODO: Pass params through?
// TODO: Pass params through
$controller_result = $controller->{$match->action}();

// TODO: trigger request ended event

// TODO: trigger response started event
// trigger request ended event
$this->dependencyContainer
->get('fuel.application.event')
->emit(new RequestFinished($this));

// generate and send response
// If the controller response is a response object then just pass that back out
Expand Down
42 changes: 42 additions & 0 deletions src/Event/AbstractAppEvent.php
@@ -0,0 +1,42 @@
<?php
/**
* @package Fuel\Foundation
* @version 2.0
* @author Fuel Development Team
* @license MIT License
* @copyright 2010 - 2016 Fuel Development Team
* @link http://fuelphp.com
*/

namespace Fuel\Foundation\Event;

use Fuel\Foundation\Application;
use League\Event\AbstractEvent;

abstract class AbstractAppEvent extends AbstractEvent
{
/**
* @var Application
*/
protected $application;

/**
* @var string
*/
protected $name;

public function __construct(Application $application)
{
$this->application = $application;
}

public function getApplication() : Application
{
return $this->application;
}

public function getName() : string
{
return $this->name;
}
}
23 changes: 23 additions & 0 deletions src/Event/AppShutdown.php
@@ -0,0 +1,23 @@
<?php
/**
* @package Fuel\Foundation
* @version 2.0
* @author Fuel Development Team
* @license MIT License
* @copyright 2010 - 2016 Fuel Development Team
* @link http://fuelphp.com
*/

declare(strict_types=1);

namespace Fuel\Foundation\Event;

/**
* Triggered when the application has finished all request processing and the app is shutting down.
*
* @package Fuel\Foundation\Event
*/
class AppShutdown extends AbstractAppEvent
{
protected $name = 'fuel.application.shutdown';
}
22 changes: 2 additions & 20 deletions src/Event/AppStarted.php
Expand Up @@ -20,25 +20,7 @@
*
* @package Fuel\Foundation\Event
*/
class AppStarted extends AbstractEvent
class AppStarted extends AbstractAppEvent
{
/**
* @var Application
*/
protected $application;

public function __construct(Application $application)
{
$this->application = $application;
}

public function getApplication() : Application
{
return $this->application;
}

public function getName() : string
{
return 'fuel.application.started';
}
protected $name = 'fuel.application.started';
}
26 changes: 26 additions & 0 deletions src/Event/RequestFinished.php
@@ -0,0 +1,26 @@
<?php
/**
* @package Fuel\Foundation
* @version 2.0
* @author Fuel Development Team
* @license MIT License
* @copyright 2010 - 2016 Fuel Development Team
* @link http://fuelphp.com
*/

declare(strict_types=1);

namespace Fuel\Foundation\Event;

use Fuel\Foundation\Application;
use League\Event\AbstractEvent;

/**
* Triggered when a request has been finished.
*
* @package Fuel\Foundation\Event
*/
class RequestFinished extends AbstractAppEvent
{
protected $name = 'fuel.request.finished';
}
26 changes: 26 additions & 0 deletions src/Event/RequestStarted.php
@@ -0,0 +1,26 @@
<?php
/**
* @package Fuel\Foundation
* @version 2.0
* @author Fuel Development Team
* @license MIT License
* @copyright 2010 - 2016 Fuel Development Team
* @link http://fuelphp.com
*/

declare(strict_types=1);

namespace Fuel\Foundation\Event;

use Fuel\Foundation\Application;
use League\Event\AbstractEvent;

/**
* Triggered when a request has been started.
*
* @package Fuel\Foundation\Event
*/
class RequestStarted extends AbstractAppEvent
{
protected $name = 'fuel.request.started';
}
26 changes: 26 additions & 0 deletions src/Event/ResponseFinished.php
@@ -0,0 +1,26 @@
<?php
/**
* @package Fuel\Foundation
* @version 2.0
* @author Fuel Development Team
* @license MIT License
* @copyright 2010 - 2016 Fuel Development Team
* @link http://fuelphp.com
*/

declare(strict_types=1);

namespace Fuel\Foundation\Event;

use Fuel\Foundation\Application;
use League\Event\AbstractEvent;

/**
* Triggered when a response has been started.
*
* @package Fuel\Foundation\Event
*/
class ResponseFinished extends AbstractAppEvent
{
protected $name = 'fuel.response.finished';
}
26 changes: 26 additions & 0 deletions src/Event/ResponseStarted.php
@@ -0,0 +1,26 @@
<?php
/**
* @package Fuel\Foundation
* @version 2.0
* @author Fuel Development Team
* @license MIT License
* @copyright 2010 - 2016 Fuel Development Team
* @link http://fuelphp.com
*/

declare(strict_types=1);

namespace Fuel\Foundation\Event;

use Fuel\Foundation\Application;
use League\Event\AbstractEvent;

/**
* Triggered when a response has been started.
*
* @package Fuel\Foundation\Event
*/
class ResponseStarted extends AbstractAppEvent
{
protected $name = 'fuel.response.started';
}
33 changes: 32 additions & 1 deletion tests/unit/ApplicationTest.php
Expand Up @@ -15,6 +15,8 @@
use Codeception\TestCase\Test;
use Fuel\Foundation\Application;
use Fuel\Foundation\Event\AppStarted;
use Fuel\Foundation\Event\RequestFinished;
use Fuel\Foundation\Event\RequestStarted;
use Fuel\Foundation\Request\Http as HttpRequest;
use Zend\Diactoros\Request;

Expand Down Expand Up @@ -106,21 +108,50 @@ public function testAppCreatedEvent()
]);

$this->assertTrue($called);
$this->assertEquals($app, $event->getApplication());
$this->assertSame($app, $event->getApplication());
}

public function testMakeRequest()
{
$requestStartCalled = false;
$requestStartApplication = null;

$requestEndCalled = false;
$requestEndApplication = null;


$app = Application::init([
'components' => [
'Basic',
],
'events' => [
[
'name' => 'fuel.request.started',
'listener' => function (RequestStarted $requestStarted) use (&$requestStartCalled, &$requestStartApplication) {
$requestStartCalled = true;
$requestStartApplication = $requestStarted->getApplication();
}
],
[
'name' => 'fuel.request.finished',
'listener' => function (RequestFinished $requestFinished) use (&$requestEndCalled, &$requestEndApplication) {
$requestEndCalled = true;
$requestEndApplication = $requestFinished->getApplication();
}
],
],
]);

$request = new HttpRequest([], [], '/testroute');
$response = $app->performRequest($request);

$this->assertEquals(200, $response->getStatusCode());
$this->assertEquals('found me', (string) $response->getBody());

$this->assertTrue($requestStartCalled);
$this->assertSame($app, $requestStartApplication);

$this->assertTrue($requestEndCalled);
$this->assertSame($app, $requestEndApplication);
}
}

0 comments on commit c989b6d

Please sign in to comment.