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
10 namespace Zend\Cache\Storage\Adapter
;
13 use Zend\Cache\Exception
;
16 * These are options specific to the Filesystem adapter
18 class FilesystemOptions
extends AdapterOptions
22 * Directory to store cache files
24 * @var null|string The cache directory
25 * or NULL for the systems temporary directory
27 protected $cacheDir = null;
30 * Call clearstatcache enabled?
34 protected $clearStatCache = true;
37 * How much sub-directaries should be created?
41 protected $dirLevel = 1;
44 * Permission creating new directories
48 protected $dirPermission = 0700;
51 * Lock files on writing
55 protected $fileLocking = true;
58 * Permission creating new files
62 protected $filePermission = 0600;
65 * Overwrite default key pattern
67 * Defined in AdapterOptions
71 protected $keyPattern = '/^[a-z0-9_\+\-]*$/Di';
78 protected $namespaceSeparator = '-';
81 * Don't get 'fileatime' as 'atime' on metadata
85 protected $noAtime = true;
88 * Don't get 'filectime' as 'ctime' on metadata
92 protected $noCtime = true;
95 * Umask to create files and directories
99 protected $umask = false;
104 * @param array|Traversable|null $options
105 * @return FilesystemOptions
106 * @throws Exception\InvalidArgumentException
108 public function __construct($options = null)
110 // disable file/directory permissions by default on windows systems
111 if (strtoupper(substr(PHP_OS
, 0, 3)) == 'WIN') {
112 $this->filePermission
= false;
113 $this->dirPermission
= false;
116 parent
::__construct($options);
122 * @param string $cacheDir
123 * @return FilesystemOptions
124 * @throws Exception\InvalidArgumentException
126 public function setCacheDir($cacheDir)
128 if ($cacheDir !== null) {
129 if (!is_dir($cacheDir)) {
130 throw new Exception\
InvalidArgumentException(
131 "Cache directory '{$cacheDir}' not found or not a directory"
133 } elseif (!is_writable($cacheDir)) {
134 throw new Exception\
InvalidArgumentException(
135 "Cache directory '{$cacheDir}' not writable"
137 } elseif (!is_readable($cacheDir)) {
138 throw new Exception\
InvalidArgumentException(
139 "Cache directory '{$cacheDir}' not readable"
143 $cacheDir = rtrim(realpath($cacheDir), DIRECTORY_SEPARATOR
);
145 $cacheDir = sys_get_temp_dir();
148 $this->triggerOptionEvent('cache_dir', $cacheDir);
149 $this->cacheDir
= $cacheDir;
156 * @return null|string
158 public function getCacheDir()
160 if ($this->cacheDir
=== null) {
161 $this->setCacheDir(null);
164 return $this->cacheDir
;
168 * Set clear stat cache
170 * @param bool $clearStatCache
171 * @return FilesystemOptions
173 public function setClearStatCache($clearStatCache)
175 $clearStatCache = (bool) $clearStatCache;
176 $this->triggerOptionEvent('clear_stat_cache', $clearStatCache);
177 $this->clearStatCache
= $clearStatCache;
182 * Get clear stat cache
186 public function getClearStatCache()
188 return $this->clearStatCache
;
194 * @param int $dirLevel
195 * @return FilesystemOptions
196 * @throws Exception\InvalidArgumentException
198 public function setDirLevel($dirLevel)
200 $dirLevel = (int) $dirLevel;
201 if ($dirLevel < 0 ||
$dirLevel > 16) {
202 throw new Exception\
InvalidArgumentException(
203 "Directory level '{$dirLevel}' must be between 0 and 16"
206 $this->triggerOptionEvent('dir_level', $dirLevel);
207 $this->dirLevel
= $dirLevel;
216 public function getDirLevel()
218 return $this->dirLevel
;
222 * Set permission to create directories on unix systems
224 * @param false|string|int $dirPermission FALSE to disable explicit permission or an octal number
225 * @return FilesystemOptions
227 * @see setFilePermission
228 * @link http://php.net/manual/function.chmod.php
230 public function setDirPermission($dirPermission)
232 if ($dirPermission !== false) {
233 if (is_string($dirPermission)) {
234 $dirPermission = octdec($dirPermission);
236 $dirPermission = (int) $dirPermission;
240 if (($dirPermission & 0700) != 0700) {
241 throw new Exception\
InvalidArgumentException(
242 'Invalid directory permission: need permission to execute, read and write by owner'
247 if ($this->dirPermission
!== $dirPermission) {
248 $this->triggerOptionEvent('dir_permission', $dirPermission);
249 $this->dirPermission
= $dirPermission;
256 * Get permission to create directories on unix systems
260 public function getDirPermission()
262 return $this->dirPermission
;
268 * @param bool $fileLocking
269 * @return FilesystemOptions
271 public function setFileLocking($fileLocking)
273 $fileLocking = (bool) $fileLocking;
274 $this->triggerOptionEvent('file_locking', $fileLocking);
275 $this->fileLocking
= $fileLocking;
284 public function getFileLocking()
286 return $this->fileLocking
;
290 * Set permission to create files on unix systems
292 * @param false|string|int $filePermission FALSE to disable explicit permission or an octal number
293 * @return FilesystemOptions
295 * @see setDirPermission
296 * @link http://php.net/manual/function.chmod.php
298 public function setFilePermission($filePermission)
300 if ($filePermission !== false) {
301 if (is_string($filePermission)) {
302 $filePermission = octdec($filePermission);
304 $filePermission = (int) $filePermission;
308 if (($filePermission & 0600) != 0600) {
309 throw new Exception\
InvalidArgumentException(
310 'Invalid file permission: need permission to read and write by owner'
312 } elseif ($filePermission & 0111) {
313 throw new Exception\
InvalidArgumentException(
314 "Invalid file permission: Cache files shoudn't be executable"
319 if ($this->filePermission
!== $filePermission) {
320 $this->triggerOptionEvent('file_permission', $filePermission);
321 $this->filePermission
= $filePermission;
328 * Get permission to create files on unix systems
332 public function getFilePermission()
334 return $this->filePermission
;
338 * Set namespace separator
340 * @param string $namespaceSeparator
341 * @return FilesystemOptions
343 public function setNamespaceSeparator($namespaceSeparator)
345 $namespaceSeparator = (string) $namespaceSeparator;
346 $this->triggerOptionEvent('namespace_separator', $namespaceSeparator);
347 $this->namespaceSeparator
= $namespaceSeparator;
352 * Get namespace separator
356 public function getNamespaceSeparator()
358 return $this->namespaceSeparator
;
364 * @param bool $noAtime
365 * @return FilesystemOptions
367 public function setNoAtime($noAtime)
369 $noAtime = (bool) $noAtime;
370 $this->triggerOptionEvent('no_atime', $noAtime);
371 $this->noAtime
= $noAtime;
380 public function getNoAtime()
382 return $this->noAtime
;
388 * @param bool $noCtime
389 * @return FilesystemOptions
391 public function setNoCtime($noCtime)
393 $noCtime = (bool) $noCtime;
394 $this->triggerOptionEvent('no_ctime', $noCtime);
395 $this->noCtime
= $noCtime;
404 public function getNoCtime()
406 return $this->noCtime
;
410 * Set the umask to create files and directories on unix systems
412 * Note: On multithreaded webservers it's better to explicit set file and dir permission.
414 * @param false|string|int $umask FALSE to disable umask or an octal number
415 * @return FilesystemOptions
416 * @see setFilePermission
417 * @see setDirPermission
418 * @link http://php.net/manual/function.umask.php
419 * @link http://en.wikipedia.org/wiki/Umask
421 public function setUmask($umask)
423 if ($umask !== false) {
424 if (is_string($umask)) {
425 $umask = octdec($umask);
427 $umask = (int) $umask;
432 throw new Exception\
InvalidArgumentException(
433 'Invalid umask: need permission to execute, read and write by owner'
438 $umask = $umask & 0777;
441 if ($this->umask
!== $umask) {
442 $this->triggerOptionEvent('umask', $umask);
443 $this->umask
= $umask;
450 * Get the umask to create files and directories on unix systems
454 public function getUmask()