Skip to content

Commit ebc42b0

Browse files
committed
Use symfony/phpunit-bridge and fix deprecations
1 parent 9a16b0e commit ebc42b0

File tree

13 files changed

+151
-65
lines changed

13 files changed

+151
-65
lines changed

.travis.yml

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,9 @@ matrix:
2323
fast_finish: true
2424
include:
2525
- php: 5.3
26-
env: SYMFONY_VERSION=2.3.* COMPOSER_FLAGS="--prefer-lowest"
26+
env: COMPOSER_FLAGS="--prefer-lowest" SYMFONY_DEPRECATIONS_HELPER=weak
2727
- php: 5.5
28-
env: SYMFONY_VERSION='2.3.* symfony/expression-language:2.4.*'
29-
- php: 5.5
30-
env: SYMFONY_VERSION='2.3.* sensio/framework-extra-bundle:2.*'
28+
env: SYMFONY_VERSION='2.3.* symfony/expression-language:2.4.*' SYMFONY_DEPRECATIONS_HELPER=weak
3129
- php: 5.5
3230
env: SYMFONY_VERSION=2.4.*
3331
- php: 5.5
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the FOSRestBundle package.
5+
*
6+
* (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace FOS\RestBundle\DependencyInjection\Compiler;
13+
14+
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
15+
use Symfony\Component\DependencyInjection\ContainerBuilder;
16+
use Symfony\Component\DependencyInjection\Reference;
17+
18+
/**
19+
* BC for symfony 2.3.
20+
*
21+
* @author Ener-Getick <[email protected]>
22+
*
23+
* @internal
24+
*/
25+
class ParamFetcherConfigurationPass implements CompilerPassInterface
26+
{
27+
public function process(ContainerBuilder $container)
28+
{
29+
if (!$container->hasDefinition('fos_rest.request.param_fetcher') || $container->hasDefinition('request_stack')) {
30+
return;
31+
}
32+
33+
$definition = $container->getDefinition('fos_rest.request.param_fetcher');
34+
$definition->addMethodCall('setContainer', array(new Reference('service_container')));
35+
}
36+
}

FOSRestBundle.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
use FOS\RestBundle\DependencyInjection\Compiler\FormatListenerRulesPass;
2020
use FOS\RestBundle\DependencyInjection\Compiler\TwigExceptionPass;
2121
use FOS\RestBundle\DependencyInjection\Compiler\CsrfExtensionPass;
22+
use FOS\RestBundle\DependencyInjection\Compiler\ParamFetcherConfigurationPass;
2223

2324
/**
2425
* @author Lukas Kahwe Smith <[email protected]>
@@ -37,5 +38,6 @@ public function build(ContainerBuilder $container)
3738
$container->addCompilerPass(new TwigExceptionPass());
3839
$container->addCompilerPass(new ExceptionWrapperHandlerPass());
3940
$container->addCompilerPass(new CsrfExtensionPass());
41+
$container->addCompilerPass(new ParamFetcherConfigurationPass());
4042
}
4143
}

Request/ParamFetcher.php

Lines changed: 38 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@
1616
use FOS\RestBundle\Controller\Annotations\RequestParam;
1717
use FOS\RestBundle\Util\ViolationFormatterInterface;
1818
use Doctrine\Common\Util\ClassUtils;
19+
use Symfony\Component\DependencyInjection\ContainerAware;
1920
use Symfony\Component\HttpFoundation\Request;
21+
use Symfony\Component\HttpFoundation\RequestStack;
2022
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
2123
use Symfony\Component\Validator\Constraints\Regex;
2224
use Symfony\Component\Validator\Constraints\NotBlank;
@@ -31,10 +33,10 @@
3133
* @author Jordi Boggiano <[email protected]>
3234
* @author Boris Guéry <[email protected]>
3335
*/
34-
class ParamFetcher implements ParamFetcherInterface
36+
class ParamFetcher extends ContainerAware implements ParamFetcherInterface
3537
{
3638
private $paramReader;
37-
private $request;
39+
private $requestStack;
3840
private $params;
3941
private $validator;
4042
private $violationFormatter;
@@ -48,14 +50,20 @@ class ParamFetcher implements ParamFetcherInterface
4850
* Initializes fetcher.
4951
*
5052
* @param ParamReader $paramReader
51-
* @param Request $request
53+
* @param Request|RequestStack $request
5254
* @param ValidatorInterface|LegacyValidatorInterface $validator
5355
* @param ViolationFormatterInterface $violationFormatter
5456
*/
55-
public function __construct(ParamReader $paramReader, Request $request, ViolationFormatterInterface $violationFormatter, $validator = null)
57+
public function __construct(ParamReader $paramReader, $requestStack = null, ViolationFormatterInterface $violationFormatter, $validator = null)
5658
{
59+
if (null === $requestStack || $requestStack instanceof Request) {
60+
@trigger_error('Support of Symfony\Component\HttpFoundation\Request in FOS\RestBundle\Request\ParamFetcher is deprecated since version 1.7 and will be removed in 2.0. Use Symfony\Component\HttpFoundation\RequestStack instead.', E_USER_DEPRECATED);
61+
} elseif (!($requestStack instanceof RequestStack)) {
62+
throw new \InvalidArgumentException(sprintf('Argument 3 of %s constructor must be either an instance of Symfony\Component\HttpFoundation\Request or Symfony\Component\HttpFoundation\RequestStack.', get_class($this)));
63+
}
64+
5765
$this->paramReader = $paramReader;
58-
$this->request = $request;
66+
$this->requestStack = $requestStack;
5967
$this->violationFormatter = $violationFormatter;
6068
$this->validator = $validator;
6169

@@ -124,9 +132,9 @@ public function get($name, $strict = null)
124132
}
125133

126134
if ($config instanceof RequestParam) {
127-
$param = $this->request->request->get($config->getKey(), $default);
135+
$param = $this->getRequest()->request->get($config->getKey(), $default);
128136
} elseif ($config instanceof QueryParam) {
129-
$param = $this->request->query->get($config->getKey(), $default);
137+
$param = $this->getRequest()->query->get($config->getKey(), $default);
130138
} else {
131139
$param = null;
132140
}
@@ -307,7 +315,7 @@ private function checkNotIncompatibleParams(Param $config)
307315
};
308316

309317
foreach ($config->incompatibles as $incompatibleParam) {
310-
$isIncompatiblePresent = $this->request->query->get(
318+
$isIncompatiblePresent = $this->getRequest()->query->get(
311319
$incompatibleParam,
312320
null
313321
) !== null;
@@ -323,4 +331,26 @@ private function checkNotIncompatibleParams(Param $config)
323331
}
324332
}
325333
}
334+
335+
/**
336+
* @throws \RuntimeException
337+
*
338+
* @return Request
339+
*/
340+
private function getRequest()
341+
{
342+
if ($this->requestStack instanceof Request) {
343+
return $this->requestStack;
344+
} elseif ($this->requestStack instanceof RequestStack) {
345+
$request = $this->requestStack->getCurrentRequest();
346+
} else {
347+
$request = $this->container->get('request');
348+
}
349+
350+
if ($request !== null) {
351+
return $request;
352+
}
353+
354+
throw new \RuntimeException('There is no current request.');
355+
}
326356
}

Resources/config/request.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@
1313

1414
<services>
1515

16-
<service id="fos_rest.request.param_fetcher" class="%fos_rest.request.param_fetcher.class%" scope="request">
16+
<service id="fos_rest.request.param_fetcher" class="%fos_rest.request.param_fetcher.class%">
1717
<argument type="service" id="fos_rest.request.param_fetcher.reader"/>
18-
<argument type="service" id="request"/>
18+
<argument type="service" id="request_stack" on-invalid="null"/>
1919
<argument type="service" id="fos_rest.violation_formatter"/>
2020
<argument type="service" id="validator" on-invalid="null"/>
2121
</service>

Tests/FOSRestBundleTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public function testBuild()
2525
$container = $this->getMockBuilder('\Symfony\Component\DependencyInjection\ContainerBuilder')
2626
->setMethods(array('addCompilerPass'))
2727
->getMock();
28-
$container->expects($this->exactly(6))
28+
$container->expects($this->exactly(7))
2929
->method('addCompilerPass')
3030
->with($this->isInstanceOf('\Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface'));
3131

Tests/Functional/Bundle/TestBundle/Controller/SerializerErrorController.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public function invalidFormAction()
3737
{
3838
$form = $this->createFormBuilder(null, array(
3939
'csrf_protection' => false,
40-
))->add('name', 'text', array(
40+
))->add('name', method_exists('Symfony\Component\Form\AbstractType', 'getBlockPrefix') ? 'Symfony\Component\Form\Extension\Core\Type\TextType' : 'text', array(
4141
'constraints' => array(new NotBlank()),
4242
))->getForm();
4343

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
test_bundle:
2-
resource: @TestBundle/Resources/config/routing.yml
2+
resource: "@TestBundle/Resources/config/routing.yml"

0 commit comments

Comments
 (0)