{
const EXTENSION = '.doctrinecache.php';
+ /**
+ * @var callable
+ *
+ * This is cached in a local static variable to avoid instantiating a closure each time we need an empty handler
+ */
+ private static $emptyErrorHandler;
+
/**
* {@inheritdoc}
*/
public function __construct($directory, $extension = self::EXTENSION, $umask = 0002)
{
parent::__construct($directory, $extension, $umask);
+
+ self::$emptyErrorHandler = function () {
+ };
}
/**
{
$value = $this->includeFileForId($id);
- if (! $value) {
+ if ($value === null) {
return false;
}
{
$value = $this->includeFileForId($id);
- if (! $value) {
+ if ($value === null) {
return false;
}
$lifeTime = time() + $lifeTime;
}
- if (is_object($data) && ! method_exists($data, '__set_state')) {
- throw new \InvalidArgumentException(
- "Invalid argument given, PhpFileCache only allows objects that implement __set_state() " .
- "and fully support var_export(). You can use the FilesystemCache to save arbitrary object " .
- "graphs using serialize()/deserialize()."
- );
- }
-
$filename = $this->getFilename($id);
- $value = array(
+ $value = [
'lifetime' => $lifeTime,
'data' => $data
- );
-
- $value = var_export($value, true);
- $code = sprintf('<?php return %s;', $value);
+ ];
+
+ if (is_object($data) && method_exists($data, '__set_state')) {
+ $value = var_export($value, true);
+ $code = sprintf('<?php return %s;', $value);
+ } else {
+ $value = var_export(serialize($value), true);
+ $code = sprintf('<?php return unserialize(%s);', $value);
+ }
return $this->writeFile($filename, $code);
}
/**
* @param string $id
*
- * @return array|false
+ * @return array|null
*/
- private function includeFileForId($id)
+ private function includeFileForId(string $id) : ?array
{
$fileName = $this->getFilename($id);
// note: error suppression is still faster than `file_exists`, `is_file` and `is_readable`
- $value = @include $fileName;
+ set_error_handler(self::$emptyErrorHandler);
+
+ $value = include $fileName;
+
+ restore_error_handler();
if (! isset($value['lifetime'])) {
- return false;
+ return null;
}
return $value;