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