2010-02-17 13:54:36 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/*
|
2011-01-15 13:29:43 +00:00
|
|
|
* This file is part of the Symfony package.
|
2010-02-17 13:54:36 +00:00
|
|
|
*
|
|
|
|
* (c) Fabien Potencier <fabien.potencier@symfony-project.com>
|
|
|
|
*
|
2011-01-15 13:29:43 +00:00
|
|
|
* For the full copyright and license information, please view the LICENSE
|
|
|
|
* file that was distributed with this source code.
|
2010-02-17 13:54:36 +00:00
|
|
|
*/
|
|
|
|
|
2011-01-15 13:29:43 +00:00
|
|
|
namespace Symfony\Component\HttpKernel;
|
|
|
|
|
2010-02-17 13:54:36 +00:00
|
|
|
/**
|
2010-04-07 01:51:29 +01:00
|
|
|
* ClassCollectionLoader.
|
2010-02-17 13:54:36 +00:00
|
|
|
*
|
2010-10-17 12:45:15 +01:00
|
|
|
* @author Fabien Potencier <fabien.potencier@symfony-project.com>
|
2010-02-17 13:54:36 +00:00
|
|
|
*/
|
|
|
|
class ClassCollectionLoader
|
|
|
|
{
|
2010-08-11 18:35:07 +01:00
|
|
|
static protected $loaded;
|
|
|
|
|
2010-05-06 12:25:53 +01:00
|
|
|
/**
|
2010-08-18 12:43:32 +01:00
|
|
|
* Loads a list of classes and caches them in one big file.
|
|
|
|
*
|
|
|
|
* @param array $classes An array of classes to load
|
|
|
|
* @param string $cacheDir A cache directory
|
|
|
|
* @param string $name The cache name prefix
|
|
|
|
* @param Boolean $autoReload Whether to flush the cache when the cache is stale or not
|
|
|
|
* @param Boolean $adaptive Whether to remove already declared classes or not
|
|
|
|
*
|
2010-05-06 12:25:53 +01:00
|
|
|
* @throws \InvalidArgumentException When class can't be loaded
|
|
|
|
*/
|
2010-08-18 12:43:32 +01:00
|
|
|
static public function load($classes, $cacheDir, $name, $autoReload, $adaptive = false)
|
2010-02-17 13:54:36 +00:00
|
|
|
{
|
2010-08-11 18:35:07 +01:00
|
|
|
// each $name can only be loaded once per PHP process
|
|
|
|
if (isset(self::$loaded[$name])) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2010-08-20 12:12:06 +01:00
|
|
|
self::$loaded[$name] = true;
|
|
|
|
|
2010-08-10 20:44:04 +01:00
|
|
|
$classes = array_unique($classes);
|
|
|
|
|
2010-05-06 12:25:53 +01:00
|
|
|
$cache = $cacheDir.'/'.$name.'.php';
|
|
|
|
|
|
|
|
// auto-reload
|
|
|
|
$reload = false;
|
2010-05-07 15:09:11 +01:00
|
|
|
if ($autoReload) {
|
2011-01-25 08:33:07 +00:00
|
|
|
if ($adaptive) {
|
|
|
|
// don't include already declared classes
|
|
|
|
$classes = array_diff($classes, get_declared_classes(), get_declared_interfaces());
|
|
|
|
}
|
|
|
|
|
2010-05-06 12:25:53 +01:00
|
|
|
$metadata = $cacheDir.'/'.$name.'.meta';
|
2010-05-07 15:09:11 +01:00
|
|
|
if (!file_exists($metadata) || !file_exists($cache)) {
|
2010-05-06 12:25:53 +01:00
|
|
|
$reload = true;
|
2010-05-07 15:09:11 +01:00
|
|
|
} else {
|
2010-05-06 12:25:53 +01:00
|
|
|
$time = filemtime($cache);
|
|
|
|
$meta = unserialize(file_get_contents($metadata));
|
|
|
|
|
2010-05-07 15:09:11 +01:00
|
|
|
if ($meta[1] != $classes) {
|
2010-05-06 12:25:53 +01:00
|
|
|
$reload = true;
|
2010-05-07 15:09:11 +01:00
|
|
|
} else {
|
2010-05-08 14:32:30 +01:00
|
|
|
foreach ($meta[0] as $resource) {
|
2010-05-07 15:09:11 +01:00
|
|
|
if (!file_exists($resource) || filemtime($resource) > $time) {
|
2010-05-06 12:25:53 +01:00
|
|
|
$reload = true;
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2010-02-17 13:54:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-05-07 15:09:11 +01:00
|
|
|
if (!$reload && file_exists($cache)) {
|
2010-05-06 12:25:53 +01:00
|
|
|
require_once $cache;
|
2010-02-17 13:54:36 +00:00
|
|
|
|
2010-05-06 12:25:53 +01:00
|
|
|
return;
|
|
|
|
}
|
2010-02-17 13:54:36 +00:00
|
|
|
|
2011-01-25 08:33:07 +00:00
|
|
|
if ($adaptive) {
|
|
|
|
// don't include already declared classes
|
|
|
|
$classes = array_diff($classes, get_declared_classes(), get_declared_interfaces());
|
|
|
|
}
|
|
|
|
|
2010-05-06 12:25:53 +01:00
|
|
|
$files = array();
|
|
|
|
$content = '';
|
2010-05-07 15:09:11 +01:00
|
|
|
foreach ($classes as $class) {
|
2010-05-08 14:32:30 +01:00
|
|
|
if (!class_exists($class) && !interface_exists($class)) {
|
2010-05-06 12:25:53 +01:00
|
|
|
throw new \InvalidArgumentException(sprintf('Unable to load class "%s"', $class));
|
|
|
|
}
|
2010-02-17 13:54:36 +00:00
|
|
|
|
2010-05-06 12:25:53 +01:00
|
|
|
$r = new \ReflectionClass($class);
|
|
|
|
$files[] = $r->getFileName();
|
2010-02-17 13:54:36 +00:00
|
|
|
|
2011-01-16 09:16:20 +00:00
|
|
|
$c = preg_replace(array('/^\s*<\?php/', '/\?>\s*$/'), '', file_get_contents($r->getFileName()));
|
|
|
|
|
|
|
|
// add namespace declaration for global code
|
|
|
|
if (!$r->inNamespace()) {
|
|
|
|
$c = "\nnamespace\n{\n$c\n}\n";
|
|
|
|
} else {
|
|
|
|
$c = self::fixNamespaceDeclarations('<?php '.$c);
|
|
|
|
$c = preg_replace('/^\s*<\?php/', '', $c);
|
|
|
|
}
|
|
|
|
|
|
|
|
$content .= $c;
|
2010-05-06 12:25:53 +01:00
|
|
|
}
|
2010-02-17 13:54:36 +00:00
|
|
|
|
2010-05-06 12:25:53 +01:00
|
|
|
// cache the core classes
|
2010-05-07 15:09:11 +01:00
|
|
|
if (!is_dir(dirname($cache))) {
|
2010-05-06 12:25:53 +01:00
|
|
|
mkdir(dirname($cache), 0777, true);
|
|
|
|
}
|
|
|
|
self::writeCacheFile($cache, Kernel::stripComments('<?php '.$content));
|
2010-02-17 13:54:36 +00:00
|
|
|
|
2010-05-07 15:09:11 +01:00
|
|
|
if ($autoReload) {
|
2010-05-06 12:25:53 +01:00
|
|
|
// save the resources
|
|
|
|
self::writeCacheFile($metadata, serialize(array($files, $classes)));
|
|
|
|
}
|
2010-02-17 13:54:36 +00:00
|
|
|
}
|
|
|
|
|
2011-01-16 09:16:20 +00:00
|
|
|
/**
|
|
|
|
* Adds brackets around each namespace if it's not already the case.
|
|
|
|
*/
|
|
|
|
static public function fixNamespaceDeclarations($source)
|
|
|
|
{
|
|
|
|
if (!function_exists('token_get_all')) {
|
|
|
|
return $source;
|
|
|
|
}
|
|
|
|
|
|
|
|
$output = '';
|
|
|
|
$inNamespace = false;
|
|
|
|
$tokens = token_get_all($source);
|
|
|
|
|
|
|
|
while ($token = array_shift($tokens)) {
|
|
|
|
if (is_string($token)) {
|
|
|
|
$output .= $token;
|
|
|
|
} elseif (T_NAMESPACE === $token[0]) {
|
|
|
|
if ($inNamespace) {
|
|
|
|
$output .= "}\n";
|
|
|
|
}
|
|
|
|
$output .= $token[1];
|
|
|
|
|
|
|
|
// namespace name and whitespaces
|
|
|
|
while (($t = array_shift($tokens)) && 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;
|
|
|
|
array_unshift($tokens, $t);
|
|
|
|
} else {
|
|
|
|
$output .= "\n{";
|
|
|
|
$inNamespace = true;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$output .= $token[1];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($inNamespace) {
|
|
|
|
$output .= "}\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
return $output;
|
|
|
|
}
|
|
|
|
|
2010-05-06 12:25:53 +01:00
|
|
|
static protected function writeCacheFile($file, $content)
|
2010-02-17 13:54:36 +00:00
|
|
|
{
|
2010-05-06 12:25:53 +01:00
|
|
|
$tmpFile = tempnam(dirname($file), basename($file));
|
2010-08-12 20:58:19 +01:00
|
|
|
if (false !== @file_put_contents($tmpFile, $content) && @rename($tmpFile, $file)) {
|
|
|
|
chmod($file, 0644);
|
2010-02-17 13:54:36 +00:00
|
|
|
|
2010-08-12 20:58:19 +01:00
|
|
|
return;
|
2010-06-23 06:35:46 +01:00
|
|
|
}
|
|
|
|
|
2010-08-12 20:58:19 +01:00
|
|
|
throw new \RuntimeException(sprintf('Failed to write cache file "%s".', $file));
|
2010-05-06 12:25:53 +01:00
|
|
|
}
|
2010-02-17 13:54:36 +00:00
|
|
|
}
|