Hoa central
ZendPlatform.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 
53 class ZendPlatform extends Backend
54 {
60  const INTERNAL_ZP_PREFIX = 'internal\ZPtag:';
61 
62 
63 
71  public function __construct(Array $parameters = [])
72  {
73  if (!function_exists('accelerator_license_info')) {
74  throw new Cache\Exception(
75  'The Zend Platform extension must be loaded to use this backend.',
76  0
77  );
78  }
79 
80  if (!function_exists('accelerator_get_configuration')) {
81  $licenseInfos = accelerator_license_info();
82  throw new Cache\Exception(
83  'The Zend Platform extension is not loaded correctly: %s.',
84  1,
85  $licenseInfos['failure_reason']
86  );
87  }
88 
89  $configurations = accelerator_get_configuration();
90 
91  if (@!$configurations['output_cache_licensed']) {
92  throw new Cache\Exception(
93  'The Zend Platform extension does not have the proper license ' .
94  'to use content caching features.',
95  2
96  );
97  }
98 
99  if (@!$configurations['output_cache_enabled']) {
100  throw new Cache\Exception(
101  'The Zend Platform content caching feature must be enabled to ' .
102  'use this backend, set the ' .
103  'zend_accelerator.output_cache_enabled directive to on.',
104  3
105  );
106  }
107 
108  if (!is_writable($configuration['output_cache_dir'])) {
109  throw new Cache\Exception(
110  'The cache copies directory %s must be writable.',
111  4,
112  $configuration['output_cache_dir']
113  );
114  }
115 
116  parent::__construct($parameters);
117 
118  return;
119  }
120 
128  public function store($data)
129  {
130  $this->clean();
131 
132  output_cache_put(
133  $this->getIdMd5(),
134  [$data, time()]
135  );
136 
137  return;
138  }
139 
145  public function load()
146  {
147  $this->clean();
148 
149  $content = output_cache_get(
150  $this->getIdMd5(),
151  $this->_parameters->getParameter('lifetime')
152  );
153 
154  if (isset($return[0])) {
155  return $return[0];
156  }
157 
158  return false;
159  }
160 
170  public function clean($lifetime = Cache::CLEAN_EXPIRED)
171  {
172  switch ($lifetime) {
173  case Cache::CLEAN_ALL:
174  break;
175 
177  $lifetime = $this->_parameters->getParameter('lifetime');
178 
179  break;
180 
181  case Cache::CLEAN_USER:
182  throw new \Hoa\Cache\Exception(
183  '\Hoa\Cache::CLEAN_USER constant is not supported by ' .
184  'ZendPlatform cache backend.',
185  3
186  );
187 
188  break;
189 
190  default:
191  $lifetime = $lifetime;
192  }
193 
194  $directory =
195  ini_get('zend_accelerator.output_cache_dir') . DS .
196  '.php_cache_api';
197 
198  try {
199  $finder = new HoaFile\Finder();
200  $finder
201  ->in($directory)
202  ->files()
203  ->modified('since ' . $lifetime . ' seconds');
204 
205  foreach ($finder as $file) {
206  $file->open()->delete();
207  $file->close();
208  }
209  } catch (File\Exception\FileDoesNotExist $e) {
210  }
211 
212  return;
213  }
214 
220  public function remove()
221  {
222  output_cache_remove_key($this->getIdMd5());
223 
224  return;
225  }
226 }
const CLEAN_EXPIRED
Definition: Cache.php:62
const CLEAN_ALL
Definition: Cache.php:55
clean($lifetime=Cache::CLEAN_EXPIRED)
$content
Definition: Hoa.php:119
__construct(Array $parameters=[])
const CLEAN_USER
Definition: Cache.php:69