Added the zend framework 2 library, the path is specified in line no.26 in zend_modul...
[openemr.git] / interface / modules / zend_modules / library / Zend / Cache / Pattern / PatternOptions.php
blobf9a9f469fb97dd58566048ae2da5b24d1238cb2a
1 <?php
2 /**
3 * Zend Framework (http://framework.zend.com/)
5 * @link http://github.com/zendframework/zf2 for the canonical source repository
6 * @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com)
7 * @license http://framework.zend.com/license/new-bsd New BSD License
8 */
10 namespace Zend\Cache\Pattern;
12 use Traversable;
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
20 /**
21 * Used by:
22 * - ClassCache
23 * - ObjectCache
24 * @var bool
26 protected $cacheByDefault = true;
28 /**
29 * Used by:
30 * - CallbackCache
31 * - ClassCache
32 * - ObjectCache
33 * @var bool
35 protected $cacheOutput = true;
37 /**
38 * Used by:
39 * - ClassCache
40 * @var null|string
42 protected $class;
44 /**
45 * Used by:
46 * - ClassCache
47 * @var array
49 protected $classCacheMethods = array();
51 /**
52 * Used by:
53 * - ClassCache
54 * @var array
56 protected $classNonCacheMethods = array();
58 /**
59 * Used by:
60 * - CaptureCache
61 * @var false|int
63 protected $umask = false;
65 /**
66 * Used by:
67 * - CaptureCache
68 * @var false|int
70 protected $dirPermission = 0700;
72 /**
73 * Used by:
74 * - CaptureCache
75 * @var false|int
77 protected $filePermission = 0600;
79 /**
80 * Used by:
81 * - CaptureCache
82 * @var bool
84 protected $fileLocking = true;
86 /**
87 * Used by:
88 * - CaptureCache
89 * @var string
91 protected $indexFilename = 'index.html';
93 /**
94 * Used by:
95 * - ObjectCache
96 * @var null|object
98 protected $object;
101 * Used by:
102 * - ObjectCache
103 * @var bool
105 protected $objectCacheMagicProperties = false;
108 * Used by:
109 * - ObjectCache
110 * @var array
112 protected $objectCacheMethods = array();
115 * Used by:
116 * - ObjectCache
117 * @var null|string
119 protected $objectKey;
122 * Used by:
123 * - ObjectCache
124 * @var array
126 protected $objectNonCacheMethods = array('__tostring');
129 * Used by:
130 * - CaptureCache
131 * @var null|string
133 protected $publicDir;
136 * Used by:
137 * - CallbackCache
138 * - ClassCache
139 * - ObjectCache
140 * - OutputCache
141 * @var null|Storage
143 protected $storage;
146 * Constructor
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
166 * Used by:
167 * - ClassCache
168 * - ObjectCache
170 * @param bool $cacheByDefault
171 * @return PatternOptions
173 public function setCacheByDefault($cacheByDefault)
175 $this->cacheByDefault = $cacheByDefault;
176 return $this;
180 * Do we cache by default?
182 * Used by:
183 * - ClassCache
184 * - ObjectCache
186 * @return bool
188 public function getCacheByDefault()
190 return $this->cacheByDefault;
194 * Set whether or not to cache output
196 * Used by:
197 * - CallbackCache
198 * - ClassCache
199 * - ObjectCache
201 * @param bool $cacheOutput
202 * @return PatternOptions
204 public function setCacheOutput($cacheOutput)
206 $this->cacheOutput = (bool) $cacheOutput;
207 return $this;
211 * Will we cache output?
213 * Used by:
214 * - CallbackCache
215 * - ClassCache
216 * - ObjectCache
218 * @return bool
220 public function getCacheOutput()
222 return $this->cacheOutput;
226 * Set class name
228 * Used by:
229 * - ClassCache
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;
241 return $this;
245 * Get class name
247 * Used by:
248 * - ClassCache
250 * @return null|string
252 public function getClass()
254 return $this->class;
258 * Set list of method return values to cache
260 * Used by:
261 * - ClassCache
263 * @param array $classCacheMethods
264 * @return PatternOptions
266 public function setClassCacheMethods(array $classCacheMethods)
268 $this->classCacheMethods = $this->recursiveStrtolower($classCacheMethods);
269 return $this;
273 * Get list of methods from which to cache return values
275 * Used by:
276 * - ClassCache
278 * @return array
280 public function getClassCacheMethods()
282 return $this->classCacheMethods;
286 * Set list of method return values NOT to cache
288 * Used by:
289 * - ClassCache
291 * @param array $classNonCacheMethods
292 * @return PatternOptions
294 public function setClassNonCacheMethods(array $classNonCacheMethods)
296 $this->classNonCacheMethods = $this->recursiveStrtolower($classNonCacheMethods);
297 return $this;
301 * Get list of methods from which NOT to cache return values
303 * Used by:
304 * - ClassCache
306 * @return array
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);
325 } else {
326 $dirPermission = (int) $dirPermission;
329 // validate
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;
338 return $this;
342 * Gets directory permission
344 * @return false|int
346 public function getDirPermission()
348 return $this->dirPermission;
352 * Set umask
354 * Used by:
355 * - CaptureCache
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);
366 } else {
367 $umask = (int) $umask;
370 // validate
371 if ($umask & 0700) {
372 throw new Exception\InvalidArgumentException(
373 'Invalid umask: need permission to execute, read and write by owner'
377 // normalize
378 $umask = $umask & 0777;
381 $this->umask = $umask;
382 return $this;
386 * Get umask
388 * Used by:
389 * - CaptureCache
391 * @return false|int
393 public function getUmask()
395 return $this->umask;
399 * Set whether or not file locking should be used
401 * Used by:
402 * - CaptureCache
404 * @param bool $fileLocking
405 * @return PatternOptions
407 public function setFileLocking($fileLocking)
409 $this->fileLocking = (bool) $fileLocking;
410 return $this;
414 * Is file locking enabled?
416 * Used by:
417 * - CaptureCache
419 * @return bool
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);
438 } else {
439 $filePermission = (int) $filePermission;
442 // validate
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;
455 return $this;
459 * Gets file permission
461 * @return false|int
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;
477 return $this;
481 * Get value for index filename
483 * @return string
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(sprintf(
501 '%s expects an object; received "%s"', __METHOD__, gettype($object)
504 $this->object = $object;
505 return $this;
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
521 * Used by:
522 * - ObjectCache
524 * @param bool $objectCacheMagicProperties
525 * @return PatternOptions
527 public function setObjectCacheMagicProperties($objectCacheMagicProperties)
529 $this->objectCacheMagicProperties = (bool) $objectCacheMagicProperties;
530 return $this;
534 * Should we cache magic properties?
536 * Used by:
537 * - ObjectCache
539 * @return bool
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);
556 return $this;
560 * Get list of object methods for which to cache return values
562 * @return array
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.
574 * Used by:
575 * - ObjectCache
577 * @param mixed $objectKey
578 * @return PatternOptions
580 public function setObjectKey($objectKey)
582 if ($objectKey !== null) {
583 $this->objectKey = (string) $objectKey;
584 } else {
585 $this->objectKey = null;
587 return $this;
591 * Get object key
593 * Used by:
594 * - ObjectCache
596 * @return mixed
598 public function getObjectKey()
600 if (!$this->objectKey) {
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);
616 return $this;
620 * Get list of object methods for which NOT to cache return values
622 * @return array
624 public function getObjectNonCacheMethods()
626 return $this->objectNonCacheMethods;
630 * Set location of public directory
632 * Used by:
633 * - CaptureCache
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);
658 return $this;
662 * Get location of public directory
664 * Used by:
665 * - CaptureCache
667 * @return null|string
669 public function getPublicDir()
671 return $this->publicDir;
675 * Set storage adapter
677 * Required for the following Pattern classes:
678 * - CallbackCache
679 * - ClassCache
680 * - ObjectCache
681 * - OutputCache
683 * @param string|array|Storage $storage
684 * @return PatternOptions
686 public function setStorage($storage)
688 $this->storage = $this->storageFactory($storage);
689 return $this;
693 * Get storage adapter
695 * Used by:
696 * - CallbackCache
697 * - ClassCache
698 * - ObjectCache
699 * - OutputCache
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
713 * @return 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
727 * @return array
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'"
739 return $methods;
743 * Create a storage object from a given specification
745 * @param array|string|Storage $storage
746 * @throws Exception\InvalidArgumentException
747 * @return Storage
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'
763 return $storage;