Skip to content

Commit

Permalink
Merge branch '2.x'
Browse files Browse the repository at this point in the history
* 2.x:
  deprecate using the ControllerNameParser
  • Loading branch information
xabbuh committed Feb 4, 2020
2 parents f0854fe + 689a4dd commit 1f8cebe
Show file tree
Hide file tree
Showing 22 changed files with 375 additions and 82 deletions.
19 changes: 16 additions & 3 deletions DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,21 @@ public function getConfigTreeBuilder()
->arrayNode('routing_loader')
->addDefaultsIfNotSet()
->children()
->booleanNode('parse_controller_name')
->defaultValue(static function () {
@trigger_error('Setting the "fos_rest.routing_loader.parse_controller_name" configuration option to "true" is deprecated since FOSRestBundle 2.8.', E_USER_DEPRECATED);

return true;
})
->validate()
->ifTrue(static function ($v) { return $v; })
->then(static function ($v) {
@trigger_error('Setting the "fos_rest.routing_loader.parse_controller_name" configuration option to "true" is deprecated since FOSRestBundle 2.8.', E_USER_DEPRECATED);

return $v;
})
->end()
->end()
->scalarNode('default_format')->defaultNull()->end()
->scalarNode('prefix_methods')->defaultTrue()->end()
->scalarNode('include_format')->defaultTrue()->end()
Expand Down Expand Up @@ -441,9 +456,7 @@ private function addExceptionSection(ArrayNodeDefinition $rootNode)
}

if (!defined('Symfony\Component\HttpFoundation\Response::'.$item)) {
throw new InvalidConfigurationException(
'Invalid HTTP code in fos_rest.exception.codes, see Symfony\Component\HttpFoundation\Response for all valid codes.'
);
throw new InvalidConfigurationException('Invalid HTTP code in fos_rest.exception.codes, see Symfony\Component\HttpFoundation\Response for all valid codes.');
}

$item = constant('Symfony\Component\HttpFoundation\Response::'.$item);
Expand Down
11 changes: 10 additions & 1 deletion DependencyInjection/FOSRestExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use Symfony\Component\DependencyInjection\ChildDefinition;
use Symfony\Component\DependencyInjection\Compiler\ServiceLocatorTagPass;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\DependencyInjection\DefinitionDecorator;
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
use Symfony\Component\DependencyInjection\Reference;
Expand Down Expand Up @@ -55,7 +56,15 @@ public function load(array $configs, ContainerBuilder $container)
$loader->load('request.xml');
$loader->load('serializer.xml');

$container->getDefinition('fos_rest.routing.loader.controller')->replaceArgument(4, $config['routing_loader']['default_format']);
$restRouteLoader = $container->getDefinition('fos_rest.routing.loader.controller');

if ($config['routing_loader']['parse_controller_name']) {
$restRouteLoader->addArgument(new Reference('controller_name_converter', ContainerInterface::NULL_ON_INVALID_REFERENCE));
}

$restRouteLoader->addArgument(new Reference('fos_rest.routing.loader.reader.controller'));
$restRouteLoader->addArgument($config['routing_loader']['default_format']);

$container->getDefinition('fos_rest.routing.loader.yaml_collection')->replaceArgument(4, $config['routing_loader']['default_format']);
$container->getDefinition('fos_rest.routing.loader.xml_collection')->replaceArgument(4, $config['routing_loader']['default_format']);

Expand Down
3 changes: 0 additions & 3 deletions Resources/config/routing.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,6 @@
<service id="fos_rest.routing.loader.controller" class="FOS\RestBundle\Routing\Loader\RestRouteLoader" public="false">
<argument type="service" id="service_container" />
<argument type="service" id="file_locator" />
<argument type="service" id="controller_name_converter" on-invalid="null" />
<argument type="service" id="fos_rest.routing.loader.reader.controller" />
<argument /> <!-- default format -->
<tag name="routing.loader" />
</service>

Expand Down
26 changes: 20 additions & 6 deletions Routing/Loader/RestRouteLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,21 +38,35 @@ class RestRouteLoader extends Loader
*
* @param ContainerInterface $container
* @param FileLocatorInterface $locator
* @param ControllerNameParser $controllerParser
* @param RestControllerReader $controllerReader
* @param string $defaultFormat
*/
public function __construct(
ContainerInterface $container,
FileLocatorInterface $locator,
$controllerParser,
RestControllerReader $controllerReader, $defaultFormat = 'html'
$controllerReader,
$defaultFormat = 'html'
) {
$this->container = $container;
$this->locator = $locator;
$this->controllerParser = $controllerParser;
$this->controllerReader = $controllerReader;
$this->defaultFormat = $defaultFormat;

if ($controllerReader instanceof ControllerNameParser || null === $controllerReader) {
@trigger_error(sprintf('Not passing an instance of %s as the 3rd argument of %s() is deprecated since FOSRestBundle 2.8.', RestControllerReader::class, __METHOD__), E_USER_DEPRECATED);

$this->controllerParser = $controllerReader;

if (!$defaultFormat instanceof RestControllerReader) {
throw new \TypeError(sprintf('Argument 4 passed to %s() must be an instance of %s, %s given.', __METHOD__, RestControllerReader::class, is_object($defaultFormat) ? get_class($defaultFormat) : gettype($defaultFormat)));
}

$this->controllerReader = $defaultFormat;
$this->defaultFormat = func_num_args() > 4 ? func_get_arg(4) : 'html';
} elseif (!$controllerReader instanceof RestControllerReader) {
throw new \TypeError(sprintf('Argument 3 passed to %s() must be an instance of %s, %s given.', __METHOD__, RestControllerReader::class, is_object($controllerReader) ? get_class($controllerReader) : gettype($controllerReader)));
} else {
$this->controllerReader = $controllerReader;
$this->defaultFormat = $defaultFormat;
}
}

/**
Expand Down
18 changes: 18 additions & 0 deletions Tests/DependencyInjection/ConfigurationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ public function testExceptionCodesAcceptsIntegers()
'exception' => [
'codes' => $expectedConfig,
],
'routing_loader' => [
'parse_controller_name' => false,
],
],
]
);
Expand All @@ -78,6 +81,9 @@ public function testThatResponseConstantsConvertedToCodes()
MethodNotAllowedException::class => 'HTTP_METHOD_NOT_ALLOWED',
],
],
'routing_loader' => [
'parse_controller_name' => false,
],
];

$config = $this->processor->processConfiguration($this->configuration, [$config]);
Expand Down Expand Up @@ -106,6 +112,9 @@ public function testThatIfExceptionCodeIncorrectExceptionIsThrown($value)
\RuntimeException::class => $value,
],
],
'routing_loader' => [
'parse_controller_name' => false,
],
],
]
);
Expand All @@ -125,6 +134,9 @@ public function testLoadBadMessagesClassThrowsException()
'UnknownException' => true,
],
],
'routing_loader' => [
'parse_controller_name' => false,
],
],
]
);
Expand All @@ -145,6 +157,9 @@ public function testLoadBadCodesClassThrowsException()
'UnknownException' => 404,
],
],
'routing_loader' => [
'parse_controller_name' => false,
],
],
]
);
Expand All @@ -168,6 +183,9 @@ public function testOverwriteFormatListenerRulesDoesNotMerge()
],
],
],
'routing_loader' => [
'parse_controller_name' => false,
],
],
[
'format_listener' => [
Expand Down
Loading

0 comments on commit 1f8cebe

Please sign in to comment.