Commit 97b7dc57 authored by Fabien Potencier's avatar Fabien Potencier

merged branch marijn/feature/serializer-provider (PR #401)

This PR was merged into the master branch.

Commits
-------

03bccc83 Add a `SerializerServiceProvider`.

Discussion
----------

Add a SerializerServiceProvider

Usage
-----

```php
<?php

use Silex\Application;
use Silex\Provider\SerializerServiceProvider;
use Symfony\Component\HttpFoundation\Response;

$app = new Application();

$app->register(new SerializerServiceProvider);

// only accept content types supported by the serializer via the assert method.
$app->get("/pages/{id}.{_format}", function ($id) use ($app) {
    // assume a page_repository service exists that returns Page objects. The
    // object returned has getters and setters exposing the state.
    $page = $app['page_repository']->find($id);
    $format = $app['request']->getFormat();

    if (!$page instanceof Page) {
        $this->abort("No page found for id: $id");
    }

    return new Response($app['serializer']->serialize($page, $format), 200, array(
        "Content-Type" => $app['request']->getMimeType($format)
    ));
})->assert("_format", "xml|json")
  ->assert("id", "\d+");
```

---------------------------------------------------------------------------

by GromNaN at 2012-06-28T14:29:50Z

You can create this ServiceProvider in its own repository and put a link on the wiki :
https://github.com/fabpot/Silex/wiki/Third-Party-ServiceProviders

---------------------------------------------------------------------------

by fabpot at 2012-06-28T14:38:42Z

I think it makes sense to have this in core.

---------------------------------------------------------------------------

by marijn at 2012-06-28T14:41:03Z

Ok. Haven't tested in "in the wild" yet (working on that as we speak). Any pointers, concerns or other comments are more than welcome 😄

Should I add more documentation or is this enough?

---------------------------------------------------------------------------

by igorw at 2012-06-28T14:47:03Z

@fabpot I agree. 👍

Please add some more tests.

---------------------------------------------------------------------------

by marijn at 2012-06-28T14:50:13Z

@igorw in regards to the tests: what would you like to see added? I figured we should only test if the serializer is configured properly, not if it actually serializes.

---------------------------------------------------------------------------

by igorw at 2012-06-28T15:11:06Z

You're right, no need to test serialization. Looks good. 👍

---------------------------------------------------------------------------

by marijn at 2012-06-28T16:02:20Z

Somehow I have trouble running the full test suite. The swift mailer configuration doesn't seem to work. Is this a known issue?

---------------------------------------------------------------------------

by GromNaN at 2012-06-28T20:33:14Z

A new trait can also be added with the method `serialize()`.

---------------------------------------------------------------------------

by marijn at 2012-06-28T21:34:58Z

At the moment I haven't got a working version of PHP 5.4 so I cannot test traits.

---------------------------------------------------------------------------

by marijn at 2012-06-29T19:01:21Z

It seems to me that adding a `trait` for the serializer doesn't really make any sense: the API for the `Serializer` component is pretty extensive, it would add a lot of methods to the `Application`. What do you think?

---------------------------------------------------------------------------

by stof at 2012-06-30T10:32:07Z

@marijn the trait would cover the simple use of the serializer, i.e. a ``serialize()`` method. It is not intended to cover all possible use cases.

---------------------------------------------------------------------------

by fabpot at 2012-06-30T18:31:32Z

Can you squash your commits before I merge? Thanks.

---------------------------------------------------------------------------

by marijn at 2012-07-10T10:58:58Z

@fabpot this has been squashed. I think it's mergeable 😄

---------------------------------------------------------------------------

by marijn at 2012-08-17T13:39:17Z

Have I missed something here? To the best of my knowledge, this is mergeable. Please let me know if there is anything I still need to do 😄

---------------------------------------------------------------------------

by alanbem at 2012-10-27T20:39:02Z

What is status of this PR? Is it going to be merged?

---------------------------------------------------------------------------

by marijn at 2012-10-27T21:14:12Z

I have no clue, @alanbem. If you're in need of a serializer for Silex you can either merge this in your own fork of Silex or use the [`jms/serializer-service-provider`][1] that I released.

[1]: https://github.com/pink-tie/JMSSerializerServiceProvider

---------------------------------------------------------------------------

by igorw at 2012-10-27T23:15:53Z

@fabpot this looks good to me.
parents 59834739 03bccc83
...@@ -52,6 +52,7 @@ the ``Silex\Provider`` namespace: ...@@ -52,6 +52,7 @@ the ``Silex\Provider`` namespace:
* :doc:`DoctrineServiceProvider <providers/doctrine>` * :doc:`DoctrineServiceProvider <providers/doctrine>`
* :doc:`MonologServiceProvider <providers/monolog>` * :doc:`MonologServiceProvider <providers/monolog>`
* :doc:`SessionServiceProvider <providers/session>` * :doc:`SessionServiceProvider <providers/session>`
* :doc:`SerializerServiceProvider <providers/serializer>`
* :doc:`SwiftmailerServiceProvider <providers/swiftmailer>` * :doc:`SwiftmailerServiceProvider <providers/swiftmailer>`
* :doc:`TwigServiceProvider <providers/twig>` * :doc:`TwigServiceProvider <providers/twig>`
* :doc:`TranslationServiceProvider <providers/translation>` * :doc:`TranslationServiceProvider <providers/translation>`
......
SerializerServiceProvider
===========================
The *SerializerServiceProvider* provides a service for serializing objects.
Parameters
----------
None.
Services
--------
* **serializer**: An instance of `Symfony\Component\Serializer\Serializer
<http://api.symfony.com/master/Symfony/Component/Serializer/Serializer.html>`_.
* **serializer.encoders**: `Symfony\Component\Serializer\Encoder\JsonEncoder
<http://api.symfony.com/master/Symfony/Component/Serializer/Encoder/JsonEncoder.html>`_
and `Symfony\Component\Serializer\Encoder\XmlEncoder
<http://api.symfony.com/master/Symfony/Component/Serializer/Encoder/XmlEncoder>`_.
* **serializer.normalizers**: `Symfony\Component\Serializer\Normalizer\CustomNormalizer
<http://api.symfony.com/master/Symfony/Component/Serializer/Normalizer/CustomNormalizer>`_
and `Symfony\Component\Serializer\Normalizer\GetSetMethodNormalizer
<http://api.symfony.com/master/Symfony/Component/Serializer/Normalizer/GetSetMethodNormalizer>`_.
Registering
-----------
.. code-block:: php
$app->register(new Silex\Provider\SerializerServiceProvider());
Usage
-----
The ``SerializerServiceProvider`` provider provides a ``serializer`` service::
.. code-block:: php
<?php
use Silex\Application;
use Silex\Provider\SerializerServiceProvider;
use Symfony\Component\HttpFoundation\Response;
$app = new Application();
$app->register(new SerializerServiceProvider());
// only accept content types supported by the serializer via the assert method.
$app->get("/pages/{id}.{_format}", function ($id) use ($app) {
// assume a page_repository service exists that returns Page objects. The
// object returned has getters and setters exposing the state.
$page = $app['page_repository']->find($id);
$format = $app['request']->getFormat();
if (!$page instanceof Page) {
$app->abort("No page found for id: $id");
}
return new Response($app['serializer']->serialize($page, $format), 200, array(
"Content-Type" => $app['request']->getMimeType($format)
));
})->assert("_format", "xml|json")
->assert("id", "\d+");
<?php
/*
* This file is part of the Silex framework.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Silex\Provider;
use Silex\Application;
use Silex\ServiceProviderInterface;
use Symfony\Component\Serializer\Serializer;
use Symfony\Component\Serializer\Encoder\JsonEncoder;
use Symfony\Component\Serializer\Encoder\XmlEncoder;
use Symfony\Component\Serializer\Normalizer\CustomNormalizer;
use Symfony\Component\Serializer\Normalizer\GetSetMethodNormalizer;
/**
* Symfony Serializer component Provider.
*
* @author Fabien Potencier <fabien@symfony.com>
* @author Marijn Huizendveld <marijn@pink-tie.com>
*/
class SerializerServiceProvider implements ServiceProviderInterface
{
/**
* {@inheritDoc}
*
* This method registers a serializer service. {@link http://api.symfony.com/master/Symfony/Component/Serializer/Serializer.html
* The service is provided by the Symfony Serializer component}.
*
* @param Silex\Application $app
*/
public function register(Application $app)
{
$app['serializer'] = $app->share(function () use ($app) {
return new Serializer($app['serializer.normalizers'], $app['serializer.encoders']);
});
$app['serializer.encoders'] = $app->share(function () {
return array(
new JsonEncoder(),
new XmlEncoder()
);
});
$app['serializer.normalizers'] = $app->share(function () {
return array(
new CustomNormalizer(),
new GetSetMethodNormalizer()
);
});
}
/**
* {@inheritDoc}
*
* This provider does not execute any code when booting.
*
* @param Silex\Application $app
*/
public function boot(Application $app)
{
}
}
<?php
/*
* This file is part of the Silex framework.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
namespace Silex\Tests\Provider;
use Silex\Application;
use Silex\Provider\SerializerServiceProvider;
/**
* SerializerServiceProvider test cases.
*
* @author Fabien Potencier <fabien@symfony.com>
*/
class SerializerServiceProviderTest extends \PHPUnit_Framework_TestCase
{
public function testRegister()
{
$app = new Application();
$app->register(new SerializerServiceProvider);
$this->assertInstanceOf("Symfony\Component\Serializer\Serializer", $app['serializer']);
$this->assertTrue($app['serializer']->supportsEncoding('xml'));
$this->assertTrue($app['serializer']->supportsEncoding('json'));
$this->assertTrue($app['serializer']->supportsDecoding('xml'));
$this->assertTrue($app['serializer']->supportsDecoding('json'));
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment