Hoa central
File.php
Go to the documentation of this file.
1 <?php
2 
37 namespace Hoa\Cache\Backend;
38 
39 use Hoa\Cache;
40 use Hoa\File as HoaFile;
41 
50 class File extends Backend
51 {
58  public function store($data)
59  {
60  $this->clean();
61 
62  if (true === $this->_parameters->getParameter('serialize_content')) {
63  $data = serialize($data);
64  }
65 
66  if (true === $this->_parameters->getParameter('file.compress.active')) {
67  $data = gzcompress(
68  $data,
69  $this->_parameters->getParameter('file.compress.level')
70  );
71  }
72 
73  $this->setId($this->getIdMd5());
74  $directory =
75  $this->_parameters->getFormattedParameter('file.cache.directory');
76 
77  @mkdir($directory, 0755, true);
78 
79  file_put_contents(
80  $directory .
81  $this->_parameters->getFormattedParameter('file.cache.file'),
82  $data
83  );
84 
85  return;
86  }
87 
93  public function load()
94  {
95  $this->clean();
96  $this->setId($this->getIdMd5());
97 
98  $filename =
99  $this->_parameters->getFormattedParameter('file.cache.directory') .
100  $this->_parameters->getFormattedParameter('file.cache.file');
101 
102  if (false === file_exists($filename)) {
103  return false;
104  }
105 
106  $content = file_get_contents($filename);
107 
108  if (true === $this->_parameters->getParameter('file.compress.active')) {
109  $content = gzuncompress($content);
110  }
111 
112  if (true === $this->_parameters->getParameter('serialize_content')) {
113  $content = unserialize($content);
114  }
115 
116  return $content;
117  }
118 
128  public function clean($lifetime = Cache::CLEAN_EXPIRED)
129  {
130  switch ($lifetime) {
131  case Cache::CLEAN_ALL:
132  break;
133 
135  $lifetime = $this->_parameters->getParameter('lifetime');
136 
137  break;
138 
139  case Cache::CLEAN_USER:
140  throw new Cache\Exception(
141  '\Hoa\Cache::CLEAN_USER constant is not supported by %s.' .
142  2,
143  __CLASS__
144  );
145 
146  default:
147  $lifetime = $lifetime;
148  }
149 
150  $this->setId($this->getIdMd5());
151 
152  try {
153  $cacheDirectory = $this->_parameters->getFormattedParameter(
154  'file.cache.directory'
155  );
156  $finder = new HoaFile\Finder();
157  $finder
158  ->in($cacheDirectory)
159  ->files()
160  ->modified('since ' . $lifetime . ' seconds');
161 
162  foreach ($finder as $file) {
163  $file->open()->delete();
164  $file->close();
165  }
166  } catch (HoaFile\Exception\FileDoesNotExist $e) {
167  }
168 
169  return;
170  }
171 
177  public function remove()
178  {
179  $this->setId($this->getIdMd5());
180 
181  $filename =
182  $this->_parameters->getFormattedParameter('file.cache.directory') .
183  $this->_parameters->getFormattedParameter('file.cache.file');
184 
185  $file = new HoaFile\Read($filename);
186  $file->delete();
187  $file->close();
188 
189  return;
190  }
191 }
clean($lifetime=Cache::CLEAN_EXPIRED)
Definition: File.php:128
const CLEAN_EXPIRED
Definition: Cache.php:62
const CLEAN_ALL
Definition: Cache.php:55
$content
Definition: Hoa.php:119
const CLEAN_USER
Definition: Cache.php:69