mirror of
https://github.com/e107inc/e107.git
synced 2025-01-18 05:09:05 +01:00
6f6556178f
e_file_inspector_json_phar is an extended implementation of e_file_inspector_json that encapsulates a JSON core image into a gzip-compressed phar. This results in space savings of over 60% compared to plain JSON.
134 lines
3.7 KiB
PHP
134 lines
3.7 KiB
PHP
<?php
|
|
/**
|
|
* e107 website system
|
|
*
|
|
* Copyright (C) 2008-2020 e107 Inc (e107.org)
|
|
* Released under the terms and conditions of the
|
|
* GNU General Public License (http://www.gnu.org/licenses/gpl.txt)
|
|
*
|
|
*/
|
|
|
|
require_once("OsHelper.php");
|
|
require_once("CoreImage.php");
|
|
|
|
class JsonCoreImage extends CoreImage
|
|
{
|
|
protected $checksums = [];
|
|
|
|
public function __construct($exportFolder, $tempFolder, $currentVersion, $imageFile)
|
|
{
|
|
$this->create_image($exportFolder, $tempFolder, $currentVersion);
|
|
|
|
$this->saveImage($imageFile);
|
|
}
|
|
|
|
/**
|
|
* @param $imageFile
|
|
*/
|
|
protected function saveImage($imageFile)
|
|
{
|
|
$json_result = json_encode($this->checksums, JSON_PRETTY_PRINT);
|
|
$json_string_result = var_export($json_result, true);
|
|
$data = $this->generateStub();
|
|
$data .= '$core_image = ' . $json_string_result . ';';
|
|
|
|
$fp = fopen($imageFile, 'w');
|
|
fwrite($fp, $data);
|
|
}
|
|
|
|
protected function generateCurrentChecksums($exportFolder, $currentVersion)
|
|
{
|
|
parent::generateCurrentChecksums($exportFolder, $currentVersion);
|
|
ksort($this->checksums);
|
|
}
|
|
|
|
protected function generateRemovedChecksums($tempFolder)
|
|
{
|
|
parent::generateRemovedChecksums($tempFolder);
|
|
ksort($this->checksums);
|
|
}
|
|
|
|
/**
|
|
* @inheritDoc
|
|
*/
|
|
protected function insertChecksumIntoDatabase(&$relativePath, &$checksum, &$version)
|
|
{
|
|
$item = self::array_get($this->checksums, $relativePath, []);
|
|
if (!in_array($checksum, $item)) $item["v{$version}"] = $checksum;
|
|
self::array_set($this->checksums, $relativePath, $item);
|
|
}
|
|
|
|
/**
|
|
* Get an item from an array using "slash" notation.
|
|
*
|
|
* Based on Illuminate\Support\Arr::get()
|
|
*
|
|
* @param array $array
|
|
* @param string $key
|
|
* @param mixed $default
|
|
* @return mixed
|
|
* @copyright Copyright (c) Taylor Otwell
|
|
* @license https://github.com/illuminate/support/blob/master/LICENSE.md MIT License
|
|
*/
|
|
private static function array_get($array, $key, $default = null)
|
|
{
|
|
if (is_null($key)) return $array;
|
|
|
|
if (isset($array[$key])) return $array[$key];
|
|
|
|
foreach (explode('/', $key) as $segment)
|
|
{
|
|
if (!is_array($array) || !array_key_exists($segment, $array))
|
|
{
|
|
return $default;
|
|
}
|
|
|
|
$array = $array[$segment];
|
|
}
|
|
|
|
return $array;
|
|
}
|
|
|
|
/**
|
|
* Set an array item to a given value using "slash" notation.
|
|
*
|
|
* If no key is given to the method, the entire array will be replaced.
|
|
*
|
|
* Based on Illuminate\Support\Arr::set()
|
|
*
|
|
* @param array $array
|
|
* @param string|null $key
|
|
* @param mixed $value
|
|
* @return array
|
|
* @copyright Copyright (c) Taylor Otwell
|
|
* @license https://github.com/illuminate/support/blob/master/LICENSE.md MIT License
|
|
*/
|
|
private static function array_set(&$array, $key, $value)
|
|
{
|
|
if (is_null($key))
|
|
{
|
|
return $array = $value;
|
|
}
|
|
|
|
$keys = explode('/', $key);
|
|
|
|
while (count($keys) > 1)
|
|
{
|
|
$key = array_shift($keys);
|
|
|
|
// If the key doesn't exist at this depth, we will just create an empty array
|
|
// to hold the next value, allowing us to create the arrays to hold final
|
|
// values at the correct depth. Then we'll keep digging into the array.
|
|
if (!isset($array[$key]) || !is_array($array[$key]))
|
|
{
|
|
$array[$key] = [];
|
|
}
|
|
|
|
$array = &$array[$key];
|
|
}
|
|
|
|
$array[array_shift($keys)] = $value;
|
|
|
|
return $array;
|
|
}
|
|
} |