1 | <?php |
||
46 | class BrowscapCache implements BrowscapCacheInterface |
||
47 | { |
||
48 | /** |
||
49 | * Path to the cache directory |
||
50 | * |
||
51 | * @var \WurflCache\Adapter\AdapterInterface |
||
52 | */ |
||
53 | private $cache = null; |
||
54 | |||
55 | /** |
||
56 | * Detected browscap version (read from INI file) |
||
57 | * |
||
58 | * @var int |
||
59 | */ |
||
60 | private $version = null; |
||
61 | |||
62 | /** |
||
63 | * Constructor class, checks for the existence of (and loads) the cache and |
||
64 | * if needed updated the definitions |
||
65 | * |
||
66 | * @param \WurflCache\Adapter\AdapterInterface $adapter |
||
67 | * @param int $updateInterval |
||
68 | */ |
||
69 | 12 | public function __construct(AdapterInterface $adapter, $updateInterval = BrowscapCacheInterface::CACHE_LIVETIME) |
|
74 | |||
75 | /** |
||
76 | * Gets the version of the Browscap data |
||
77 | * |
||
78 | * @return int |
||
79 | */ |
||
80 | 4 | public function getVersion() |
|
81 | { |
||
82 | 4 | if ($this->version === null) { |
|
83 | 4 | $success = true; |
|
84 | |||
85 | 4 | $version = $this->getItem('browscap.version', false, $success); |
|
86 | |||
87 | 4 | if ($version !== null && $success) { |
|
88 | 3 | $this->version = (int) $version; |
|
89 | } |
||
90 | } |
||
91 | |||
92 | 4 | return $this->version; |
|
93 | } |
||
94 | |||
95 | /** |
||
96 | * Get an item. |
||
97 | * |
||
98 | * @param string $cacheId |
||
99 | * @param bool $withVersion |
||
100 | * @param bool $success |
||
101 | * |
||
102 | * @return mixed Data on success, null on failure |
||
103 | */ |
||
104 | 4 | public function getItem($cacheId, $withVersion = true, & $success = null) |
|
105 | { |
||
106 | 4 | if ($withVersion) { |
|
107 | $cacheId .= '.'.$this->getVersion(); |
||
108 | } |
||
109 | |||
110 | 4 | if (!$this->cache->hasItem($cacheId)) { |
|
111 | 4 | $success = false; |
|
112 | |||
113 | 4 | return null; |
|
114 | } |
||
115 | |||
116 | 3 | $success = null; |
|
117 | 3 | $data = $this->cache->getItem($cacheId, $success); |
|
118 | |||
119 | 3 | if (!$success) { |
|
|
|||
120 | $success = false; |
||
121 | |||
122 | return null; |
||
123 | } |
||
124 | |||
125 | 3 | if (!isset($data['content'])) { |
|
126 | $success = false; |
||
127 | |||
128 | return null; |
||
129 | } |
||
130 | |||
131 | 3 | $success = true; |
|
132 | |||
133 | 3 | return unserialize($data['content']); |
|
134 | } |
||
135 | |||
136 | /** |
||
137 | * save the content into an php file |
||
138 | * |
||
139 | * @param string $cacheId The cache id |
||
140 | * @param mixed $content The content to store |
||
141 | * @param bool $withVersion |
||
142 | * |
||
143 | * @return boolean whether the file was correctly written to the disk |
||
144 | */ |
||
145 | 3 | public function setItem($cacheId, $content, $withVersion = true) |
|
159 | |||
160 | /** |
||
161 | * Test if an item exists. |
||
162 | * |
||
163 | * @param string $cacheId |
||
164 | * @param bool $withVersion |
||
165 | * |
||
166 | * @return bool |
||
167 | */ |
||
168 | 1 | public function hasItem($cacheId, $withVersion = true) |
|
176 | |||
177 | /** |
||
178 | * Remove an item. |
||
179 | * |
||
180 | * @param string $cacheId |
||
181 | * @param bool $withVersion |
||
182 | * |
||
183 | * @return bool |
||
184 | */ |
||
185 | public function removeItem($cacheId, $withVersion = true) |
||
193 | |||
194 | /** |
||
195 | * Flush the whole storage |
||
196 | * |
||
197 | * @return bool |
||
198 | */ |
||
199 | public function flush() |
||
203 | } |
||
204 |
If an expression can have both
false
, andnull
as possible values. It is generally a good practice to always use strict comparison to clearly distinguish between those two values.