2010-02-17 13:54:36 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Symfony\Foundation;
|
|
|
|
|
|
|
|
/*
|
2010-04-07 01:51:29 +01:00
|
|
|
* This file is part of the Symfony framework.
|
2010-02-17 13:54:36 +00:00
|
|
|
*
|
|
|
|
* (c) Fabien Potencier <fabien.potencier@symfony-project.com>
|
|
|
|
*
|
|
|
|
* This source file is subject to the MIT license that is bundled
|
|
|
|
* with this source code in the file LICENSE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
2010-04-07 01:51:29 +01:00
|
|
|
* ClassCollectionLoader.
|
2010-02-17 13:54:36 +00:00
|
|
|
*
|
2010-04-07 01:51:29 +01:00
|
|
|
* @package Symfony
|
|
|
|
* @subpackage Foundation
|
2010-02-17 13:54:36 +00:00
|
|
|
* @author Fabien Potencier <fabien.potencier@symfony-project.com>
|
|
|
|
*/
|
|
|
|
class ClassCollectionLoader
|
|
|
|
{
|
2010-05-06 12:25:53 +01:00
|
|
|
/**
|
|
|
|
* @throws \InvalidArgumentException When class can't be loaded
|
|
|
|
*/
|
|
|
|
static public function load($classes, $cacheDir, $name, $autoReload)
|
2010-02-17 13:54:36 +00:00
|
|
|
{
|
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) {
|
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
|
|
|
|
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
|
|
|
|
2010-05-06 12:25:53 +01:00
|
|
|
$content .= preg_replace(array('/^\s*<\?php/', '/\?>\s*$/'), '', file_get_contents($r->getFileName()));
|
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
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-05-07 15:09:11 +01:00
|
|
|
if (!$fp = @fopen($tmpFile, 'wb')) {
|
2010-05-06 12:25:53 +01:00
|
|
|
die(sprintf('Failed to write cache file "%s".', $tmpFile));
|
|
|
|
}
|
|
|
|
@fwrite($fp, $content);
|
|
|
|
@fclose($fp);
|
2010-02-17 13:54:36 +00:00
|
|
|
|
2010-05-07 15:09:11 +01:00
|
|
|
if ($content != file_get_contents($tmpFile)) {
|
2010-05-06 12:25:53 +01:00
|
|
|
die(sprintf('Failed to write cache file "%s" (cache corrupted).', $tmpFile));
|
|
|
|
}
|
2010-02-17 13:54:36 +00:00
|
|
|
|
2010-05-06 12:25:53 +01:00
|
|
|
@rename($tmpFile, $file);
|
|
|
|
chmod($file, 0644);
|
|
|
|
}
|
2010-02-17 13:54:36 +00:00
|
|
|
}
|