8000 Corrections & improvments · symfony/symfony@0efdb67 · GitHub
[go: up one dir, main page]

Skip to content

Commit 0efdb67

Browse files
Olivier DolbeauOlivier Dolbeau
Olivier Dolbeau
authored and
Olivier Dolbeau
committed
Corrections & improvments
1 parent 9286fec commit 0efdb67

File tree

12 files changed

+47
-59
lines changed

12 files changed

+47
-59
lines changed

src/Symfony/Component/Routing/Loader/Configurator/CollectionConfigurator.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,9 @@ public function __destruct()
4343
if (null === $this->prefixes) {
4444
$this->collection->addPrefix($this->route->getPath());
4545
}
46-
$this->addHost($this->collection, $this->host);
46+
if (null !== $this->host) {
47+
$this->addHost($this->collection, $this->host);
48+
}
4749

4850
$this->parent->addCollection($this->collection);
4951
}

src/Symfony/Component/Routing/Loader/Configurator/ImportConfigurator.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ final public function namePrefix(string $namePrefix): self
6262
}
6363

6464
/**
65-
* Sets the host to add to use for all child routes.
65+
* Sets the host to use for all child routes.
6666
*
6767
* @param string|array $host the host, or the localized hosts
6868
*

src/Symfony/Component/Routing/Loader/Configurator/Traits/HostTrait.php

Lines changed: 9 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -18,42 +18,30 @@
1818
*/
1919
trait HostTrait
2020
{
21-
final protected function addHost(RouteCollection $routes, $host)
21+
final protected function addHost(RouteCollection $routes, $hosts)
2222
{
23-
if (null === $host) {
24-
return;
25-
}
26-
27-
if (!\is_array($host)) {
28-
$routes->setHost($host);
29-
30-
return;
31-
}
23+
if (!$hosts || !\is_array($hosts)) {
24+
$routes->setHost($hosts ?: '');
3225

33-
if (0 === \count($host)) {
3426
return;
3527
}
3628

37-
foreach ($host as $locale => $localeHost) {
38-
$host[$locale] = trim(trim($localeHost), '/');
39-
}
40-
4129
foreach ($routes->all() as $name => $route) {
4230
if (null === $locale = $route->getDefault('_locale')) {
4331
$routes->remove($name);
44-
foreach ($host as $locale => $localeHost) {
32+
foreach ($hosts as $locale => $host) {
4533
$localizedRoute = clone $route;
4634
$localizedRoute->setDefault('_locale', $locale);
47-
$localizedRoute->setRequirement('_locale', $locale);
35+
$localizedRoute->setRequirement('_locale', preg_quote($locale));
4836
$localizedRoute->setDefault('_canonical_route', $name);
49-
$localizedRoute->setHost($localeHost);
37+
$localizedRoute->setHost($host);
5038
$routes->add($name.'.'.$locale, $localizedRoute);
5139
}
52-
} elseif (!isset($host[$locale])) {
40+
} elseif (!isset($hosts[$locale])) {
5341
throw new \InvalidArgumentException(sprintf('Route "%s" with locale "%s" is missing a corresponding host in its parent collection.', $name, $locale));
5442
} else {
55-
$route->setHost($host[$locale]);
56-
$route->setRequirement('_locale', $locale);
43+
$route->setHost($hosts[$locale]);
44+
$route->setRequirement('_locale', preg_quote($locale));
5745
$routes->add($name, $route);
5846
}
5947
}

src/Symfony/Component/Routing/Loader/XmlFileLoader.php

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ protected function parseRoute(RouteCollection $collection, \DOMElement $node, st
118118
$schemes = preg_split('/[\s,\|]++/', $node->getAttribute('schemes'), -1, PREG_SPLIT_NO_EMPTY);
119119
$methods = preg_split('/[\s,\|]++/', $node->getAttribute('methods'), -1, PREG_SPLIT_NO_EMPTY);
120120

121-
list($defaults, $requirements, $options, $condition, $paths, /** $prefixes */, $hosts) = $this->parseConfigs($node, $filepath);
121+
list($defaults, $requirements, $options, $condition, $paths, /* $prefixes */, $hosts) = $this->parseConfigs($node, $filepath);
122122

123123
$path = $node->getAttribute('path');
124124

@@ -137,7 +137,10 @@ protected function parseRoute(RouteCollection $collection, \DOMElement $node, st
137137
$routes->setSchemes($schemes);
138138
$routes->setMethods($methods);
139139
$routes->setCondition($condition);
140-
$this->addHost($routes, $hosts);
140+
141+
if (null !== $hosts) {
142+
$this->addHost($routes, $hosts);
143+
}
141144
}
142145

143146
/**
@@ -162,7 +165,7 @@ protected function parseImport(RouteCollection $collection, \DOMElement $node, s
162165
$trailingSlashOnRoot = $node->hasAttribute('trailing-slash-on-root') ? XmlUtils::phpize($node->getAttribute('trailing-slash-on-root')) : true;
163166
$namePrefix = $node->getAttribute('name-prefix') ?: null;
164167

165-
list($defaults, $requirements, $options, $condition, /** $paths */, $prefixes, $hosts) = $this->parseConfigs($node, $path);
168+
list($defaults, $requirements, $options, $condition, /* $paths */, $prefixes, $hosts) = $this->parseConfigs($node, $path);
166169

167170
if ('' !== $prefix && $prefixes) {
168171
throw new \InvalidArgumentException(sprintf('The <route> element in file "%s" must not have both a "prefix" attribute and <prefix> child nodes.', $path));
@@ -193,7 +196,10 @@ protected function parseImport(RouteCollection $collection, \DOMElement $node, s
193196

194197
foreach ($imported as $subCollection) {
195198
$this->addPrefix($subCollection, $prefixes ?: $prefix, $trailingSlashOnRoot);
196-
$this->addHost($subCollection, $hosts);
199+
200+
if (null !== $hosts) {
201+
$this->addHost($subCollection, $hosts);
202+
}
197203

198204
if (null !== $condition) {
199205
$subCollection->setCondition($condition);
@@ -312,10 +318,8 @@ private function parseConfigs(\DOMElement $node, string $path): array
312318
$defaults['_stateless'] = XmlUtils::phpize($stateless);
313319
}
314320

315-
if ([] === $hosts) {
321+
if (!$hosts) {
316322
$hosts = $node->hasAttribute('host') ? $node->getAttribute('host') : null;
317-
} elseif ('' === array_key_first($hosts)) {
318-
$hosts = reset($hosts);
319323
}
320324

321325
return [$defaults, $requirements, $options, $condition, $paths, $prefixes, $hosts];

src/Symfony/Component/Routing/Loader/YamlFileLoader.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,10 @@ protected function parseRoute(RouteCollection $collection, string $name, array $
146146
$routes->setSchemes($config['schemes'] ?? []);
147147
$routes->setMethods($config['methods'] ?? []);
148148
$routes->setCondition($config['condition'] ?? null);
149-
$this->addHost($routes, $config['host'] ?? null);
149+
150+
if (isset($config['host'])) {
151+
$this->addHost($routes, $config['host']);
152+
}
150153
}
151154

152155
/**
@@ -198,8 +201,10 @@ protected function parseImport(RouteCollection $collection, array $config, strin
198201

199202
foreach ($imported as $subCollection) {
200203
$this->addPrefix($subCollection, $prefix, $trailingSlashOnRoot);
201-
$this->addHost($subCollection, $host);
202204

205+
if (null !== $host) {
206+
$this->addHost($subCollection, $host);
207+
}
203208
if (null !== $condition) {
204209
$subCollection->setCondition($condition);
205210
}

src/Symfony/Component/Routing/Loader/schema/routing/routing-1.0.xsd

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,6 @@
3232
</xsd:simpleContent>
3333
</xsd:complexType>
3434

35-
<xsd:complexType name="faculative-localized-path">
36-
<xsd:simpleContent>
37-
<xsd:extension base="xsd:string">
38-
<xsd:attribute name="locale" type="xsd:string" />
39-
</xsd:extension>
40-
</xsd:simpleContent>
41-
</xsd:complexType>
42-
4335
<xsd:group name="configs">
4436
<xsd:choice>
4537
<xsd:element name="default" nillable="true" type="default" />
@@ -53,7 +45,7 @@
5345
<xsd:sequence>
5446
<xsd:group ref="configs" minOccurs="0" maxOccurs="unbounded" />
5547
<xsd:element name="path" type="localized-path" minOccurs="0" maxOccurs="unbounded" />
56-
<xsd:element name="host" type="faculative-localized-path" minOccurs="0" maxOccurs="unbounded" />
48+
<xsd:element name="host" type="localized-path" minOccurs="0" maxOccurs="unbounded" />
5749
</xsd:sequence>
5850
<xsd:attribute name="id" type="xsd:string" use="required" />
5951
<xsd:attribute name="path" type="xsd:string" />
@@ -72,7 +64,7 @@
7264
<xsd:group ref="configs" minOccurs="0" maxOccurs="unbounded" />
7365
<xsd:element name="prefix" type="localized-path" minOccurs="0" maxOccurs="unbounded" />
7466
<xsd:element name="exclude" type="xsd:string" minOccurs="0" maxOccurs="unbounded" />
75-
<xsd:element name="host" type="faculative-localized-path" minOccurs="0" maxOccurs="unbounded" />
67+
<xsd:element name="host" type="localized-path" minOccurs="0" maxOccurs="unbounded" />
7668
</xsd:sequence>
7769
<xsd:attribute name="resource" type="xsd:string" use="required" />
7870
<xsd:attribute name="type" type="xsd:string" />

src/Symfony/Component/Routing/Tests/Fixtures/locale_and_host/imported.xml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,7 @@
1313
<route id="imported_not_localized" path="/here">
1414
<default key="_controller">ImportedController::someAction</default>
1515
</route>
16-
<route id="imported_single_host" path="/here_again">
16+
<route id="imported_single_host" path="/here_again" host="www.custom.com">
1717
<default key="_controller">ImportedController::someAction</default>
18-
<host>www.custom.com</host>
1918
</route>
2019
</routes>

src/Symfony/Component/Routing/Tests/Fixtures/locale_and_host/importer-with-single-host.xml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
44
xsi:schemaLocation="http://symfony.com/schema/routing
55
https://symfony.com/schema/routing/routing-1.0.xsd">
6-
<import resource="./imported.xml">
7-
<host>www.example.com</host>
6+
<import resource="./imported.xml" host="www.example.com">
87
</import>
98
</routes>

src/Symfony/Component/Routing/Tests/Loader/PhpFileLoaderTest.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,7 @@ public function testImportingRoutesWithHostsInImporter()
249249
$loader = new PhpFileLoader(new FileLocator([__DIR__.'/../Fixtures/locale_and_host']));
250250
$routes = $loader->load('importer-with-host.php');
251251

252-
$expectedRoutes = require(__DIR__.'/../Fixtures/locale_and_host/import-with-host-expected-collection.php');
252+
$expectedRoutes = require __DIR__.'/../Fixtures/locale_and_host/import-with-host-expected-collection.php';
253253

254254
$this->assertEquals($expectedRoutes('php'), $routes);
255255
}
@@ -259,7 +259,7 @@ public function testImportingRoutesWithLocalesAndHostInImporter()
259259
$loader = new PhpFileLoader(new FileLocator([__DIR__.'/../Fixtures/locale_and_host']));
260260
$routes = $loader->load('importer-with-locale-and-host.php');
261261

262-
$expectedRoutes = require(__DIR__.'/../Fixtures/locale_and_host/import-with-locale-and-host-expected-collection.php');
262+
$expectedRoutes = require __DIR__.'/../Fixtures/locale_and_host/import-with-locale-and-host-expected-collection.php';
263263

264264
$this->assertEquals($expectedRoutes('php'), $routes);
265265
}
@@ -269,7 +269,7 @@ public function testImportingRoutesWithoutHostInImporter()
269269
$loader = new PhpFileLoader(new FileLocator([__DIR__.'/../Fixtures/locale_and_host']));
270270
$routes = $loader->load('importer-without-host.php');
271271

272-
$expectedRoutes = require(__DIR__.'/../Fixtures/locale_and_host/import-without-host-expected-collection.php');
272+
$expectedRoutes = require __DIR__.'/../Fixtures/locale_and_host/import-without-host-expected-collection.php';
273273

274274
$this->assertEquals($expectedRoutes('php'), $routes);
275275
}
@@ -279,7 +279,7 @@ public function testImportingRoutesWithSingleHostInImporter()
279279
$loader = new PhpFileLoader(new FileLocator([__DIR__.'/../Fixtures/locale_and_host']));
280280
$routes = $loader->load('importer-with-single-host.php');
281281

282-
$expectedRoutes = require(__DIR__.'/../Fixtures/locale_and_host/import-with-single-host-expected-collection.php');
282+
$expectedRoutes = require __DIR__.'/../Fixtures/locale_and_host/import-with-single-host-expected-collection.php';
283283

284284
$this->assertEquals($expectedRoutes('php'), $routes);
285285
}

src/Symfony/Component/Routing/Tests/Loader/XmlFileLoaderTest.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -526,7 +526,7 @@ public function testImportingRoutesWithHostsInImporter()
526526
$loader = new XmlFileLoader(new FileLocator([__DIR__.'/../Fixtures/locale_and_host']));
527527
$routes = $loader->load('importer-with-host.xml');
528528

529-
$expectedRoutes = require(__DIR__.'/../Fixtures/locale_and_host/import-with-host-expected-collection.php');
529+
$expectedRoutes = require __DIR__.'/../Fixtures/locale_and_host/import-with-host-expected-collection.php';
530530

531531
$this->assertEquals($expectedRoutes('xml'), $routes);
532532
}
@@ -536,7 +536,7 @@ public function testImportingRoutesWithLocalesAndHostInImporter()
536536
$loader = new XmlFileLoader(new FileLocator([__DIR__.'/../Fixtures/locale_and_host']));
537537
$routes = $loader->load('importer-with-locale-and-host.xml');
538538

539-
$expectedRoutes = require(__DIR__.'/../Fixtures/locale_and_host/import-with-locale-and-host-expected-collection.php');
539+
$expectedRoutes = require __DIR__.'/../Fixtures/locale_and_host/import-with-locale-and-host-expected-collection.php';
540540

541541
$this->assertEquals($expectedRoutes('xml'), $routes);
542542
}
@@ -546,7 +546,7 @@ public function testImportingRoutesWithoutHostsInImporter()
546546
$loader = new XmlFileLoader(new FileLocator([__DIR__.'/../Fixtures/locale_and_host']));
547547
$routes = $loader->load('importer-without-host.xml');
548548

549-
$expectedRoutes = require(__DIR__.'/../Fixtures/locale_and_host/import-without-host-expected-collection.php');
549+
$expectedRoutes = require __DIR__.'/../Fixtures/locale_and_host/import-without-host-expected-collection.php';
550550

551551
$this->assertEquals($expectedRoutes('xml'), $routes);
552552
}
@@ -556,7 +556,7 @@ public function testImportingRoutesWithSingleHostsInImporter()
556556
$loader = new XmlFileLoader(new FileLocator([__DIR__.'/../Fixtures/locale_and_host']));
557557
$routes = $loader->load('importer-with-single-host.xml');
558558

559-
$expectedRoutes = require(__DIR__.'/../Fixtures/locale_and_host/import-with-single-host-expected-collection.php');
559+
$expectedRoutes = require __DIR__.'/../Fixtures/locale_and_host/import-with-single-host-expected-collection.php';
560560

561561
$this->assertEquals($expectedRoutes('xml'), $routes);
562562
}

src/Symfony/Component/Routing/Tests/Loader/YamlFileLoaderTest.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -398,7 +398,7 @@ public function testImportingRoutesWithHostsInImporter()
398398
$loader = new YamlFileLoader(new FileLocator([__DIR__.'/../Fixtures/locale_and_host']));
399399
$routes = $loader->load('importer-with-host.yml');
400400

401-
$expectedRoutes = require(__DIR__.'/../Fixtures/locale_and_host/import-with-host-expected-collection.php');
401+
$expectedRoutes = require __DIR__.'/../Fixtures/locale_and_host/import-with-host-expected-collection.php';
402402

403403
$this->assertEquals($expectedRoutes('yml'), $routes);
404404
}
@@ -408,7 +408,7 @@ public function testImportingRoutesWithLocalesAndHostInImporter()
408408
$loader = new YamlFileLoader(new FileLocator([__DIR__.'/../Fixtures/locale_and_host']));
409409
$routes = $loader->load('importer-with-locale-and-host.yml');
410410

411-
$expectedRoutes = require(__DIR__.'/../Fixtures/locale_and_host/import-with-locale-and-host-expected-collection.php');
411+
$expectedRoutes = require __DIR__.'/../Fixtures/locale_and_host/import-with-locale-and-host-expected-collection.php';
412412

413413
$this->assertEquals($expectedRoutes('yml'), $routes);
414414
}
@@ -418,7 +418,7 @@ public function testImportingRoutesWithoutHostInImporter()
418418
$loader = new YamlFileLoader(new FileLocator([__DIR__.'/../Fixtures/locale_and_host']));
419419
$routes = $loader->load('importer-without-host.yml');
420420

421-
$expectedRoutes = require(__DIR__.'/../Fixtures/locale_and_host/import-without-host-expected-collection.php');
421+
$expectedRoutes = require __DIR__.'/../Fixtures/locale_and_host/import-without-host-expected-collection.php';
422422

423423
$this->assertEquals($expectedRoutes('yml'), $routes);
424424
}
@@ -428,7 +428,7 @@ public function testImportingRoutesWithSingleHostInImporter()
428428
$loader = new YamlFileLoader(new FileLocator([__DIR__.'/../Fixtures/locale_and_host']));
429429
$routes = $loader->load('importer-with-single-host.yml');
430430

431-
$expectedRoutes = require(__DIR__.'/../Fixtures/locale_and_host/import-with-single-host-expected-collection.php');
431+
$expectedRoutes = require __DIR__.'/../Fixtures/locale_and_host/import-with-single-host-expected-collection.php';
432432

433433
$this->assertEquals($expectedRoutes('yml'), $routes);
434434
}

src/Symfony/Component/Routing/composer.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
"require": {
1919
"php": "^7.2.5",
2020
"symfony/deprecation-contracts": "^2.1",
21-
"symfony/polyfill-php73": "^1.9",
2221
"symfony/polyfill-php80": "^1.15"
2322
},
2423
"require-dev": {

0 commit comments

Comments
 (0)
0