Source of file AdapterOptions.php
Size: 6,068 Bytes - Last Modified: 2014-03-12T23:21:18+01:00
/home/theseer/Downloads/ZendFramework-2.3.0/library/Zend/Cache/Storage/Adapter/AdapterOptions.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266 | <?php /** * Zend Framework (http://framework.zend.com/) * * @link http://github.com/zendframework/zf2 for the canonical source repository * @copyright Copyright (c) 2005-2014 Zend Technologies USA Inc. (http://www.zend.com) * @license http://framework.zend.com/license/new-bsd New BSD License */ namespace Zend\Cache\Storage\Adapter; use ArrayObject; use Zend\Cache\Exception; use Zend\Cache\Storage\Event; use Zend\Cache\Storage\StorageInterface; use Zend\EventManager\EventsCapableInterface; use Zend\Stdlib\AbstractOptions; use Zend\Stdlib\ErrorHandler; /** * Unless otherwise marked, all options in this class affect all adapters. */ class AdapterOptions extends AbstractOptions { /** * The adapter using these options * * @var null|Filesystem */ protected $adapter; /** * Validate key against pattern * * @var string */ protected $keyPattern = ''; /** * Namespace option * * @var string */ protected $namespace = 'zfcache'; /** * Readable option * * @var bool */ protected $readable = true; /** * TTL option * * @var int|float 0 means infinite or maximum of adapter */ protected $ttl = 0; /** * Writable option * * @var bool */ protected $writable = true; /** * Adapter using this instance * * @param StorageInterface|null $adapter * @return AdapterOptions */ public function setAdapter(StorageInterface $adapter = null) { $this->adapter = $adapter; return $this; } /** * Set key pattern * * @param null|string $keyPattern * @throws Exception\InvalidArgumentException * @return AdapterOptions */ public function setKeyPattern($keyPattern) { $keyPattern = (string) $keyPattern; if ($this->keyPattern !== $keyPattern) { // validate pattern if ($keyPattern !== '') { ErrorHandler::start(E_WARNING); $result = preg_match($keyPattern, ''); $error = ErrorHandler::stop(); if ($result === false) { throw new Exception\InvalidArgumentException(sprintf( 'Invalid pattern "%s"%s', $keyPattern, ($error ? ': ' . $error->getMessage() : '') ), 0, $error); } } $this->triggerOptionEvent('key_pattern', $keyPattern); $this->keyPattern = $keyPattern; } return $this; } /** * Get key pattern * * @return string */ public function getKeyPattern() { return $this->keyPattern; } /** * Set namespace. * * @param string $namespace * @return AdapterOptions */ public function setNamespace($namespace) { $namespace = (string) $namespace; if ($this->namespace !== $namespace) { $this->triggerOptionEvent('namespace', $namespace); $this->namespace = $namespace; } return $this; } /** * Get namespace * * @return string */ public function getNamespace() { return $this->namespace; } /** * Enable/Disable reading data from cache. * * @param bool $readable * @return AbstractAdapter */ public function setReadable($readable) { $readable = (bool) $readable; if ($this->readable !== $readable) { $this->triggerOptionEvent('readable', $readable); $this->readable = $readable; } return $this; } /** * If reading data from cache enabled. * * @return bool */ public function getReadable() { return $this->readable; } /** * Set time to live. * * @param int|float $ttl * @return AdapterOptions */ public function setTtl($ttl) { $this->normalizeTtl($ttl); if ($this->ttl !== $ttl) { $this->triggerOptionEvent('ttl', $ttl); $this->ttl = $ttl; } return $this; } /** * Get time to live. * * @return float */ public function getTtl() { return $this->ttl; } /** * Enable/Disable writing data to cache. * * @param bool $writable * @return AdapterOptions */ public function setWritable($writable) { $writable = (bool) $writable; if ($this->writable !== $writable) { $this->triggerOptionEvent('writable', $writable); $this->writable = $writable; } return $this; } /** * If writing data to cache enabled. * * @return bool */ public function getWritable() { return $this->writable; } /** * Triggers an option event if this options instance has a connection to * an adapter implements EventsCapableInterface. * * @param string $optionName * @param mixed $optionValue * @return void */ protected function triggerOptionEvent($optionName, $optionValue) { if ($this->adapter instanceof EventsCapableInterface) { $event = new Event('option', $this->adapter, new ArrayObject(array($optionName => $optionValue))); $this->adapter->getEventManager()->trigger($event); } } /** * Validates and normalize a TTL. * * @param int|float $ttl * @throws Exception\InvalidArgumentException * @return void */ protected function normalizeTtl(&$ttl) { if (!is_int($ttl)) { $ttl = (float) $ttl; // convert to int if possible if ($ttl === (float) (int) $ttl) { $ttl = (int) $ttl; } } if ($ttl < 0) { throw new Exception\InvalidArgumentException("TTL can't be negative"); } } } |