private $file;
/**
- * @var ResourceCheckerInterface[]
+ * @var iterable|ResourceCheckerInterface[]
*/
private $resourceCheckers;
/**
- * @param string $file The absolute cache path
- * @param ResourceCheckerInterface[] $resourceCheckers The ResourceCheckers to use for the freshness check
+ * @param string $file The absolute cache path
+ * @param iterable|ResourceCheckerInterface[] $resourceCheckers The ResourceCheckers to use for the freshness check
*/
- public function __construct($file, array $resourceCheckers = array())
+ public function __construct($file, $resourceCheckers = array())
{
$this->file = $file;
$this->resourceCheckers = $resourceCheckers;
return false;
}
- if (!$this->resourceCheckers) {
+ if ($this->resourceCheckers instanceof \Traversable && !$this->resourceCheckers instanceof \Countable) {
+ $this->resourceCheckers = iterator_to_array($this->resourceCheckers);
+ }
+
+ if (!count($this->resourceCheckers)) {
return true; // shortcut - if we don't have any checkers we don't need to bother with the meta file at all
}
$metadata = $this->getMetaFile();
+
if (!is_file($metadata)) {
return false;
}
- $e = null;
- $meta = false;
- $time = filemtime($this->file);
- $signalingException = new \UnexpectedValueException();
- $prevUnserializeHandler = ini_set('unserialize_callback_func', '');
- $prevErrorHandler = set_error_handler(function ($type, $msg, $file, $line, $context) use (&$prevErrorHandler, $signalingException) {
- if (E_WARNING === $type && 'Class __PHP_Incomplete_Class has no unserializer' === $msg) {
- throw $signalingException;
- }
-
- return $prevErrorHandler ? $prevErrorHandler($type, $msg, $file, $line, $context) : false;
- });
+ $meta = $this->safelyUnserialize($metadata);
- try {
- $meta = unserialize(file_get_contents($metadata));
- } catch (\Error $e) {
- } catch (\Exception $e) {
- }
- restore_error_handler();
- ini_set('unserialize_callback_func', $prevUnserializeHandler);
- if (null !== $e && $e !== $signalingException) {
- throw $e;
- }
if (false === $meta) {
return false;
}
+ $time = filemtime($this->file);
+
foreach ($meta as $resource) {
/* @var ResourceInterface $resource */
foreach ($this->resourceCheckers as $checker) {
$mode = 0666;
$umask = umask();
$filesystem = new Filesystem();
- $filesystem->dumpFile($this->file, $content, null);
+ $filesystem->dumpFile($this->file, $content);
try {
$filesystem->chmod($this->file, $mode, $umask);
} catch (IOException $e) {
}
if (null !== $metadata) {
- $filesystem->dumpFile($this->getMetaFile(), serialize($metadata), null);
+ $filesystem->dumpFile($this->getMetaFile(), serialize($metadata));
try {
$filesystem->chmod($this->getMetaFile(), $mode, $umask);
} catch (IOException $e) {
// discard chmod failure (some filesystem may not support it)
}
}
+
+ if (\function_exists('opcache_invalidate') && ini_get('opcache.enable')) {
+ @opcache_invalidate($this->file, true);
+ }
}
/**
{
return $this->file.'.meta';
}
+
+ private function safelyUnserialize($file)
+ {
+ $e = null;
+ $meta = false;
+ $signalingException = new \UnexpectedValueException();
+ $prevUnserializeHandler = ini_set('unserialize_callback_func', '');
+ $prevErrorHandler = set_error_handler(function ($type, $msg, $file, $line, $context = array()) use (&$prevErrorHandler, $signalingException) {
+ if (E_WARNING === $type && 'Class __PHP_Incomplete_Class has no unserializer' === $msg) {
+ throw $signalingException;
+ }
+
+ return $prevErrorHandler ? $prevErrorHandler($type, $msg, $file, $line, $context) : false;
+ });
+
+ try {
+ $meta = unserialize(file_get_contents($file));
+ } catch (\Error $e) {
+ } catch (\Exception $e) {
+ }
+ restore_error_handler();
+ ini_set('unserialize_callback_func', $prevUnserializeHandler);
+ if (null !== $e && $e !== $signalingException) {
+ throw $e;
+ }
+
+ return $meta;
+ }
}