Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 2d8f649

Browse filesBrowse files
committed
[HttpKernel] deprecate global dir to load resources from
1 parent a9ace36 commit 2d8f649
Copy full SHA for 2d8f649

File tree

Expand file treeCollapse file tree

5 files changed

+72
-24
lines changed
Filter options
Expand file treeCollapse file tree

5 files changed

+72
-24
lines changed

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/HttpKernel/Config/FileLocator.php
+38-9Lines changed: 38 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,28 @@ 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]) && '/' !== $file[0] && '\\' !== $file[0]) {
64+
// no need to trigger deprecations when the loaded file is given as absolute path
65+
foreach ($this->paths as $deprecatedPath) {
66+
if (\is_array($locations)) {
67+
foreach ($locations as $location) {
68+
if (0 === \strpos($location, $deprecatedPath)) {
69+
@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);
70+
}
71+
}
72+
} else {
73+
if (0 === \strpos($locations, $deprecatedPath)) {
74+
@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);
75+
}
76+
}
77+
}
4978
}
5079

51-
return parent::locate($file, $currentPath, $first);
80+
return $locations;
5281
}
5382
}

‎src/Symfony/Component/HttpKernel/Kernel.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/HttpKernel/Kernel.php
+16-3Lines 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

Copy file name to clipboardExpand all lines: src/Symfony/Component/HttpKernel/KernelInterface.php
+2-11Lines changed: 2 additions & 11 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 bundle.
8282
*
83-
* If $dir is passed, and the first segment of the path is "Resources",
84-
* this method will look for a file named:
85-
*
86-
* $dir/<BundleName>/path/without/Resources
87-
*
88-
* before looking in the bundle resource folder.
89-
*
9083
* @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
9384
*
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

Copy file name to clipboardExpand all lines: src/Symfony/Component/HttpKernel/Tests/Config/FileLocatorTest.php
+3Lines 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

Copy file name to clipboardExpand all lines: src/Symfony/Component/HttpKernel/Tests/KernelTest.php
+13-1Lines 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)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.