Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 610a238

Browse files
committed
feature #22459 [HttpKernel] Fix deprecation of Extension::addClassesToCompile() / AddClassesToCachePass (nicolas-grekas)
This PR was merged into the 3.3-dev branch. Discussion ---------- [HttpKernel] Fix deprecation of Extension::addClassesToCompile() / AddClassesToCachePass | Q | A | ------------- | --- | Branch? | 3.3 | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | - | License | MIT | Doc PR | - As done in #20735 Commits ------- f4b5784 [HttpKernel] Fix deprecation of Extension::addClassesToCompile() / AddClassesToCachePass
2 parents 6c7bced + f4b5784 commit 610a238

File tree

7 files changed

+170
-147
lines changed

7 files changed

+170
-147
lines changed

UPGRADE-3.3.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,8 @@ HttpFoundation
255255
HttpKernel
256256
-----------
257257

258+
* The `Extension::addClassesToCompile()` method has been deprecated and will be removed in 4.0.
259+
258260
* The `Psr6CacheClearer::addPool()` method has been deprecated. Pass an array
259261
of pools indexed by name to the constructor instead.
260262

UPGRADE-4.0.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -366,6 +366,8 @@ HttpFoundation
366366
HttpKernel
367367
----------
368368

369+
* The `Extension::addClassesToCompile()` method has been removed.
370+
369371
* Possibility to pass non-scalar values as URI attributes to the ESI and SSI
370372
renderers has been removed. The inline fragment renderer should be used with
371373
non-scalar attributes.

src/Symfony/Component/HttpKernel/CHANGELOG.md

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,13 @@ CHANGELOG
44
3.3.0
55
-----
66

7-
* Added `kernel.project_dir` and `Kernel::getProjectDir()`
8-
* Deprecated `kernel.root_dir` and `Kernel::getRootDir()`
9-
* Deprecated `Kernel::getEnvParameters()`
10-
* Deprecated the special `SYMFONY__` environment variables
7+
* added `kernel.project_dir` and `Kernel::getProjectDir()`
8+
* deprecated `kernel.root_dir` and `Kernel::getRootDir()`
9+
* deprecated `Kernel::getEnvParameters()`
10+
* deprecated the special `SYMFONY__` environment variables
1111
* added the possibility to change the query string parameter used by `UriSigner`
1212
* deprecated `LazyLoadingFragmentHandler::addRendererService()`
13+
* deprecated `Extension::addClassesToCompile()`
1314

1415
3.2.0
1516
-----
Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\HttpKernel\DependencyInjection;
13+
14+
use Composer\Autoload\ClassLoader;
15+
use Symfony\Component\Debug\DebugClassLoader;
16+
use Symfony\Component\DependencyInjection\ContainerBuilder;
17+
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
18+
use Symfony\Component\HttpKernel\Kernel;
19+
20+
/**
21+
* Sets the classes to compile in the cache for the container.
22+
*
23+
* @author Fabien Potencier <[email protected]>
24+
*/
25+
class AddAnnotatedClassesToCachePass implements CompilerPassInterface
26+
{
27+
private $kernel;
28+
29+
public function __construct(Kernel $kernel)
30+
{
31+
$this->kernel = $kernel;
32+
}
33+
34+
/**
35+
* {@inheritdoc}
36+
*/
37+
public function process(ContainerBuilder $container)
38+
{
39+
$classes = array();
40+
$annotatedClasses = array();
41+
foreach ($container->getExtensions() as $extension) {
42+
if ($extension instanceof Extension) {
43+
if (PHP_VERSION_ID < 70000) {
44+
$classes = array_merge($classes, $extension->getClassesToCompile());
45+
}
46+
$annotatedClasses = array_merge($annotatedClasses, $extension->getAnnotatedClassesToCompile());
47+
}
48+
}
49+
50+
$existingClasses = $this->getClassesInComposerClassMaps();
51+
52+
if (PHP_VERSION_ID < 70000) {
53+
$classes = $container->getParameterBag()->resolveValue($classes);
54+
$this->kernel->setClassCache($this->expandClasses($classes, $existingClasses));
55+
}
56+
$annotatedClasses = $container->getParameterBag()->resolveValue($annotatedClasses);
57+
$this->kernel->setAnnotatedClassCache($this->expandClasses($annotatedClasses, $existingClasses));
58+
}
59+
60+
/**
61+
* Expands the given class patterns using a list of existing classes.
62+
*
63+
* @param array $patterns The class patterns to expand
64+
* @param array $classes The existing classes to match against the patterns
65+
*
66+
* @return array A list of classes derivated from the patterns
67+
*/
68+
private function expandClasses(array $patterns, array $classes)
69+
{
70+
$expanded = array();
71+
72+
// Explicit classes declared in the patterns are returned directly
73+
foreach ($patterns as $key => $pattern) {
74+
if (substr($pattern, -1) !== '\\' && false === strpos($pattern, '*')) {
75+
unset($patterns[$key]);
76+
$expanded[] = ltrim($pattern, '\\');
77+
}
78+
}
79+
80+
// Match patterns with the classes list
81+
$regexps = $this->patternsToRegexps($patterns);
82+
83+
foreach ($classes as $class) {
84+
$class = ltrim($class, '\\');
85+
86+
if ($this->matchAnyRegexps($class, $regexps)) {
87+
$expanded[] = $class;
88+
}
89+
}
90+
91+
return array_unique($expanded);
92+
}
93+
94+
private function getClassesInComposerClassMaps()
95+
{
96+
$classes = array();
97+
98+
foreach (spl_autoload_functions() as $function) {
99+
if (!is_array($function)) {
100+
continue;
101+
}
102+
103+
if ($function[0] instanceof DebugClassLoader) {
104+
$function = $function[0]->getClassLoader();
105+
}
106+
107+
if (is_array($function) && $function[0] instanceof ClassLoader) {
108+
$classes += array_filter($function[0]->getClassMap());
109+
}
110+
}
111+
112+
return array_keys($classes);
113+
}
114+
115+
private function patternsToRegexps($patterns)
116+
{
117+
$regexps = array();
118+
119+
foreach ($patterns as $pattern) {
120+
// Escape user input
121+
$regex = preg_quote(ltrim($pattern, '\\'));
122+
123+
// Wildcards * and **
124+
$regex = strtr($regex, array('\\*\\*' => '.*?', '\\*' => '[^\\\\]*?'));
125+
126+
// If this class does not end by a slash, anchor the end
127+
if (substr($regex, -1) !== '\\') {
128+
$regex .= '$';
129+
}
130+
131+
$regexps[] = '{^\\\\'.$regex.'}';
132+
}
133+
134+
return $regexps;
135+
}
136+
137+
private function matchAnyRegexps($class, $regexps)
138+
{
139+
$blacklisted = false !== strpos($class, 'Test');
140+
141+
foreach ($regexps as $regex) {
142+
if ($blacklisted && false === strpos($regex, 'Test')) {
143+
continue;
144+
}
145+
146+
if (preg_match($regex, '\\'.$class)) {
147+
return true;
148+
}
149+
}
150+
151+
return false;
152+
}
153+
}

src/Symfony/Component/HttpKernel/DependencyInjection/AddClassesToCachePass.php

Lines changed: 2 additions & 132 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,7 @@
1111

1212
namespace Symfony\Component\HttpKernel\DependencyInjection;
1313

14-
if (PHP_VERSION_ID >= 70000) {
15-
@trigger_error('The '.__NAMESPACE__.'\AddClassesToCachePass class is deprecated since version 3.3 and will be removed in 4.0.', E_USER_DEPRECATED);
16-
}
17-
18-
use Composer\Autoload\ClassLoader;
19-
use Symfony\Component\Debug\DebugClassLoader;
20-
use Symfony\Component\DependencyInjection\ContainerBuilder;
21-
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
22-
use Symfony\Component\HttpKernel\Kernel;
14+
@trigger_error('The '.__NAMESPACE__.'\AddClassesToCachePass class is deprecated since version 3.3 and will be removed in 4.0.', E_USER_DEPRECATED);
2315

2416
/**
2517
* Sets the classes to compile in the cache for the container.
@@ -28,128 +20,6 @@
2820
*
2921
* @deprecated since version 3.3, to be removed in 4.0.
3022
*/
31-
class AddClassesToCachePass implements CompilerPassInterface
23+
class AddClassesToCachePass extends AddAnnotatedClassesToCachePass
3224
{
33-
private $kernel;
34-
35-
public function __construct(Kernel $kernel)
36-
{
37-
$this->kernel = $kernel;
38-
}
39-
40-
/**
41-
* {@inheritdoc}
42-
*/
43-
public function process(ContainerBuilder $container)
44-
{
45-
$classes = array();
46-
$annotatedClasses = array();
47-
foreach ($container->getExtensions() as $extension) {
48-
if ($extension instanceof Extension) {
49-
$classes = array_merge($classes, $extension->getClassesToCompile());
50-
$annotatedClasses = array_merge($annotatedClasses, $extension->getAnnotatedClassesToCompile());
51-
}
52-
}
53-
54-
$classes = $container->getParameterBag()->resolveValue($classes);
55-
$annotatedClasses = $container->getParameterBag()->resolveValue($annotatedClasses);
56-
$existingClasses = $this->getClassesInComposerClassMaps();
57-
58-
$this->kernel->setClassCache($this->expandClasses($classes, $existingClasses));
59-
$this->kernel->setAnnotatedClassCache($this->expandClasses($annotatedClasses, $existingClasses));
60-
}
61-
62-
/**
63-
* Expands the given class patterns using a list of existing classes.
64-
*
65-
* @param array $patterns The class patterns to expand
66-
* @param array $classes The existing classes to match against the patterns
67-
*
68-
* @return array A list of classes derivated from the patterns
69-
*/
70-
private function expandClasses(array $patterns, array $classes)
71-
{
72-
$expanded = array();
73-
74-
// Explicit classes declared in the patterns are returned directly
75-
foreach ($patterns as $key => $pattern) {
76-
if (substr($pattern, -1) !== '\\' && false === strpos($pattern, '*')) {
77-
unset($patterns[$key]);
78-
$expanded[] = ltrim($pattern, '\\');
79-
}
80-
}
81-
82-
// Match patterns with the classes list
83-
$regexps = $this->patternsToRegexps($patterns);
84-
85-
foreach ($classes as $class) {
86-
$class = ltrim($class, '\\');
87-
88-
if ($this->matchAnyRegexps($class, $regexps)) {
89-
$expanded[] = $class;
90-
}
91-
}
92-
93-
return array_unique($expanded);
94-
}
95-
96-
private function getClassesInComposerClassMaps()
97-
{
98-
$classes = array();
99-
100-
foreach (spl_autoload_functions() as $function) {
101-
if (!is_array($function)) {
102-
continue;
103-
}
104-
105-
if ($function[0] instanceof DebugClassLoader) {
106-
$function = $function[0]->getClassLoader();
107-
}
108-
109-
if (is_array($function) && $function[0] instanceof ClassLoader) {
110-
$classes += array_filter($function[0]->getClassMap());
111-
}
112-
}
113-
114-
return array_keys($classes);
115-
}
116-
117-
private function patternsToRegexps($patterns)
118-
{
119-
$regexps = array();
120-
121-
foreach ($patterns as $pattern) {
122-
// Escape user input
123-
$regex = preg_quote(ltrim($pattern, '\\'));
124-
125-
// Wildcards * and **
126-
$regex = strtr($regex, array('\\*\\*' => '.*?', '\\*' => '[^\\\\]*?'));
127-
128-
// If this class does not end by a slash, anchor the end
129-
if (substr($regex, -1) !== '\\') {
130-
$regex .= '$';
131-
}
132-
133-
$regexps[] = '{^\\\\'.$regex.'}';
134-
}
135-
136-
return $regexps;
137-
}
138-
139-
private function matchAnyRegexps($class, $regexps)
140-
{
141-
$blacklisted = false !== strpos($class, 'Test');
142-
143-
foreach ($regexps as $regex) {
144-
if ($blacklisted && false === strpos($regex, 'Test')) {
145-
continue;
146-
}
147-
148-
if (preg_match($regex, '\\'.$class)) {
149-
return true;
150-
}
151-
}
152-
153-
return false;
154-
}
15525
}

src/Symfony/Component/HttpKernel/Kernel.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
use Symfony\Component\HttpKernel\Config\EnvParametersResource;
2929
use Symfony\Component\HttpKernel\Config\FileLocator;
3030
use Symfony\Component\HttpKernel\DependencyInjection\MergeExtensionConfigurationPass;
31-
use Symfony\Component\HttpKernel\DependencyInjection\AddClassesToCachePass;
31+
use Symfony\Component\HttpKernel\DependencyInjection\AddAnnotatedClassesToCachePass;
3232
use Symfony\Component\Config\Loader\GlobFileLoader;
3333
use Symfony\Component\Config\Loader\LoaderResolver;
3434
use Symfony\Component\Config\Loader\DelegatingLoader;
@@ -652,9 +652,7 @@ protected function buildContainer()
652652
$container->merge($cont);
653653
}
654654

655-
if (PHP_VERSION_ID < 70000) {
656-
$container->addCompilerPass(new AddClassesToCachePass($this));
657-
}
655+
$container->addCompilerPass(new AddAnnotatedClassesToCachePass($this));
658656
$container->addResource(new EnvParametersResource('SYMFONY__'));
659657

660658
return $container;

src/Symfony/Component/HttpKernel/Tests/DependencyInjection/AddClassesToCachePassTest.php renamed to src/Symfony/Component/HttpKernel/Tests/DependencyInjection/AddAnnotatedClassesToCachePassTest.php

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,15 @@
1212
namespace Symfony\Component\HttpKernel\Tests\DependencyInjection;
1313

1414
use PHPUnit\Framework\TestCase;
15-
use Symfony\Component\HttpKernel\DependencyInjection\AddClassesToCachePass;
15+
use Symfony\Component\HttpKernel\DependencyInjection\AddAnnotatedClassesToCachePass;
1616

17-
/**
18-
* @group legacy
19-
*/
20-
class AddClassesToCachePassTest extends TestCase
17+
class AddAnnotatedClassesToCachePassTest extends TestCase
2118
{
2219
public function testExpandClasses()
2320
{
24-
$r = new \ReflectionClass(AddClassesToCachePass::class);
21+
$r = new \ReflectionClass(AddAnnotatedClassesToCachePass::class);
2522
$pass = $r->newInstanceWithoutConstructor();
26-
$r = new \ReflectionMethod(AddClassesToCachePass::class, 'expandClasses');
23+
$r = new \ReflectionMethod(AddAnnotatedClassesToCachePass::class, 'expandClasses');
2724
$r->setAccessible(true);
2825
$expand = $r->getClosure($pass);
2926

0 commit comments

Comments
 (0)