Skip to content
This repository was archived by the owner on Feb 10, 2023. It is now read-only.
Open
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
3 changes: 3 additions & 0 deletions src/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,9 @@ public function performRequest(RequestInterface $request) : ResponseInterface

// route to and call controller
$match = $this->getRouteMatch($request);
$this->dependencyContainer
->add('fuel.application.routeMatch', $match);

$response = $this->getControllerResult($match);

// trigger request ended event
Expand Down
82 changes: 74 additions & 8 deletions src/ApplicationServicesProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,13 @@
namespace Fuel\Foundation;

use Fuel\Config\Container;
use Fuel\Foundation\Exception\Formatter;
use Fuel\Foundation\Request\Http;
use Fuel\Foundation\Request\RequestInterface;
use Fuel\Foundation\Response\ResponseInterface;
use Fuel\Routing\Router;
use League\Container\ServiceProvider\AbstractServiceProvider;
use Symfony\Component\DomCrawler\Form;

class ApplicationServicesProvider extends AbstractServiceProvider
{
Expand All @@ -40,6 +42,10 @@ class ApplicationServicesProvider extends AbstractServiceProvider
'fuel.application.component_manager',

'fuel.application.router',

'Fuel\Foundation\Formatter\Noop',
'Fuel\Foundation\Formatter\HttpAcceptJson',
'Fuel\Foundation\ResponseFormatter',
];

/**
Expand All @@ -50,21 +56,69 @@ public function register()
$this->getContainer()->add('fuel.application.event', 'League\Event\Emitter', true);

$this->getContainer()->add('Fuel\Foundation\Request\Cli', 'Fuel\Foundation\Request\Cli', false);
$this->getContainer()->add('Fuel\Foundation\Request\Http', Http::forge(), false);
$this->getContainer()->add('fuel.application.request', $this->constructRequest(), true);
$this->getContainer()->add(
'Fuel\Foundation\Request\Http',
function() {
return Http::forge();
},
false
);

$this->getContainer()->add(
'fuel.application.request',
function() {
return $this->constructRequest();
},
true
);

$this->getContainer()->add('Fuel\Foundation\Response\Cli', 'Fuel\Foundation\Response\Cli', false);
$this->getContainer()->add('Fuel\Foundation\Response\Http', 'Fuel\Foundation\Response\Http', false);
$this->getContainer()->add('fuel.application.response', $this->constructResponse(), true);
$this->getContainer()->add(
'fuel.application.response',
function() {
return $this->constructResponse();
},
true
);

$this->getContainer()->add('fuel.application.finder', 'Fuel\FileSystem\Finder', true);

// Also create a config container for our services
$this->getContainer()->add('fuel.config', new Container(null, $this->getContainer()->get('fuel.application.finder')), true);

$this->getContainer()->add('fuel.application.component_manager', $this->constructComponentManager(), true);

$this->getContainer()->add('fuel.application.router', $this->constructRouter(), true);
$this->getContainer()->add(
'fuel.config',
function() {
return new Container(null, $this->getContainer()->get('fuel.application.finder'));
},
true
);

$this->getContainer()->add(
'fuel.application.component_manager',
function () {
return $this->constructComponentManager();
},
true
);

$this->getContainer()->add(
'fuel.application.router',
function() {
return $this->constructRouter();
},
true
);

// Add in the various formatters
$this->getContainer()->add('Fuel\Foundation\Formatter\Noop', 'Fuel\Foundation\Formatter\Noop', true);
$this->getContainer()->add('Fuel\Foundation\Formatter\HttpAcceptJson', 'Fuel\Foundation\Formatter\HttpAcceptJson', true);
$this->getContainer()->add(
'Fuel\Foundation\ResponseFormatter',
function() {
return $this->constructResponseFormatter();
},
true
);
}

/**
Expand Down Expand Up @@ -109,6 +163,18 @@ protected function constructResponse() : ResponseInterface
return $this->getContainer()->get('Fuel\Foundation\Response\Http');
}

protected function constructResponseFormatter() : ResponseFormatter
{
/** @var Container $config */
$config = $this->getContainer()->get('fuel.config');
$config->load('output_formatters', 'output_formatters');

return new ResponseFormatter(
$config->get('output_formatters', ['Fuel\Foundation\Formatter\Noop']),
$this->getContainer()
);
}

/**
* @return bool
*/
Expand Down
18 changes: 18 additions & 0 deletions src/Exception/Formatter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php
/**
* @package Fuel\Foundation
* @version 2.0
* @author Fuel Development Team
* @license MIT License
* @copyright 2010 - 2017 Fuel Development Team
* @link http://fuelphp.com
*/

namespace Fuel\Foundation\Exception;

use RuntimeException;

class Formatter extends RuntimeException
{

}
22 changes: 22 additions & 0 deletions src/Exception/FormatterLoad.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php
/**
* @package Fuel\Foundation
* @version 2.0
* @author Fuel Development Team
* @license MIT License
* @copyright 2010 - 2017 Fuel Development Team
* @link http://fuelphp.com
*/

declare(strict_types=1);

namespace Fuel\Foundation\Exception;

/**
* Indicates an error with loading a data formatter.
*
* @package Fuel\Foundation\Exception
*/
class FormatterLoad extends Formatter
{
}
43 changes: 43 additions & 0 deletions src/Formatter/AbstractFormatter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php
/**
* @package Fuel\Foundation
* @version 2.0
* @author Fuel Development Team
* @license MIT License
* @copyright 2010 - 2017 Fuel Development Team
* @link http://fuelphp.com
*/

declare(strict_types=1);

namespace Fuel\Foundation\Formatter;

use League\Container\ContainerInterface;

abstract class AbstractFormatter implements FormatterInterface
{
/**
* @var ContainerInterface
*/
protected $container;

/**
* Sets the dependency container that the formatter will use.
*
* @param ContainerInterface $container
*/
public function setContainer(ContainerInterface $container)
{
$this->container = $container;
}

/**
* Gets the current dependency container.
*
* @return ContainerInterface
*/
public function getContainer(): ContainerInterface
{
return $this->container;
}
}
55 changes: 55 additions & 0 deletions src/Formatter/FormatterInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php
/**
* @package Fuel\Foundation
* @version 2.0
* @author Fuel Development Team
* @license MIT License
* @copyright 2010 - 2017 Fuel Development Team
* @link http://fuelphp.com
*/

declare(strict_types=1);

namespace Fuel\Foundation\Formatter;

use Fuel\Foundation\Response\ResponseInterface;
use League\Container\ContainerInterface;

/**
* Defines a common interface for taking a controller result and formatting that for return to the client.
*/
interface FormatterInterface
{
/**
* Sets the dependency container that the formatter will use.
*
* @param ContainerInterface $container
*/
public function setContainer(ContainerInterface $container);

/**
* Gets the current dependency container.
*
* @return ContainerInterface
*/
public function getContainer(): ContainerInterface;

/**
* Should return true if the formatter is able to handle the current data.
*
* @param mixed $data Result returned from the controller's action. This allows the formatter to determine if the
* data is in a format that it can support or not.
*
* @return bool
*/
public function canActivate($data): bool;

/**
* Should produce a ResponseInterface object that contains the formatted data and any necessary headers.
*
* @param mixed $data Result returned from the controller's action.
*
* @return ResponseInterface
*/
public function format($data): ResponseInterface;
}
53 changes: 53 additions & 0 deletions src/Formatter/HttpAcceptJson.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php
/**
* @package Fuel\Foundation
* @version 2.0
* @author Fuel Development Team
* @license MIT License
* @copyright 2010 - 2017 Fuel Development Team
* @link http://fuelphp.com
*/

declare(strict_types=1);

namespace Fuel\Foundation\Formatter;

use Fuel\Foundation\Request\RequestInterface;
use Fuel\Foundation\Response\ResponseInterface;
use Zend\Diactoros\CallbackStream;

/**
* Formats data in to json if the header "Accept: application/json" is present in the request.
*/
class HttpAcceptJson extends AbstractFormatter
{

/**
* {@inheritdoc}
*/
public function canActivate($data) : bool
{
/** @var RequestInterface $request */
$request = $this->getContainer()->get('fuel.application.request');

return $request->hasHeader('Accept') &&
in_array('application/json', $request->getHeader('Accept'));
}

/**
* {@inheritdoc}
*/
public function format($data) : ResponseInterface
{
/** @var ResponseInterface $response */
$response = $this->getContainer()->get('fuel.application.response');

return $response
->withBody(new CallbackStream(
function() use ($data) {
return json_encode($data);
}
))
->withHeader('Content-Type', 'application/json');
}
}
46 changes: 46 additions & 0 deletions src/Formatter/Noop.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php
/**
* @package Fuel\Foundation
* @version 2.0
* @author Fuel Development Team
* @license MIT License
* @copyright 2010 - 2017 Fuel Development Team
* @link http://fuelphp.com
*/

declare(strict_types=1);

namespace Fuel\Foundation\Formatter;

use Fuel\Foundation\Response\ResponseInterface;
use Zend\Diactoros\CallbackStream;

/**
* Dummy formatter that simply passes the response right through.
*/
class Noop extends AbstractFormatter
{

/**
* {@inheritdoc}
*/
public function canActivate($data) : bool
{
return true;
}

/**
* {@inheritdoc}
*/
public function format($data) : ResponseInterface
{
/** @var ResponseInterface $response */
$response = $this->getContainer()->get('fuel.application.response');

return $response->withBody(new CallbackStream(
function() use ($data) {
return $data;
}
));
}
}
Loading