Commit e7d4a2fd authored by Fabien Potencier's avatar Fabien Potencier

merged branch igorw/convert-only-existing-attribute (PR #769)

This PR was squashed before being merged into the master branch (closes #769).

Discussion
----------

Only convert attributes on the request that actually exist

Fixes #768.

Note: This could be considered a BC break, since it on longer implicitly creates attributes on the request. I'd consider it quite a minor break. The name `convert` implies that the attribute must be present, and I personally would  consider this a bug fix instead.

Commits
-------

f2fc754c Only convert attributes on the request that actually exist
parents d6fe48ea f2fc754c
......@@ -4,7 +4,7 @@ Changelog
1.1.1 (2013-XX-XX)
------------------
* n/a
* Only convert attributes on the request that actually exist.
1.1.0 (2013-07-04)
------------------
......
......@@ -46,7 +46,9 @@ class ConverterListener implements EventSubscriberInterface
$route = $this->routes->get($request->attributes->get('_route'));
if ($route && $converters = $route->getOption('_converters')) {
foreach ($converters as $name => $callback) {
$request->attributes->set($name, call_user_func($callback, $request->attributes->get($name), $request));
if ($request->attributes->has($name)) {
$request->attributes->set($name, call_user_func($callback, $request->attributes->get($name), $request));
}
}
}
}
......
<?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;
use Silex\Application;
use Symfony\Component\HttpFoundation\Request;
/**
* Converter listener test cases.
*
* @author Igor Wiedler <igor@wiedler.ch>
*/
class ConverterTest extends \PHPUnit_Framework_TestCase
{
public function testConvertingNonExistentAttributeShouldNotCallConverter()
{
$called = false;
$converter = function () use (&$called) {
$called = true;
};
$app = new Application();
$app->get('/', function () { return 'hallo'; });
$app['controllers']->convert('foo', $converter);
$request = Request::create('/');
$app->handle($request);
$this->assertFalse($called);
}
}
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