Skip to content

Commit ed38673

Browse files
feature #61487 [Routing][Serializer] Deprecate annotation aliases and getters and setters in favor of public properties on attributes (nicolas-grekas)
This PR was merged into the 7.4 branch. Discussion ---------- [Routing][Serializer] Deprecate annotation aliases and getters and setters in favor of public properties on attributes | Q | A | ------------- | --- | Branch? | 7.4 | Bug fix? | no | New feature? | no | Deprecations? | yes | Issues | - | License | MIT Let's remove more needless boilerplate. These deprecations shouldn't affect anyone since I don't expected other code than ours to read these attributes. Note that I don't make properties on `Route` readonly because of the existing setters. (Note also that having readonly on the other attribute properties isn't useful in pragmatic terms, that's mostly pedantic ;) ) Commits ------- d179c52 [Routing][Serializer] Deprecate annotation aliases and getters and setters in favor of public properties on attributes
2 parents b086124 + d179c52 commit ed38673

28 files changed

+226
-172
lines changed

UPGRADE-7.4.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,8 @@ Mime
7777
Routing
7878
-------
7979

80-
* Deprecate `getEnv()` and `setEnv()` methods of the `Symfony\Component\Routing\Attribute\Route` class in favor of the plurialized `getEnvs()` and `setEnvs()` methods
80+
* Deprecate class aliases in the `Annotation` namespace, use attributes instead
81+
* Deprecate getters and setters in attribute classes in favor of public properties
8182

8283
Security
8384
--------
@@ -90,6 +91,8 @@ Serializer
9091
----------
9192

9293
* Make `AttributeMetadata` and `ClassMetadata` final
94+
* Deprecate class aliases in the `Annotation` namespace, use attributes instead
95+
* Deprecate getters in attribute classes in favor of public properties
9396

9497
String
9598
------

src/Symfony/Component/Routing/Annotation/Route.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@
1616
class_exists(\Symfony\Component\Routing\Attribute\Route::class);
1717

1818
if (false) {
19+
/**
20+
* @deprecated since Symfony 7.4, use {@see \Symfony\Component\Routing\Attribute\Route} instead
21+
*/
1922
#[\Attribute(\Attribute::IS_REPEATABLE | \Attribute::TARGET_CLASS | \Attribute::TARGET_METHOD)]
2023
class Route extends \Symfony\Component\Routing\Attribute\Route
2124
{

src/Symfony/Component/Routing/Attribute/DeprecatedAlias.php

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,28 +17,32 @@
1717
class DeprecatedAlias
1818
{
1919
public function __construct(
20-
private string $aliasName,
21-
private string $package,
22-
private string $version,
23-
private string $message = '',
20+
public readonly string $aliasName,
21+
public readonly string $package,
22+
public readonly string $version,
23+
public readonly string $message = '',
2424
) {
2525
}
2626

27+
#[\Deprecated('Use the "message" property instead', 'symfony/routing:7.4')]
2728
public function getMessage(): string
2829
{
2930
return $this->message;
3031
}
3132

33+
#[\Deprecated('Use the "aliasName" property instead', 'symfony/routing:7.4')]
3234
public function getAliasName(): string
3335
{
3436
return $this->aliasName;
3537
}
3638

39+
#[\Deprecated('Use the "package" property instead', 'symfony/routing:7.4')]
3740
public function getPackage(): string
3841
{
3942
return $this->package;
4043
}
4144

45+
#[\Deprecated('Use the "version" property instead', 'symfony/routing:7.4')]
4246
public function getVersion(): string
4347
{
4448
return $this->version;

src/Symfony/Component/Routing/Attribute/Route.php

Lines changed: 57 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,17 @@
2020
#[\Attribute(\Attribute::IS_REPEATABLE | \Attribute::TARGET_CLASS | \Attribute::TARGET_METHOD)]
2121
class Route
2222
{
23-
private ?string $path = null;
24-
private array $localizedPaths = [];
25-
private array $methods;
2623
/** @var string[] */
27-
private array $env;
28-
private array $schemes;
29-
/**
30-
* @var (string|DeprecatedAlias)[]
31-
*/
32-
private array $aliases = [];
24+
public array $methods;
25+
26+
/** @var string[] */
27+
public array $envs;
28+
29+
/** @var string[] */
30+
public array $schemes;
31+
32+
/** @var (string|DeprecatedAlias)[] */
33+
public array $aliases = [];
3334

3435
/**
3536
* @param string|array<string,string>|null $path The route path (i.e. "/user/login")
@@ -50,32 +51,28 @@ class Route
5051
* @param string|DeprecatedAlias|(string|DeprecatedAlias)[] $alias The list of aliases for this route
5152
*/
5253
public function __construct(
53-
string|array|null $path = null,
54-
private ?string $name = null,
55-
private array $requirements = [],
56-
private array $options = [],
57-
private array $defaults = [],
58-
private ?string $host = null,
54+
public string|array|null $path = null,
55+
public ?string $name = null,
56+
public array $requirements = [],
57+
public array $options = [],
58+
public array $defaults = [],
59+
public ?string $host = null,
5960
array|string $methods = [],
6061
array|string $schemes = [],
61-
private ?string $condition = null,
62-
private ?int $priority = null,
62+
public ?string $condition = null,
63+
public ?int $priority = null,
6364
?string $locale = null,
6465
?string $format = null,
6566
?bool $utf8 = null,
6667
?bool $stateless = null,
6768
string|array|null $env = null,
6869
string|DeprecatedAlias|array $alias = [],
6970
) {
70-
if (\is_array($path)) {
71-
$this->localizedPaths = $path;
72-
} else {
73-
$this->path = $path;
74-
}
75-
$this->setMethods($methods);
76-
$this->setSchemes($schemes);
77-
$this->setAliases($alias);
78-
$this->setEnvs((array) $env);
71+
$this->path = $path;
72+
$this->methods = (array) $methods;
73+
$this->schemes = (array) $schemes;
74+
$this->envs = (array) $env;
75+
$this->aliases = \is_array($alias) ? $alias : [$alias];
7976

8077
if (null !== $locale) {
8178
$this->defaults['_locale'] = $locale;
@@ -94,154 +91,161 @@ public function __construct(
9491
}
9592
}
9693

94+
#[\Deprecated('Use the "path" property instead', 'symfony/routing:7.4')]
9795
public function setPath(string $path): void
9896
{
9997
$this->path = $path;
10098
}
10199

100+
#[\Deprecated('Use the "path" property instead', 'symfony/routing:7.4')]
102101
public function getPath(): ?string
103102
{
104-
return $this->path;
103+
return \is_array($this->path) ? null : $this->path;
105104
}
106105

106+
#[\Deprecated('Use the "path" property instead', 'symfony/routing:7.4')]
107107
public function setLocalizedPaths(array $localizedPaths): void
108108
{
109-
$this->localizedPaths = $localizedPaths;
109+
$this->path = $localizedPaths;
110110
}
111111

112+
#[\Deprecated('Use the "path" property instead', 'symfony/routing:7.4')]
112113
public function getLocalizedPaths(): array
113114
{
114-
return $this->localizedPaths;
115+
return \is_array($this->path) ? $this->path : [];
115116
}
116117

118+
#[\Deprecated('Use the "host" property instead', 'symfony/routing:7.4')]
117119
public function setHost(string $pattern): void
118120
{
119121
$this->host = $pattern;
120122
}
121123

124+
#[\Deprecated('Use the "host" property instead', 'symfony/routing:7.4')]
122125
public function getHost(): ?string
123126
{
124127
return $this->host;
125128
}
126129

130+
#[\Deprecated('Use the "name" property instead', 'symfony/routing:7.4')]
127131
public function setName(string $name): void
128132
{
129133
$this->name = $name;
130134
}
131135

136+
#[\Deprecated('Use the "name" property instead', 'symfony/routing:7.4')]
132137
public function getName(): ?string
133138
{
134139
return $this->name;
135140
}
136141

142+
#[\Deprecated('Use the "requirements" property instead', 'symfony/routing:7.4')]
137143
public function setRequirements(array $requirements): void
138144
{
139145
$this->requirements = $requirements;
140146
}
141147

148+
#[\Deprecated('Use the "requirements" property instead', 'symfony/routing:7.4')]
142149
public function getRequirements(): array
143150
{
144151
return $this->requirements;
145152
}
146153

154+
#[\Deprecated('Use the "options" property instead', 'symfony/routing:7.4')]
147155
public function setOptions(array $options): void
148156
{
149157
$this->options = $options;
150158
}
151159

160+
#[\Deprecated('Use the "options" property instead', 'symfony/routing:7.4')]
152161
public function getOptions(): array
153162
{
154163
return $this->options;
155164
}
156165

166+
#[\Deprecated('Use the "defaults" property instead', 'symfony/routing:7.4')]
157167
public function setDefaults(array $defaults): void
158168
{
159169
$this->defaults = $defaults;
160170
}
161171

172+
#[\Deprecated('Use the "defaults" property instead', 'symfony/routing:7.4')]
162173
public function getDefaults(): array
163174
{
164175
return $this->defaults;
165176
}
166177

178+
#[\Deprecated('Use the "schemes" property instead', 'symfony/routing:7.4')]
167179
public function setSchemes(array|string $schemes): void
168180
{
169181
$this->schemes = (array) $schemes;
170182
}
171183

184+
#[\Deprecated('Use the "schemes" property instead', 'symfony/routing:7.4')]
172185
public function getSchemes(): array
173186
{
174187
return $this->schemes;
175188
}
176189

190+
#[\Deprecated('Use the "methods" property instead', 'symfony/routing:7.4')]
177191
public function setMethods(array|string $methods): void
178192
{
179193
$this->methods = (array) $methods;
180194
}
181195

196+
#[\Deprecated('Use the "methods" property instead', 'symfony/routing:7.4')]
182197
public function getMethods(): array
183198
{
184199
return $this->methods;
185200
}
186201

202+
#[\Deprecated('Use the "condition" property instead', 'symfony/routing:7.4')]
187203
public function setCondition(?string $condition): void
188204
{
189205
$this->condition = $condition;
190206
}
191207

208+
#[\Deprecated('Use the "condition" property instead', 'symfony/routing:7.4')]
192209
public function getCondition(): ?string
193210
{
194211
return $this->condition;
195212
}
196213

214+
#[\Deprecated('Use the "priority" property instead', 'symfony/routing:7.4')]
197215
public function setPriority(int $priority): void
198216
{
199217
$this->priority = $priority;
200218
}
201219

220+
#[\Deprecated('Use the "priority" property instead', 'symfony/routing:7.4')]
202221
public function getPriority(): ?int
203222
{
204223
return $this->priority;
205224
}
206225

207-
/**
208-
* @deprecated since Symfony 7.4, use the {@see setEnvs()} method instead
209-
*/
226+
#[\Deprecated('Use the "envs" property instead', 'symfony/routing:7.4')]
210227
public function setEnv(?string $env): void
211228
{
212-
trigger_deprecation('symfony/routing', '7.4', 'The "%s()" method is deprecated, use "setEnvs()" instead.', __METHOD__);
213-
$this->env = (array) $env;
229+
$this->envs = (array) $env;
214230
}
215231

216-
/**
217-
* @deprecated since Symfony 7.4, use {@see getEnvs()} method instead
218-
*/
232+
#[\Deprecated('Use the "envs" property instead', 'symfony/routing:7.4')]
219233
public function getEnv(): ?string
220234
{
221-
trigger_deprecation('symfony/routing', '7.4', 'The "%s()" method is deprecated, use "getEnvs()" instead.', __METHOD__);
222-
if (!$this->env) {
235+
if (!$this->envs) {
223236
return null;
224237
}
225-
if (\count($this->env) > 1) {
226-
throw new LogicException(\sprintf('The "env" property has %d environments. Use "getEnvs()" to get all of them.', \count($this->env)));
238+
if (\count($this->envs) > 1) {
239+
throw new LogicException(\sprintf('The "env" property has %d environments. Use "getEnvs()" to get all of them.', \count($this->envs)));
227240
}
228241

229-
return $this->env[0];
230-
}
231-
232-
public function setEnvs(array|string $env): void
233-
{
234-
$this->env = (array) $env;
235-
}
236-
237-
public function getEnvs(): array
238-
{
239-
return $this->env;
242+
return $this->envs[0];
240243
}
241244

242245
/**
243246
* @return (string|DeprecatedAlias)[]
244247
*/
248+
#[\Deprecated('Use the "aliases" property instead', 'symfony/routing:7.4')]
245249
public function getAliases(): array
246250
{
247251
return $this->aliases;
@@ -250,6 +254,7 @@ public function getAliases(): array
250254
/**
251255
* @param string|DeprecatedAlias|(string|DeprecatedAlias)[] $aliases
252256
*/
257+
#[\Deprecated('Use the "aliases" property instead', 'symfony/routing:7.4')]
253258
public function setAliases(string|DeprecatedAlias|array $aliases): void
254259
{
255260
$this->aliases = \is_array($aliases) ? $aliases : [$aliases];

src/Symfony/Component/Routing/CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ CHANGELOG
77
* Allow query-specific parameters in `UrlGenerator` using `_query`
88
* Add support of multiple env names in the `Symfony\Component\Routing\Attribute\Route` attribute
99
* Add argument `$parameters` to `RequestContext`'s constructor
10+
* Deprecate class aliases in the `Annotation` namespace, use attributes instead
11+
* Deprecate getters and setters in attribute classes in favor of public properties
1012

1113
7.3
1214
---

0 commit comments

Comments
 (0)