composer package updates
[openemr.git] / vendor / zendframework / zend-cache / src / Storage / Adapter / FilesystemOptions.php
blob68180c0aadb0faea442540a5bb796ee705ff6c4e
1 <?php
2 /**
3 * @see https://github.com/zendframework/zend-cache for the canonical source repository
4 * @copyright Copyright (c) 2005-2018 Zend Technologies USA Inc. (https://www.zend.com)
5 * @license https://github.com/zendframework/zend-cache/blob/master/LICENSE.md New BSD License
6 */
8 namespace Zend\Cache\Storage\Adapter;
10 use Traversable;
11 use Zend\Cache\Exception;
13 /**
14 * These are options specific to the Filesystem adapter
16 class FilesystemOptions extends AdapterOptions
18 /**
19 * Directory to store cache files
21 * @var null|string The cache directory
22 * or NULL for the systems temporary directory
24 protected $cacheDir = null;
26 /**
27 * Call clearstatcache enabled?
29 * @var bool
31 protected $clearStatCache = true;
33 /**
34 * How much sub-directaries should be created?
36 * @var int
38 protected $dirLevel = 1;
40 /**
41 * Permission creating new directories
43 * @var false|int
45 protected $dirPermission = 0700;
47 /**
48 * Lock files on writing
50 * @var bool
52 protected $fileLocking = true;
54 /**
55 * Permission creating new files
57 * @var false|int
59 protected $filePermission = 0600;
61 /**
62 * Overwrite default key pattern
64 * Defined in AdapterOptions
66 * @var string
68 protected $keyPattern = '/^[a-z0-9_\+\-]*$/Di';
70 /**
71 * Namespace separator
73 * @var string
75 protected $namespaceSeparator = '-';
77 /**
78 * Don't get 'fileatime' as 'atime' on metadata
80 * @var bool
82 protected $noAtime = true;
84 /**
85 * Don't get 'filectime' as 'ctime' on metadata
87 * @var bool
89 protected $noCtime = true;
91 /**
92 * Umask to create files and directories
94 * @var false|int
96 protected $umask = false;
98 /**
99 * Suffix for cache files
101 * @var string
103 protected $suffix = 'dat';
106 * Suffix for tag files
108 * @var string
110 protected $tagSuffix = 'tag';
113 * Constructor
115 * @param array|Traversable|null $options
116 * @return FilesystemOptions
117 * @throws Exception\InvalidArgumentException
119 public function __construct($options = null)
121 // disable file/directory permissions by default on windows systems
122 if (strtoupper(substr(PHP_OS, 0, 3)) == 'WIN') {
123 $this->filePermission = false;
124 $this->dirPermission = false;
127 parent::__construct($options);
131 * Set cache dir
133 * @param string $cacheDir
134 * @return FilesystemOptions Provides a fluent interface
135 * @throws Exception\InvalidArgumentException
137 public function setCacheDir($cacheDir)
139 if ($cacheDir !== null) {
140 if (! is_dir($cacheDir)) {
141 throw new Exception\InvalidArgumentException(
142 "Cache directory '{$cacheDir}' not found or not a directory"
144 } elseif (! is_writable($cacheDir)) {
145 throw new Exception\InvalidArgumentException(
146 "Cache directory '{$cacheDir}' not writable"
148 } elseif (! is_readable($cacheDir)) {
149 throw new Exception\InvalidArgumentException(
150 "Cache directory '{$cacheDir}' not readable"
154 $cacheDir = rtrim(realpath($cacheDir), DIRECTORY_SEPARATOR);
155 } else {
156 $cacheDir = sys_get_temp_dir();
159 $this->triggerOptionEvent('cache_dir', $cacheDir);
160 $this->cacheDir = $cacheDir;
161 return $this;
165 * Get cache dir
167 * @return null|string
169 public function getCacheDir()
171 if ($this->cacheDir === null) {
172 $this->setCacheDir(null);
175 return $this->cacheDir;
179 * Set clear stat cache
181 * @param bool $clearStatCache
182 * @return FilesystemOptions Provides a fluent interface
184 public function setClearStatCache($clearStatCache)
186 $clearStatCache = (bool) $clearStatCache;
187 $this->triggerOptionEvent('clear_stat_cache', $clearStatCache);
188 $this->clearStatCache = $clearStatCache;
189 return $this;
193 * Get clear stat cache
195 * @return bool
197 public function getClearStatCache()
199 return $this->clearStatCache;
203 * Set dir level
205 * @param int $dirLevel
206 * @return FilesystemOptions Provides a fluent interface
207 * @throws Exception\InvalidArgumentException
209 public function setDirLevel($dirLevel)
211 $dirLevel = (int) $dirLevel;
212 if ($dirLevel < 0 || $dirLevel > 16) {
213 throw new Exception\InvalidArgumentException(
214 "Directory level '{$dirLevel}' must be between 0 and 16"
217 $this->triggerOptionEvent('dir_level', $dirLevel);
218 $this->dirLevel = $dirLevel;
219 return $this;
223 * Get dir level
225 * @return int
227 public function getDirLevel()
229 return $this->dirLevel;
233 * Set permission to create directories on unix systems
235 * @param false|string|int $dirPermission FALSE to disable explicit permission or an octal number
236 * @return FilesystemOptions Provides a fluent interface
237 * @see setUmask
238 * @see setFilePermission
239 * @link http://php.net/manual/function.chmod.php
241 public function setDirPermission($dirPermission)
243 if ($dirPermission !== false) {
244 if (is_string($dirPermission)) {
245 $dirPermission = octdec($dirPermission);
246 } else {
247 $dirPermission = (int) $dirPermission;
250 // validate
251 if (($dirPermission & 0700) != 0700) {
252 throw new Exception\InvalidArgumentException(
253 'Invalid directory permission: need permission to execute, read and write by owner'
258 if ($this->dirPermission !== $dirPermission) {
259 $this->triggerOptionEvent('dir_permission', $dirPermission);
260 $this->dirPermission = $dirPermission;
263 return $this;
267 * Get permission to create directories on unix systems
269 * @return false|int
271 public function getDirPermission()
273 return $this->dirPermission;
277 * Set file locking
279 * @param bool $fileLocking
280 * @return FilesystemOptions Provides a fluent interface
282 public function setFileLocking($fileLocking)
284 $fileLocking = (bool) $fileLocking;
285 $this->triggerOptionEvent('file_locking', $fileLocking);
286 $this->fileLocking = $fileLocking;
287 return $this;
291 * Get file locking
293 * @return bool
295 public function getFileLocking()
297 return $this->fileLocking;
301 * Set permission to create files on unix systems
303 * @param false|string|int $filePermission FALSE to disable explicit permission or an octal number
304 * @return FilesystemOptions Provides a fluent interface
305 * @see setUmask
306 * @see setDirPermission
307 * @link http://php.net/manual/function.chmod.php
309 public function setFilePermission($filePermission)
311 if ($filePermission !== false) {
312 if (is_string($filePermission)) {
313 $filePermission = octdec($filePermission);
314 } else {
315 $filePermission = (int) $filePermission;
318 // validate
319 if (($filePermission & 0600) != 0600) {
320 throw new Exception\InvalidArgumentException(
321 'Invalid file permission: need permission to read and write by owner'
323 } elseif ($filePermission & 0111) {
324 throw new Exception\InvalidArgumentException(
325 "Invalid file permission: Cache files shoudn't be executable"
330 if ($this->filePermission !== $filePermission) {
331 $this->triggerOptionEvent('file_permission', $filePermission);
332 $this->filePermission = $filePermission;
335 return $this;
339 * Get permission to create files on unix systems
341 * @return false|int
343 public function getFilePermission()
345 return $this->filePermission;
349 * Set namespace separator
351 * @param string $namespaceSeparator
352 * @return FilesystemOptions Provides a fluent interface
354 public function setNamespaceSeparator($namespaceSeparator)
356 $namespaceSeparator = (string) $namespaceSeparator;
357 $this->triggerOptionEvent('namespace_separator', $namespaceSeparator);
358 $this->namespaceSeparator = $namespaceSeparator;
359 return $this;
363 * Get namespace separator
365 * @return string
367 public function getNamespaceSeparator()
369 return $this->namespaceSeparator;
373 * Set no atime
375 * @param bool $noAtime
376 * @return FilesystemOptions Provides a fluent interface
378 public function setNoAtime($noAtime)
380 $noAtime = (bool) $noAtime;
381 $this->triggerOptionEvent('no_atime', $noAtime);
382 $this->noAtime = $noAtime;
383 return $this;
387 * Get no atime
389 * @return bool
391 public function getNoAtime()
393 return $this->noAtime;
397 * Set no ctime
399 * @param bool $noCtime
400 * @return FilesystemOptions
402 public function setNoCtime($noCtime)
404 $noCtime = (bool) $noCtime;
405 $this->triggerOptionEvent('no_ctime', $noCtime);
406 $this->noCtime = $noCtime;
407 return $this;
411 * Get no ctime
413 * @return bool
415 public function getNoCtime()
417 return $this->noCtime;
421 * Set the umask to create files and directories on unix systems
423 * Note: On multithreaded webservers it's better to explicit set file and dir permission.
425 * @param false|string|int $umask FALSE to disable umask or an octal number
426 * @return FilesystemOptions
427 * @see setFilePermission
428 * @see setDirPermission
429 * @link http://php.net/manual/function.umask.php
430 * @link http://en.wikipedia.org/wiki/Umask
432 public function setUmask($umask)
434 if ($umask !== false) {
435 if (is_string($umask)) {
436 $umask = octdec($umask);
437 } else {
438 $umask = (int) $umask;
441 // validate
442 if ($umask & 0700) {
443 throw new Exception\InvalidArgumentException(
444 'Invalid umask: need permission to execute, read and write by owner'
448 // normalize
449 $umask = $umask & ~0002;
452 if ($this->umask !== $umask) {
453 $this->triggerOptionEvent('umask', $umask);
454 $this->umask = $umask;
457 return $this;
461 * Get the umask to create files and directories on unix systems
463 * @return false|int
465 public function getUmask()
467 return $this->umask;
471 * Get the suffix for cache files
473 * @return string
475 public function getSuffix()
477 return $this->suffix;
481 * Set the suffix for cache files
483 * @param string $suffix
485 public function setSuffix($suffix)
487 $this->suffix = $suffix;
488 return $this;
492 * Get the suffix for tag files
494 * @return the $tagSuffix
496 public function getTagSuffix()
498 return $this->tagSuffix;
502 * Set the suffix for cache files
504 * @param string $tagSuffix
506 public function setTagSuffix($tagSuffix)
508 $this->tagSuffix = $tagSuffix;
509 return $this;