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;
|
|
|
|
|
2016-05-25 08:44:10 +01:00
|
|
|
use Symfony\Component\Cache\Exception\CacheException;
|
|
|
|
use Symfony\Component\Cache\Exception\InvalidArgumentException;
|
2016-05-24 14:06:45 +01:00
|
|
|
|
2016-05-25 08:44:10 +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
|
|
|
|
{
|
2016-05-25 08:44:10 +01:00
|
|
|
use FilesystemAdapterTrait;
|
2016-05-24 14:06:45 +01:00
|
|
|
|
2016-05-25 08:44:10 +01:00
|
|
|
private $includeHandler;
|
|
|
|
|
|
|
|
public static function isSupported()
|
|
|
|
{
|
|
|
|
return function_exists('opcache_compile_file') && ini_get('opcache.enable');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function __construct($namespace = '', $defaultLifetime = 0, $directory = null)
|
2016-05-24 14:06:45 +01:00
|
|
|
{
|
2016-05-25 08:44:10 +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();
|
2016-05-25 08:44:10 +01:00
|
|
|
$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
|
|
|
|
2016-05-25 08:44:10 +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)
|
|
|
|
{
|
2016-05-25 08:44:10 +01:00
|
|
|
return (bool) $this->doFetch(array($id));
|
2016-05-24 14:06:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
|
|
|
protected function doSave(array $values, $lifetime)
|
|
|
|
{
|
|
|
|
$ok = true;
|
2016-05-25 08:44:10 +01:00
|
|
|
$data = array($lifetime ? time() + $lifetime : PHP_INT_MAX, '');
|
2016-12-13 14:25:53 +00:00
|
|
|
$allowCompile = 'cli' !== PHP_SAPI || ini_get('opcache.enable_cli');
|
2016-05-24 14:06:45 +01:00
|
|
|
|
2016-12-12 19:41:46 +00:00
|
|
|
foreach ($values as $key => $value) {
|
2016-05-25 08:44:10 +01:00
|
|
|
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);
|
2016-05-25 08:44:10 +01:00
|
|
|
// 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)) {
|
2016-12-12 19:41:46 +00:00
|
|
|
throw new InvalidArgumentException(sprintf('Cache key "%s" has non-serializable %s value.', $key, gettype($value)));
|
2016-05-24 14:06:45 +01:00
|
|
|
}
|
|
|
|
|
2016-05-25 08:44:10 +01:00
|
|
|
$data[1] = $value;
|
2016-12-12 19:41:46 +00:00
|
|
|
$file = $this->getFile($key, true);
|
2016-05-25 08:44:10 +01:00
|
|
|
$ok = $this->write($file, '<?php return '.var_export($data, true).';') && $ok;
|
2016-12-12 19:41:46 +00:00
|
|
|
|
2016-12-13 14:25:53 +00:00
|
|
|
if ($allowCompile) {
|
2016-12-12 19:41:46 +00:00
|
|
|
@opcache_compile_file($file);
|
|
|
|
}
|
2016-05-24 14:06:45 +01:00
|
|
|
}
|
|
|
|
|
2016-05-25 08:44:10 +01:00
|
|
|
return $ok;
|
2016-05-24 14:06:45 +01:00
|
|
|
}
|
|
|
|
}
|