Rajout de doctrine/orm
[zf2.biz/galerie.git] / vendor / doctrine / common / lib / Doctrine / Common / Cache / FileCache.php
1 <?php
2
3 /*
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.
15  *
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>.
19  */
20
21 namespace Doctrine\Common\Cache;
22
23 /**
24  * Base file cache driver.
25  *
26  * @since   2.3
27  * @author  Fabio B. Silva <fabio.bat.silva@gmail.com>
28  */
29 abstract class FileCache extends CacheProvider
30 {
31     /**
32      * @var string Cache directory.
33      */
34     protected $directory;
35
36     /**
37      * @var string Cache file extension.
38      */
39     protected $extension;
40
41     /**
42      * Constructor
43      *
44      * @param string $directory Cache directory.
45      * @param string $directory Cache file extension.
46      *
47      * @throws \InvalidArgumentException
48      */
49     public function __construct($directory, $extension = null)
50     {
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.',
54                 $directory
55             ));
56         }
57
58         if ( ! is_writable($directory)) {
59             throw new \InvalidArgumentException(sprintf(
60                 'The directory "%s" is not writable.',
61                 $directory
62             ));
63         }
64
65         $this->directory = realpath($directory);
66         $this->extension = $extension ?: $this->extension;
67     }
68
69     /**
70      * Gets the cache directory.
71      * 
72      * @return string
73      */
74     public function getDirectory()
75     {
76         return $this->directory;
77     }
78
79     /**
80      * Gets the cache file extension.
81      * 
82      * @return string
83      */
84     public function getExtension()
85     {
86         return $this->extension;
87     }
88
89     /**
90      * @return string
91      */
92     protected function getFilename($id)
93     {
94         $path = implode(str_split(md5($id), 12), DIRECTORY_SEPARATOR);
95         $path = $this->directory . DIRECTORY_SEPARATOR . $path;
96
97         return $path . DIRECTORY_SEPARATOR . $id . $this->extension;
98     }
99
100     /**
101      * {@inheritdoc}
102      */
103     protected function doDelete($id)
104     {
105         return @unlink($this->getFilename($id));
106     }
107
108     /**
109      * {@inheritdoc}
110      */
111     protected function doFlush()
112     {
113         $pattern  = '/^.+\\' . $this->extension . '$/i';
114         $iterator = new \RecursiveDirectoryIterator($this->directory);
115         $iterator = new \RecursiveIteratorIterator($iterator);
116         $iterator = new \RegexIterator($iterator, $pattern);
117
118         foreach ($iterator as $name => $file) {
119             @unlink($name);
120         }
121
122         return true;
123     }
124
125     /**
126      * {@inheritdoc}
127      */
128     protected function doGetStats()
129     {
130         return null;
131     }
132 }