/libraries/Zend/Cache/Pattern/PatternOptions.php

https://github.com/kiranatama/sagalaya · PHP · 763 lines · 461 code · 44 blank · 258 comment · 17 complexity · 84c72a6578b2c1ab43ab74cea6aa6e3d 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-2012 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\Pattern;
  11. use Zend\Cache\Exception;
  12. use Zend\Cache\StorageFactory;
  13. use Zend\Cache\Storage\StorageInterface as Storage;
  14. use Zend\Stdlib\AbstractOptions;
  15. /**
  16. * @category Zend
  17. * @package Zend_Cache
  18. * @subpackage Pattern
  19. */
  20. class PatternOptions extends AbstractOptions
  21. {
  22. /**
  23. * Used by:
  24. * - ClassCache
  25. * - ObjectCache
  26. * @var bool
  27. */
  28. protected $cacheByDefault = true;
  29. /**
  30. * Used by:
  31. * - CallbackCache
  32. * - ClassCache
  33. * - ObjectCache
  34. * @var bool
  35. */
  36. protected $cacheOutput = true;
  37. /**
  38. * Used by:
  39. * - ClassCache
  40. * @var null|string
  41. */
  42. protected $class;
  43. /**
  44. * Used by:
  45. * - ClassCache
  46. * @var array
  47. */
  48. protected $classCacheMethods = array();
  49. /**
  50. * Used by:
  51. * - ClassCache
  52. * @var array
  53. */
  54. protected $classNonCacheMethods = array();
  55. /**
  56. * Used by:
  57. * - CaptureCache
  58. * @var false|int
  59. */
  60. protected $umask = false;
  61. /**
  62. * Used by:
  63. * - CaptureCache
  64. * @var false|int
  65. */
  66. protected $dirPermission = 0700;
  67. /**
  68. * Used by:
  69. * - CaptureCache
  70. * @var false|int
  71. */
  72. protected $filePermission = 0600;
  73. /**
  74. * Used by:
  75. * - CaptureCache
  76. * @var bool
  77. */
  78. protected $fileLocking = true;
  79. /**
  80. * Used by:
  81. * - CaptureCache
  82. * @var string
  83. */
  84. protected $indexFilename = 'index.html';
  85. /**
  86. * Used by:
  87. * - ObjectCache
  88. * @var null|object
  89. */
  90. protected $object;
  91. /**
  92. * Used by:
  93. * - ObjectCache
  94. * @var bool
  95. */
  96. protected $objectCacheMagicProperties = false;
  97. /**
  98. * Used by:
  99. * - ObjectCache
  100. * @var array
  101. */
  102. protected $objectCacheMethods = array();
  103. /**
  104. * Used by:
  105. * - ObjectCache
  106. * @var null|string
  107. */
  108. protected $objectKey;
  109. /**
  110. * Used by:
  111. * - ObjectCache
  112. * @var array
  113. */
  114. protected $objectNonCacheMethods = array('__tostring');
  115. /**
  116. * Used by:
  117. * - CaptureCache
  118. * @var null|string
  119. */
  120. protected $publicDir;
  121. /**
  122. * Used by:
  123. * - CallbackCache
  124. * - ClassCache
  125. * - ObjectCache
  126. * - OutputCache
  127. * @var null|Storage
  128. */
  129. protected $storage;
  130. /**
  131. * Constructor
  132. *
  133. * @param array|Traversable|null $options
  134. * @return AbstractOptions
  135. * @throws Exception\InvalidArgumentException
  136. */
  137. public function __construct($options = null)
  138. {
  139. // disable file/directory permissions by default on windows systems
  140. if (strtoupper(substr(PHP_OS, 0, 3)) == 'WIN') {
  141. $this->filePermission = false;
  142. $this->dirPermission = false;
  143. }
  144. parent::__construct($options);
  145. }
  146. /**
  147. * Set flag indicating whether or not to cache by default
  148. *
  149. * Used by:
  150. * - ClassCache
  151. * - ObjectCache
  152. *
  153. * @param bool $cacheByDefault
  154. * @return PatternOptions
  155. */
  156. public function setCacheByDefault($cacheByDefault)
  157. {
  158. $this->cacheByDefault = $cacheByDefault;
  159. return $this;
  160. }
  161. /**
  162. * Do we cache by default?
  163. *
  164. * Used by:
  165. * - ClassCache
  166. * - ObjectCache
  167. *
  168. * @return bool
  169. */
  170. public function getCacheByDefault()
  171. {
  172. return $this->cacheByDefault;
  173. }
  174. /**
  175. * Set whether or not to cache output
  176. *
  177. * Used by:
  178. * - CallbackCache
  179. * - ClassCache
  180. * - ObjectCache
  181. *
  182. * @param bool $cacheOutput
  183. * @return PatternOptions
  184. */
  185. public function setCacheOutput($cacheOutput)
  186. {
  187. $this->cacheOutput = (bool) $cacheOutput;
  188. return $this;
  189. }
  190. /**
  191. * Will we cache output?
  192. *
  193. * Used by:
  194. * - CallbackCache
  195. * - ClassCache
  196. * - ObjectCache
  197. *
  198. * @return bool
  199. */
  200. public function getCacheOutput()
  201. {
  202. return $this->cacheOutput;
  203. }
  204. /**
  205. * Set class name
  206. *
  207. * Used by:
  208. * - ClassCache
  209. *
  210. * @param string $class
  211. * @return PatternOptions
  212. */
  213. public function setClass($class)
  214. {
  215. if (!is_string($class)) {
  216. throw new Exception\InvalidArgumentException('Invalid classname provided; must be a string');
  217. }
  218. $this->class = $class;
  219. return $this;
  220. }
  221. /**
  222. * Get class name
  223. *
  224. * Used by:
  225. * - ClassCache
  226. *
  227. * @return null|string
  228. */
  229. public function getClass()
  230. {
  231. return $this->class;
  232. }
  233. /**
  234. * Set list of method return values to cache
  235. *
  236. * Used by:
  237. * - ClassCache
  238. *
  239. * @param array $classCacheMethods
  240. * @return PatternOptions
  241. */
  242. public function setClassCacheMethods(array $classCacheMethods)
  243. {
  244. $this->classCacheMethods = $this->recursiveStrtolower($classCacheMethods);
  245. return $this;
  246. }
  247. /**
  248. * Get list of methods from which to cache return values
  249. *
  250. * Used by:
  251. * - ClassCache
  252. *
  253. * @return array
  254. */
  255. public function getClassCacheMethods()
  256. {
  257. return $this->classCacheMethods;
  258. }
  259. /**
  260. * Set list of method return values NOT to cache
  261. *
  262. * Used by:
  263. * - ClassCache
  264. *
  265. * @param array $classNonCacheMethods
  266. * @return PatternOptions
  267. */
  268. public function setClassNonCacheMethods(array $classNonCacheMethods)
  269. {
  270. $this->classNonCacheMethods = $this->recursiveStrtolower($classNonCacheMethods);
  271. return $this;
  272. }
  273. /**
  274. * Get list of methods from which NOT to cache return values
  275. *
  276. * Used by:
  277. * - ClassCache
  278. *
  279. * @return array
  280. */
  281. public function getClassNonCacheMethods()
  282. {
  283. return $this->classNonCacheMethods;
  284. }
  285. /**
  286. * Set directory permission
  287. *
  288. * @param false|int $dirPermission
  289. * @return PatternOptions
  290. */
  291. public function setDirPermission($dirPermission)
  292. {
  293. if ($dirPermission !== false) {
  294. if (is_string($dirPermission)) {
  295. $dirPermission = octdec($dirPermission);
  296. } else {
  297. $dirPermission = (int) $dirPermission;
  298. }
  299. // validate
  300. if (($dirPermission & 0700) != 0700) {
  301. throw new Exception\InvalidArgumentException(
  302. 'Invalid directory permission: need permission to execute, read and write by owner'
  303. );
  304. }
  305. }
  306. $this->dirPermission = $dirPermission;
  307. return $this;
  308. }
  309. /**
  310. * Gets directory permission
  311. *
  312. * @return false|int
  313. */
  314. public function getDirPermission()
  315. {
  316. return $this->dirPermission;
  317. }
  318. /**
  319. * Set umask
  320. *
  321. * Used by:
  322. * - CaptureCache
  323. *
  324. * @param false|int $umask
  325. * @return PatternOptions
  326. */
  327. public function setUmask($umask)
  328. {
  329. if ($umask !== false) {
  330. if (is_string($umask)) {
  331. $umask = octdec($umask);
  332. } else {
  333. $umask = (int) $umask;
  334. }
  335. // validate
  336. if ($umask & 0700) {
  337. throw new Exception\InvalidArgumentException(
  338. 'Invalid umask: need permission to execute, read and write by owner'
  339. );
  340. }
  341. // normalize
  342. $umask = $umask & 0777;
  343. }
  344. $this->umask = $umask;
  345. return $this;
  346. }
  347. /**
  348. * Get umask
  349. *
  350. * Used by:
  351. * - CaptureCache
  352. *
  353. * @return false|int
  354. */
  355. public function getUmask()
  356. {
  357. return $this->umask;
  358. }
  359. /**
  360. * Set whether or not file locking should be used
  361. *
  362. * Used by:
  363. * - CaptureCache
  364. *
  365. * @param bool $fileLocking
  366. * @return PatternOptions
  367. */
  368. public function setFileLocking($fileLocking)
  369. {
  370. $this->fileLocking = (bool) $fileLocking;
  371. return $this;
  372. }
  373. /**
  374. * Is file locking enabled?
  375. *
  376. * Used by:
  377. * - CaptureCache
  378. *
  379. * @return bool
  380. */
  381. public function getFileLocking()
  382. {
  383. return $this->fileLocking;
  384. }
  385. /**
  386. * Set file permission
  387. *
  388. * @param false|int $filePermission
  389. * @return PatternOptions
  390. */
  391. public function setFilePermission($filePermission)
  392. {
  393. if ($filePermission !== false) {
  394. if (is_string($filePermission)) {
  395. $filePermission = octdec($filePermission);
  396. } else {
  397. $filePermission = (int) $filePermission;
  398. }
  399. // validate
  400. if (($filePermission & 0600) != 0600) {
  401. throw new Exception\InvalidArgumentException(
  402. 'Invalid file permission: need permission to read and write by owner'
  403. );
  404. } elseif ($filePermission & 0111) {
  405. throw new Exception\InvalidArgumentException(
  406. "Invalid file permission: Files shoudn't be executable"
  407. );
  408. }
  409. }
  410. $this->filePermission = $filePermission;
  411. return $this;
  412. }
  413. /**
  414. * Gets file permission
  415. *
  416. * @return false|int
  417. */
  418. public function getFilePermission()
  419. {
  420. return $this->filePermission;
  421. }
  422. /**
  423. * Set value for index filename
  424. *
  425. * @param string $indexFilename
  426. * @return PatternOptions
  427. */
  428. public function setIndexFilename($indexFilename)
  429. {
  430. $this->indexFilename = (string) $indexFilename;
  431. return $this;
  432. }
  433. /**
  434. * Get value for index filename
  435. *
  436. * @return string
  437. */
  438. public function getIndexFilename()
  439. {
  440. return $this->indexFilename;
  441. }
  442. /**
  443. * Set object to cache
  444. *
  445. * @param mixed $value
  446. * @return $this
  447. */
  448. public function setObject($object)
  449. {
  450. if (!is_object($object)) {
  451. throw new Exception\InvalidArgumentException(sprintf(
  452. '%s expects an object; received "%s"', __METHOD__, gettype($object)
  453. ));
  454. }
  455. $this->object = $object;
  456. return $this;
  457. }
  458. /**
  459. * Get object to cache
  460. *
  461. * @return null|object
  462. */
  463. public function getObject()
  464. {
  465. return $this->object;
  466. }
  467. /**
  468. * Set flag indicating whether or not to cache magic properties
  469. *
  470. * Used by:
  471. * - ObjectCache
  472. *
  473. * @param bool $objectCacheMagicProperties
  474. * @return PatternOptions
  475. */
  476. public function setObjectCacheMagicProperties($objectCacheMagicProperties)
  477. {
  478. $this->objectCacheMagicProperties = (bool) $objectCacheMagicProperties;
  479. return $this;
  480. }
  481. /**
  482. * Should we cache magic properties?
  483. *
  484. * Used by:
  485. * - ObjectCache
  486. *
  487. * @return bool
  488. */
  489. public function getObjectCacheMagicProperties()
  490. {
  491. return $this->objectCacheMagicProperties;
  492. }
  493. /**
  494. * Set list of object methods for which to cache return values
  495. *
  496. * @param array $objectCacheMethods
  497. * @return PatternOptions
  498. * @throws Exception\InvalidArgumentException
  499. */
  500. public function setObjectCacheMethods(array $objectCacheMethods)
  501. {
  502. $this->objectCacheMethods = $this->normalizeObjectMethods($objectCacheMethods);
  503. return $this;
  504. }
  505. /**
  506. * Get list of object methods for which to cache return values
  507. *
  508. * @return array
  509. */
  510. public function getObjectCacheMethods()
  511. {
  512. return $this->objectCacheMethods;
  513. }
  514. /**
  515. * Set the object key part.
  516. *
  517. * Used to generate a callback key in order to speed up key generation.
  518. *
  519. * Used by:
  520. * - ObjectCache
  521. *
  522. * @param mixed $value
  523. * @return $this
  524. */
  525. public function setObjectKey($objectKey)
  526. {
  527. if ($objectKey !== null) {
  528. $this->objectKey = (string) $objectKey;
  529. } else {
  530. $this->objectKey = null;
  531. }
  532. return $this;
  533. }
  534. /**
  535. * Get object key
  536. *
  537. * Used by:
  538. * - ObjectCache
  539. *
  540. * @return mixed
  541. */
  542. public function getObjectKey()
  543. {
  544. if (!$this->objectKey) {
  545. return get_class($this->getObject());
  546. }
  547. return $this->objectKey;
  548. }
  549. /**
  550. * Set list of object methods for which NOT to cache return values
  551. *
  552. * @param array $objectNonCacheMethods
  553. * @return PatternOptions
  554. * @throws Exception\InvalidArgumentException
  555. */
  556. public function setObjectNonCacheMethods(array $objectNonCacheMethods)
  557. {
  558. $this->objectNonCacheMethods = $this->normalizeObjectMethods($objectNonCacheMethods);
  559. return $this;
  560. }
  561. /**
  562. * Get list of object methods for which NOT to cache return values
  563. *
  564. * @return array
  565. */
  566. public function getObjectNonCacheMethods()
  567. {
  568. return $this->objectNonCacheMethods;
  569. }
  570. /**
  571. * Set location of public directory
  572. *
  573. * Used by:
  574. * - CaptureCache
  575. *
  576. * @param string $publicDir
  577. * @return PatternOptions
  578. */
  579. public function setPublicDir($publicDir)
  580. {
  581. $publicDir = (string) $publicDir;
  582. if (!is_dir($publicDir)) {
  583. throw new Exception\InvalidArgumentException(
  584. "Public directory '{$publicDir}' not found or not a directoy"
  585. );
  586. } elseif (!is_writable($publicDir)) {
  587. throw new Exception\InvalidArgumentException(
  588. "Public directory '{$publicDir}' not writable"
  589. );
  590. } elseif (!is_readable($publicDir)) {
  591. throw new Exception\InvalidArgumentException(
  592. "Public directory '{$publicDir}' not readable"
  593. );
  594. }
  595. $this->publicDir = rtrim(realpath($publicDir), \DIRECTORY_SEPARATOR);
  596. return $this;
  597. }
  598. /**
  599. * Get location of public directory
  600. *
  601. * Used by:
  602. * - CaptureCache
  603. *
  604. * @return null|string
  605. */
  606. public function getPublicDir()
  607. {
  608. return $this->publicDir;
  609. }
  610. /**
  611. * Set storage adapter
  612. *
  613. * Required for the following Pattern classes:
  614. * - CallbackCache
  615. * - ClassCache
  616. * - ObjectCache
  617. * - OutputCache
  618. *
  619. * @param string|array|Storage $storage
  620. * @return PatternOptions
  621. */
  622. public function setStorage($storage)
  623. {
  624. $this->storage = $this->storageFactory($storage);
  625. return $this;
  626. }
  627. /**
  628. * Get storage adapter
  629. *
  630. * Used by:
  631. * - CallbackCache
  632. * - ClassCache
  633. * - ObjectCache
  634. * - OutputCache
  635. *
  636. * @return null|Storage
  637. */
  638. public function getStorage()
  639. {
  640. return $this->storage;
  641. }
  642. /**
  643. * Recursively apply strtolower on all values of an array, and return as a
  644. * list of unique values
  645. *
  646. * @param array $array
  647. * @return array
  648. */
  649. protected function recursiveStrtolower(array $array)
  650. {
  651. return array_values(array_unique(array_map('strtolower', $array)));
  652. }
  653. /**
  654. * Normalize object methods
  655. *
  656. * Recursively casts values to lowercase, then determines if any are in a
  657. * list of methods not handled, raising an exception if so.
  658. *
  659. * @param array $methods
  660. * @return array
  661. * @throws Exception\InvalidArgumentException
  662. */
  663. protected function normalizeObjectMethods(array $methods)
  664. {
  665. $methods = $this->recursiveStrtolower($methods);
  666. $intersect = array_intersect(array('__set', '__get', '__unset', '__isset'), $methods);
  667. if (!empty($intersect)) {
  668. throw new Exception\InvalidArgumentException(
  669. "Magic properties are handled by option 'cache_magic_properties'"
  670. );
  671. }
  672. return $methods;
  673. }
  674. /**
  675. * Create a storage object from a given specification
  676. *
  677. * @param array|string|Storage $storage
  678. * @return StorageAdapter
  679. */
  680. protected function storageFactory($storage)
  681. {
  682. if (is_array($storage)) {
  683. $storage = StorageFactory::factory($storage);
  684. } elseif (is_string($storage)) {
  685. $storage = StorageFactory::adapterFactory($storage);
  686. } elseif ( !($storage instanceof Storage) ) {
  687. throw new Exception\InvalidArgumentException(
  688. 'The storage must be an instanceof Zend\Cache\Storage\StorageInterface '
  689. . 'or an array passed to Zend\Cache\Storage::factory '
  690. . 'or simply the name of the storage adapter'
  691. );
  692. }
  693. return $storage;
  694. }
  695. }