scratch – Blame information for rev 115

Subversion Repositories:
Rev:
Rev Author Line No. Line
115 office 1 <?php
2 /*
3 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
4 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
5 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
6 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
7 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
8 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
9 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
10 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
11 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
12 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
13 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
14 *
15 * This software consists of voluntary contributions made by many individuals
16 * and is licensed under the MIT license. For more information, see
17 * <http://www.doctrine-project.org>.
18 */
19  
20 namespace Doctrine\Common\Cache;
21  
22 /**
23 * Php file cache driver.
24 *
25 * @since 2.3
26 * @author Fabio B. Silva <fabio.bat.silva@gmail.com>
27 */
28 class PhpFileCache extends FileCache
29 {
30 const EXTENSION = '.doctrinecache.php';
31  
32 /**
33 * {@inheritdoc}
34 */
35 public function __construct($directory, $extension = self::EXTENSION, $umask = 0002)
36 {
37 parent::__construct($directory, $extension, $umask);
38 }
39  
40 /**
41 * {@inheritdoc}
42 */
43 protected function doFetch($id)
44 {
45 $value = $this->includeFileForId($id);
46  
47 if (! $value) {
48 return false;
49 }
50  
51 if ($value['lifetime'] !== 0 && $value['lifetime'] < time()) {
52 return false;
53 }
54  
55 return $value['data'];
56 }
57  
58 /**
59 * {@inheritdoc}
60 */
61 protected function doContains($id)
62 {
63 $value = $this->includeFileForId($id);
64  
65 if (! $value) {
66 return false;
67 }
68  
69 return $value['lifetime'] === 0 || $value['lifetime'] > time();
70 }
71  
72 /**
73 * {@inheritdoc}
74 */
75 protected function doSave($id, $data, $lifeTime = 0)
76 {
77 if ($lifeTime > 0) {
78 $lifeTime = time() + $lifeTime;
79 }
80  
81 if (is_object($data) && ! method_exists($data, '__set_state')) {
82 throw new \InvalidArgumentException(
83 "Invalid argument given, PhpFileCache only allows objects that implement __set_state() " .
84 "and fully support var_export(). You can use the FilesystemCache to save arbitrary object " .
85 "graphs using serialize()/deserialize()."
86 );
87 }
88  
89 $filename = $this->getFilename($id);
90  
91 $value = array(
92 'lifetime' => $lifeTime,
93 'data' => $data
94 );
95  
96 $value = var_export($value, true);
97 $code = sprintf('<?php return %s;', $value);
98  
99 return $this->writeFile($filename, $code);
100 }
101  
102 /**
103 * @param string $id
104 *
105 * @return array|false
106 */
107 private function includeFileForId($id)
108 {
109 $fileName = $this->getFilename($id);
110  
111 // note: error suppression is still faster than `file_exists`, `is_file` and `is_readable`
112 $value = @include $fileName;
113  
114 if (! isset($value['lifetime'])) {
115 return false;
116 }
117  
118 return $value;
119 }
120 }