3 * Zend Framework (http://framework.zend.com/)
5 * @link http://github.com/zendframework/zf2 for the canonical source repository
6 * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
7 * @license http://framework.zend.com/license/new-bsd New BSD License
10 namespace Zend\Cache\Pattern
;
13 use Zend\Cache\Exception
;
14 use Zend\Cache\StorageFactory
;
15 use Zend\Cache\Storage\StorageInterface
as Storage
;
16 use Zend\Stdlib\AbstractOptions
;
18 class PatternOptions
extends AbstractOptions
26 protected $cacheByDefault = true;
35 protected $cacheOutput = true;
49 protected $classCacheMethods = array();
56 protected $classNonCacheMethods = array();
63 protected $umask = false;
70 protected $dirPermission = 0700;
77 protected $filePermission = 0600;
84 protected $fileLocking = true;
91 protected $indexFilename = 'index.html';
105 protected $objectCacheMagicProperties = false;
112 protected $objectCacheMethods = array();
119 protected $objectKey;
126 protected $objectNonCacheMethods = array('__tostring');
133 protected $publicDir;
148 * @param array|Traversable|null $options
149 * @return PatternOptions
150 * @throws Exception\InvalidArgumentException
152 public function __construct($options = null)
154 // disable file/directory permissions by default on windows systems
155 if (strtoupper(substr(PHP_OS
, 0, 3)) == 'WIN') {
156 $this->filePermission
= false;
157 $this->dirPermission
= false;
160 parent
::__construct($options);
164 * Set flag indicating whether or not to cache by default
170 * @param bool $cacheByDefault
171 * @return PatternOptions
173 public function setCacheByDefault($cacheByDefault)
175 $this->cacheByDefault
= $cacheByDefault;
180 * Do we cache by default?
188 public function getCacheByDefault()
190 return $this->cacheByDefault
;
194 * Set whether or not to cache output
201 * @param bool $cacheOutput
202 * @return PatternOptions
204 public function setCacheOutput($cacheOutput)
206 $this->cacheOutput
= (bool) $cacheOutput;
211 * Will we cache output?
220 public function getCacheOutput()
222 return $this->cacheOutput
;
231 * @param string $class
232 * @throws Exception\InvalidArgumentException
233 * @return PatternOptions
235 public function setClass($class)
237 if (!is_string($class)) {
238 throw new Exception\
InvalidArgumentException('Invalid classname provided; must be a string');
240 $this->class = $class;
250 * @return null|string
252 public function getClass()
258 * Set list of method return values to cache
263 * @param array $classCacheMethods
264 * @return PatternOptions
266 public function setClassCacheMethods(array $classCacheMethods)
268 $this->classCacheMethods
= $this->recursiveStrtolower($classCacheMethods);
273 * Get list of methods from which to cache return values
280 public function getClassCacheMethods()
282 return $this->classCacheMethods
;
286 * Set list of method return values NOT to cache
291 * @param array $classNonCacheMethods
292 * @return PatternOptions
294 public function setClassNonCacheMethods(array $classNonCacheMethods)
296 $this->classNonCacheMethods
= $this->recursiveStrtolower($classNonCacheMethods);
301 * Get list of methods from which NOT to cache return values
308 public function getClassNonCacheMethods()
310 return $this->classNonCacheMethods
;
314 * Set directory permission
316 * @param false|int $dirPermission
317 * @throws Exception\InvalidArgumentException
318 * @return PatternOptions
320 public function setDirPermission($dirPermission)
322 if ($dirPermission !== false) {
323 if (is_string($dirPermission)) {
324 $dirPermission = octdec($dirPermission);
326 $dirPermission = (int) $dirPermission;
330 if (($dirPermission & 0700) != 0700) {
331 throw new Exception\
InvalidArgumentException(
332 'Invalid directory permission: need permission to execute, read and write by owner'
337 $this->dirPermission
= $dirPermission;
342 * Gets directory permission
346 public function getDirPermission()
348 return $this->dirPermission
;
357 * @param false|int $umask
358 * @throws Exception\InvalidArgumentException
359 * @return PatternOptions
361 public function setUmask($umask)
363 if ($umask !== false) {
364 if (is_string($umask)) {
365 $umask = octdec($umask);
367 $umask = (int) $umask;
372 throw new Exception\
InvalidArgumentException(
373 'Invalid umask: need permission to execute, read and write by owner'
378 $umask = $umask & ~
0002;
381 $this->umask
= $umask;
393 public function getUmask()
399 * Set whether or not file locking should be used
404 * @param bool $fileLocking
405 * @return PatternOptions
407 public function setFileLocking($fileLocking)
409 $this->fileLocking
= (bool) $fileLocking;
414 * Is file locking enabled?
421 public function getFileLocking()
423 return $this->fileLocking
;
427 * Set file permission
429 * @param false|int $filePermission
430 * @throws Exception\InvalidArgumentException
431 * @return PatternOptions
433 public function setFilePermission($filePermission)
435 if ($filePermission !== false) {
436 if (is_string($filePermission)) {
437 $filePermission = octdec($filePermission);
439 $filePermission = (int) $filePermission;
443 if (($filePermission & 0600) != 0600) {
444 throw new Exception\
InvalidArgumentException(
445 'Invalid file permission: need permission to read and write by owner'
447 } elseif ($filePermission & 0111) {
448 throw new Exception\
InvalidArgumentException(
449 "Invalid file permission: Files shoudn't be executable"
454 $this->filePermission
= $filePermission;
459 * Gets file permission
463 public function getFilePermission()
465 return $this->filePermission
;
469 * Set value for index filename
471 * @param string $indexFilename
472 * @return PatternOptions
474 public function setIndexFilename($indexFilename)
476 $this->indexFilename
= (string) $indexFilename;
481 * Get value for index filename
485 public function getIndexFilename()
487 return $this->indexFilename
;
491 * Set object to cache
493 * @param mixed $object
494 * @throws Exception\InvalidArgumentException
495 * @return PatternOptions
497 public function setObject($object)
499 if (!is_object($object)) {
500 throw new Exception\
InvalidArgumentException(
501 sprintf('%s expects an object; received "%s"', __METHOD__
, gettype($object))
504 $this->object = $object;
509 * Get object to cache
511 * @return null|object
513 public function getObject()
515 return $this->object;
519 * Set flag indicating whether or not to cache magic properties
524 * @param bool $objectCacheMagicProperties
525 * @return PatternOptions
527 public function setObjectCacheMagicProperties($objectCacheMagicProperties)
529 $this->objectCacheMagicProperties
= (bool) $objectCacheMagicProperties;
534 * Should we cache magic properties?
541 public function getObjectCacheMagicProperties()
543 return $this->objectCacheMagicProperties
;
547 * Set list of object methods for which to cache return values
549 * @param array $objectCacheMethods
550 * @return PatternOptions
551 * @throws Exception\InvalidArgumentException
553 public function setObjectCacheMethods(array $objectCacheMethods)
555 $this->objectCacheMethods
= $this->normalizeObjectMethods($objectCacheMethods);
560 * Get list of object methods for which to cache return values
564 public function getObjectCacheMethods()
566 return $this->objectCacheMethods
;
570 * Set the object key part.
572 * Used to generate a callback key in order to speed up key generation.
577 * @param null|string $objectKey The object key or NULL to use the objects class name
578 * @return PatternOptions
580 public function setObjectKey($objectKey)
582 if ($objectKey !== null) {
583 $this->objectKey
= (string) $objectKey;
585 $this->objectKey
= null;
598 public function getObjectKey()
600 if ($this->objectKey
=== null) {
601 return get_class($this->getObject());
603 return $this->objectKey
;
607 * Set list of object methods for which NOT to cache return values
609 * @param array $objectNonCacheMethods
610 * @return PatternOptions
611 * @throws Exception\InvalidArgumentException
613 public function setObjectNonCacheMethods(array $objectNonCacheMethods)
615 $this->objectNonCacheMethods
= $this->normalizeObjectMethods($objectNonCacheMethods);
620 * Get list of object methods for which NOT to cache return values
624 public function getObjectNonCacheMethods()
626 return $this->objectNonCacheMethods
;
630 * Set location of public directory
635 * @param string $publicDir
636 * @throws Exception\InvalidArgumentException
637 * @return PatternOptions
639 public function setPublicDir($publicDir)
641 $publicDir = (string) $publicDir;
643 if (!is_dir($publicDir)) {
644 throw new Exception\
InvalidArgumentException(
645 "Public directory '{$publicDir}' not found or not a directory"
647 } elseif (!is_writable($publicDir)) {
648 throw new Exception\
InvalidArgumentException(
649 "Public directory '{$publicDir}' not writable"
651 } elseif (!is_readable($publicDir)) {
652 throw new Exception\
InvalidArgumentException(
653 "Public directory '{$publicDir}' not readable"
657 $this->publicDir
= rtrim(realpath($publicDir), DIRECTORY_SEPARATOR
);
662 * Get location of public directory
667 * @return null|string
669 public function getPublicDir()
671 return $this->publicDir
;
675 * Set storage adapter
677 * Required for the following Pattern classes:
683 * @param string|array|Storage $storage
684 * @return PatternOptions
686 public function setStorage($storage)
688 $this->storage
= $this->storageFactory($storage);
693 * Get storage adapter
701 * @return null|Storage
703 public function getStorage()
705 return $this->storage
;
709 * Recursively apply strtolower on all values of an array, and return as a
710 * list of unique values
712 * @param array $array
715 protected function recursiveStrtolower(array $array)
717 return array_values(array_unique(array_map('strtolower', $array)));
721 * Normalize object methods
723 * Recursively casts values to lowercase, then determines if any are in a
724 * list of methods not handled, raising an exception if so.
726 * @param array $methods
728 * @throws Exception\InvalidArgumentException
730 protected function normalizeObjectMethods(array $methods)
732 $methods = $this->recursiveStrtolower($methods);
733 $intersect = array_intersect(array('__set', '__get', '__unset', '__isset'), $methods);
734 if (!empty($intersect)) {
735 throw new Exception\
InvalidArgumentException(
736 "Magic properties are handled by option 'cache_magic_properties'"
743 * Create a storage object from a given specification
745 * @param array|string|Storage $storage
746 * @throws Exception\InvalidArgumentException
749 protected function storageFactory($storage)
751 if (is_array($storage)) {
752 $storage = StorageFactory
::factory($storage);
753 } elseif (is_string($storage)) {
754 $storage = StorageFactory
::adapterFactory($storage);
755 } elseif (!($storage instanceof Storage
)) {
756 throw new Exception\
InvalidArgumentException(
757 'The storage must be an instanceof Zend\Cache\Storage\StorageInterface '
758 . 'or an array passed to Zend\Cache\Storage::factory '
759 . 'or simply the name of the storage adapter'