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