namespace Symfony\Component\ClassLoader;
+if (\PHP_VERSION_ID >= 70000) {
+ @trigger_error('The '.__NAMESPACE__.'\ClassCollectionLoader class is deprecated since Symfony 3.3 and will be removed in 4.0.', E_USER_DEPRECATED);
+}
+
/**
* ClassCollectionLoader.
*
* @author Fabien Potencier <fabien@symfony.com>
+ *
+ * @deprecated since version 3.3, to be removed in 4.0.
*/
class ClassCollectionLoader
{
self::$loaded[$name] = true;
if ($adaptive) {
- $declared = array_merge(get_declared_classes(), get_declared_interfaces());
- if (function_exists('get_declared_traits')) {
- $declared = array_merge($declared, get_declared_traits());
- }
+ $declared = array_merge(get_declared_classes(), get_declared_interfaces(), get_declared_traits());
// don't include already declared classes
$classes = array_diff($classes, $declared);
// the cache is different depending on which classes are already declared
- $name = $name.'-'.substr(hash('sha256', implode('|', $classes)), 0, 5);
+ $name .= '-'.substr(hash('sha256', implode('|', $classes)), 0, 5);
}
$classes = array_unique($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) ?: $cacheDir, '/'.DIRECTORY_SEPARATOR);
+ $cacheDir = rtrim(realpath($cacheDir) ?: $cacheDir, '/'.\DIRECTORY_SEPARATOR);
$cache = $cacheDir.'/'.$name.$extension;
// auto-reload
return;
}
if (!$adaptive) {
- $declared = array_merge(get_declared_classes(), get_declared_interfaces());
- if (function_exists('get_declared_traits')) {
- $declared = array_merge($declared, get_declared_traits());
- }
+ $declared = array_merge(get_declared_classes(), get_declared_interfaces(), get_declared_traits());
+ }
+
+ $files = self::inline($classes, $cache, $declared);
+
+ 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|/\*(?:(?<!\*/).)++)?+)*+';
REGEX;
$dontInlineRegex = str_replace('.', $spacesRegex, $dontInlineRegex);
- $cacheDir = explode('/', str_replace(DIRECTORY_SEPARATOR, '/', $cacheDir));
+ $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($dontInlineRegex, $c)) {
- $file = explode('/', str_replace(DIRECTORY_SEPARATOR, '/', $file));
+ $file = explode('/', str_replace(\DIRECTORY_SEPARATOR, '/', $file));
for ($i = 0; isset($file[$i], $cacheDir[$i]); ++$i) {
if ($file[$i] !== $cacheDir[$i]) {
if (1 >= $i) {
$file = var_export(implode('/', $file), true);
} else {
- $file = array_slice($file, $i);
- $file = str_repeat('../', count($cacheDir) - $i).implode('/', $file);
+ $file = \array_slice($file, $i);
+ $file = str_repeat('../', \count($cacheDir) - $i).implode('/', $file);
$file = '__DIR__.'.var_export('/'.$file, true);
}
}
self::writeCacheFile($cache, '<?php '.$content);
- if ($autoReload) {
- // save the resources
- self::writeCacheFile($metadata, serialize(array($files, $classes)));
- }
+ return $files;
}
/**
*/
public static function fixNamespaceDeclarations($source)
{
- if (!function_exists('token_get_all') || !self::$useTokenizer) {
+ if (!\function_exists('token_get_all') || !self::$useTokenizer) {
if (preg_match('/(^|\s)namespace(.*?)\s*;/', $source)) {
$source = preg_replace('/(^|\s)namespace(.*?)\s*;/', "$1namespace$2\n{", $source)."}\n";
}
$token = $tokens[$i];
if (!isset($token[1]) || 'b"' === $token) {
$rawChunk .= $token;
- } elseif (in_array($token[0], array(T_COMMENT, T_DOC_COMMENT))) {
+ } elseif (\in_array($token[0], array(T_COMMENT, T_DOC_COMMENT))) {
// strip comments
continue;
} elseif (T_NAMESPACE === $token[0]) {
$rawChunk .= $token[1];
// namespace name and whitespaces
- while (isset($tokens[++$i][1]) && in_array($tokens[$i][0], array(T_WHITESPACE, T_NS_SEPARATOR, T_STRING))) {
+ while (isset($tokens[++$i][1]) && \in_array($tokens[$i][0], array(T_WHITESPACE, T_NS_SEPARATOR, T_STRING))) {
$rawChunk .= $tokens[$i][1];
}
if ('{' === $tokens[$i]) {
do {
$token = $tokens[++$i];
$output .= isset($token[1]) && 'b"' !== $token ? $token[1] : $token;
- } while ($token[0] !== T_END_HEREDOC);
+ } while (T_END_HEREDOC !== $token[0]);
$output .= "\n";
$rawChunk = '';
} elseif (T_CONSTANT_ENCAPSED_STRING === $token[0]) {
$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();
*/
private static function writeCacheFile($file, $content)
{
- $dir = dirname($file);
+ $dir = \dirname($file);
if (!is_writable($dir)) {
throw new \RuntimeException(sprintf('Cache directory "%s" is not writable.', $dir));
}
/**
* Gets an ordered array of passed classes including all their dependencies.
*
- * @param array $classes
- *
* @return \ReflectionClass[] An array of sorted \ReflectionClass instances (dependencies added if needed)
*
* @throws \InvalidArgumentException When a class can't be loaded
$traits = array();
- if (method_exists('ReflectionClass', 'getTraits')) {
- foreach ($classes as $c) {
- foreach (self::resolveDependencies(self::computeTraitDeps($c), $c) as $trait) {
- if ($trait !== $c) {
- $traits[] = $trait;
- }
+ foreach ($classes as $c) {
+ foreach (self::resolveDependencies(self::computeTraitDeps($c), $c) as $trait) {
+ if ($trait !== $c) {
+ $traits[] = $trait;
}
}
}