From 215a887978051f4a5385237b113d30d49f02a2de Mon Sep 17 00:00:00 2001 From: Awilum Date: Thu, 29 Jul 2021 17:04:22 +0300 Subject: [PATCH] feat(storage): code standard updates #199 --- src/flextype/Foundation/Storage.php | 353 +++++++++++++++------------- 1 file changed, 185 insertions(+), 168 deletions(-) diff --git a/src/flextype/Foundation/Storage.php b/src/flextype/Foundation/Storage.php index a524839cb..72b6845d3 100755 --- a/src/flextype/Foundation/Storage.php +++ b/src/flextype/Foundation/Storage.php @@ -13,10 +13,16 @@ use function array_merge; use function arrays; +use function cache; +use function count; +use function emitter; +use function file_exists; use function filesystem; use function filter; use function find; -use function flextype; +use function is_array; +use function registry; +use function serializers; use function strings; class Storage @@ -27,20 +33,19 @@ class Storage * Local entries registry used for storing current requested * storage data and allow to change them on fly. * - * @var Arrays * @access private */ private Arrays $registry; /** * Storage options - * + * * directory - Storage data files directory. * filename - Storage data filename. * extension - Storage data extension. * serializer - Storage data serializer. - * fields - Array of fields for storage. - * + * fields - Array of fields for storage. + * * @var array * @access private */ @@ -58,28 +63,32 @@ public function __construct(array $options = []) /** * Init Storage Fields - * - * @return void */ - private function initFields(): void + private function initFields(): void { - if (isset($this->options['fields']) && - is_array($this->options['fields']) && - count($this->options['fields']) > 0) { - foreach ($this->options['fields'] as $field) { - if (isset($field['path'])) { - if (file_exists(ROOT_DIR . $field['path'])) { - include_once ROOT_DIR . $field['path']; - } - } + if ( + ! isset($this->options['fields']) || + ! is_array($this->options['fields']) || + count($this->options['fields']) <= 0 + ) { + return; + } + + foreach ($this->options['fields'] as $field) { + if (! isset($field['path'])) { + continue; + } + + if (! file_exists(ROOT_DIR . $field['path'])) { + continue; } + + include_once ROOT_DIR . $field['path']; } } /** * Get Storage Registry - * - * @return Arrays */ public function registry(): Arrays { @@ -92,153 +101,161 @@ public function registry(): Arrays * @param string $id Unique identifier of the storage entry. * @param array $options Options array. * - * @access public - * * @return Arrays Returns instance of The Arrays class with items. + * + * @access public */ public function fetch(string $id, array $options = []): Arrays { // Store data - $this->registry()->set('fetch.id', $id); - $this->registry()->set('fetch.options', $options); - $this->registry()->set('fetch.data', []); + $this->registry()->set('fetch.id', $id); + $this->registry()->set('fetch.options', $options); + $this->registry()->set('fetch.data', []); // Run event - flextype('emitter')->emit('on' . strings($this->options['directory'])->capitalize()->toString() . 'Fetch'); + emitter()->emit('on' . strings($this->options['directory'])->capitalize()->toString() . 'Fetch'); // Single fetch helper - $single = function ($id, $options) { - - // Store data - $this->registry()->set('fetch.id', $id); - $this->registry()->set('fetch.options', $options); - $this->registry()->set('fetch.data', []); - - // Run event - flextype('emitter')->emit('on' . strings($this->options['directory'])->capitalize()->toString() . 'FetchSingle'); - - // Get Cache ID for current requested storage entry - $storageEntryCacheID = $this->getCacheID($this->registry()->get('fetch.id')); - - // 1. Try to get current requested storage entry from cache - if (flextype('cache')->has($storageEntryCacheID)) { - - // Fetch storage entry from cache and Apply filter for fetch data - $this->registry()->set('fetch.data', filter(flextype('cache')->get($storageEntryCacheID), - $this->registry()->get('fetch.options.filter', []))); - - // Run event - flextype('emitter')->emit('on' . strings($this->options['directory'])->capitalize()->toString() . 'FetchSingleCacheHasResult'); - - // Return storage entry from cache - return arrays($this->registry()->get('fetch.data')); - } - - // 2. Try to get current requested storage entry from filesystem - if ($this->has($this->registry()->get('fetch.id'))) { - // Get storage entry file location - $storageEntryFile = $this->getFileLocation($this->registry()->get('fetch.id')); - - // Try to get requested storage entry from the filesystem - $storageEntryFileContent = filesystem()->file($storageEntryFile)->get(); - - if ($storageEntryFileContent === false) { - // Run event - flextype('emitter')->emit('on' . strings($this->options['directory'])->capitalize()->toString() . 'FetchSingleNoResult'); - return arrays($this->registry()->get('fetch.data')); - } - - // Decode storage entry file content - $this->registry()->set('fetch.data', flextype('serializers')->{$this->options['serializer']}()->decode($storageEntryFileContent)); - - // Run event - flextype('emitter')->emit('on' . strings($this->options['directory'])->capitalize()->toString() . 'FetchSingleHasResult'); - - // Apply filter for fetch data - $this->registry()->set('fetch.data', filter($this->registry()->get('fetch.data'), - $this->registry()->get('fetch.options.filter', []))); - - // Set cache state - $cache = $this->registry()->get('fetch.data.cache.enabled', - flextype('registry')->get('flextype.settings.cache.enabled')); - - // Save storage entry data to cache - if ($cache) { - flextype('cache')->set($storageEntryCacheID, $this->registry()->get('fetch.data')); - } - - // Return storage entry data - return arrays($this->registry()->get('fetch.data')); - } - - // Run event - flextype('emitter')->emit('on' . strings($this->options['directory'])->capitalize()->toString() . 'FetchSingleNoResult'); - - // Return empty array if storage entry is not founded - return arrays($this->registry()->get('fetch.data')); - }; - - if (isset($this->registry['fetch']['options']['collection']) && - strings($this->registry['fetch']['options']['collection'])->isTrue()) { - - // Run event - flextype('emitter')->emit('on' . strings($this->options['directory'])->capitalize()->toString() . 'FetchCollection'); - - if (! $this->getDirectoryLocation($id)) { - // Run event - flextype('emitter')->emit('on' . strings($this->options['directory'])->capitalize()->toString() . 'FetchCollectionNoResult'); - - // Return entries array - return arrays($this->registry()->get('fetch.data')); - } - - // Find entries in the filesystem - $entries = find($this->getDirectoryLocation($id), - isset($options['find']) ? - $options['find'] : - []); - - // Walk through entries results - if ($entries->hasResults()) { - - $data = []; - - foreach ($entries as $currenEntry) { - if ($currenEntry->getType() !== 'file' || $currenEntry->getFilename() !== $this->options['filename'] . '.' . $this->options['extension']) { - continue; - } - - $currentEntryID = strings($currenEntry->getPath()) - ->replace('\\', '/') - ->replace(PATH['project'] . '/' . $this->options['directory'] . '/', '') - ->trim('/') - ->toString(); - - $data[$currentEntryID] = $single($currentEntryID, [])->toArray(); - } - - $this->registry()->set('fetch.data', $data); - - // Run event - flextype('emitter')->emit('on' . strings($this->options['directory'])->capitalize()->toString() . 'FetchCollectionHasResult'); - - // Apply filter for fetch data - $this->registry()->set('fetch.data', filter($this->registry()->get('fetch.data'), - isset($options['filter']) ? - $options['filter'] : - [])); - } - - // Run event: - flextype('emitter')->emit('on' . strings($this->options['directory'])->capitalize()->toString() . 'FetchCollectionNoResult'); - - // Return entries array - return arrays($this->registry()->get('fetch.data')); - } else { - - return $single($this->registry['fetch']['id'], - $this->registry['fetch']['options']); - } + $single = function ($id, $options) { + // Store data + $this->registry()->set('fetch.id', $id); + $this->registry()->set('fetch.options', $options); + $this->registry()->set('fetch.data', []); + + // Run event + emitter()->emit('on' . strings($this->options['directory'])->capitalize()->toString() . 'FetchSingle'); + + // Get Cache ID for current requested storage entry + $storageEntryCacheID = $this->getCacheID($this->registry()->get('fetch.id')); + + // 1. Try to get current requested storage entry from cache + if (cache()->has($storageEntryCacheID)) { + // Fetch storage entry from cache and Apply filter for fetch data + $this->registry()->set('fetch.data', filter( + cache()->get($storageEntryCacheID), + $this->registry()->get('fetch.options.filter', []) + )); + + // Run event + emitter()->emit('on' . strings($this->options['directory'])->capitalize()->toString() . 'FetchSingleCacheHasResult'); + + // Return storage entry from cache + return arrays($this->registry()->get('fetch.data')); + } + + // 2. Try to get current requested storage entry from filesystem + if ($this->has($this->registry()->get('fetch.id'))) { + // Get storage entry file location + $storageEntryFile = $this->getFileLocation($this->registry()->get('fetch.id')); + + // Try to get requested storage entry from the filesystem + $storageEntryFileContent = filesystem()->file($storageEntryFile)->get(); + + if ($storageEntryFileContent === false) { + // Run event + emitter()->emit('on' . strings($this->options['directory'])->capitalize()->toString() . 'FetchSingleNoResult'); + + return arrays($this->registry()->get('fetch.data')); + } + + // Decode storage entry file content + $this->registry()->set('fetch.data', serializers()->{$this->options['serializer']}()->decode($storageEntryFileContent)); + + // Run event + emitter()->emit('on' . strings($this->options['directory'])->capitalize()->toString() . 'FetchSingleHasResult'); + + // Apply filter for fetch data + $this->registry()->set('fetch.data', filter( + $this->registry()->get('fetch.data'), + $this->registry()->get('fetch.options.filter', []) + )); + + // Set cache state + $cache = $this->registry()->get( + 'fetch.data.cache.enabled', + registry()->get('flextype.settings.cache.enabled') + ); + + // Save storage entry data to cache + if ($cache) { + cache()->set($storageEntryCacheID, $this->registry()->get('fetch.data')); + } + + // Return storage entry data + return arrays($this->registry()->get('fetch.data')); + } + + // Run event + emitter()->emit('on' . strings($this->options['directory'])->capitalize()->toString() . 'FetchSingleNoResult'); + + // Return empty array if storage entry is not founded + return arrays($this->registry()->get('fetch.data')); + }; + + if ( + ! isset($this->registry['fetch']['options']['collection']) || + ! strings($this->registry['fetch']['options']['collection'])->isTrue() + ) { + return $single( + $this->registry['fetch']['id'], + $this->registry['fetch']['options'] + ); + } + + // Run event + emitter()->emit('on' . strings($this->options['directory'])->capitalize()->toString() . 'FetchCollection'); + + if (! $this->getDirectoryLocation($id)) { + // Run event + emitter()->emit('on' . strings($this->options['directory'])->capitalize()->toString() . 'FetchCollectionNoResult'); + + // Return entries array + return arrays($this->registry()->get('fetch.data')); + } + + // Find entries in the filesystem + $entries = find( + $this->getDirectoryLocation($id), + $options['find'] ?? + [] + ); + + // Walk through entries results + if ($entries->hasResults()) { + $data = []; + + foreach ($entries as $currenEntry) { + if ($currenEntry->getType() !== 'file' || $currenEntry->getFilename() !== $this->options['filename'] . '.' . $this->options['extension']) { + continue; + } + + $currentEntryID = strings($currenEntry->getPath()) + ->replace('\\', '/') + ->replace(PATH['project'] . '/' . $this->options['directory'] . '/', '') + ->trim('/') + ->toString(); + + $data[$currentEntryID] = $single($currentEntryID, [])->toArray(); + } + + $this->registry()->set('fetch.data', $data); + + // Run event + emitter()->emit('on' . strings($this->options['directory'])->capitalize()->toString() . 'FetchCollectionHasResult'); + + // Apply filter for fetch data + $this->registry()->set('fetch.data', filter( + $this->registry()->get('fetch.data'), + $options['filter'] ?? + [] + )); + } + + // Run event: + emitter()->emit('on' . strings($this->options['directory'])->capitalize()->toString() . 'FetchCollectionNoResult'); + + // Return entries array + return arrays($this->registry()->get('fetch.data')); } /** @@ -258,7 +275,7 @@ public function move(string $id, string $newID): bool $this->registry()->set('move.newID', $newID); // Run event - flextype('emitter')->emit('on' . strings($this->options['directory'])->capitalize()->toString() . 'Move'); + emitter()->emit('on' . strings($this->options['directory'])->capitalize()->toString() . 'Move'); if (! $this->has($this->registry()->get('move.newID'))) { return filesystem() @@ -286,15 +303,15 @@ public function update(string $id, array $data): bool $this->registry()->set('update.data', $data); // Run event - flextype('emitter')->emit('on' . strings($this->options['directory'])->capitalize()->toString() . 'Update'); + emitter()->emit('on' . strings($this->options['directory'])->capitalize()->toString() . 'Update'); $storageEntryFile = $this->getFileLocation($this->registry()->get('update.id')); if (filesystem()->file($storageEntryFile)->exists()) { - $body = filesystem()->file($storageEntryFile)->get(); - $storageEntry = flextype('serializers')->{$this->options['serializer']}()->decode($body); + $body = filesystem()->file($storageEntryFile)->get(); + $storageEntry = serializers()->{$this->options['serializer']}()->decode($body); - return (bool) filesystem()->file($storageEntryFile)->put(flextype('serializers')->{$this->options['serializer']}()->encode(array_merge($storageEntry, $this->registry()->get('update.data')))); + return (bool) filesystem()->file($storageEntryFile)->put(serializers()->{$this->options['serializer']}()->encode(array_merge($storageEntry, $this->registry()->get('update.data')))); } return false; @@ -317,7 +334,7 @@ public function create(string $id, array $data = []): bool $this->registry()->set('create.data', $data); // Run event - flextype('emitter')->emit('on' . strings($this->options['directory'])->capitalize()->toString() . 'Create'); + emitter()->emit('on' . strings($this->options['directory'])->capitalize()->toString() . 'Create'); // Create storage entry directory first if it is not exists $storageEntryDirectory = $this->getDirectoryLocation($this->registry()->get('create.id')); @@ -332,7 +349,7 @@ public function create(string $id, array $data = []): bool // Create storage entry file $storageEntryFile = $storageEntryDirectory . '/' . $this->options['filename'] . '.' . $this->options['extension']; if (! filesystem()->file($storageEntryFile)->exists()) { - return (bool) filesystem()->file($storageEntryFile)->put(flextype('serializers')->{$this->options['serializer']}()->encode($this->registry()->get('create.data'))); + return (bool) filesystem()->file($storageEntryFile)->put(serializers()->{$this->options['serializer']}()->encode($this->registry()->get('create.data'))); } return false; @@ -353,7 +370,7 @@ public function delete(string $id): bool $this->registry()->set('delete.id', $id); // Run event - flextype('emitter')->emit('on' . strings($this->options['directory'])->capitalize()->toString() . 'Delete'); + emitter()->emit('on' . strings($this->options['directory'])->capitalize()->toString() . 'Delete'); return filesystem() ->directory($this->getDirectoryLocation($this->registry()->get('delete.id'))) @@ -377,7 +394,7 @@ public function copy(string $id, string $newID): ?bool $this->registry()->set('copy.newID', $newID); // Run event - flextype('emitter')->emit('on' . strings($this->options['directory'])->capitalize()->toString() . 'Copy'); + emitter()->emit('on' . strings($this->options['directory'])->capitalize()->toString() . 'Copy'); return filesystem() ->directory($this->getDirectoryLocation($this->registry()->get('copy.id'))) @@ -399,7 +416,7 @@ public function has(string $id): bool $this->registry()->set('has.id', $id); // Run event: - flextype('emitter')->emit('on' . strings($this->options['directory'])->capitalize()->toString() . 'Has'); + emitter()->emit('on' . strings($this->options['directory'])->capitalize()->toString() . 'Has'); return filesystem()->file($this->getFileLocation($this->registry()->get('has.id')))->exists(); } @@ -443,7 +460,7 @@ public function getDirectoryLocation(string $id): string */ public function getCacheID(string $id): string { - if (flextype('registry')->get('flextype.settings.cache.enabled') === false) { + if (registry()->get('flextype.settings.cache.enabled') === false) { return ''; }