Skip to content

Commit fb38467

Browse files
Merge branch '4.4'
* 4.4: Do not extend the new SF 4.3 ControllerEvent so we can make it final Backported return type violation bugfixes. fix deprecated call to setLocale with null [FrameworkBundle] Fix BrowserKit assertions to make them compatible with Panther [HttpKernel] deprecate global dir to load resources from
2 parents f608c75 + 2693b87 commit fb38467

File tree

7 files changed

+86
-26
lines changed

7 files changed

+86
-26
lines changed

CHANGELOG.md

+6
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,12 @@ CHANGELOG
2828

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

3238
4.3.0
3339
-----

Config/FileLocator.php

+42-9
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(string $file, string $currentPath = null, bool $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
}

EventListener/LocaleAwareListener.php

+4-2
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,9 @@ public function onKernelRequest(RequestEvent $event): void
4646
public function onKernelFinishRequest(FinishRequestEvent $event): void
4747
{
4848
if (null === $parentRequest = $this->requestStack->getParentRequest()) {
49-
$this->setLocale($event->getRequest()->getDefaultLocale());
49+
foreach ($this->localeAwareServices as $service) {
50+
$service->setLocale($event->getRequest()->getDefaultLocale());
51+
}
5052

5153
return;
5254
}
@@ -63,7 +65,7 @@ public static function getSubscribedEvents()
6365
];
6466
}
6567

66-
private function setLocale(string $locale, string $defaultLocale = null): void
68+
private function setLocale(string $locale, string $defaultLocale): void
6769
{
6870
foreach ($this->localeAwareServices as $service) {
6971
try {

Kernel.php

+16-3
Original file line numberDiff line numberDiff line change
@@ -226,11 +226,21 @@ public function getBundle(string $name)
226226

227227
/**
228228
* {@inheritdoc}
229-
*
230-
* @throws \RuntimeException if a custom resource is hidden by a resource in a derived bundle
231229
*/
232-
public function locateResource(string $name, string $dir = null, bool $first = true)
230+
public function locateResource(string $name/*, $dir = null, $first = true, $triggerDeprecation = true*/)
233231
{
232+
if (2 <= \func_num_args()) {
233+
$dir = func_get_arg(1);
234+
$first = 3 <= \func_num_args() ? func_get_arg(2) : true;
235+
236+
if (4 !== \func_num_args() || func_get_arg(3)) {
237+
@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);
238+
}
239+
} else {
240+
$dir = null;
241+
$first = true;
242+
}
243+
234244
if ('@' !== $name[0]) {
235245
throw new \InvalidArgumentException(sprintf('A resource name must start with @ ("%s" given).', $name));
236246
}
@@ -252,6 +262,9 @@ public function locateResource(string $name, string $dir = null, bool $first = t
252262

253263
if ($isResource && file_exists($file = $dir.'/'.$bundle->getName().$overridePath)) {
254264
$files[] = $file;
265+
266+
// see https://symfony.com/doc/current/bundles/override.html on how to overwrite parts of a bundle
267+
@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);
255268
}
256269

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

KernelInterface.php

+2-11
Original file line numberDiff line numberDiff line change
@@ -76,21 +76,12 @@ public function getBundle(string $name);
7676
* where BundleName is the name of the bundle
7777
* and the remaining part is the relative path in the bundle.
7878
*
79-
* If $dir is passed, and the first segment of the path is "Resources",
80-
* this method will look for a file named:
81-
*
82-
* $dir/<BundleName>/path/without/Resources
83-
*
84-
* before looking in the bundle resource folder.
85-
*
86-
* @param bool $first Whether to return the first path or paths for all matching bundles
87-
*
88-
* @return string|array The absolute path of the resource or an array if $first is false
79+
* @return string The absolute path of the resource
8980
*
9081
* @throws \InvalidArgumentException if the file cannot be found or the name is not valid
9182
* @throws \RuntimeException if the name contains invalid/unsafe characters
9283
*/
93-
public function locateResource(string $name, string $dir = null, bool $first = true);
84+
public function locateResource(string $name);
9485

9586
/**
9687
* Gets the environment.

Tests/Config/FileLocatorTest.php

+3
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();

Tests/KernelTest.php

+13-1
Original file line numberDiff line numberDiff line change
@@ -358,6 +358,9 @@ public function testLocateResourceReturnsTheFirstThatMatches()
358358
$this->assertEquals(__DIR__.'/Fixtures/Bundle1Bundle/foo.txt', $kernel->locateResource('@Bundle1Bundle/foo.txt'));
359359
}
360360

361+
/**
362+
* @group legacy
363+
*/
361364
public function testLocateResourceIgnoresDirOnNonResource()
362365
{
363366
$kernel = $this->getKernel(['getBundle']);
@@ -373,6 +376,9 @@ public function testLocateResourceIgnoresDirOnNonResource()
373376
);
374377
}
375378

379+
/**
380+
* @group legacy
381+
*/
376382
public function testLocateResourceReturnsTheDirOneForResources()
377383
{
378384
$kernel = $this->getKernel(['getBundle']);
@@ -388,7 +394,10 @@ public function testLocateResourceReturnsTheDirOneForResources()
388394
);
389395
}
390396

391-
public function testLocateResourceOnDirectories()
397+
/**
398+
* @group legacy
399+
*/
400+
public function testLocateResourceOnDirectoriesWithOverwrite()
392401
{
393402
$kernel = $this->getKernel(['getBundle']);
394403
$kernel
@@ -405,7 +414,10 @@ public function testLocateResourceOnDirectories()
405414
__DIR__.'/Fixtures/Resources/FooBundle',
406415
$kernel->locateResource('@FooBundle/Resources', __DIR__.'/Fixtures/Resources')
407416
);
417+
}
408418

419+
public function testLocateResourceOnDirectories()
420+
{
409421
$kernel = $this->getKernel(['getBundle']);
410422
$kernel
411423
->expects($this->exactly(2))

0 commit comments

Comments
 (0)