/src/main/php/PHP/Depend/Util/Cache/Driver/File/Directory.php

https://github.com/Proudio-Interactive/pdepend · PHP · 239 lines · 78 code · 15 blank · 146 comment · 8 complexity · d904f68f6ae3c1db4d22354fd69154a0 MD5 · raw file

  1. <?php
  2. /**
  3. * This file is part of PHP_Depend.
  4. *
  5. * PHP Version 5
  6. *
  7. * Copyright (c) 2008-2011, Manuel Pichler <mapi@pdepend.org>.
  8. * All rights reserved.
  9. *
  10. * Redistribution and use in source and binary forms, with or without
  11. * modification, are permitted provided that the following conditions
  12. * are met:
  13. *
  14. * * Redistributions of source code must retain the above copyright
  15. * notice, this list of conditions and the following disclaimer.
  16. *
  17. * * Redistributions in binary form must reproduce the above copyright
  18. * notice, this list of conditions and the following disclaimer in
  19. * the documentation and/or other materials provided with the
  20. * distribution.
  21. *
  22. * * Neither the name of Manuel Pichler nor the names of his
  23. * contributors may be used to endorse or promote products derived
  24. * from this software without specific prior written permission.
  25. *
  26. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  27. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  28. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  29. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  30. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  31. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  32. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  33. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  34. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  35. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  36. * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  37. * POSSIBILITY OF SUCH DAMAGE.
  38. *
  39. * @category QualityAssurance
  40. * @package PHP_Depend
  41. * @subpackage Util_Cache_Driver_File
  42. * @author Manuel Pichler <mapi@pdepend.org>
  43. * @copyright 2008-2011 Manuel Pichler. All rights reserved.
  44. * @license http://www.opensource.org/licenses/bsd-license.php BSD License
  45. * @version SVN: $Id$
  46. * @link http://pdepend.org/
  47. * @since 0.10.0
  48. */
  49. /**
  50. * Directory helper for the file system based cache implementation.
  51. *
  52. * @category QualityAssurance
  53. * @package PHP_Depend
  54. * @subpackage Util_Cache_Driver_File
  55. * @author Manuel Pichler <mapi@pdepend.org>
  56. * @copyright 2008-2011 Manuel Pichler. All rights reserved.
  57. * @license http://www.opensource.org/licenses/bsd-license.php BSD License
  58. * @version Release: @package_version@
  59. * @link http://pdepend.org/
  60. * @since 0.10.0
  61. */
  62. class PHP_Depend_Util_Cache_Driver_File_Directory
  63. {
  64. /**
  65. * The current cache version/hash number.
  66. */
  67. const VERSION = PHP_Depend_Util_Cache_Driver::VERSION;
  68. /**
  69. * The cache root directory.
  70. *
  71. * @var string
  72. */
  73. protected $cacheDir = null;
  74. /**
  75. * Constructs a new cache directory helper instance.
  76. *
  77. * @param string $cacheDir The cache root directory.
  78. */
  79. public function __construct($cacheDir)
  80. {
  81. $this->cacheDir = $this->ensureExists($cacheDir);
  82. if (false === $this->isValidVersion()) {
  83. $this->flush();
  84. }
  85. }
  86. /**
  87. * Creates a cache directory for the given cache entry key and returns the
  88. * full qualified path for that cache directory.
  89. *
  90. * @param string $key The cache for an entry.
  91. *
  92. * @return string
  93. */
  94. public function createCacheDirectory($key)
  95. {
  96. return $this->createOrReturnCacheDirectory($key);
  97. }
  98. /**
  99. * Returns the full qualified path for an existing cache directory or
  100. * creates a new cache directory for the given cache entry key and returns
  101. * the full qualified path for that cache directory.
  102. *
  103. * @param string $key The cache for an entry.
  104. *
  105. * @return string
  106. */
  107. protected function createOrReturnCacheDirectory($key)
  108. {
  109. $path = $this->getCacheDir() . '/' . substr($key, 0, 2);
  110. if (false === file_exists($path)) {
  111. mkdir($path, 0775, true);
  112. }
  113. return $path;
  114. }
  115. /**
  116. * Ensures that the given <b>$cacheDir</b> really exists.
  117. *
  118. * @param string $cacheDir The cache root directory.
  119. *
  120. * @return string
  121. */
  122. protected function ensureExists($cacheDir)
  123. {
  124. if (false === file_exists($cacheDir)) {
  125. mkdir($cacheDir, 0775, true);
  126. }
  127. return $cacheDir;
  128. }
  129. /**
  130. * Tests if the current software cache version is similar to the stored
  131. * file system cache version.
  132. *
  133. * @return boolean
  134. */
  135. protected function isValidVersion()
  136. {
  137. return (self::VERSION === $this->readVersion());
  138. }
  139. /**
  140. * Reads the stored cache version number from the cache root directory.
  141. *
  142. * @return string
  143. */
  144. protected function readVersion()
  145. {
  146. if (file_exists($this->getVersionFile())) {
  147. return trim(file_get_contents($this->getVersionFile()));
  148. }
  149. return null;
  150. }
  151. /**
  152. * Writes the current software cache version into a file in the cache root
  153. * directory.
  154. *
  155. * @return void
  156. */
  157. protected function writeVersion()
  158. {
  159. file_put_contents($this->getVersionFile(), self::VERSION);
  160. }
  161. /**
  162. * Returns the file name for the used version file.
  163. *
  164. * @return string
  165. */
  166. protected function getVersionFile()
  167. {
  168. return $this->getCacheDir() . '/_version';
  169. }
  170. /**
  171. * Returns the cache root directory.
  172. *
  173. * @return string
  174. */
  175. protected function getCacheDir()
  176. {
  177. return $this->cacheDir;
  178. }
  179. /**
  180. * Flushes all contents below the configured cache root directory and writes
  181. * a version file with the current software version.
  182. *
  183. * @return void
  184. */
  185. protected function flush()
  186. {
  187. $this->flushDirectory($this->getCacheDir());
  188. $this->writeVersion();
  189. }
  190. /**
  191. * Deletes all files and directories below the given <b>$cacheDir</b>.
  192. *
  193. * @param string $cacheDir A cache directory.
  194. *
  195. * @return void
  196. */
  197. protected function flushDirectory($cacheDir)
  198. {
  199. foreach (new DirectoryIterator($cacheDir) as $child) {
  200. $this->flushEntry($child);
  201. }
  202. }
  203. /**
  204. * Flushes the cache record for the given file info instance, independent if
  205. * it is a file, directory or symlink.
  206. *
  207. * @param SplFileInfo $file File info object that represents an entity
  208. * within the cache's file system.
  209. *
  210. * @return void
  211. */
  212. protected function flushEntry(SplFileInfo $file)
  213. {
  214. $path = $file->getRealPath();
  215. if ($file->isDot()) {
  216. return;
  217. } else if ($file->isFile()) {
  218. unlink($path);
  219. } else {
  220. $this->flushDirectory($path);
  221. rmdir($path);
  222. }
  223. }
  224. }