Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Backend.php
Go to the documentation of this file.
1 <?php
31 {
44  protected $_directives = array(
45  'lifetime' => 3600,
46  'logging' => false,
47  'logger' => null
48  );
49 
55  protected $_options = array();
56 
62  public function __construct(array $options = array())
63  {
64  foreach ($options as $name => $value) {
65  $this->setOption($name, $value);
66  }
67  }
68 
76  public function setDirectives($directives)
77  {
78  if (!is_array($directives)) Zend_Cache::throwException('Directives parameter must be an array');
79  foreach ($directives as $name => $value) {
80  if (!is_string($name)) {
81  Zend_Cache::throwException("Incorrect option name : $name");
82  }
83  $name = strtolower($name);
84  if (array_key_exists($name, $this->_directives)) {
85  $this->_directives[$name] = $value;
86  }
87 
88  }
89 
90  $this->_loggerSanity();
91  }
92 
101  public function setOption($name, $value)
102  {
103  if (!is_string($name)) {
104  Zend_Cache::throwException("Incorrect option name : $name");
105  }
106  $name = strtolower($name);
107  if (array_key_exists($name, $this->_options)) {
108  $this->_options[$name] = $value;
109  }
110  }
111 
119  public function getOption($name)
120  {
121  $name = strtolower($name);
122 
123  if (array_key_exists($name, $this->_options)) {
124  return $this->_options[$name];
125  }
126 
127  if (array_key_exists($name, $this->_directives)) {
128  return $this->_directives[$name];
129  }
130 
131  Zend_Cache::throwException("Incorrect option name : {$name}");
132  }
133 
143  public function getLifetime($specificLifetime)
144  {
145  if ($specificLifetime === false) {
146  return $this->_directives['lifetime'];
147  }
148  return $specificLifetime;
149  }
150 
160  {
161  return true;
162  }
163 
172  public function getTmpDir()
173  {
174  $tmpdir = array();
175  foreach (array($_ENV, $_SERVER) as $tab) {
176  foreach (array('TMPDIR', 'TEMP', 'TMP', 'windir', 'SystemRoot') as $key) {
177  if (isset($tab[$key]) && is_string($tab[$key])) {
178  if (($key == 'windir') or ($key == 'SystemRoot')) {
179  $dir = realpath($tab[$key] . '\\temp');
180  } else {
181  $dir = realpath($tab[$key]);
182  }
183  if ($this->_isGoodTmpDir($dir)) {
184  return $dir;
185  }
186  }
187  }
188  }
189  $upload = ini_get('upload_tmp_dir');
190  if ($upload) {
191  $dir = realpath($upload);
192  if ($this->_isGoodTmpDir($dir)) {
193  return $dir;
194  }
195  }
196  if (function_exists('sys_get_temp_dir')) {
197  $dir = sys_get_temp_dir();
198  if ($this->_isGoodTmpDir($dir)) {
199  return $dir;
200  }
201  }
202  // Attemp to detect by creating a temporary file
203  $tempFile = tempnam(md5(uniqid(rand(), TRUE)), '');
204  if ($tempFile) {
205  $dir = realpath(dirname($tempFile));
206  unlink($tempFile);
207  if ($this->_isGoodTmpDir($dir)) {
208  return $dir;
209  }
210  }
211  if ($this->_isGoodTmpDir('/tmp')) {
212  return '/tmp';
213  }
214  if ($this->_isGoodTmpDir('\\temp')) {
215  return '\\temp';
216  }
217  Zend_Cache::throwException('Could not determine temp directory, please specify a cache_dir manually');
218  }
219 
226  protected function _isGoodTmpDir($dir)
227  {
228  if (is_readable($dir)) {
229  if (is_writable($dir)) {
230  return true;
231  }
232  }
233  return false;
234  }
235 
244  protected function _loggerSanity()
245  {
246  if (!isset($this->_directives['logging']) || !$this->_directives['logging']) {
247  return;
248  }
249 
250  if (isset($this->_directives['logger'])) {
251  if ($this->_directives['logger'] instanceof Zend_Log) {
252  return;
253  }
254  Zend_Cache::throwException('Logger object is not an instance of Zend_Log class.');
255  }
256 
257  // Create a default logger to the standard output stream
258  #require_once 'Zend/Log.php';
259  #require_once 'Zend/Log/Writer/Stream.php';
260  #require_once 'Zend/Log/Filter/Priority.php';
261  $logger = new Zend_Log(new Zend_Log_Writer_Stream('php://output'));
262  $logger->addFilter(new Zend_Log_Filter_Priority(Zend_Log::WARN, '<='));
263  $this->_directives['logger'] = $logger;
264  }
265 
273  protected function _log($message, $priority = 4)
274  {
275  if (!$this->_directives['logging']) {
276  return;
277  }
278 
279  if (!isset($this->_directives['logger'])) {
280  Zend_Cache::throwException('Logging is enabled but logger is not set.');
281  }
282  $logger = $this->_directives['logger'];
283  if (!$logger instanceof Zend_Log) {
284  Zend_Cache::throwException('Logger object is not an instance of Zend_Log class.');
285  }
286  $logger->log($message, $priority);
287  }
288 }
const WARN
Definition: Log.php:46
$message
__construct(array $options=array())
Definition: Backend.php:62
$logger
_log($message, $priority=4)
Definition: Backend.php:273
setOption($name, $value)
Definition: Backend.php:101
setDirectives($directives)
Definition: Backend.php:76
$value
Definition: gender.phtml:16
isAutomaticCleaningAvailable()
Definition: Backend.php:159
static throwException($msg, Exception $e=null)
Definition: Cache.php:205
is_writable($path)
Definition: io.php:25
getLifetime($specificLifetime)
Definition: Backend.php:143
if(!isset($_GET['name'])) $name
Definition: log.php:14