4 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
5 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
6 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
7 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
8 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
9 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
10 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
11 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
12 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
13 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
14 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
16 * This software consists of voluntary contributions made by many individuals
17 * and is licensed under the MIT license. For more information, see
18 * <http://www.doctrine-project.org>.
21 namespace Doctrine\Common\Cache;
24 * Base file cache driver.
27 * @author Fabio B. Silva <fabio.bat.silva@gmail.com>
29 abstract class FileCache extends CacheProvider
32 * @var string Cache directory.
37 * @var string Cache file extension.
44 * @param string $directory Cache directory.
45 * @param string $directory Cache file extension.
47 * @throws \InvalidArgumentException
49 public function __construct($directory, $extension = null)
51 if ( ! is_dir($directory) && ! @mkdir($directory, 0777, true)) {
52 throw new \InvalidArgumentException(sprintf(
53 'The directory "%s" does not exist and could not be created.',
58 if ( ! is_writable($directory)) {
59 throw new \InvalidArgumentException(sprintf(
60 'The directory "%s" is not writable.',
65 $this->directory = realpath($directory);
66 $this->extension = $extension ?: $this->extension;
70 * Gets the cache directory.
74 public function getDirectory()
76 return $this->directory;
80 * Gets the cache file extension.
84 public function getExtension()
86 return $this->extension;
92 protected function getFilename($id)
94 $path = implode(str_split(md5($id), 12), DIRECTORY_SEPARATOR);
95 $path = $this->directory . DIRECTORY_SEPARATOR . $path;
97 return $path . DIRECTORY_SEPARATOR . $id . $this->extension;
103 protected function doDelete($id)
105 return @unlink($this->getFilename($id));
111 protected function doFlush()
113 $pattern = '/^.+\\' . $this->extension . '$/i';
114 $iterator = new \RecursiveDirectoryIterator($this->directory);
115 $iterator = new \RecursiveIteratorIterator($iterator);
116 $iterator = new \RegexIterator($iterator, $pattern);
118 foreach ($iterator as $name => $file) {
128 protected function doGetStats()