mirror of
https://github.com/symfony/class-loader.git
synced 2026-03-24 17:22:11 +01:00
Compare commits
41 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
e192d96b15 | ||
|
|
48b96f2fa9 | ||
|
|
8bfdaa2e7a | ||
|
|
fc4c04bfd1 | ||
|
|
6789dc86c9 | ||
|
|
fdb4806d2e | ||
|
|
2c8de07a8a | ||
|
|
b6e2bb88a8 | ||
|
|
c29a5bc6ca | ||
|
|
a6f009ccd5 | ||
|
|
64d2af707a | ||
|
|
2847d56f51 | ||
|
|
06c783d45f | ||
|
|
4f6bbee7b6 | ||
|
|
44816c55e9 | ||
|
|
0152f7a47a | ||
|
|
1c67601ae9 | ||
|
|
7c46951128 | ||
|
|
1108382429 | ||
|
|
f0ecf2161f | ||
|
|
96b69c7817 | ||
|
|
4332e482b5 | ||
|
|
2e19afbcc7 | ||
|
|
87cd4e6943 | ||
|
|
d31a6b4c5c | ||
|
|
f7fe9d14e1 | ||
|
|
65ac4dbf59 | ||
|
|
e0ca63b29a | ||
|
|
61de6c27f9 | ||
|
|
db9c33f62d | ||
|
|
c060834942 | ||
|
|
7747d6c305 | ||
|
|
ca881fd5b4 | ||
|
|
a4d05c1031 | ||
|
|
886fe1940b | ||
|
|
67ee799bed | ||
|
|
319b42119e | ||
|
|
0d794c4d29 | ||
|
|
2a7d07c88e | ||
|
|
a155e9640c | ||
|
|
5ba9dc365a |
@@ -59,8 +59,8 @@ class ClassCollectionLoader
|
||||
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;
|
||||
$cacheDir = rtrim(realpath($cacheDir) ?: $cacheDir, '/'.DIRECTORY_SEPARATOR);
|
||||
$cache = $cacheDir.'/'.$name.$extension;
|
||||
|
||||
// auto-reload
|
||||
$reload = false;
|
||||
@@ -98,22 +98,62 @@ class ClassCollectionLoader
|
||||
$declared = array_merge(get_declared_classes(), get_declared_interfaces(), get_declared_traits());
|
||||
}
|
||||
|
||||
$c = '(?:\s*+(?:(?:#|//)[^\n]*+\n|/\*(?:(?<!\*/).)++)?+)*+';
|
||||
$strictTypesRegex = str_replace('.', $c, "'^<\?php\s.declare.\(.strict_types.=.1.\).;'is");
|
||||
$files = self::inline($classes, $cache, $declared);
|
||||
|
||||
$cacheDir = explode(DIRECTORY_SEPARATOR, $cacheDir);
|
||||
if ($autoReload) {
|
||||
// save the resources
|
||||
self::writeCacheFile($metadata, serialize(array(array_values($files), $classes)));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Generates a file where classes and their parents are inlined.
|
||||
*
|
||||
* @param array $classes An array of classes to load
|
||||
* @param string $cache The file where classes are inlined
|
||||
* @param array $excluded An array of classes that won't be inlined
|
||||
*
|
||||
* @return array The source map of inlined classes, with classes as keys and files as values
|
||||
*
|
||||
* @throws \RuntimeException When class can't be loaded
|
||||
*/
|
||||
public static function inline($classes, $cache, array $excluded)
|
||||
{
|
||||
$declared = array();
|
||||
foreach (self::getOrderedClasses($excluded) as $class) {
|
||||
$declared[$class->getName()] = true;
|
||||
}
|
||||
|
||||
// cache the core classes
|
||||
$cacheDir = dirname($cache);
|
||||
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));
|
||||
}
|
||||
|
||||
$spacesRegex = '(?:\s*+(?:(?:\#|//)[^\n]*+\n|/\*(?:(?<!\*/).)++)?+)*+';
|
||||
$dontInlineRegex = <<<REGEX
|
||||
'(?:
|
||||
^<\?php\s.declare.\(.strict_types.=.1.\).;
|
||||
| \b__halt_compiler.\(.\)
|
||||
| \b__(?:DIR|FILE)__\b
|
||||
)'isx
|
||||
REGEX;
|
||||
$dontInlineRegex = str_replace('.', $spacesRegex, $dontInlineRegex);
|
||||
|
||||
$cacheDir = explode('/', str_replace(DIRECTORY_SEPARATOR, '/', $cacheDir));
|
||||
$files = array();
|
||||
$content = '';
|
||||
foreach (self::getOrderedClasses($classes) as $class) {
|
||||
if (in_array($class->getName(), $declared)) {
|
||||
if (isset($declared[$class->getName()])) {
|
||||
continue;
|
||||
}
|
||||
$declared[$class->getName()] = true;
|
||||
|
||||
$files[] = $file = $class->getFileName();
|
||||
$files[$class->getName()] = $file = $class->getFileName();
|
||||
$c = file_get_contents($file);
|
||||
|
||||
if (preg_match($strictTypesRegex, $c)) {
|
||||
$file = explode(DIRECTORY_SEPARATOR, $file);
|
||||
if (preg_match($dontInlineRegex, $c)) {
|
||||
$file = explode('/', str_replace(DIRECTORY_SEPARATOR, '/', $file));
|
||||
|
||||
for ($i = 0; isset($file[$i], $cacheDir[$i]); ++$i) {
|
||||
if ($file[$i] !== $cacheDir[$i]) {
|
||||
@@ -121,11 +161,11 @@ class ClassCollectionLoader
|
||||
}
|
||||
}
|
||||
if (1 >= $i) {
|
||||
$file = var_export(implode(DIRECTORY_SEPARATOR, $file), true);
|
||||
$file = var_export(implode('/', $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);
|
||||
$file = str_repeat('../', count($cacheDir) - $i).implode('/', $file);
|
||||
$file = '__DIR__.'.var_export('/'.$file, true);
|
||||
}
|
||||
|
||||
$c = "\nnamespace {require $file;}";
|
||||
@@ -145,10 +185,7 @@ class ClassCollectionLoader
|
||||
}
|
||||
self::writeCacheFile($cache, '<?php '.$content);
|
||||
|
||||
if ($autoReload) {
|
||||
// save the resources
|
||||
self::writeCacheFile($metadata, serialize(array($files, $classes)));
|
||||
}
|
||||
return $files;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -219,7 +256,7 @@ class ClassCollectionLoader
|
||||
|
||||
$output .= self::compressCode($rawChunk);
|
||||
|
||||
if (PHP_VERSION_ID >= 70000) {
|
||||
if (\PHP_VERSION_ID >= 70000) {
|
||||
// PHP 7 memory manager will not release after token_get_all(), see https://bugs.php.net/70098
|
||||
unset($tokens, $rawChunk);
|
||||
gc_mem_caches();
|
||||
@@ -262,7 +299,13 @@ class ClassCollectionLoader
|
||||
*/
|
||||
private static function writeCacheFile($file, $content)
|
||||
{
|
||||
$tmpFile = tempnam(dirname($file), basename($file));
|
||||
$dir = dirname($file);
|
||||
if (!is_writable($dir)) {
|
||||
throw new \RuntimeException(sprintf('Cache directory "%s" is not writable.', $dir));
|
||||
}
|
||||
|
||||
$tmpFile = tempnam($dir, basename($file));
|
||||
|
||||
if (false !== @file_put_contents($tmpFile, $content) && @rename($tmpFile, $file)) {
|
||||
@chmod($file, 0666 & ~umask());
|
||||
|
||||
|
||||
@@ -56,7 +56,7 @@ class ClassMapGenerator
|
||||
continue;
|
||||
}
|
||||
|
||||
$path = $file->getRealPath();
|
||||
$path = $file->getRealPath() ?: $file->getPathname();
|
||||
|
||||
if (pathinfo($path, PATHINFO_EXTENSION) !== 'php') {
|
||||
continue;
|
||||
@@ -64,7 +64,7 @@ class ClassMapGenerator
|
||||
|
||||
$classes = self::findClasses($path);
|
||||
|
||||
if (PHP_VERSION_ID >= 70000) {
|
||||
if (\PHP_VERSION_ID >= 70000) {
|
||||
// PHP 7 memory manager will not release after token_get_all(), see https://bugs.php.net/70098
|
||||
gc_mem_caches();
|
||||
}
|
||||
|
||||
2
LICENSE
2
LICENSE
@@ -1,4 +1,4 @@
|
||||
Copyright (c) 2004-2016 Fabien Potencier
|
||||
Copyright (c) 2004-2017 Fabien Potencier
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
|
||||
@@ -11,10 +11,11 @@
|
||||
|
||||
namespace Symfony\Component\ClassLoader\Tests;
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Symfony\Component\ClassLoader\ApcClassLoader;
|
||||
use Symfony\Component\ClassLoader\ClassLoader;
|
||||
|
||||
class ApcClassLoaderTest extends \PHPUnit_Framework_TestCase
|
||||
class ApcClassLoaderTest extends TestCase
|
||||
{
|
||||
protected function setUp()
|
||||
{
|
||||
|
||||
@@ -11,14 +11,17 @@
|
||||
|
||||
namespace Symfony\Component\ClassLoader\Tests;
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Symfony\Component\ClassLoader\ClassCollectionLoader;
|
||||
use Symfony\Component\ClassLoader\Tests\Fixtures\DeclaredClass;
|
||||
use Symfony\Component\ClassLoader\Tests\Fixtures\WarmedClass;
|
||||
|
||||
require_once __DIR__.'/Fixtures/ClassesWithParents/GInterface.php';
|
||||
require_once __DIR__.'/Fixtures/ClassesWithParents/CInterface.php';
|
||||
require_once __DIR__.'/Fixtures/ClassesWithParents/B.php';
|
||||
require_once __DIR__.'/Fixtures/ClassesWithParents/A.php';
|
||||
|
||||
class ClassCollectionLoaderTest extends \PHPUnit_Framework_TestCase
|
||||
class ClassCollectionLoaderTest extends TestCase
|
||||
{
|
||||
public function testTraitDependencies()
|
||||
{
|
||||
@@ -228,7 +231,7 @@ class ClassCollectionLoaderTest extends \PHPUnit_Framework_TestCase
|
||||
$strictTypes = defined('HHVM_VERSION') ? '' : "\nnamespace {require __DIR__.'/Fixtures/Namespaced/WithStrictTypes.php';}";
|
||||
|
||||
ClassCollectionLoader::load(
|
||||
array('Namespaced\\WithComments', 'Pearlike_WithComments', $strictTypes ? 'Namespaced\\WithStrictTypes' : 'Namespaced\\WithComments'),
|
||||
array('Namespaced\\WithComments', 'Pearlike_WithComments', 'Namespaced\\WithDirMagic', 'Namespaced\\WithFileMagic', 'Namespaced\\WithHaltCompiler', $strictTypes ? 'Namespaced\\WithStrictTypes' : 'Namespaced\\WithComments'),
|
||||
__DIR__,
|
||||
'bar',
|
||||
false
|
||||
@@ -268,6 +271,9 @@ class Pearlike_WithComments
|
||||
public static $loaded = true;
|
||||
}
|
||||
}
|
||||
namespace {require __DIR__.'/Fixtures/Namespaced/WithDirMagic.php';}
|
||||
namespace {require __DIR__.'/Fixtures/Namespaced/WithFileMagic.php';}
|
||||
namespace {require __DIR__.'/Fixtures/Namespaced/WithHaltCompiler.php';}
|
||||
EOF
|
||||
.$strictTypes,
|
||||
str_replace(array("<?php \n", '\\\\'), array('', '/'), file_get_contents($file))
|
||||
@@ -275,4 +281,36 @@ EOF
|
||||
|
||||
unlink($file);
|
||||
}
|
||||
|
||||
public function testInline()
|
||||
{
|
||||
$this->assertTrue(class_exists(WarmedClass::class, true));
|
||||
|
||||
@unlink($cache = sys_get_temp_dir().'/inline.php');
|
||||
|
||||
$classes = array(WarmedClass::class);
|
||||
$excluded = array(DeclaredClass::class);
|
||||
|
||||
ClassCollectionLoader::inline($classes, $cache, $excluded);
|
||||
|
||||
$this->assertSame(<<<'EOTXT'
|
||||
<?php
|
||||
namespace Symfony\Component\ClassLoader\Tests\Fixtures
|
||||
{
|
||||
interface WarmedInterface
|
||||
{
|
||||
}
|
||||
}
|
||||
namespace Symfony\Component\ClassLoader\Tests\Fixtures
|
||||
{
|
||||
class WarmedClass extends DeclaredClass implements WarmedInterface
|
||||
{
|
||||
}
|
||||
}
|
||||
EOTXT
|
||||
, file_get_contents($cache)
|
||||
);
|
||||
|
||||
unlink($cache);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -11,9 +11,10 @@
|
||||
|
||||
namespace Symfony\Component\ClassLoader\Tests;
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Symfony\Component\ClassLoader\ClassLoader;
|
||||
|
||||
class ClassLoaderTest extends \PHPUnit_Framework_TestCase
|
||||
class ClassLoaderTest extends TestCase
|
||||
{
|
||||
public function testGetPrefixes()
|
||||
{
|
||||
|
||||
@@ -11,9 +11,10 @@
|
||||
|
||||
namespace Symfony\Component\ClassLoader\Tests;
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Symfony\Component\ClassLoader\ClassMapGenerator;
|
||||
|
||||
class ClassMapGeneratorTest extends \PHPUnit_Framework_TestCase
|
||||
class ClassMapGeneratorTest extends TestCase
|
||||
{
|
||||
/**
|
||||
* @var string|null
|
||||
@@ -76,9 +77,11 @@ class ClassMapGeneratorTest extends \PHPUnit_Framework_TestCase
|
||||
'Namespaced\\Foo' => realpath(__DIR__).'/Fixtures/Namespaced/Foo.php',
|
||||
'Namespaced\\Baz' => realpath(__DIR__).'/Fixtures/Namespaced/Baz.php',
|
||||
'Namespaced\\WithComments' => realpath(__DIR__).'/Fixtures/Namespaced/WithComments.php',
|
||||
'Namespaced\WithStrictTypes' => realpath(__DIR__).'/Fixtures/Namespaced/WithStrictTypes.php',
|
||||
),
|
||||
),
|
||||
'Namespaced\\WithStrictTypes' => realpath(__DIR__).'/Fixtures/Namespaced/WithStrictTypes.php',
|
||||
'Namespaced\\WithHaltCompiler' => realpath(__DIR__).'/Fixtures/Namespaced/WithHaltCompiler.php',
|
||||
'Namespaced\\WithDirMagic' => realpath(__DIR__).'/Fixtures/Namespaced/WithDirMagic.php',
|
||||
'Namespaced\\WithFileMagic' => realpath(__DIR__).'/Fixtures/Namespaced/WithFileMagic.php',
|
||||
)),
|
||||
array(__DIR__.'/Fixtures/beta/NamespaceCollision', array(
|
||||
'NamespaceCollision\\A\\B\\Bar' => realpath(__DIR__).'/Fixtures/beta/NamespaceCollision/A/B/Bar.php',
|
||||
'NamespaceCollision\\A\\B\\Foo' => realpath(__DIR__).'/Fixtures/beta/NamespaceCollision/A/B/Foo.php',
|
||||
|
||||
7
Tests/Fixtures/DeclaredClass.php
Normal file
7
Tests/Fixtures/DeclaredClass.php
Normal file
@@ -0,0 +1,7 @@
|
||||
<?php
|
||||
|
||||
namespace Symfony\Component\ClassLoader\Tests\Fixtures;
|
||||
|
||||
class DeclaredClass implements DeclaredInterface
|
||||
{
|
||||
}
|
||||
7
Tests/Fixtures/DeclaredInterface.php
Normal file
7
Tests/Fixtures/DeclaredInterface.php
Normal file
@@ -0,0 +1,7 @@
|
||||
<?php
|
||||
|
||||
namespace Symfony\Component\ClassLoader\Tests\Fixtures;
|
||||
|
||||
interface DeclaredInterface
|
||||
{
|
||||
}
|
||||
15
Tests/Fixtures/Namespaced/WithDirMagic.php
Normal file
15
Tests/Fixtures/Namespaced/WithDirMagic.php
Normal file
@@ -0,0 +1,15 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* foo
|
||||
*/
|
||||
|
||||
namespace Namespaced;
|
||||
|
||||
class WithDirMagic
|
||||
{
|
||||
public function getDir()
|
||||
{
|
||||
return __DIR__;
|
||||
}
|
||||
}
|
||||
15
Tests/Fixtures/Namespaced/WithFileMagic.php
Normal file
15
Tests/Fixtures/Namespaced/WithFileMagic.php
Normal file
@@ -0,0 +1,15 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* foo
|
||||
*/
|
||||
|
||||
namespace Namespaced;
|
||||
|
||||
class WithFileMagic
|
||||
{
|
||||
public function getFile()
|
||||
{
|
||||
return __FILE__;
|
||||
}
|
||||
}
|
||||
18
Tests/Fixtures/Namespaced/WithHaltCompiler.php
Normal file
18
Tests/Fixtures/Namespaced/WithHaltCompiler.php
Normal file
@@ -0,0 +1,18 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* foo
|
||||
*/
|
||||
|
||||
namespace Namespaced;
|
||||
|
||||
class WithHaltCompiler
|
||||
{
|
||||
}
|
||||
|
||||
// the end of the script execution
|
||||
__halt_compiler(); data
|
||||
data
|
||||
data
|
||||
data
|
||||
...
|
||||
@@ -4,7 +4,7 @@
|
||||
* foo
|
||||
*/
|
||||
|
||||
declare (strict_types = 1);
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Namespaced;
|
||||
|
||||
|
||||
7
Tests/Fixtures/WarmedClass.php
Normal file
7
Tests/Fixtures/WarmedClass.php
Normal file
@@ -0,0 +1,7 @@
|
||||
<?php
|
||||
|
||||
namespace Symfony\Component\ClassLoader\Tests\Fixtures;
|
||||
|
||||
class WarmedClass extends DeclaredClass implements WarmedInterface
|
||||
{
|
||||
}
|
||||
7
Tests/Fixtures/WarmedInterface.php
Normal file
7
Tests/Fixtures/WarmedInterface.php
Normal file
@@ -0,0 +1,7 @@
|
||||
<?php
|
||||
|
||||
namespace Symfony\Component\ClassLoader\Tests\Fixtures;
|
||||
|
||||
interface WarmedInterface
|
||||
{
|
||||
}
|
||||
@@ -11,9 +11,10 @@
|
||||
|
||||
namespace Symfony\Component\ClassLoader\Tests;
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Symfony\Component\ClassLoader\Psr4ClassLoader;
|
||||
|
||||
class Psr4ClassLoaderTest extends \PHPUnit_Framework_TestCase
|
||||
class Psr4ClassLoaderTest extends TestCase
|
||||
{
|
||||
/**
|
||||
* @param string $className
|
||||
|
||||
@@ -34,7 +34,7 @@
|
||||
},
|
||||
"extra": {
|
||||
"branch-alias": {
|
||||
"dev-master": "3.1-dev"
|
||||
"dev-master": "3.2-dev"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,6 +5,8 @@
|
||||
backupGlobals="false"
|
||||
colors="true"
|
||||
bootstrap="vendor/autoload.php"
|
||||
failOnRisky="true"
|
||||
failOnWarning="true"
|
||||
>
|
||||
<php>
|
||||
<ini name="error_reporting" value="-1" />
|
||||
|
||||
Reference in New Issue
Block a user