-
-
Notifications
You must be signed in to change notification settings - Fork 9.8k
[Routing] allow setting multiple envs in #[Route] attribute
#61358
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -11,6 +11,8 @@ | |
|
|
||
| namespace Symfony\Component\Routing\Attribute; | ||
|
|
||
| use Symfony\Component\Routing\Exception\LogicException; | ||
|
|
||
| /** | ||
| * @author Fabien Potencier <fabien@symfony.com> | ||
| * @author Alexander M. Turek <me@derrabus.de> | ||
|
|
@@ -21,6 +23,8 @@ class Route | |
| private ?string $path = null; | ||
| private array $localizedPaths = []; | ||
| private array $methods; | ||
| /** @var string[] */ | ||
| private array $env; | ||
| private array $schemes; | ||
| /** | ||
| * @var (string|DeprecatedAlias)[] | ||
|
|
@@ -42,7 +46,7 @@ class Route | |
| * @param string|null $format The format returned by the route (i.e. "json", "xml") | ||
| * @param bool|null $utf8 Whether the route accepts UTF-8 in its parameters | ||
| * @param bool|null $stateless Whether the route is defined as stateless or stateful, @see https://symfony.com/doc/current/routing.html#stateless-routes | ||
| * @param string|null $env The env in which the route is defined (i.e. "dev", "test", "prod") | ||
| * @param string|string[]|null $env The env(s) in which the route is defined (i.e. "dev", "test", "prod", ["dev", "test"]) | ||
| * @param string|DeprecatedAlias|(string|DeprecatedAlias)[] $alias The list of aliases for this route | ||
| */ | ||
| public function __construct( | ||
|
|
@@ -60,7 +64,7 @@ public function __construct( | |
| ?string $format = null, | ||
| ?bool $utf8 = null, | ||
| ?bool $stateless = null, | ||
| private ?string $env = null, | ||
| string|array|null $env = null, | ||
| string|DeprecatedAlias|array $alias = [], | ||
| ) { | ||
| if (\is_array($path)) { | ||
|
|
@@ -71,6 +75,7 @@ public function __construct( | |
| $this->setMethods($methods); | ||
| $this->setSchemes($schemes); | ||
| $this->setAliases($alias); | ||
| $this->setEnvs((array) $env); | ||
|
|
||
| if (null !== $locale) { | ||
| $this->defaults['_locale'] = $locale; | ||
|
|
@@ -199,12 +204,37 @@ public function getPriority(): ?int | |
| return $this->priority; | ||
| } | ||
|
|
||
| /** | ||
| * @deprecated since Symfony 7.4, use the {@see setEnvs()} method instead | ||
| */ | ||
| public function setEnv(?string $env): void | ||
| { | ||
| $this->env = $env; | ||
| trigger_deprecation('symfony/routing', '7.4', 'The "%s()" method is deprecated, use "setEnvs()" instead.', __METHOD__); | ||
| $this->env = (array) $env; | ||
| } | ||
|
|
||
| /** | ||
| * @deprecated since Symfony 7.4, use {@see getEnvs()} method instead | ||
| */ | ||
| public function getEnv(): ?string | ||
| { | ||
| trigger_deprecation('symfony/routing', '7.4', 'The "%s()" method is deprecated, use "getEnvs()" instead.', __METHOD__); | ||
| if (!$this->env) { | ||
| return null; | ||
| } | ||
| if (\count($this->env) > 1) { | ||
| throw new LogicException(\sprintf('The "env" property has %d environments. Use "getEnvs()" to get all of them.', \count($this->env))); | ||
| } | ||
|
|
||
| return $this->env[0]; | ||
| } | ||
|
|
||
| public function setEnvs(array|string $env): void | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why do we have setters at all?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think adding the setter helps keep consistency with the current class design. But if you feel it’s unnecessary, I can remove it |
||
| { | ||
| $this->env = (array) $env; | ||
| } | ||
|
|
||
| public function getEnvs(): array | ||
| { | ||
| return $this->env; | ||
| } | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.