"Fossies" - the Fresh Open Source Software Archive

Member "grav/system/src/Grav/Framework/Flex/Storage/AbstractFilesystemStorage.php" (8 Jun 2020, 4088 Bytes) of package /linux/www/grav-v1.6.26.zip:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) PHP source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "AbstractFilesystemStorage.php" see the Fossies "Dox" file reference documentation.

    1 <?php
    2 
    3 declare(strict_types=1);
    4 
    5 /**
    6  * @package    Grav\Framework\Flex
    7  *
    8  * @copyright  Copyright (C) 2015 - 2019 Trilby Media, LLC. All rights reserved.
    9  * @license    MIT License; see LICENSE file for details.
   10  */
   11 
   12 namespace Grav\Framework\Flex\Storage;
   13 
   14 use Grav\Common\File\CompiledJsonFile;
   15 use Grav\Common\File\CompiledMarkdownFile;
   16 use Grav\Common\File\CompiledYamlFile;
   17 use Grav\Common\Grav;
   18 use Grav\Framework\File\Formatter\JsonFormatter;
   19 use Grav\Framework\File\Formatter\MarkdownFormatter;
   20 use Grav\Framework\File\Formatter\YamlFormatter;
   21 use Grav\Framework\File\Interfaces\FileFormatterInterface;
   22 use Grav\Framework\Flex\Interfaces\FlexStorageInterface;
   23 use RocketTheme\Toolbox\File\File;
   24 use RocketTheme\Toolbox\ResourceLocator\UniformResourceLocator;
   25 use RuntimeException;
   26 
   27 /**
   28  * Class AbstractFilesystemStorage
   29  * @package Grav\Framework\Flex\Storage
   30  */
   31 abstract class AbstractFilesystemStorage implements FlexStorageInterface
   32 {
   33     /** @var FileFormatterInterface */
   34     protected $dataFormatter;
   35     /** @var string */
   36     protected $keyField = 'storage_key';
   37 
   38 
   39     /**
   40      * {@inheritdoc}
   41      * @see FlexStorageInterface::hasKey()
   42      */
   43     public function hasKeys(array $keys): array
   44     {
   45         $list = [];
   46         foreach ($keys as $key) {
   47             $list[$key] = $this->hasKey((string)$key);
   48         }
   49 
   50         return $list;
   51     }
   52 
   53     /**
   54      * @return string
   55      */
   56     public function getKeyField(): string
   57     {
   58         return $this->keyField;
   59     }
   60 
   61     protected function initDataFormatter($formatter): void
   62     {
   63         // Initialize formatter.
   64         if (!\is_array($formatter)) {
   65             $formatter = ['class' => $formatter];
   66         }
   67         $formatterClassName = $formatter['class'] ?? JsonFormatter::class;
   68         $formatterOptions = $formatter['options'] ?? [];
   69 
   70         $this->dataFormatter = new $formatterClassName($formatterOptions);
   71     }
   72 
   73     /**
   74      * @param string $filename
   75      * @return null|string
   76      */
   77     protected function detectDataFormatter(string $filename): ?string
   78     {
   79         if (preg_match('|(\.[a-z0-9]*)$|ui', $filename, $matches)) {
   80             switch ($matches[1]) {
   81                 case '.json':
   82                     return JsonFormatter::class;
   83                 case '.yaml':
   84                     return YamlFormatter::class;
   85                 case '.md':
   86                     return MarkdownFormatter::class;
   87             }
   88         }
   89 
   90         return null;
   91     }
   92 
   93     /**
   94      * @param string $filename
   95      * @return File
   96      */
   97     protected function getFile(string $filename)
   98     {
   99         $filename = $this->resolvePath($filename);
  100 
  101         switch ($this->dataFormatter->getDefaultFileExtension()) {
  102             case '.json':
  103                 $file = CompiledJsonFile::instance($filename);
  104                 break;
  105             case '.yaml':
  106                 $file = CompiledYamlFile::instance($filename);
  107                 break;
  108             case '.md':
  109                 $file = CompiledMarkdownFile::instance($filename);
  110                 break;
  111             default:
  112                 throw new RuntimeException('Unknown extension type ' . $this->dataFormatter->getDefaultFileExtension());
  113         }
  114 
  115         return $file;
  116     }
  117 
  118     /**
  119      * @param string $path
  120      * @return string
  121      */
  122     protected function resolvePath(string $path): string
  123     {
  124         /** @var UniformResourceLocator $locator */
  125         $locator = Grav::instance()['locator'];
  126 
  127         if (!$locator->isStream($path)) {
  128             return $path;
  129         }
  130 
  131         return (string)($locator->findResource($path) ?: $locator->findResource($path, true, true));
  132     }
  133 
  134     /**
  135      * Generates a random, unique key for the row.
  136      *
  137      * @return string
  138      */
  139     protected function generateKey(): string
  140     {
  141         return substr(hash('sha256', random_bytes(32)), 0, 32);
  142     }
  143 
  144     /**
  145      * Checks if a key is valid.
  146      *
  147      * @param  string $key
  148      * @return bool
  149      */
  150     protected function validateKey(string $key): bool
  151     {
  152         return (bool) preg_match('/^[^\\/\\?\\*:;{}\\\\\\n]+$/u', $key);
  153     }
  154 }