Skip to content

Commit

Permalink
Use symfony/phpunit-bridge and fix deprecations
Browse files Browse the repository at this point in the history
  • Loading branch information
GuilhemN committed Nov 28, 2015
1 parent 9a16b0e commit ebc42b0
Show file tree
Hide file tree
Showing 13 changed files with 151 additions and 65 deletions.
6 changes: 2 additions & 4 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,9 @@ matrix:
fast_finish: true
include:
- php: 5.3
env: SYMFONY_VERSION=2.3.* COMPOSER_FLAGS="--prefer-lowest"
env: COMPOSER_FLAGS="--prefer-lowest" SYMFONY_DEPRECATIONS_HELPER=weak
- php: 5.5
env: SYMFONY_VERSION='2.3.* symfony/expression-language:2.4.*'
- php: 5.5
env: SYMFONY_VERSION='2.3.* sensio/framework-extra-bundle:2.*'
env: SYMFONY_VERSION='2.3.* symfony/expression-language:2.4.*' SYMFONY_DEPRECATIONS_HELPER=weak
- php: 5.5
env: SYMFONY_VERSION=2.4.*
- php: 5.5
Expand Down
36 changes: 36 additions & 0 deletions DependencyInjection/Compiler/ParamFetcherConfigurationPass.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

/*
* This file is part of the FOSRestBundle package.
*
* (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace FOS\RestBundle\DependencyInjection\Compiler;

use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Reference;

/**
* BC for symfony 2.3.
*
* @author Ener-Getick <egetick@gmail.com>
*
* @internal
*/
class ParamFetcherConfigurationPass implements CompilerPassInterface
{
public function process(ContainerBuilder $container)
{
if (!$container->hasDefinition('fos_rest.request.param_fetcher') || $container->hasDefinition('request_stack')) {
return;
}

$definition = $container->getDefinition('fos_rest.request.param_fetcher');
$definition->addMethodCall('setContainer', array(new Reference('service_container')));
}
}
2 changes: 2 additions & 0 deletions FOSRestBundle.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use FOS\RestBundle\DependencyInjection\Compiler\FormatListenerRulesPass;
use FOS\RestBundle\DependencyInjection\Compiler\TwigExceptionPass;
use FOS\RestBundle\DependencyInjection\Compiler\CsrfExtensionPass;
use FOS\RestBundle\DependencyInjection\Compiler\ParamFetcherConfigurationPass;

/**
* @author Lukas Kahwe Smith <smith@pooteeweet.org>
Expand All @@ -37,5 +38,6 @@ public function build(ContainerBuilder $container)
$container->addCompilerPass(new TwigExceptionPass());
$container->addCompilerPass(new ExceptionWrapperHandlerPass());
$container->addCompilerPass(new CsrfExtensionPass());
$container->addCompilerPass(new ParamFetcherConfigurationPass());
}
}
46 changes: 38 additions & 8 deletions Request/ParamFetcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@
use FOS\RestBundle\Controller\Annotations\RequestParam;
use FOS\RestBundle\Util\ViolationFormatterInterface;
use Doctrine\Common\Util\ClassUtils;
use Symfony\Component\DependencyInjection\ContainerAware;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
use Symfony\Component\Validator\Constraints\Regex;
use Symfony\Component\Validator\Constraints\NotBlank;
Expand All @@ -31,10 +33,10 @@
* @author Jordi Boggiano <j.boggiano@seld.be>
* @author Boris Guéry <guery.b@gmail.com>
*/
class ParamFetcher implements ParamFetcherInterface
class ParamFetcher extends ContainerAware implements ParamFetcherInterface
{
private $paramReader;
private $request;
private $requestStack;
private $params;
private $validator;
private $violationFormatter;
Expand All @@ -48,14 +50,20 @@ class ParamFetcher implements ParamFetcherInterface
* Initializes fetcher.
*
* @param ParamReader $paramReader
* @param Request $request
* @param Request|RequestStack $request
* @param ValidatorInterface|LegacyValidatorInterface $validator
* @param ViolationFormatterInterface $violationFormatter
*/
public function __construct(ParamReader $paramReader, Request $request, ViolationFormatterInterface $violationFormatter, $validator = null)
public function __construct(ParamReader $paramReader, $requestStack = null, ViolationFormatterInterface $violationFormatter, $validator = null)
{
if (null === $requestStack || $requestStack instanceof Request) {
@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);
} elseif (!($requestStack instanceof RequestStack)) {
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)));
}

$this->paramReader = $paramReader;
$this->request = $request;
$this->requestStack = $requestStack;
$this->violationFormatter = $violationFormatter;
$this->validator = $validator;

Expand Down Expand Up @@ -124,9 +132,9 @@ public function get($name, $strict = null)
}

if ($config instanceof RequestParam) {
$param = $this->request->request->get($config->getKey(), $default);
$param = $this->getRequest()->request->get($config->getKey(), $default);
} elseif ($config instanceof QueryParam) {
$param = $this->request->query->get($config->getKey(), $default);
$param = $this->getRequest()->query->get($config->getKey(), $default);
} else {
$param = null;
}
Expand Down Expand Up @@ -307,7 +315,7 @@ private function checkNotIncompatibleParams(Param $config)
};

foreach ($config->incompatibles as $incompatibleParam) {
$isIncompatiblePresent = $this->request->query->get(
$isIncompatiblePresent = $this->getRequest()->query->get(
$incompatibleParam,
null
) !== null;
Expand All @@ -323,4 +331,26 @@ private function checkNotIncompatibleParams(Param $config)
}
}
}

/**
* @throws \RuntimeException
*
* @return Request
*/
private function getRequest()
{
if ($this->requestStack instanceof Request) {
return $this->requestStack;
} elseif ($this->requestStack instanceof RequestStack) {
$request = $this->requestStack->getCurrentRequest();
} else {
$request = $this->container->get('request');
}

if ($request !== null) {
return $request;
}

throw new \RuntimeException('There is no current request.');
}
}
4 changes: 2 additions & 2 deletions Resources/config/request.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@

<services>

<service id="fos_rest.request.param_fetcher" class="%fos_rest.request.param_fetcher.class%" scope="request">
<service id="fos_rest.request.param_fetcher" class="%fos_rest.request.param_fetcher.class%">
<argument type="service" id="fos_rest.request.param_fetcher.reader"/>
<argument type="service" id="request"/>
<argument type="service" id="request_stack" on-invalid="null"/>
<argument type="service" id="fos_rest.violation_formatter"/>
<argument type="service" id="validator" on-invalid="null"/>
</service>
Expand Down
2 changes: 1 addition & 1 deletion Tests/FOSRestBundleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public function testBuild()
$container = $this->getMockBuilder('\Symfony\Component\DependencyInjection\ContainerBuilder')
->setMethods(array('addCompilerPass'))
->getMock();
$container->expects($this->exactly(6))
$container->expects($this->exactly(7))
->method('addCompilerPass')
->with($this->isInstanceOf('\Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface'));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public function invalidFormAction()
{
$form = $this->createFormBuilder(null, array(
'csrf_protection' => false,
))->add('name', 'text', array(
))->add('name', method_exists('Symfony\Component\Form\AbstractType', 'getBlockPrefix') ? 'Symfony\Component\Form\Extension\Core\Type\TextType' : 'text', array(
'constraints' => array(new NotBlank()),
))->getForm();

Expand Down
2 changes: 1 addition & 1 deletion Tests/Functional/app/config/routing.yml
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
test_bundle:
resource: @TestBundle/Resources/config/routing.yml
resource: "@TestBundle/Resources/config/routing.yml"
Loading

0 comments on commit ebc42b0

Please sign in to comment.