-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[ClassLoader] Fix ClassCollectionLoader inlining with declare(strict_types=1) #19859
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -58,7 +58,12 @@ public static function load($classes, $cacheDir, $name, $autoReload, $adaptive = | |
|
||
$classes = array_unique($classes); | ||
|
||
$cache = $cacheDir.'/'.$name.$extension; | ||
// cache the core classes | ||
if (!is_dir($cacheDir) && !@mkdir($cacheDir, 0777, true) && !is_dir($cacheDir)) { | ||
throw new \RuntimeException(sprintf('Class Collection Loader was not able to create directory "%s"', $cacheDir)); | ||
} | ||
$cacheDir = rtrim(realpath($cacheDir), '/'.DIRECTORY_SEPARATOR); | ||
$cache = $cacheDir.DIRECTORY_SEPARATOR.$name.$extension; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This whole section does not look related to the fix. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It is: these are some lines moved from the bottom to the before the below loop. |
||
|
||
// auto-reload | ||
$reload = false; | ||
|
@@ -99,31 +104,50 @@ public static function load($classes, $cacheDir, $name, $autoReload, $adaptive = | |
} | ||
} | ||
|
||
$c = '(?:\s*+(?:(?:#|//)[^\n]*+\n|/\*(?:(?<!\*/).)++)?+)*+'; | ||
$strictTypesRegex = str_replace('.', $c, "'^<\?php\s.declare.\(.strict_types.=.1.\).;'is"); | ||
|
||
$cacheDir = explode(DIRECTORY_SEPARATOR, $cacheDir); | ||
$files = array(); | ||
$content = ''; | ||
foreach (self::getOrderedClasses($classes) as $class) { | ||
if (in_array($class->getName(), $declared)) { | ||
continue; | ||
} | ||
|
||
$files[] = $class->getFileName(); | ||
$files[] = $file = $class->getFileName(); | ||
$c = file_get_contents($file); | ||
|
||
$c = preg_replace(array('/^\s*<\?php/', '/\?>\s*$/'), '', file_get_contents($class->getFileName())); | ||
if (preg_match($strictTypesRegex, $c)) { | ||
$file = explode(DIRECTORY_SEPARATOR, $file); | ||
|
||
// fakes namespace declaration for global code | ||
if (!$class->inNamespace()) { | ||
$c = "\nnamespace\n{\n".$c."\n}\n"; | ||
} | ||
for ($i = 0; isset($file[$i], $cacheDir[$i]); ++$i) { | ||
if ($file[$i] !== $cacheDir[$i]) { | ||
break; | ||
} | ||
} | ||
if (1 >= $i) { | ||
$file = var_export(implode(DIRECTORY_SEPARATOR, $file), true); | ||
} else { | ||
$file = array_slice($file, $i); | ||
$file = str_repeat('..'.DIRECTORY_SEPARATOR, count($cacheDir) - $i).implode(DIRECTORY_SEPARATOR, $file); | ||
$file = '__DIR__.'.var_export(DIRECTORY_SEPARATOR.$file, true); | ||
} | ||
|
||
$c = self::fixNamespaceDeclarations('<?php '.$c); | ||
$c = preg_replace('/^\s*<\?php/', '', $c); | ||
$c = "\nnamespace {require $file;}"; | ||
} else { | ||
$c = preg_replace(array('/^\s*<\?php/', '/\?>\s*$/'), '', $c); | ||
|
||
$content .= $c; | ||
} | ||
// fakes namespace declaration for global code | ||
if (!$class->inNamespace()) { | ||
$c = "\nnamespace\n{\n".$c."\n}\n"; | ||
} | ||
|
||
// cache the core classes | ||
if (!is_dir($cacheDir) && !@mkdir($cacheDir, 0777, true) && !is_dir($cacheDir)) { | ||
throw new \RuntimeException(sprintf('Class Collection Loader was not able to create directory "%s"', $cacheDir)); | ||
$c = self::fixNamespaceDeclarations('<?php '.$c); | ||
$c = preg_replace('/^\s*<\?php/', '', $c); | ||
} | ||
|
||
$content .= $c; | ||
} | ||
self::writeCacheFile($cache, '<?php '.$content); | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<?php | ||
|
||
/* | ||
* foo | ||
*/ | ||
|
||
declare (strict_types = 1); | ||
|
||
namespace Namespaced; | ||
|
||
class WithStrictTypes | ||
{ | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
then why not throwing at the very beginning of the method instead?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Because it wouldn't work, look at the code before...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've looked at the code, I just didn't get it :)