PageRenderTime 24ms CodeModel.GetById 14ms app.highlight 7ms RepoModel.GetById 1ms app.codeStats 0ms

/library/Zend/Cache/Storage/Adapter/FilesystemOptions.php

https://bitbucket.org/dagnet_d/zf2
PHP | 463 lines | 265 code | 39 blank | 159 comment | 24 complexity | 631ab18c88143879c0f5a72dcf9605d7 MD5 | raw file
  1<?php
  2/**
  3 * Zend Framework (http://framework.zend.com/)
  4 *
  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 * @package   Zend_Cache
  9 */
 10
 11namespace Zend\Cache\Storage\Adapter;
 12
 13use Traversable;
 14use Zend\Cache\Exception;
 15
 16/**
 17 * These are options specific to the Filesystem adapter
 18 *
 19 * @category   Zend
 20 * @package    Zend_Cache
 21 * @subpackage Storage
 22 */
 23class FilesystemOptions extends AdapterOptions
 24{
 25
 26    /**
 27     * Directory to store cache files
 28     *
 29     * @var null|string The cache directory
 30     *                  or NULL for the systems temporary directory
 31     */
 32    protected $cacheDir = null;
 33
 34    /**
 35     * Call clearstatcache enabled?
 36     *
 37     * @var bool
 38     */
 39    protected $clearStatCache = true;
 40
 41    /**
 42     * How much sub-directaries should be created?
 43     *
 44     * @var int
 45     */
 46    protected $dirLevel = 1;
 47
 48    /**
 49     * Permission creating new directories
 50     *
 51     * @var false|int
 52     */
 53    protected $dirPermission = 0700;
 54
 55    /**
 56     * Lock files on writing
 57     *
 58     * @var bool
 59     */
 60    protected $fileLocking = true;
 61
 62    /**
 63     * Permission creating new files
 64     *
 65     * @var false|int
 66     */
 67    protected $filePermission = 0600;
 68
 69    /**
 70     * Overwrite default key pattern
 71     *
 72     * Defined in AdapterOptions
 73     *
 74     * @var string
 75     */
 76    protected $keyPattern = '/^[a-z0-9_\+\-]*$/Di';
 77
 78    /**
 79     * Namespace separator
 80     *
 81     * @var string
 82     */
 83    protected $namespaceSeparator = '-';
 84
 85    /**
 86     * Don't get 'fileatime' as 'atime' on metadata
 87     *
 88     * @var bool
 89     */
 90    protected $noAtime = true;
 91
 92    /**
 93     * Don't get 'filectime' as 'ctime' on metadata
 94     *
 95     * @var bool
 96     */
 97    protected $noCtime = true;
 98
 99    /**
100     * Umask to create files and directories
101     *
102     * @var false|int
103     */
104    protected $umask = false;
105
106    /**
107     * Constructor
108     *
109     * @param  array|Traversable|null $options
110     * @return FilesystemOptions
111     * @throws Exception\InvalidArgumentException
112     */
113    public function __construct($options = null)
114    {
115        // disable file/directory permissions by default on windows systems
116        if (strtoupper(substr(PHP_OS, 0, 3)) == 'WIN') {
117            $this->filePermission = false;
118            $this->dirPermission = false;
119        }
120
121        parent::__construct($options);
122    }
123
124    /**
125     * Set cache dir
126     *
127     * @param  string $cacheDir
128     * @return FilesystemOptions
129     * @throws Exception\InvalidArgumentException
130     */
131    public function setCacheDir($cacheDir)
132    {
133        if ($cacheDir !== null) {
134            if (!is_dir($cacheDir)) {
135                throw new Exception\InvalidArgumentException(
136                    "Cache directory '{$cacheDir}' not found or not a directory"
137                );
138            } elseif (!is_writable($cacheDir)) {
139                throw new Exception\InvalidArgumentException(
140                    "Cache directory '{$cacheDir}' not writable"
141                );
142            } elseif (!is_readable($cacheDir)) {
143                throw new Exception\InvalidArgumentException(
144                    "Cache directory '{$cacheDir}' not readable"
145                );
146            }
147
148            $cacheDir = rtrim(realpath($cacheDir), \DIRECTORY_SEPARATOR);
149        } else {
150            $cacheDir = sys_get_temp_dir();
151        }
152
153        $this->triggerOptionEvent('cache_dir', $cacheDir);
154        $this->cacheDir = $cacheDir;
155        return $this;
156    }
157
158    /**
159     * Get cache dir
160     *
161     * @return null|string
162     */
163    public function getCacheDir()
164    {
165        if ($this->cacheDir === null) {
166            $this->setCacheDir(null);
167        }
168
169        return $this->cacheDir;
170    }
171
172    /**
173     * Set clear stat cache
174     *
175     * @param  bool $clearStatCache
176     * @return FilesystemOptions
177     */
178    public function setClearStatCache($clearStatCache)
179    {
180        $clearStatCache = (bool) $clearStatCache;
181        $this->triggerOptionEvent('clear_stat_cache', $clearStatCache);
182        $this->clearStatCache = $clearStatCache;
183        return $this;
184    }
185
186    /**
187     * Get clear stat cache
188     *
189     * @return bool
190     */
191    public function getClearStatCache()
192    {
193        return $this->clearStatCache;
194    }
195
196    /**
197     * Set dir level
198     *
199     * @param  int $dirLevel
200     * @return FilesystemOptions
201     * @throws Exception\InvalidArgumentException
202     */
203    public function setDirLevel($dirLevel)
204    {
205        $dirLevel = (int) $dirLevel;
206        if ($dirLevel < 0 || $dirLevel > 16) {
207            throw new Exception\InvalidArgumentException(
208                "Directory level '{$dirLevel}' must be between 0 and 16"
209            );
210        }
211        $this->triggerOptionEvent('dir_level', $dirLevel);
212        $this->dirLevel = $dirLevel;
213        return $this;
214    }
215
216    /**
217     * Get dir level
218     *
219     * @return int
220     */
221    public function getDirLevel()
222    {
223        return $this->dirLevel;
224    }
225
226    /**
227     * Set permission to create directories on unix systems
228     *
229     * @param false|string|int $dirPermission FALSE to disable explicit permission or an octal number
230     * @return FilesystemOptions
231     * @see setUmask
232     * @see setFilePermission
233     * @link http://php.net/manual/function.chmod.php
234     */
235    public function setDirPermission($dirPermission)
236    {
237        if ($dirPermission !== false) {
238            if (is_string($dirPermission)) {
239                $dirPermission = octdec($dirPermission);
240            } else {
241                $dirPermission = (int) $dirPermission;
242            }
243
244            // validate
245            if (($dirPermission & 0700) != 0700) {
246                throw new Exception\InvalidArgumentException(
247                    'Invalid directory permission: need permission to execute, read and write by owner'
248                );
249            }
250        }
251
252        if ($this->dirPermission !== $dirPermission) {
253            $this->triggerOptionEvent('dir_permission', $dirPermission);
254            $this->dirPermission = $dirPermission;
255        }
256
257        return $this;
258    }
259
260    /**
261     * Get permission to create directories on unix systems
262     *
263     * @return false|int
264     */
265    public function getDirPermission()
266    {
267        return $this->dirPermission;
268    }
269
270    /**
271     * Set file locking
272     *
273     * @param  bool $fileLocking
274     * @return FilesystemOptions
275     */
276    public function setFileLocking($fileLocking)
277    {
278        $fileLocking = (bool) $fileLocking;
279        $this->triggerOptionEvent('file_locking', $fileLocking);
280        $this->fileLocking = $fileLocking;
281        return $this;
282    }
283
284    /**
285     * Get file locking
286     *
287     * @return bool
288     */
289    public function getFileLocking()
290    {
291        return $this->fileLocking;
292    }
293
294    /**
295     * Set permission to create files on unix systems
296     *
297     * @param false|string|int $filePermission FALSE to disable explicit permission or an octal number
298     * @return FilesystemOptions
299     * @see setUmask
300     * @see setDirPermission
301     * @link http://php.net/manual/function.chmod.php
302     */
303    public function setFilePermission($filePermission)
304    {
305        if ($filePermission !== false) {
306            if (is_string($filePermission)) {
307                $filePermission = octdec($filePermission);
308            } else {
309                $filePermission = (int) $filePermission;
310            }
311
312            // validate
313            if (($filePermission & 0600) != 0600) {
314                throw new Exception\InvalidArgumentException(
315                    'Invalid file permission: need permission to read and write by owner'
316                );
317            } elseif ($filePermission & 0111) {
318                throw new Exception\InvalidArgumentException(
319                    "Invalid file permission: Cache files shoudn't be executable"
320                );
321            }
322        }
323
324        if ($this->filePermission !== $filePermission) {
325            $this->triggerOptionEvent('file_permission', $filePermission);
326            $this->filePermission = $filePermission;
327        }
328
329        return $this;
330    }
331
332    /**
333     * Get permission to create files on unix systems
334     *
335     * @return false|int
336     */
337    public function getFilePermission()
338    {
339        return $this->filePermission;
340    }
341
342    /**
343     * Set namespace separator
344     *
345     * @param  string $namespaceSeparator
346     * @return FilesystemOptions
347     */
348    public function setNamespaceSeparator($namespaceSeparator)
349    {
350        $namespaceSeparator = (string) $namespaceSeparator;
351        $this->triggerOptionEvent('namespace_separator', $namespaceSeparator);
352        $this->namespaceSeparator = $namespaceSeparator;
353        return $this;
354    }
355
356    /**
357     * Get namespace separator
358     *
359     * @return string
360     */
361    public function getNamespaceSeparator()
362    {
363        return $this->namespaceSeparator;
364    }
365
366    /**
367     * Set no atime
368     *
369     * @param  bool $noAtime
370     * @return FilesystemOptions
371     */
372    public function setNoAtime($noAtime)
373    {
374        $noAtime = (bool) $noAtime;
375        $this->triggerOptionEvent('no_atime', $noAtime);
376        $this->noAtime = $noAtime;
377        return $this;
378    }
379
380    /**
381     * Get no atime
382     *
383     * @return bool
384     */
385    public function getNoAtime()
386    {
387        return $this->noAtime;
388    }
389
390    /**
391     * Set no ctime
392     *
393     * @param  bool $noCtime
394     * @return FilesystemOptions
395     */
396    public function setNoCtime($noCtime)
397    {
398        $noCtime = (bool) $noCtime;
399        $this->triggerOptionEvent('no_ctime', $noCtime);
400        $this->noCtime = $noCtime;
401        return $this;
402    }
403
404    /**
405     * Get no ctime
406     *
407     * @return bool
408     */
409    public function getNoCtime()
410    {
411        return $this->noCtime;
412    }
413
414    /**
415     * Set the umask to create files and directories on unix systems
416     *
417     * Note: On multithreaded webservers it's better to explicit set file and dir permission.
418     *
419     * @param false|string|int $umask FALSE to disable umask or an octal number
420     * @return FilesystemOptions
421     * @see setFilePermission
422     * @see setDirPermission
423     * @link http://php.net/manual/function.umask.php
424     * @link http://en.wikipedia.org/wiki/Umask
425     */
426    public function setUmask($umask)
427    {
428        if ($umask !== false) {
429            if (is_string($umask)) {
430                $umask = octdec($umask);
431            } else {
432                $umask = (int) $umask;
433            }
434
435            // validate
436            if ($umask & 0700) {
437                throw new Exception\InvalidArgumentException(
438                    'Invalid umask: need permission to execute, read and write by owner'
439                );
440            }
441
442            // normalize
443            $umask = $umask & 0777;
444        }
445
446        if ($this->umask !== $umask) {
447            $this->triggerOptionEvent('umask', $umask);
448            $this->umask = $umask;
449        }
450
451        return $this;
452    }
453
454    /**
455     * Get the umask to create files and directories on unix systems
456     *
457     * @return false|int
458     */
459    public function getUmask()
460    {
461        return $this->umask;
462    }
463}