/www/silex/vendor/Symfony/Component/ClassLoader/ClassCollectionLoader.php
https://bitbucket.org/wayfarer/verse · PHP · 129 lines · 129 code · 0 blank · 0 comment · 37 complexity · 23e538f6b3dc17a52eb2b89570138090 MD5 · raw file
- <?php
- namespace Symfony\Component\ClassLoader;
- class ClassCollectionLoader
- {
- static private $loaded;
- static public function load($classes, $cacheDir, $name, $autoReload, $adaptive = false, $extension = '.php')
- {
- if (isset(self::$loaded[$name])) {
- return;
- }
- self::$loaded[$name] = true;
- if ($adaptive) {
- $classes = array_diff($classes, get_declared_classes(), get_declared_interfaces());
- $name = $name.'-'.substr(md5(implode('|', $classes)), 0, 5);
- }
- $cache = $cacheDir.'/'.$name.$extension;
- $reload = false;
- if ($autoReload) {
- $metadata = $cacheDir.'/'.$name.$extension.'.meta';
- if (!is_file($metadata) || !is_file($cache)) {
- $reload = true;
- } else {
- $time = filemtime($cache);
- $meta = unserialize(file_get_contents($metadata));
- if ($meta[1] != $classes) {
- $reload = true;
- } else {
- foreach ($meta[0] as $resource) {
- if (!is_file($resource) || filemtime($resource) > $time) {
- $reload = true;
- break;
- }
- }
- }
- }
- }
- if (!$reload && is_file($cache)) {
- require_once $cache;
- return;
- }
- $files = array();
- $content = '';
- foreach ($classes as $class) {
- if (!class_exists($class) && !interface_exists($class) && (!function_exists('trait_exists') || !trait_exists($class))) {
- throw new \InvalidArgumentException(sprintf('Unable to load class "%s"', $class));
- }
- $r = new \ReflectionClass($class);
- $files[] = $r->getFileName();
- $c = preg_replace(array('/^\s*<\?php/', '/\?>\s*$/'), '', file_get_contents($r->getFileName()));
- if (!$r->inNamespace()) {
- $c = "\nnamespace\n{\n".self::stripComments($c)."\n}\n";
- } else {
- $c = self::fixNamespaceDeclarations('<?php '.$c);
- $c = preg_replace('/^\s*<\?php/', '', $c);
- }
- $content .= $c;
- }
- if (!is_dir(dirname($cache))) {
- mkdir(dirname($cache), 0777, true);
- }
- self::writeCacheFile($cache, '<?php '.$content);
- if ($autoReload) {
- self::writeCacheFile($metadata, serialize(array($files, $classes)));
- }
- }
- static public function fixNamespaceDeclarations($source)
- {
- if (!function_exists('token_get_all')) {
- return $source;
- }
- $output = '';
- $inNamespace = false;
- $tokens = token_get_all($source);
- for ($i = 0, $max = count($tokens); $i < $max; $i++) {
- $token = $tokens[$i];
- if (is_string($token)) {
- $output .= $token;
- } elseif (in_array($token[0], array(T_COMMENT, T_DOC_COMMENT))) {
- continue;
- } elseif (T_NAMESPACE === $token[0]) {
- if ($inNamespace) {
- $output .= "}\n";
- }
- $output .= $token[1];
- while (($t = $tokens[++$i]) && is_array($t) && in_array($t[0], array(T_WHITESPACE, T_NS_SEPARATOR, T_STRING))) {
- $output .= $t[1];
- }
- if (is_string($t) && '{' === $t) {
- $inNamespace = false;
- --$i;
- } else {
- $output .= "\n{";
- $inNamespace = true;
- }
- } else {
- $output .= $token[1];
- }
- }
- if ($inNamespace) {
- $output .= "}\n";
- }
- return $output;
- }
- static private function writeCacheFile($file, $content)
- {
- $tmpFile = tempnam(dirname($file), basename($file));
- if (false !== @file_put_contents($tmpFile, $content) && @rename($tmpFile, $file)) {
- chmod($file, 0644);
- return;
- }
- throw new \RuntimeException(sprintf('Failed to write cache file "%s".', $file));
- }
- static private function stripComments($source)
- {
- if (!function_exists('token_get_all')) {
- return $source;
- }
- $output = '';
- foreach (token_get_all($source) as $token) {
- if (is_string($token)) {
- $output .= $token;
- } elseif (!in_array($token[0], array(T_COMMENT, T_DOC_COMMENT))) {
- $output .= $token[1];
- }
- }
- $output = preg_replace(array('/\s+$/Sm', '/\n+/S'), "\n", $output);
- return $output;
- }
- }