Skip to content

[Routing] Remove deprecation layer #41371

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 27, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
160 changes: 43 additions & 117 deletions src/Symfony/Component/Routing/Annotation/Route.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,133 +24,59 @@
#[\Attribute(\Attribute::IS_REPEATABLE | \Attribute::TARGET_CLASS | \Attribute::TARGET_METHOD)]
class Route
{
private $path;
private $localizedPaths = [];
private $name;
private $requirements = [];
private $options = [];
private $defaults = [];
private $host;
private $methods = [];
private $schemes = [];
private $condition;
private $priority;
private $env;
private ?string $path = null;
private array $localizedPaths = [];
private array $methods;
private array $schemes;

/**
* @param array|string $data data array managed by the Doctrine Annotations library or the path
* @param array|string|null $path
* @param string[] $requirements
* @param string[]|string $methods
* @param string[]|string $schemes
*
* @throws \BadMethodCallException
* @param string[] $requirements
* @param string[]|string $methods
* @param string[]|string $schemes
*/
public function __construct(
$data = [],
$path = null,
string $name = null,
array $requirements = [],
array $options = [],
array $defaults = [],
string $host = null,
$methods = [],
$schemes = [],
string $condition = null,
int $priority = null,
string | array | null $path = null,
private ?string $name = null,
private array $requirements = [],
private array $options = [],
private array $defaults = [],
private ?string $host = null,
array | string $methods = [],
array | string $schemes = [],
private ?string $condition = null,
private ?int $priority = null,
string $locale = null,
string $format = null,
bool $utf8 = null,
bool $stateless = null,
string $env = null
private ?string $env = null
) {
if (\is_string($data)) {
$data = ['path' => $data];
} elseif (!\is_array($data)) {
throw new \TypeError(sprintf('"%s": Argument $data is expected to be a string or array, got "%s".', __METHOD__, get_debug_type($data)));
} elseif ([] !== $data) {
$deprecation = false;
foreach ($data as $key => $val) {
if (\in_array($key, ['path', 'name', 'requirements', 'options', 'defaults', 'host', 'methods', 'schemes', 'condition', 'priority', 'locale', 'format', 'utf8', 'stateless', 'env', 'value'])) {
$deprecation = true;
}
}

if ($deprecation) {
trigger_deprecation('symfony/routing', '5.3', 'Passing an array as first argument to "%s" is deprecated. Use named arguments instead.', __METHOD__);
} else {
$localizedPaths = $data;
$data = ['path' => $localizedPaths];
}
}
if (null !== $path && !\is_string($path) && !\is_array($path)) {
throw new \TypeError(sprintf('"%s": Argument $path is expected to be a string, array or null, got "%s".', __METHOD__, get_debug_type($path)));
}

$data['path'] = $data['path'] ?? $path;
$data['name'] = $data['name'] ?? $name;
$data['requirements'] = $data['requirements'] ?? $requirements;
$data['options'] = $data['options'] ?? $options;
$data['defaults'] = $data['defaults'] ?? $defaults;
$data['host'] = $data['host'] ?? $host;
$data['methods'] = $data['methods'] ?? $methods;
$data['schemes'] = $data['schemes'] ?? $schemes;
$data['condition'] = $data['condition'] ?? $condition;
$data['priority'] = $data['priority'] ?? $priority;
$data['locale'] = $data['locale'] ?? $locale;
$data['format'] = $data['format'] ?? $format;
$data['utf8'] = $data['utf8'] ?? $utf8;
$data['stateless'] = $data['stateless'] ?? $stateless;
$data['env'] = $data['env'] ?? $env;

$data = array_filter($data, static function ($value): bool {
return null !== $value;
});

if (isset($data['localized_paths'])) {
throw new \BadMethodCallException(sprintf('Unknown property "localized_paths" on annotation "%s".', static::class));
}

if (isset($data['value'])) {
$data[\is_array($data['value']) ? 'localized_paths' : 'path'] = $data['value'];
unset($data['value']);
}

if (isset($data['path']) && \is_array($data['path'])) {
$data['localized_paths'] = $data['path'];
unset($data['path']);
}

if (isset($data['locale'])) {
$data['defaults']['_locale'] = $data['locale'];
unset($data['locale']);
if (\is_array($path)) {
$this->localizedPaths = $path;
} else {
$this->path = $path;
}
$this->setMethods($methods);
$this->setSchemes($schemes);

if (isset($data['format'])) {
$data['defaults']['_format'] = $data['format'];
unset($data['format']);
if (null !== $locale) {
$this->defaults['_locale'] = $locale;
}

if (isset($data['utf8'])) {
$data['options']['utf8'] = filter_var($data['utf8'], \FILTER_VALIDATE_BOOLEAN) ?: false;
unset($data['utf8']);
if (null !== $format) {
$this->defaults['_format'] = $format;
}

if (isset($data['stateless'])) {
$data['defaults']['_stateless'] = filter_var($data['stateless'], \FILTER_VALIDATE_BOOLEAN) ?: false;
unset($data['stateless']);
if (null !== $utf8) {
$this->options['utf8'] = $utf8;
}

foreach ($data as $key => $value) {
$method = 'set'.str_replace('_', '', $key);
if (!method_exists($this, $method)) {
throw new \BadMethodCallException(sprintf('Unknown property "%s" on annotation "%s".', $key, static::class));
}
$this->$method($value);
if (null !== $stateless) {
$this->defaults['_stateless'] = $stateless;
}
}

public function setPath($path)
public function setPath(string $path)
{
$this->path = $path;
}
Expand All @@ -170,7 +96,7 @@ public function getLocalizedPaths(): array
return $this->localizedPaths;
}

public function setHost($pattern)
public function setHost(string $pattern)
{
$this->host = $pattern;
}
Expand All @@ -180,7 +106,7 @@ public function getHost()
return $this->host;
}

public function setName($name)
public function setName(string $name)
{
$this->name = $name;
}
Expand All @@ -190,7 +116,7 @@ public function getName()
return $this->name;
}

public function setRequirements($requirements)
public function setRequirements(array $requirements)
{
$this->requirements = $requirements;
}
Expand All @@ -200,7 +126,7 @@ public function getRequirements()
return $this->requirements;
}

public function setOptions($options)
public function setOptions(array $options)
{
$this->options = $options;
}
Expand All @@ -210,7 +136,7 @@ public function getOptions()
return $this->options;
}

public function setDefaults($defaults)
public function setDefaults(array $defaults)
{
$this->defaults = $defaults;
}
Expand All @@ -220,27 +146,27 @@ public function getDefaults()
return $this->defaults;
}

public function setSchemes($schemes)
public function setSchemes(array | string $schemes)
{
$this->schemes = \is_array($schemes) ? $schemes : [$schemes];
$this->schemes = (array) $schemes;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for a lower branch?

}

public function getSchemes()
{
return $this->schemes;
}

public function setMethods($methods)
public function setMethods(array | string $methods)
{
$this->methods = \is_array($methods) ? $methods : [$methods];
$this->methods = (array) $methods;
}

public function getMethods()
{
return $this->methods;
}

public function setCondition($condition)
public function setCondition(?string $condition)
{
$this->condition = $condition;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,28 +25,15 @@ class RoutingResolverPass implements CompilerPassInterface
{
use PriorityTaggedServiceTrait;

private $resolverServiceId;
private $loaderTag;

public function __construct(string $resolverServiceId = 'routing.resolver', string $loaderTag = 'routing.loader')
{
if (0 < \func_num_args()) {
trigger_deprecation('symfony/routing', '5.3', 'Configuring "%s" is deprecated.', __CLASS__);
}

$this->resolverServiceId = $resolverServiceId;
$this->loaderTag = $loaderTag;
}

public function process(ContainerBuilder $container)
{
if (false === $container->hasDefinition($this->resolverServiceId)) {
if (false === $container->hasDefinition('routing.resolver')) {
return;
}

$definition = $container->getDefinition($this->resolverServiceId);
$definition = $container->getDefinition('routing.resolver');

foreach ($this->findAndSortTaggedServices($this->loaderTag, $container) as $id) {
foreach ($this->findAndSortTaggedServices('routing.loader', $container) as $id) {
$definition->addMethodCall('addLoader', [new Reference($id)]);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,8 @@ class MethodNotAllowedException extends \RuntimeException implements ExceptionIn
/**
* @param string[] $allowedMethods
*/
public function __construct(array $allowedMethods, ?string $message = '', int $code = 0, \Throwable $previous = null)
public function __construct(array $allowedMethods, string $message = '', int $code = 0, \Throwable $previous = null)
{
if (null === $message) {
trigger_deprecation('symfony/routing', '5.3', 'Passing null as $message to "%s()" is deprecated, pass an empty string instead.', __METHOD__);

$message = '';
}

$this->allowedMethods = array_map('strtoupper', $allowedMethods);

parent::__construct($message, $code, $previous);
Expand Down
11 changes: 2 additions & 9 deletions src/Symfony/Component/Routing/RouteCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,20 +71,13 @@ public function count()
return \count($this->routes);
}

/**
* @param int $priority
*/
public function add(string $name, Route $route/*, int $priority = 0*/)
public function add(string $name, Route $route, int $priority = 0)
{
if (\func_num_args() < 3 && __CLASS__ !== static::class && __CLASS__ !== (new \ReflectionMethod($this, __FUNCTION__))->getDeclaringClass()->getName() && !$this instanceof \PHPUnit\Framework\MockObject\MockObject && !$this instanceof \Prophecy\Prophecy\ProphecySubjectInterface && !$this instanceof \Mockery\MockInterface) {
trigger_deprecation('symfony/routing', '5.1', 'The "%s()" method will have a new "int $priority = 0" argument in version 6.0, not defining it is deprecated.', __METHOD__);
}

unset($this->routes[$name], $this->priorities[$name]);

$this->routes[$name] = $route;

if ($priority = 3 <= \func_num_args() ? func_get_arg(2) : 0) {
if ($priority) {
$this->priorities[$name] = $priority;
}
}
Expand Down
Loading