Skip to content

Commit 11cf77a

Browse files
committed
[ClassLoader] Fix ClassCollectionLoader inlining with __FILE__ and __DIR__
1 parent a00ca56 commit 11cf77a

File tree

5 files changed

+45
-5
lines changed

5 files changed

+45
-5
lines changed

src/Symfony/Component/ClassLoader/ClassCollectionLoader.php

+10-4
Original file line numberDiff line numberDiff line change
@@ -104,9 +104,15 @@ public static function load($classes, $cacheDir, $name, $autoReload, $adaptive =
104104
}
105105
}
106106

107-
$c = '(?:\s*+(?:(?:#|//)[^\n]*+\n|/\*(?:(?<!\*/).)++)?+)*+';
108-
$strictTypesRegex = str_replace('.', $c, "'^<\?php\s.declare.\(.strict_types.=.1.\).;'is");
109-
$haltCompilerRegex = str_replace('.', $c, "'\b__halt_compiler.\(.\)'is");
107+
$spacesRegex = '(?:\s*+(?:(?:\#|//)[^\n]*+\n|/\*(?:(?<!\*/).)++)?+)*+';
108+
$dontInlineRegex = <<<REGEX
109+
'(?:
110+
^<\?php\s.declare.\(.strict_types.=.1.\).;
111+
| \b__halt_compiler.\(.\)
112+
| \b__(?:DIR|FILE)__\b
113+
)'isx
114+
REGEX;
115+
$dontInlineRegex = str_replace('.', $spacesRegex, $dontInlineRegex);
110116

111117
$cacheDir = explode(DIRECTORY_SEPARATOR, $cacheDir);
112118
$files = array();
@@ -119,7 +125,7 @@ public static function load($classes, $cacheDir, $name, $autoReload, $adaptive =
119125
$files[] = $file = $class->getFileName();
120126
$c = file_get_contents($file);
121127

122-
if (preg_match($strictTypesRegex, $c) || preg_match($haltCompilerRegex, $c)) {
128+
if (preg_match($dontInlineRegex, $c)) {
123129
$file = explode(DIRECTORY_SEPARATOR, $file);
124130

125131
for ($i = 0; isset($file[$i], $cacheDir[$i]); ++$i) {

src/Symfony/Component/ClassLoader/Tests/ClassCollectionLoaderTest.php

+3-1
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,7 @@ public function testCommentStripping()
235235
$strictTypes = defined('HHVM_VERSION') ? '' : "\nnamespace {require __DIR__.'/Fixtures/Namespaced/WithStrictTypes.php';}";
236236

237237
ClassCollectionLoader::load(
238-
array('Namespaced\\WithComments', 'Pearlike_WithComments', 'Namespaced\\WithHaltCompiler', $strictTypes ? 'Namespaced\\WithStrictTypes' : 'Namespaced\\WithComments'),
238+
array('Namespaced\\WithComments', 'Pearlike_WithComments', 'Namespaced\\WithDirMagic', 'Namespaced\\WithFileMagic', 'Namespaced\\WithHaltCompiler', $strictTypes ? 'Namespaced\\WithStrictTypes' : 'Namespaced\\WithComments'),
239239
__DIR__,
240240
'bar',
241241
false
@@ -275,6 +275,8 @@ class Pearlike_WithComments
275275
public static $loaded = true;
276276
}
277277
}
278+
namespace {require __DIR__.'/Fixtures/Namespaced/WithDirMagic.php';}
279+
namespace {require __DIR__.'/Fixtures/Namespaced/WithFileMagic.php';}
278280
namespace {require __DIR__.'/Fixtures/Namespaced/WithHaltCompiler.php';}
279281
EOF
280282
.$strictTypes,

src/Symfony/Component/ClassLoader/Tests/ClassMapGeneratorTest.php

+2
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,8 @@ public function getTestCreateMapTests()
7878
'Namespaced\\WithComments' => realpath(__DIR__).'/Fixtures/Namespaced/WithComments.php',
7979
'Namespaced\\WithStrictTypes' => realpath(__DIR__).'/Fixtures/Namespaced/WithStrictTypes.php',
8080
'Namespaced\\WithHaltCompiler' => realpath(__DIR__).'/Fixtures/Namespaced/WithHaltCompiler.php',
81+
'Namespaced\\WithDirMagic' => realpath(__DIR__).'/Fixtures/Namespaced/WithDirMagic.php',
82+
'Namespaced\\WithFileMagic' => realpath(__DIR__).'/Fixtures/Namespaced/WithFileMagic.php',
8183
)),
8284
array(__DIR__.'/Fixtures/beta/NamespaceCollision', array(
8385
'NamespaceCollision\\A\\B\\Bar' => realpath(__DIR__).'/Fixtures/beta/NamespaceCollision/A/B/Bar.php',
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
/*
4+
* foo
5+
*/
6+
7+
namespace Namespaced;
8+
9+
class WithDirMagic
10+
{
11+
public function getDir()
12+
{
13+
return __DIR__;
14+
}
15+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
/*
4+
* foo
5+
*/
6+
7+
namespace Namespaced;
8+
9+
class WithFileMagic
10+
{
11+
public function getFile()
12+
{
13+
return __FILE__;
14+
}
15+
}

0 commit comments

Comments
 (0)