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

Add parameters #7

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
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: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
vendor
/vendor
/composer.lock
64 changes: 60 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,66 @@ You get accept header support and request body decoding.

## Registering

$app->register(new Flintstones\Rest\ServiceProvider(), array(
'rest.fos.class_path' => __DIR__.'/vendor',
'rest.serializer.class_path' => __DIR__.'/vendor',
));
$app->register(new Flintstones\Rest\ServiceProvider());

## Overriding parameters

The following can be overridden:

* `'rest.priorities'`
This is an array of formats your code is capable of
sending back a response as. A negotiation takes place
to determine which is most suitable based on the
request's Accept header.
* `'rest.fallback'`
This is the format to fall back on if no suitable
format could be negotiated. This can be set to NULL
if you would like a 406 HTTP status error to be
returned instead.
* `'rest.decoders'`
This is an array that refers to decoder services
which are used to decode request body content.
Default decoders are included for JSON and XML,
but you can override this if necessary.

Any of these you wish to override must be overridden
prior to the service completing its setup, i.e. before
its `register()` method has executed. Therefore they must
either be set on `$app` prior to registering the service,
or they must be given in an array to it's constructor
(*not* the second optional parameter of `$app->register()`
unlike most Silex service providers).

Example #1:

$app->register(new Flintstones\Rest\ServiceProvider(array(
'rest.priorities' => array('html', 'json', 'xml'),
'rest.fallback' => null,
)));

Example #2:

$app['rest.priorities'] = array('html', 'json', 'xml');
$app['rest.fallback'] = null;

$app->register(new Flintstones\Rest\ServiceProvider());

Example #3:

$app['rest.decoders.custom'] = function ($app) {
//code...
};

$app->register(new Flintstones\Rest\ServiceProvider(array(
'rest.decoders' => array(
'json' => 'rest.decoders.json',
'xml' => 'rest.decoders.xml',
'custom' => 'rest.decoders.custom',
),
)));

Note that if you wish to replace `$app['rest.decoder.json']`
and/or `$app['rest.decoder.xml']`, do so after registering.

## Running the tests

Expand Down
235 changes: 0 additions & 235 deletions composer.lock

This file was deleted.

41 changes: 20 additions & 21 deletions src/Flintstones/Rest/ServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,19 @@

class ServiceProvider implements ServiceProviderInterface
{
private $parameters = array();

public function __construct(array $parameters = array())
{
$this->parameters = $parameters;
}

public function register(Application $app)
{
foreach ($this->parameters as $key => $parameter) {
$app[$key] = $parameter;
}

$app['rest.serializer'] = $app->share(function () {
$encoders = array (
'json' => new JsonEncoder(),
Expand All @@ -42,8 +53,12 @@ public function register(Application $app)
$app['rest.priorities'] = array('json', 'xml');
}

if (!isset($app['rest.fallback'])) {
$app['rest.fallback'] = 'html';
}

$app['rest.format_negotiator'] = function ($app) {
return new FormatNegotiator($app['request'], $app['rest.priorities']);
return new FormatNegotiator();
};

$app['rest.decoder.json'] = function ($app) {
Expand All @@ -59,29 +74,13 @@ public function register(Application $app)
'xml' => 'rest.decoder.xml',
);

if (isset($app['rest.fos.class_path'])) {
$app['autoloader']->registerNamespace('FOS\RestBundle', $app['rest.fos.class_path']);
}

if (isset($app['rest.serializer.class_path'])) {
$app['autoloader']->registerNamespace('Symfony\Component\Serializer', $app['rest.serializer.class_path']);
}

$listener = new BodyListener(new PimpleDecoderProvider($app, $app['rest.decoders']));
$app['dispatcher']->addListener(HttpKernelEvents::REQUEST, array($listener, 'onKernelRequest'));

$listener = new FormatListener($app['rest.format_negotiator'], 'html', $app['rest.priorities']);
$listener = new FormatListener($app['rest.format_negotiator'], $app['rest.fallback'], $app['rest.priorities']);
$app['dispatcher']->addListener(HttpKernelEvents::CONTROLLER, array($listener, 'onKernelController'), 10);
}

/**
* Bootstraps the application.
*
* This method is called after all services are registers
* and should be used for "dynamic" configuration (whenever
* a service must be requested).
*/
public function boot(Application $app) {
// TODO: Implement boot() method.
}

public function boot(Application $app) {
}
}
Loading