2018-04-17 04:11:51 +02: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\Simple ;
2024-03-20 03:35:09 +01:00
use Psr\SimpleCache\CacheInterface as Psr16CacheInterface ;
use Symfony\Component\Cache\Adapter\PhpArrayAdapter ;
2018-04-17 04:11:51 +02:00
use Symfony\Component\Cache\Exception\InvalidArgumentException ;
use Symfony\Component\Cache\PruneableInterface ;
use Symfony\Component\Cache\ResettableInterface ;
2024-01-12 06:08:24 +01:00
use Symfony\Component\Cache\Traits\PhpArrayTrait ;
2024-03-20 03:35:09 +01:00
use Symfony\Contracts\Cache\CacheInterface ;
@ trigger_error ( sprintf ( 'The "%s" class is deprecated since Symfony 4.3, use "%s" and type-hint for "%s" instead.' , PhpArrayCache :: class , PhpArrayAdapter :: class , CacheInterface :: class ), \E_USER_DEPRECATED );
2018-04-17 04:11:51 +02:00
/**
2024-03-20 03:35:09 +01:00
* @ deprecated since Symfony 4.3 , use PhpArrayAdapter and type - hint for CacheInterface instead .
2018-04-17 04:11:51 +02:00
*/
2024-03-20 03:35:09 +01:00
class PhpArrayCache implements Psr16CacheInterface , PruneableInterface , ResettableInterface
2018-04-17 04:11:51 +02:00
{
use PhpArrayTrait ;
/**
2024-03-20 03:35:09 +01:00
* @ param string $file The PHP file were values are cached
* @ param Psr16CacheInterface $fallbackPool A pool to fallback on when an item is not hit
2018-04-17 04:11:51 +02:00
*/
2024-03-20 03:35:09 +01:00
public function __construct ( string $file , Psr16CacheInterface $fallbackPool )
2018-04-17 04:11:51 +02:00
{
$this -> file = $file ;
$this -> pool = $fallbackPool ;
}
/**
2024-03-20 03:35:09 +01:00
* This adapter takes advantage of how PHP stores arrays in its latest versions .
2018-04-17 04:11:51 +02:00
*
2024-01-12 06:08:24 +01:00
* @ param string $file The PHP file were values are cached
* @ param CacheInterface $fallbackPool A pool to fallback on when an item is not hit
2018-04-17 04:11:51 +02:00
*
2024-03-20 03:35:09 +01:00
* @ return Psr16CacheInterface
2018-04-17 04:11:51 +02:00
*/
2024-03-20 03:35:09 +01:00
public static function create ( $file , Psr16CacheInterface $fallbackPool )
2018-04-17 04:11:51 +02:00
{
2024-03-20 03:35:09 +01:00
return new static ( $file , $fallbackPool );
2018-04-17 04:11:51 +02:00
}
/**
* { @ inheritdoc }
*/
public function get ( $key , $default = null )
{
2024-01-12 06:08:24 +01:00
if ( ! \is_string ( $key )) {
throw new InvalidArgumentException ( sprintf ( 'Cache key must be string, "%s" given.' , \is_object ( $key ) ? \get_class ( $key ) : \gettype ( $key )));
2018-04-17 04:11:51 +02:00
}
if ( null === $this -> values ) {
$this -> initialize ();
}
2024-03-20 03:35:09 +01:00
if ( ! isset ( $this -> keys [ $key ])) {
2018-04-17 04:11:51 +02:00
return $this -> pool -> get ( $key , $default );
}
2024-03-20 03:35:09 +01:00
$value = $this -> values [ $this -> keys [ $key ]];
2018-04-17 04:11:51 +02:00
if ( 'N;' === $value ) {
2024-03-20 03:35:09 +01:00
return null ;
}
if ( $value instanceof \Closure ) {
2018-04-17 04:11:51 +02:00
try {
2024-03-20 03:35:09 +01:00
return $value ();
} catch ( \Throwable $e ) {
2018-04-17 04:11:51 +02:00
return $default ;
}
}
return $value ;
}
/**
* { @ inheritdoc }
2024-03-20 03:35:09 +01:00
*
* @ return iterable
2018-04-17 04:11:51 +02:00
*/
public function getMultiple ( $keys , $default = null )
{
if ( $keys instanceof \Traversable ) {
$keys = iterator_to_array ( $keys , false );
2024-01-12 06:08:24 +01:00
} elseif ( ! \is_array ( $keys )) {
throw new InvalidArgumentException ( sprintf ( 'Cache keys must be array or Traversable, "%s" given.' , \is_object ( $keys ) ? \get_class ( $keys ) : \gettype ( $keys )));
2018-04-17 04:11:51 +02:00
}
foreach ( $keys as $key ) {
2024-01-12 06:08:24 +01:00
if ( ! \is_string ( $key )) {
throw new InvalidArgumentException ( sprintf ( 'Cache key must be string, "%s" given.' , \is_object ( $key ) ? \get_class ( $key ) : \gettype ( $key )));
2018-04-17 04:11:51 +02:00
}
}
if ( null === $this -> values ) {
$this -> initialize ();
}
return $this -> generateItems ( $keys , $default );
}
/**
* { @ inheritdoc }
2024-03-20 03:35:09 +01:00
*
* @ return bool
2018-04-17 04:11:51 +02:00
*/
public function has ( $key )
{
2024-01-12 06:08:24 +01:00
if ( ! \is_string ( $key )) {
throw new InvalidArgumentException ( sprintf ( 'Cache key must be string, "%s" given.' , \is_object ( $key ) ? \get_class ( $key ) : \gettype ( $key )));
2018-04-17 04:11:51 +02:00
}
if ( null === $this -> values ) {
$this -> initialize ();
}
2024-03-20 03:35:09 +01:00
return isset ( $this -> keys [ $key ]) || $this -> pool -> has ( $key );
2018-04-17 04:11:51 +02:00
}
/**
* { @ inheritdoc }
2024-03-20 03:35:09 +01:00
*
* @ return bool
2018-04-17 04:11:51 +02:00
*/
public function delete ( $key )
{
2024-01-12 06:08:24 +01:00
if ( ! \is_string ( $key )) {
throw new InvalidArgumentException ( sprintf ( 'Cache key must be string, "%s" given.' , \is_object ( $key ) ? \get_class ( $key ) : \gettype ( $key )));
2018-04-17 04:11:51 +02:00
}
if ( null === $this -> values ) {
$this -> initialize ();
}
2024-03-20 03:35:09 +01:00
return ! isset ( $this -> keys [ $key ]) && $this -> pool -> delete ( $key );
2018-04-17 04:11:51 +02:00
}
/**
* { @ inheritdoc }
2024-03-20 03:35:09 +01:00
*
* @ return bool
2018-04-17 04:11:51 +02:00
*/
public function deleteMultiple ( $keys )
{
2024-01-12 06:08:24 +01:00
if ( ! \is_array ( $keys ) && ! $keys instanceof \Traversable ) {
throw new InvalidArgumentException ( sprintf ( 'Cache keys must be array or Traversable, "%s" given.' , \is_object ( $keys ) ? \get_class ( $keys ) : \gettype ( $keys )));
2018-04-17 04:11:51 +02:00
}
$deleted = true ;
2024-01-12 06:08:24 +01:00
$fallbackKeys = [];
2018-04-17 04:11:51 +02:00
foreach ( $keys as $key ) {
2024-01-12 06:08:24 +01:00
if ( ! \is_string ( $key )) {
throw new InvalidArgumentException ( sprintf ( 'Cache key must be string, "%s" given.' , \is_object ( $key ) ? \get_class ( $key ) : \gettype ( $key )));
2018-04-17 04:11:51 +02:00
}
2024-03-20 03:35:09 +01:00
if ( isset ( $this -> keys [ $key ])) {
2018-04-17 04:11:51 +02:00
$deleted = false ;
} else {
$fallbackKeys [] = $key ;
}
}
if ( null === $this -> values ) {
$this -> initialize ();
}
if ( $fallbackKeys ) {
$deleted = $this -> pool -> deleteMultiple ( $fallbackKeys ) && $deleted ;
}
return $deleted ;
}
/**
* { @ inheritdoc }
2024-03-20 03:35:09 +01:00
*
* @ return bool
2018-04-17 04:11:51 +02:00
*/
public function set ( $key , $value , $ttl = null )
{
2024-01-12 06:08:24 +01:00
if ( ! \is_string ( $key )) {
throw new InvalidArgumentException ( sprintf ( 'Cache key must be string, "%s" given.' , \is_object ( $key ) ? \get_class ( $key ) : \gettype ( $key )));
2018-04-17 04:11:51 +02:00
}
if ( null === $this -> values ) {
$this -> initialize ();
}
2024-03-20 03:35:09 +01:00
return ! isset ( $this -> keys [ $key ]) && $this -> pool -> set ( $key , $value , $ttl );
2018-04-17 04:11:51 +02:00
}
/**
* { @ inheritdoc }
2024-03-20 03:35:09 +01:00
*
* @ return bool
2018-04-17 04:11:51 +02:00
*/
public function setMultiple ( $values , $ttl = null )
{
2024-01-12 06:08:24 +01:00
if ( ! \is_array ( $values ) && ! $values instanceof \Traversable ) {
throw new InvalidArgumentException ( sprintf ( 'Cache values must be array or Traversable, "%s" given.' , \is_object ( $values ) ? \get_class ( $values ) : \gettype ( $values )));
2018-04-17 04:11:51 +02:00
}
$saved = true ;
2024-01-12 06:08:24 +01:00
$fallbackValues = [];
2018-04-17 04:11:51 +02:00
foreach ( $values as $key => $value ) {
2024-01-12 06:08:24 +01:00
if ( ! \is_string ( $key ) && ! \is_int ( $key )) {
throw new InvalidArgumentException ( sprintf ( 'Cache key must be string, "%s" given.' , \is_object ( $key ) ? \get_class ( $key ) : \gettype ( $key )));
2018-04-17 04:11:51 +02:00
}
2024-03-20 03:35:09 +01:00
if ( isset ( $this -> keys [ $key ])) {
2018-04-17 04:11:51 +02:00
$saved = false ;
} else {
$fallbackValues [ $key ] = $value ;
}
}
if ( $fallbackValues ) {
$saved = $this -> pool -> setMultiple ( $fallbackValues , $ttl ) && $saved ;
}
return $saved ;
}
2024-03-20 03:35:09 +01:00
private function generateItems ( array $keys , $default ) : iterable
2018-04-17 04:11:51 +02:00
{
2024-01-12 06:08:24 +01:00
$fallbackKeys = [];
2018-04-17 04:11:51 +02:00
foreach ( $keys as $key ) {
2024-03-20 03:35:09 +01:00
if ( isset ( $this -> keys [ $key ])) {
$value = $this -> values [ $this -> keys [ $key ]];
2018-04-17 04:11:51 +02:00
if ( 'N;' === $value ) {
yield $key => null ;
2024-03-20 03:35:09 +01:00
} elseif ( $value instanceof \Closure ) {
2018-04-17 04:11:51 +02:00
try {
2024-03-20 03:35:09 +01:00
yield $key => $value ();
} catch ( \Throwable $e ) {
2018-04-17 04:11:51 +02:00
yield $key => $default ;
}
} else {
yield $key => $value ;
}
} else {
$fallbackKeys [] = $key ;
}
}
if ( $fallbackKeys ) {
2024-03-20 03:35:09 +01:00
yield from $this -> pool -> getMultiple ( $fallbackKeys , $default );
2018-04-17 04:11:51 +02:00
}
}
}