[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

/plugins/system/debug/src/Storage/ -> FileStorage.php (source)

   1  <?php
   2  
   3  /**
   4   * @package     Joomla.Plugin
   5   * @subpackage  System.Debug
   6   *
   7   * @copyright   (C) 2018 Open Source Matters, Inc. <https://www.joomla.org>
   8   * @license     GNU General Public License version 2 or later; see LICENSE.txt
   9   */
  10  
  11  namespace Joomla\Plugin\System\Debug\Storage;
  12  
  13  use Joomla\CMS\Filesystem\File;
  14  use Joomla\CMS\Filesystem\Folder;
  15  
  16  // phpcs:disable PSR1.Files.SideEffects
  17  \defined('_JEXEC') or die;
  18  // phpcs:enable PSR1.Files.SideEffects
  19  
  20  /**
  21   * Stores collected data into files
  22   *
  23   * @since  4.0.0
  24   */
  25  class FileStorage extends \DebugBar\Storage\FileStorage
  26  {
  27      /**
  28       * Saves collected data
  29       *
  30       * @param   string  $id    The log id
  31       * @param   string  $data  The log data
  32       *
  33       * @return  void
  34       *
  35       * @since  4.0.0
  36       */
  37      public function save($id, $data)
  38      {
  39          if (!file_exists($this->dirname)) {
  40              Folder::create($this->dirname);
  41          }
  42  
  43          $dataStr = '<?php die(); ?>#(^-^)#' . json_encode($data);
  44  
  45          File::write($this->makeFilename($id), $dataStr);
  46      }
  47  
  48      /**
  49       * Returns collected data with the specified id
  50       *
  51       * @param   string  $id  The log id
  52       *
  53       * @return  array
  54       *
  55       * @since  4.0.0
  56       */
  57      public function get($id)
  58      {
  59          $dataStr = file_get_contents($this->makeFilename($id));
  60          $dataStr = str_replace('<?php die(); ?>#(^-^)#', '', $dataStr);
  61  
  62          return json_decode($dataStr, true) ?: [];
  63      }
  64  
  65      /**
  66       * Returns a metadata about collected data
  67       *
  68       * @param   array    $filters  Filtering options
  69       * @param   integer  $max      The limit, items per page
  70       * @param   integer  $offset   The offset
  71       *
  72       * @return  array
  73       *
  74       * @since 4.0.0
  75       */
  76      public function find(array $filters = [], $max = 20, $offset = 0)
  77      {
  78          // Loop through all .php files and remember the modified time and id.
  79          $files = [];
  80  
  81          foreach (new \DirectoryIterator($this->dirname) as $file) {
  82              if ($file->getExtension() == 'php') {
  83                  $files[] = [
  84                      'time' => $file->getMTime(),
  85                      'id' => $file->getBasename('.php'),
  86                  ];
  87              }
  88          }
  89  
  90          // Sort the files, newest first
  91          usort(
  92              $files,
  93              function ($a, $b) {
  94                  if ($a['time'] === $b['time']) {
  95                      return 0;
  96                  }
  97  
  98                  return $a['time'] < $b['time'] ? 1 : -1;
  99              }
 100          );
 101  
 102          // Load the metadata and filter the results.
 103          $results = [];
 104          $i = 0;
 105  
 106          foreach ($files as $file) {
 107              // When filter is empty, skip loading the offset
 108              if ($i++ < $offset && empty($filters)) {
 109                  $results[] = null;
 110                  continue;
 111              }
 112  
 113              $data = $this->get($file['id']);
 114              $meta = $data['__meta'];
 115              unset($data);
 116  
 117              if ($this->filter($meta, $filters)) {
 118                  $results[] = $meta;
 119              }
 120  
 121              if (\count($results) >= ($max + $offset)) {
 122                  break;
 123              }
 124          }
 125  
 126          return \array_slice($results, $offset, $max);
 127      }
 128  
 129      /**
 130       * Get a full path to the file
 131       *
 132       * @param   string  $id  The log id
 133       *
 134       * @return string
 135       *
 136       * @since 4.0.0
 137       */
 138      public function makeFilename($id)
 139      {
 140          return $this->dirname . basename($id) . '.php';
 141      }
 142  }


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