CoreFiles by mySites.guru
FileStorage.php137 lines, 2,692 bytes, from Joomla_4.0.0-alpha8-Alpha-Full_Package.zip
Raw History Compare with 4.0.0-alpha7
md5
974f76352cea7da1be79beb47a82f83a
sha1
a658b6240d4f0bc9dbb54c6633a067037a44c81f
sha256
f0e42220959cb6723e32f60956a3c7e0f1920579baa1ec4672789327966b4d20
<?php
/**
 * @package     Joomla.Plugin
 * @subpackage  System.Debug
 *
 * @copyright   Copyright (C) 2017 Open Source Matters, Inc. All rights reserved.
 * @license     GNU General Public License version 2 or later; see LICENSE.txt
 */

namespace Joomla\Plugin\System\Debug\Storage;

use Joomla\Filesystem\Folder;

/**
 * Stores collected data into files
 *
 * @since  4.0.0
 */
class FileStorage extends \DebugBar\Storage\FileStorage
{
	/**
	 * Saves collected data
	 *
	 * @param   string  $id    The log id
	 * @param   string  $data  The log data
	 *
	 * @return  void
	 *
	 * @since  4.0.0
	 */
	public function save($id, $data)
	{
		if (!file_exists($this->dirname))
		{
			Folder::create($this->dirname);
		}

		$dataStr = '<?php die(); ?>#(^-^)#' . json_encode($data);

		file_put_contents($this->makeFilename($id), $dataStr);
	}

	/**
	 * Returns collected data with the specified id
	 *
	 * @param   string  $id  The log id
	 *
	 * @return  array
	 *
	 * @since  4.0.0
	 */
	public function get($id)
	{
		$dataStr = file_get_contents($this->makeFilename($id));
		$dataStr = str_replace('<?php die(); ?>#(^-^)#', '', $dataStr);

		return json_decode($dataStr, true) ?: array();
	}

	/**
	 * Returns a metadata about collected data
	 *
	 * @param   array    $filters  Filtering options
	 * @param   integer  $max      The limit, items per page
	 * @param   integer  $offset   The offset
	 *
	 * @return  array
	 *
	 * @since 4.0.0
	 */
	public function find(array $filters = [], $max = 20, $offset = 0)
	{
		// Loop through all .php files and remember the modified time and id.
		$files = [];
		foreach (new \DirectoryIterator($this->dirname) as $file)
		{
			if ($file->getExtension() == 'php')
			{
				$files[] = array(
					'time' => $file->getMTime(),
					'id' => $file->getBasename('.php')
				);
			}
		}

		// Sort the files, newest first
		usort(
			$files,
			function ($a, $b) {
				return $a['time'] < $b['time'];
			}
		);

		// Load the metadata and filter the results.
		$results = [];
		$i = 0;
		foreach ($files as $file)
		{
			// When filter is empty, skip loading the offset
			if ($i++ < $offset && empty($filters))
			{
				$results[] = null;
				continue;
			}

			$data = $this->get($file['id']);
			$meta = $data['__meta'];
			unset($data);

			if ($this->filter($meta, $filters))
			{
				$results[] = $meta;
			}

			if (count($results) >= ($max + $offset))
			{
				break;
			}
		}

		return array_slice($results, $offset, $max);
	}

	/**
	 * Get a full path to the file
	 *
	 * @param   string  $id  The log id
	 *
	 * @return string
	 *
	 * @since 4.0.0
	 */
	public function makeFilename($id)
	{
		return $this->dirname . basename($id) . '.php';
	}
}

The same content is in 4 releases, Joomla 4.0.0-alpha5 to 4.0.0-alpha8. See them all, or every version of this file.