This repository has been archived on 2023-08-20. You can view files and clone it, but cannot push or open issues or pull requests.
symfony/src/Symfony/Component/Cache/Adapter/PhpFilesAdapter.php

132 lines
4.0 KiB
PHP
Raw Normal View History

2016-05-24 14:06:45 +01:00
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Cache\Adapter;
use Symfony\Component\Cache\Exception\CacheException;
use Symfony\Component\Cache\Exception\InvalidArgumentException;
2016-05-24 14:06:45 +01:00
/**
* @author Piotr Stankowski <git@trakos.pl>
* @author Nicolas Grekas <p@tchwork.com>
*/
2016-05-24 14:06:45 +01:00
class PhpFilesAdapter extends AbstractAdapter
{
use FilesystemAdapterTrait;
2016-05-24 14:06:45 +01:00
private $includeHandler;
public static function isSupported()
{
return function_exists('opcache_invalidate') && ini_get('opcache.enable');
}
public function __construct($namespace = '', $defaultLifetime = 0, $directory = null)
2016-05-24 14:06:45 +01:00
{
if (!static::isSupported()) {
throw new CacheException('OPcache is not enabled');
}
parent::__construct('', $defaultLifetime);
$this->init($namespace, $directory);
$e = new \Exception();
$this->includeHandler = function () use ($e) { throw $e; };
2016-05-24 14:06:45 +01:00
}
/**
* {@inheritdoc}
*/
protected function doFetch(array $ids)
{
$values = array();
$now = time();
set_error_handler($this->includeHandler);
try {
foreach ($ids as $id) {
try {
$file = $this->getFile($id);
list($expiresAt, $values[$id]) = include $file;
if ($now >= $expiresAt) {
unset($values[$id]);
}
} catch (\Exception $e) {
continue;
}
}
} finally {
restore_error_handler();
}
2016-05-24 14:06:45 +01:00
foreach ($values as $id => $value) {
if ('N;' === $value) {
$values[$id] = null;
} elseif (is_string($value) && isset($value[2]) && ':' === $value[1]) {
2016-08-03 15:45:27 +01:00
$values[$id] = parent::unserialize($value);
2016-05-24 14:06:45 +01:00
}
}
return $values;
}
/**
* {@inheritdoc}
*/
protected function doHave($id)
{
return (bool) $this->doFetch(array($id));
2016-05-24 14:06:45 +01:00
}
/**
* {@inheritdoc}
*/
protected function doSave(array $values, $lifetime)
{
$ok = true;
$data = array($lifetime ? time() + $lifetime : PHP_INT_MAX, '');
$allowCompile = 'cli' !== PHP_SAPI || ini_get('opcache.enable_cli');
2016-05-24 14:06:45 +01:00
foreach ($values as $key => $value) {
if (null === $value || is_object($value)) {
$value = serialize($value);
} elseif (is_array($value)) {
$serialized = serialize($value);
2016-08-03 15:45:27 +01:00
$unserialized = parent::unserialize($serialized);
// Store arrays serialized if they contain any objects or references
if ($unserialized !== $value || (false !== strpos($serialized, ';R:') && preg_match('/;R:[1-9]/', $serialized))) {
$value = $serialized;
}
} elseif (is_string($value)) {
// Serialize strings if they could be confused with serialized objects or arrays
if ('N;' === $value || (isset($value[2]) && ':' === $value[1])) {
$value = serialize($value);
}
} elseif (!is_scalar($value)) {
throw new InvalidArgumentException(sprintf('Cache key "%s" has non-serializable %s value.', $key, gettype($value)));
2016-05-24 14:06:45 +01:00
}
$data[1] = $value;
$file = $this->getFile($key, true);
$ok = $this->write($file, '<?php return '.var_export($data, true).';') && $ok;
if ($allowCompile) {
@opcache_invalidate($file, true);
}
2016-05-24 14:06:45 +01:00
}
if (!$ok && !is_writable($this->directory)) {
throw new CacheException(sprintf('Cache directory is not writable (%s)', $this->directory));
}
return $ok;
2016-05-24 14:06:45 +01:00
}
}