PageRenderTime 51ms CodeModel.GetById 19ms app.highlight 26ms RepoModel.GetById 1ms app.codeStats 0ms

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

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