[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

/libraries/vendor/maximebf/debugbar/src/DebugBar/Storage/ -> FileStorage.php (source)

   1  <?php
   2  /*
   3   * This file is part of the DebugBar package.
   4   *
   5   * (c) 2013 Maxime Bouroumeau-Fuseau
   6   *
   7   * For the full copyright and license information, please view the LICENSE
   8   * file that was distributed with this source code.
   9   */
  10  
  11  namespace DebugBar\Storage;
  12  
  13  /**
  14   * Stores collected data into files
  15   */
  16  class FileStorage implements StorageInterface
  17  {
  18      protected $dirname;
  19  
  20      /**
  21       * @param string $dirname Directories where to store files
  22       */
  23      public function __construct($dirname)
  24      {
  25          $this->dirname = rtrim($dirname, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR;
  26      }
  27  
  28      /**
  29       * {@inheritdoc}
  30       */
  31      public function save($id, $data)
  32      {
  33          if (!file_exists($this->dirname)) {
  34              mkdir($this->dirname, 0777, true);
  35          }
  36          file_put_contents($this->makeFilename($id), json_encode($data));
  37      }
  38  
  39      /**
  40       * {@inheritdoc}
  41       */
  42      public function get($id)
  43      {
  44          return json_decode(file_get_contents($this->makeFilename($id)), true);
  45      }
  46  
  47      /**
  48       * {@inheritdoc}
  49       */
  50      public function find(array $filters = array(), $max = 20, $offset = 0)
  51      {
  52          //Loop through all .json files and remember the modified time and id.
  53          $files = array();
  54          foreach (new \DirectoryIterator($this->dirname) as $file) {
  55              if ($file->getExtension() == 'json') {
  56                  $files[] = array(
  57                      'time' => $file->getMTime(),
  58                      'id' => $file->getBasename('.json')
  59                  );
  60              }
  61          }
  62  
  63          //Sort the files, newest first
  64          usort($files, function ($a, $b) {
  65                  return $a['time'] < $b['time'];
  66              });
  67  
  68          //Load the metadata and filter the results.
  69          $results = array();
  70          $i = 0;
  71          foreach ($files as $file) {
  72              //When filter is empty, skip loading the offset
  73              if ($i++ < $offset && empty($filters)) {
  74                  $results[] = null;
  75                  continue;
  76              }
  77              $data = $this->get($file['id']);
  78              $meta = $data['__meta'];
  79              unset($data);
  80              if ($this->filter($meta, $filters)) {
  81                  $results[] = $meta;
  82              }
  83              if (count($results) >= ($max + $offset)) {
  84                  break;
  85              }
  86          }
  87  
  88          return array_slice($results, $offset, $max);
  89      }
  90  
  91      /**
  92       * Filter the metadata for matches.
  93       * 
  94       * @param  array $meta
  95       * @param  array $filters
  96       * @return bool
  97       */
  98      protected function filter($meta, $filters)
  99      {
 100          foreach ($filters as $key => $value) {
 101              if (!isset($meta[$key]) || fnmatch($value, $meta[$key]) === false) {
 102                  return false;
 103              }
 104          }
 105          return true;
 106      }
 107  
 108      /**
 109       * {@inheritdoc}
 110       */
 111      public function clear()
 112      {
 113          foreach (new \DirectoryIterator($this->dirname) as $file) {
 114              if (substr($file->getFilename(), 0, 1) !== '.') {
 115                  unlink($file->getPathname());
 116              }
 117          }
 118      }
 119  
 120      /**
 121       * @param  string $id
 122       * @return string 
 123       */
 124      public function makeFilename($id)
 125      {
 126          return $this->dirname . basename($id). ".json";
 127      }
 128  }


Generated: Wed Sep 7 05:41:13 2022 Chilli.vc Blog - For Webmaster,Blog-Writer,System Admin and Domainer