8000 feature #33258 [HttpKernel] deprecate global dir to load resources fr… · symfony/symfony@f499083 · GitHub
[go: up one dir, main page]

Skip to content

Commit f499083

Browse files
feature #33258 [HttpKernel] deprecate global dir to load resources from (Tobion)
This PR was merged into the 4.4 branch. Discussion ---------- [HttpKernel] deprecate global dir to load resources from | Q | A | ------------- | --- | Branch? | 4.4 | Bug fix? | no | New feature? | no <!-- please update src/**/CHANGELOG.md files --> | BC breaks? | no <!-- see https://symfony.com/bc --> | Deprecations? | yes <!-- please update UPGRADE-*.md and src/**/CHANGELOG.md files --> | Tests pass? | yes <!-- please add some, will be required by reviewers --> | Fixed tickets | #31915 <!-- #-prefixed issue number(s), if any --> | License | MIT | Doc PR | Replaces #31958 Here two example deprecations by adding files in the deprecated locations: ``` Overwriting the resource "@AcmeBundle/Resources/config/routing.yaml" with "/vagrant/src/Resources/AcmeBundle/config/routing.yaml" is deprecated since Symfony 4.4 and will be removed in 5.0. Loading the file "foobar.yaml" from the global resource directory "/vagrant/src" is deprecated since Symfony 4.4 and will be removed in 5.0. ``` Commits ------- aa82566 [HttpKernel] deprecate global dir to load resources from
2 parents 2984ab7 + aa82566 commit f499083

File tree

9 files changed

+94
-25
lines changed

9 files changed

+94
-25
lines changed

UPGRADE-4.4.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,12 @@ HttpKernel
143143

144144
As many bundles must be compatible with a range of Symfony versions, the current
145145
directory convention is not deprecated yet, but it will be in the future.
146+
* Deprecated the second and third argument of `KernelInterface::locateResource`
147+
* Deprecated the second and third argument of `FileLocator::__construct`
148+
* Deprecated loading resources from `%kernel.root_dir%/Resources` and `%kernel.root_dir%` as
149+
fallback directories. Resources like service definitions are usually loaded relative to the
150+
current directory or with a glob pattern. The fallback directories have never been advocated
151+
so you likely do not use those in any app based on the SF Standard or Flex edition.
146152

147153
Lock
148154
----

UPGRADE-5.0.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -333,6 +333,10 @@ HttpKernel
333333

334334
As many bundles must be compatible with a range of Symfony versions, the current
335335
directory convention is not deprecated yet, but it will be in the future.
336+
* Removed the second and third argument of `KernelInterface::locateResource`
337+
* Removed the second and third argument of `FileLocator::__construct`
338+
* Removed loading resources from `%kernel.root_dir%/Resources` and `%kernel.root_dir%` as
339+
fallback directories.
336340

337341
Intl
338342
----

src/Symfony/Bundle/FrameworkBundle/Resources/config/services.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@
9191
<argument type="collection">
9292
<argument>%kernel.root_dir%</argument>
9393
</argument>
94+
<argument>false</argument>
9495
</service>
9596
<service id="Symfony\Component\HttpKernel\Config\FileLocator" alias="file_locator" />
9697

src/Symfony/Component/HttpKernel/CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,12 @@ CHANGELOG
66

77
* The `DebugHandlersListener` class has been marked as `final`
88
* Added new Bundle directory convention consistent with standard skeletons
9+
* Deprecated the second and third argument of `KernelInterface::locateResource`
10+
* Deprecated the second and third argument of `FileLocator::__construct`
11+
* Deprecated loading resources from `%kernel.root_dir%/Resources` and `%kernel.root_dir%` as
12+
fallback directories. Resources like service definitions are usually loaded relative to the
13+
current directory or with a glob pattern. The fallback directories have never been advocated
14+
so you likely do not use those in any app based on the SF Standard or Flex edition.
915

1016
4.3.0
1117
-----

src/Symfony/Component/HttpKernel/Config/FileLocator.php

Lines changed: 42 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,18 +22,28 @@
2222
class FileLocator extends BaseFileLocator
2323
{
2424
private $kernel;
25-
private $path;
2625

2726
/**
28-
* @param string|null $path The path the global resource directory
29-
* @param array $paths An array of paths where to look for resources
27+
* @deprecated since Symfony 4.4
3028
*/
31-
public function __construct(KernelInterface $kernel, string $path = null, array $paths = [])
29+
private $path;
30+
31+
public function __construct(KernelInterface $kernel/*, string $path = null, array $paths = [], bool $triggerDeprecation = true*/)
3232
{
3333
$this->kernel = $kernel;
34-
if (null !== $path) {
35-
$this->path = $path;
36-
$paths[] = $path;
34+
35+
if (2 <= \func_num_args()) {
36+
$this->path = func_get_arg(1);
37+
$paths = 3 <= \func_num_args() ? func_get_arg(2) : [];
38+
if (null !== $this->path) {
39+
$paths[] = $this->path;
40+
}
41+
42+
if (4 !== \func_num_args() || func_get_arg(3)) {
43+
@trigger_error(sprintf('Passing more than one argument to %s is deprecated since Symfony 4.4 and will be removed in 5.0.', __METHOD__), E_USER_DEPRECATED);
44+
}
45+
} else {
46+
$paths = [];
3747
}
3848

3949
parent::__construct($paths);
@@ -45,9 +55,32 @@ public function __construct(KernelInterface $kernel, string $path = null, array
4555
public function locate($file, $currentPath = null, $first = true)
4656
{
4757
if (isset($file[0]) && '@' === $file[0]) {
48-
return $this->kernel->locateResource($file, $this->path, $first);
58+
return $this->kernel->locateResource($file, $this->path, $first, false);
59+
}
60+
61+
$locations = parent::locate($file, $currentPath, $first);
62+
63+
if (isset($file[0]) && !(
64+
'/' === $file[0] || '\\' === $file[0]
65+
|| (\strlen($file) > 3 && ctype_alpha($file[0]) && ':' === $file[1] && ('\\' === $file[2] || '/' === $file[2]))
66+
|| null !== parse_url($file, PHP_URL_SCHEME)
67+
)) {
68+
// no need to trigger deprecations when the loaded file is given as absolute path
69+
foreach ($this->paths as $deprecatedPath) {
70+
if (\is_array($locations)) {
71+
foreach ($locations as $location) {
72+
if (0 === strpos($location, $deprecatedPath) && (null === $currentPath || false === strpos($location, $currentPath))) {
73+
@trigger_error(sprintf('Loading the file "%s" from the global resource directory "%s" is deprecated since Symfony 4.4 and will be removed in 5.0.', $file, $deprecatedPath), E_USER_DEPRECATED);
74+
}
75+
}
76+
} else {
77+
if (0 === strpos($locations, $deprecatedPath) && (null === $currentPath || false === strpos($locations, $currentPath))) {
78+
@trigger_error(sprintf('Loading the file "%s" from the global resource directory "%s" is deprecated since Symfony 4.4 and will be removed in 5.0.', $file, $deprecatedPath), E_USER_DEPRECATED);
79+
}
80+
}
81+
}
4982
}
5083

51-
return parent::locate($file, $currentPath, $first);
84+
return $locations;
5285
}
5386
}

src/Symfony/Component/HttpKernel/Kernel.php

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -236,11 +236,21 @@ public function getBundle($name)
236236

237237
/**
238238
* {@inheritdoc}
239-
*
240-
* @throws \RuntimeException if a custom resource is hidden by a resource in a derived bundle
241239
*/
242-
public function locateResource($name, $dir = null, $first = true)
240+
public function locateResource($name/*, $dir = null, $first = true, $triggerDeprecation = true*/)
243241
{
242+
if (2 <= \func_num_args()) {
243+
$dir = func_get_arg(1);
244+
$first = 3 <= \func_num_args() ? func_get_arg(2) : true;
245+
246+
if (4 !== \func_num_args() || func_get_arg(3)) {
247+
@trigger_error(sprintf('Passing more than one argument to %s is deprecated since Symfony 4.4 and will be removed in 5.0.', __METHOD__), E_USER_DEPRECATED);
248+
}
249+
} else {
250+
$dir = null;
251+
$first = true;
252+
}
253+
244254
if ('@' !== $name[0]) {
245255
throw new \InvalidArgumentException(sprintf('A resource name must start with @ ("%s" given).', $name));
246256
}
@@ -262,6 +272,9 @@ public function locateResource($name, $dir = null, $first = true)
262272

263273
if ($isResource && file_exists($file = $dir.'/'.$bundle->getName().$overridePath)) {
264274
$files[] = $file;
275+
276+
// see https://symfony.com/doc/current/bundles/override.html on how to overwrite parts of a bundle
277+
@trigger_error(sprintf('Overwriting the resource "%s" with "%s" is deprecated since Symfony 4.4 and will be removed in 5.0.', $name, $file), E_USER_DEPRECATED);
265278
}
266279

267280
if (file_exists($file = $bundle->getPath().'/'.$path)) {

src/Symfony/Component/HttpKernel/KernelInterface.php

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -80,23 +80,14 @@ public function getBundle($name);
8080
* where BundleName is the name of the bundle
8181
* and the remaining part is the relative path in the bun 1C6A dle.
8282
*
83-
* If $dir is passed, and the first segment of the path is "Resources",
84-
* this method will look for a file named:
83+
* @param string $name A resource name to locate
8584
*
86-
* $dir/<BundleName>/path/without/Resources
87-
*
88-
* before looking in the bundle resource folder.
89-
*
90-
* @param string $name A resource name to locate
91-
* @param string $dir A directory where to look for the resource first
92-
* @param bool $first Whether to return the first path or paths for all matching bundles
93-
*
94-
* @return string|array The absolute path of the resource or an array if $first is false
85+
* @return string|array The absolute path of the resource or an array if $first is false (array return value is deprecated)
9586
*
9687
* @throws \InvalidArgumentException if the file cannot be found or the name is not valid
9788
* @throws \RuntimeException if the name contains invalid/unsafe characters
9889
*/
99-
public function locateResource($name, $dir = null, $first = true);
90+
public function locateResource($name/*, $dir = null, $first = true*/);
10091

10192
/**
10293
* Gets the name of the kernel.

src/Symfony/Component/HttpKernel/Tests/Config/FileLocatorTest.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@ public function testLocate()
3434
$locator->locate('/some/path');
3535
}
3636

37+
/**
38+
* @group legacy
39+
*/
3740
public function testLocateWithGlobalResourcePath()
3841
{
3942
$kernel = $this->getMockBuilder('Symfony\Component\HttpKernel\KernelInterface')->getMock();

src/Symfony/Component/HttpKernel/Tests/KernelTest.php

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -389,6 +389,9 @@ public function testLocateResourceReturnsTheFirstThatMatches()
389389
$this->assertEquals(__DIR__.'/Fixtures/Bundle1Bundle/foo.txt', $kernel->locateResource('@Bundle1Bundle/foo.txt'));
390390
}
391391

392+
/**
393+
* @group legacy
394+
*/
392395
public function testLocateResourceIgnoresDirOnNonResource()
393396
{
394397
$kernel = $this->getKernel(['getBundle']);
@@ -404,6 +407,9 @@ public function testLocateResourceIgnoresDirOnNonResource()
404407
);
405408
}
406409

410+
/**
411+
* @group legacy
412+
*/
407413
public function testLocateResourceReturnsTheDirOneForResources()
408414
{
409415
$kernel = $this->getKernel(['getBundle']);
@@ -419,7 +425,10 @@ public function testLocateResourceReturnsTheDirOneForResources()
419425
);
420426
}
421427

422-
public function testLocateResourceOnDirectories()
428+
/**
429+
* @group legacy
430+
*/
431+
public function testLocateResourceOnDirectoriesWithOverwrite()
423432
{
424433
$kernel = $this->getKernel(['getBundle']);
425434
$kernel
@@ -436,7 +445,10 @@ public function testLocateResourceOnDirectories()
436445
__DIR__.'/Fixtures/Resources/FooBundle',
437446
$kernel->locateResource('@FooBundle/Resources', __DIR__.'/Fixtures/Resources')
438447
);
448+
}
439449

450+
public function testLocateResourceOnDirectories()
451+
{
440452
$kernel = $this->getKernel(['getBundle']);
441453
$kernel
442454
->expects($this->exactly(2))

0 commit comments

Comments
 (0)
0