PageRenderTime 47ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/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
Possible License(s): ISC, AGPL-3.0, LGPL-2.1, BSD-3-Clause, LGPL-3.0
  1. <?php
  2. namespace Symfony\Component\ClassLoader;
  3. class ClassCollectionLoader
  4. {
  5. static private $loaded;
  6. static public function load($classes, $cacheDir, $name, $autoReload, $adaptive = false, $extension = '.php')
  7. {
  8. if (isset(self::$loaded[$name])) {
  9. return;
  10. }
  11. self::$loaded[$name] = true;
  12. if ($adaptive) {
  13. $classes = array_diff($classes, get_declared_classes(), get_declared_interfaces());
  14. $name = $name.'-'.substr(md5(implode('|', $classes)), 0, 5);
  15. }
  16. $cache = $cacheDir.'/'.$name.$extension;
  17. $reload = false;
  18. if ($autoReload) {
  19. $metadata = $cacheDir.'/'.$name.$extension.'.meta';
  20. if (!is_file($metadata) || !is_file($cache)) {
  21. $reload = true;
  22. } else {
  23. $time = filemtime($cache);
  24. $meta = unserialize(file_get_contents($metadata));
  25. if ($meta[1] != $classes) {
  26. $reload = true;
  27. } else {
  28. foreach ($meta[0] as $resource) {
  29. if (!is_file($resource) || filemtime($resource) > $time) {
  30. $reload = true;
  31. break;
  32. }
  33. }
  34. }
  35. }
  36. }
  37. if (!$reload && is_file($cache)) {
  38. require_once $cache;
  39. return;
  40. }
  41. $files = array();
  42. $content = '';
  43. foreach ($classes as $class) {
  44. if (!class_exists($class) && !interface_exists($class) && (!function_exists('trait_exists') || !trait_exists($class))) {
  45. throw new \InvalidArgumentException(sprintf('Unable to load class "%s"', $class));
  46. }
  47. $r = new \ReflectionClass($class);
  48. $files[] = $r->getFileName();
  49. $c = preg_replace(array('/^\s*<\?php/', '/\?>\s*$/'), '', file_get_contents($r->getFileName()));
  50. if (!$r->inNamespace()) {
  51. $c = "\nnamespace\n{\n".self::stripComments($c)."\n}\n";
  52. } else {
  53. $c = self::fixNamespaceDeclarations('<?php '.$c);
  54. $c = preg_replace('/^\s*<\?php/', '', $c);
  55. }
  56. $content .= $c;
  57. }
  58. if (!is_dir(dirname($cache))) {
  59. mkdir(dirname($cache), 0777, true);
  60. }
  61. self::writeCacheFile($cache, '<?php '.$content);
  62. if ($autoReload) {
  63. self::writeCacheFile($metadata, serialize(array($files, $classes)));
  64. }
  65. }
  66. static public function fixNamespaceDeclarations($source)
  67. {
  68. if (!function_exists('token_get_all')) {
  69. return $source;
  70. }
  71. $output = '';
  72. $inNamespace = false;
  73. $tokens = token_get_all($source);
  74. for ($i = 0, $max = count($tokens); $i < $max; $i++) {
  75. $token = $tokens[$i];
  76. if (is_string($token)) {
  77. $output .= $token;
  78. } elseif (in_array($token[0], array(T_COMMENT, T_DOC_COMMENT))) {
  79. continue;
  80. } elseif (T_NAMESPACE === $token[0]) {
  81. if ($inNamespace) {
  82. $output .= "}\n";
  83. }
  84. $output .= $token[1];
  85. while (($t = $tokens[++$i]) && is_array($t) && in_array($t[0], array(T_WHITESPACE, T_NS_SEPARATOR, T_STRING))) {
  86. $output .= $t[1];
  87. }
  88. if (is_string($t) && '{' === $t) {
  89. $inNamespace = false;
  90. --$i;
  91. } else {
  92. $output .= "\n{";
  93. $inNamespace = true;
  94. }
  95. } else {
  96. $output .= $token[1];
  97. }
  98. }
  99. if ($inNamespace) {
  100. $output .= "}\n";
  101. }
  102. return $output;
  103. }
  104. static private function writeCacheFile($file, $content)
  105. {
  106. $tmpFile = tempnam(dirname($file), basename($file));
  107. if (false !== @file_put_contents($tmpFile, $content) && @rename($tmpFile, $file)) {
  108. chmod($file, 0644);
  109. return;
  110. }
  111. throw new \RuntimeException(sprintf('Failed to write cache file "%s".', $file));
  112. }
  113. static private function stripComments($source)
  114. {
  115. if (!function_exists('token_get_all')) {
  116. return $source;
  117. }
  118. $output = '';
  119. foreach (token_get_all($source) as $token) {
  120. if (is_string($token)) {
  121. $output .= $token;
  122. } elseif (!in_array($token[0], array(T_COMMENT, T_DOC_COMMENT))) {
  123. $output .= $token[1];
  124. }
  125. }
  126. $output = preg_replace(array('/\s+$/Sm', '/\n+/S'), "\n", $output);
  127. return $output;
  128. }
  129. }