From 678c4a178987774a90e58f9588ae31b42276a737 Mon Sep 17 00:00:00 2001 From: Awilum Date: Sun, 15 Mar 2020 15:17:16 +0300 Subject: [PATCH] refactor(core): total refactor from the core next round #414 --- src/flextype/bootstrap.php | 1 + src/flextype/core/Entries/Entries.php | 27 +++++----- src/flextype/core/Parsers/Frontmatter.php | 2 +- src/flextype/core/Parsers/Json.php | 4 +- src/flextype/core/Parsers/Yaml.php | 2 +- src/flextype/core/Plugins/Plugins.php | 9 +--- src/flextype/dependencies.php | 9 ++-- src/flextype/endpoints/delivery/entries.php | 52 ++++++++++---------- src/flextype/endpoints/delivery/images.php | 52 ++++++++++---------- src/flextype/endpoints/delivery/registry.php | 43 ++++++++-------- 10 files changed, 98 insertions(+), 103 deletions(-) diff --git a/src/flextype/bootstrap.php b/src/flextype/bootstrap.php index 1f19e83b..d5c6765f 100755 --- a/src/flextype/bootstrap.php +++ b/src/flextype/bootstrap.php @@ -23,6 +23,7 @@ use function function_exists; use function mb_internal_encoding; use function mb_language; use function mb_regex_encoding; +use function trim; /** * The version of Flextype diff --git a/src/flextype/core/Entries/Entries.php b/src/flextype/core/Entries/Entries.php index 88fde5a3..291e5d7c 100755 --- a/src/flextype/core/Entries/Entries.php +++ b/src/flextype/core/Entries/Entries.php @@ -15,10 +15,13 @@ use Doctrine\Common\Collections\Expr\Comparison; use Flextype\Component\Filesystem\Filesystem; use Flextype\Component\Session\Session; use Ramsey\Uuid\Uuid; -use function array_replace_recursive; +use function array_merge; use function count; use function date; use function error_reporting; +use function in_array; +use function is_array; +use function is_bool; use function json_encode; use function ltrim; use function md5; @@ -26,8 +29,8 @@ use function rename; use function rtrim; use function str_replace; use function strpos; -use function time; use function strtotime; +use function time; class Entries { @@ -80,7 +83,7 @@ class Entries 'like' => Comparison::CONTAINS, 'member_of' => Comparison::MEMBER_OF, 'start_with' => Comparison::STARTS_WITH, - 'ends_with' => Comparison::ENDS_WITH + 'ends_with' => Comparison::ENDS_WITH, ]; /** @@ -103,7 +106,7 @@ class Entries public $visibility = [ 'draft' => 'draft', 'hidden' => 'hidden', - 'visible' => 'visible' + 'visible' => 'visible', ]; /** @@ -133,14 +136,14 @@ class Entries * * @access public */ - public function fetch(string $id, $args = null) : array + public function fetch(string $id, ?array $args = null) : array { // If args is array then it is entries collection request if (is_array($args)) { return $this->fetchCollection($id, $args); - } else { - return $this->fetchSingle($id); } + + return $this->fetchSingle($id); } /** @@ -186,10 +189,7 @@ class Entries $entry_decoded = $this->flextype['parser']->decode(Filesystem::read($entry_file), 'frontmatter'); - // // Add predefined entry items - // - // Entry Published At $entry_decoded['published_at'] = isset($entry_decoded['published_at']) ? (int) strtotime($entry_decoded['published_at']) : (int) Filesystem::getTimestamp($entry_file); @@ -232,8 +232,8 @@ class Entries /** * Fetch entries collection * - * @param string $id Entry ID - * @param array $args Query arguments. + * @param string $id Entry ID + * @param array $args Query arguments. * * @return array The entries array data. * @@ -476,6 +476,7 @@ class Entries if (Filesystem::has($entry_file)) { $body = Filesystem::read($entry_file); $entry = $this->flextype['parser']->decode($body, 'frontmatter'); + return Filesystem::write($entry_file, $this->flextype['parser']->encode(array_merge($entry, $data), 'frontmatter')); } @@ -554,7 +555,7 @@ class Entries * * @access public */ - public function copy(string $id, string $new_id, bool $recursive = false) + public function copy(string $id, string $new_id, bool $recursive = false) : ?bool { return Filesystem::copy($this->getDirLocation($id), $this->getDirLocation($new_id), $recursive); } diff --git a/src/flextype/core/Parsers/Frontmatter.php b/src/flextype/core/Parsers/Frontmatter.php index a6883d67..242f8c22 100644 --- a/src/flextype/core/Parsers/Frontmatter.php +++ b/src/flextype/core/Parsers/Frontmatter.php @@ -10,13 +10,13 @@ declare(strict_types=1); namespace Flextype; use Flextype\Component\Arr\Arr; +use const PHP_EOL; use function array_slice; use function count; use function implode; use function ltrim; use function preg_split; use function trim; -use const PHP_EOL; class Frontmatter { diff --git a/src/flextype/core/Parsers/Json.php b/src/flextype/core/Parsers/Json.php index 03b5a5ae..71c80171 100644 --- a/src/flextype/core/Parsers/Json.php +++ b/src/flextype/core/Parsers/Json.php @@ -10,11 +10,11 @@ declare(strict_types=1); namespace Flextype; use RuntimeException; -use function json_decode; -use function json_encode; use const JSON_PRETTY_PRINT; use const JSON_UNESCAPED_SLASHES; use const JSON_UNESCAPED_UNICODE; +use function json_decode; +use function json_encode; class Json { diff --git a/src/flextype/core/Parsers/Yaml.php b/src/flextype/core/Parsers/Yaml.php index 1c370d65..2db3adca 100644 --- a/src/flextype/core/Parsers/Yaml.php +++ b/src/flextype/core/Parsers/Yaml.php @@ -103,7 +103,7 @@ class Yaml public static function decode(string $input) : array { // Try native PECL YAML PHP extension first if available. - if (\function_exists('yaml_parse') && self::$native) { + if (function_exists('yaml_parse') && self::$native) { // Safely decode YAML. $saved = @ini_get('yaml.decode_php'); @ini_set('yaml.decode_php', '0'); diff --git a/src/flextype/core/Plugins/Plugins.php b/src/flextype/core/Plugins/Plugins.php index a4753d0d..48feaa3f 100755 --- a/src/flextype/core/Plugins/Plugins.php +++ b/src/flextype/core/Plugins/Plugins.php @@ -13,12 +13,12 @@ use Flextype\Component\Arr\Arr; use Flextype\Component\Filesystem\Filesystem; use Flextype\Component\I18n\I18n; use RuntimeException; -use function array_merge; use function array_replace_recursive; use function count; use function filemtime; use function is_array; use function md5; +use function trim; class Plugins { @@ -60,8 +60,6 @@ class Plugins /** * Init Plugins * - * @return void - * * @access private */ public function init($flextype, $app) : void @@ -105,9 +103,8 @@ class Plugins // Go through... foreach ($plugins_list as $plugin) { - // Set plugin settings directory - $site_plugin_settings_dir = PATH['config']['site'] . '/plugins/' . $plugin['dirname']; + $site_plugin_settings_dir = PATH['config']['site'] . '/plugins/' . $plugin['dirname']; // Set default plugin settings and manifest files $default_plugin_settings_file = PATH['plugins'] . '/' . $plugin['dirname'] . '/settings.yaml'; @@ -264,8 +261,6 @@ class Plugins /** * Include enabled plugins * - * @return void - * * @access protected */ private function includeEnabledPlugins($flextype, $app) : void diff --git a/src/flextype/dependencies.php b/src/flextype/dependencies.php index 07b6bdd2..70af9be7 100644 --- a/src/flextype/dependencies.php +++ b/src/flextype/dependencies.php @@ -34,11 +34,9 @@ use League\Glide\Responses\SlimResponseFactory; use League\Glide\ServerFactory; use Monolog\Handler\StreamHandler; use Monolog\Logger; -use Slim\Http\Environment; -use Slim\Http\Uri; use function date; -use function ucfirst; use function extension_loaded; +use function ucfirst; /** * Supply a custom callable resolver, which resolves PSR-15 middlewares. @@ -58,8 +56,9 @@ $flextype['registry'] = static function ($container) use ($registry) { * Add logger service to Flextype container */ $flextype['logger'] = static function ($container) { - $logger = new Logger('flextype'); + $logger = new Logger('flextype'); $logger->pushHandler(new StreamHandler(PATH['logs'] . '/' . date('Y-m-d') . '.log')); + return $logger; }; @@ -100,7 +99,7 @@ $flextype['cache_adapter'] = static function ($container) use ($flextype) { } } - $class = ucfirst($driver_name); + $class = ucfirst($driver_name); $adapter = "Flextype\\Cache\\{$class}Adapter"; return new $adapter($flextype); diff --git a/src/flextype/endpoints/delivery/entries.php b/src/flextype/endpoints/delivery/entries.php index 3a211ef3..15ff3f7b 100644 --- a/src/flextype/endpoints/delivery/entries.php +++ b/src/flextype/endpoints/delivery/entries.php @@ -12,6 +12,8 @@ namespace Flextype; use Flextype\Component\Filesystem\Filesystem; use Psr\Http\Message\ResponseInterface as Response; use Psr\Http\Message\ServerRequestInterface as Request; +use function array_replace_recursive; +use function count; /** * Validate delivery entries token @@ -27,53 +29,51 @@ function validate_delivery_entries_token($request, $flextype) : bool * endpoint: /api/delivery/entries */ $app->get('/api/delivery/entries', function (Request $request, Response $response) use ($flextype) { - // Get Query Params $query = $request->getQueryParams(); // Set variables $id = $query['id']; - $args = isset($query['args']) ? $query['args'] : null; + $args = $query['args'] ?? null; if ($flextype['registry']->get('flextype.api.entries.enabled')) { - // Validate delivery token if (validate_delivery_entries_token($request, $flextype)) { $delivery_entries_token_file_path = PATH['tokens'] . '/delivery/entries/' . $request->getQueryParams()['token'] . '/token.yaml'; // Set delivery token file if ($delivery_entries_token_file_data = $flextype['parser']->decode(Filesystem::read($delivery_entries_token_file_path), 'yaml')) { - if ($delivery_entries_token_file_data['state'] == 'disabled' || - ($delivery_entries_token_file_data['limit_calls'] != 0 && $delivery_entries_token_file_data['calls'] >= $delivery_entries_token_file_data['limit_calls'])) { - return $response->withJson(["detail" => "Incorrect authentication credentials."], 401); - } else { - // Fetch entry - $data = $flextype['entries']->fetch($id, $args); - - // Set response code - $response_code = (count($data) > 0) ? 200 : 404 ; - - // Update calls counter - Filesystem::write($delivery_entries_token_file_path, $flextype['parser']->encode(array_replace_recursive($delivery_entries_token_file_data, ['calls' => $delivery_entries_token_file_data['calls'] + 1]), 'yaml')); - - // Return response - return $response - ->withJson($data, $response_code) - ->withHeader('Access-Control-Allow-Origin', '*'); + if ($delivery_entries_token_file_data['state'] === 'disabled' || + ($delivery_entries_token_file_data['limit_calls'] !== 0 && $delivery_entries_token_file_data['calls'] >= $delivery_entries_token_file_data['limit_calls'])) { + return $response->withJson(['detail' => 'Incorrect authentication credentials.'], 401); } - } else { + + // Fetch entry + $data = $flextype['entries']->fetch($id, $args); + + // Set response code + $response_code = count($data) > 0 ? 200 : 404; + + // Update calls counter + Filesystem::write($delivery_entries_token_file_path, $flextype['parser']->encode(array_replace_recursive($delivery_entries_token_file_data, ['calls' => $delivery_entries_token_file_data['calls'] + 1]), 'yaml')); + + // Return response return $response - ->withJson(["detail" => "Incorrect authentication credentials."], 401) + ->withJson($data, $response_code) ->withHeader('Access-Control-Allow-Origin', '*'); } - } else { + return $response - ->withJson(["detail" => "Incorrect authentication credentials."], 401) + ->withJson(['detail' => 'Incorrect authentication credentials.'], 401) ->withHeader('Access-Control-Allow-Origin', '*'); } - } else { + return $response - ->withJson(["detail" => "Incorrect authentication credentials."], 401) + ->withJson(['detail' => 'Incorrect authentication credentials.'], 401) ->withHeader('Access-Control-Allow-Origin', '*'); } + + return $response + ->withJson(['detail' => 'Incorrect authentication credentials.'], 401) + ->withHeader('Access-Control-Allow-Origin', '*'); }); diff --git a/src/flextype/endpoints/delivery/images.php b/src/flextype/endpoints/delivery/images.php index 78e786f6..6398703d 100644 --- a/src/flextype/endpoints/delivery/images.php +++ b/src/flextype/endpoints/delivery/images.php @@ -12,6 +12,8 @@ namespace Flextype; use Flextype\Component\Filesystem\Filesystem; use Psr\Http\Message\ResponseInterface as Response; use Psr\Http\Message\ServerRequestInterface as Request; +use function array_replace_recursive; +use function header; /** * Validate delivery images token @@ -27,52 +29,50 @@ function validate_delivery_images_token($request, $flextype) : bool * endpoint: /api/delivery/images */ $app->get('/api/delivery/images/{path:.+}', function (Request $request, Response $response, array $args) use ($flextype) { - // Get Query Params $query = $request->getQueryParams(); if ($flextype['registry']->get('flextype.api.images.enabled')) { - // Validate delivery image token if (validate_delivery_images_token($request, $flextype)) { $delivery_images_token_file_path = PATH['tokens'] . '/delivery/images/' . $request->getQueryParams()['token'] . '/token.yaml'; // Set delivery token file if ($delivery_images_token_file_data = $flextype['parser']->decode(Filesystem::read($delivery_images_token_file_path), 'yaml')) { - if ($delivery_images_token_file_data['state'] == 'disabled' || - ($delivery_images_token_file_data['limit_calls'] != 0 && $delivery_images_token_file_data['calls'] >= $delivery_images_token_file_data['limit_calls'])) { - return $response->withJson(["detail" => "Incorrect authentication credentials."], 401); - } else { - - // Update calls counter - Filesystem::write($delivery_images_token_file_path, $flextype['parser']->encode(array_replace_recursive($delivery_images_token_file_data, ['calls' => $delivery_images_token_file_data['calls'] + 1]), 'yaml')); - - if (Filesystem::has(PATH['uploads'] . '/entries/' . $args['path'])) { - header('Access-Control-Allow-Origin: *'); - return $flextype['images']->getImageResponse($args['path'], $_GET); - } else { - return $response - ->withJson([], 404) - ->withHeader('Access-Control-Allow-Origin', '*'); - } + if ($delivery_images_token_file_data['state'] === 'disabled' || + ($delivery_images_token_file_data['limit_calls'] !== 0 && $delivery_images_token_file_data['calls'] >= $delivery_images_token_file_data['limit_calls'])) { + return $response->withJson(['detail' => 'Incorrect authentication credentials.'], 401); } - } else { + + // Update calls counter + Filesystem::write($delivery_images_token_file_path, $flextype['parser']->encode(array_replace_recursive($delivery_images_token_file_data, ['calls' => $delivery_images_token_file_data['calls'] + 1]), 'yaml')); + + if (Filesystem::has(PATH['uploads'] . '/entries/' . $args['path'])) { + header('Access-Control-Allow-Origin: *'); + + return $flextype['images']->getImageResponse($args['path'], $_GET); + } + return $response - ->withJson(["detail" => "Incorrect authentication credentials."], 401) - ->withHeader('Access-Control-Allow-Origin', '*'); + ->withJson([], 404) + ->withHeader('Access-Control-Allow-Origin', '*'); } + + return $response + ->withJson(['detail' => 'Incorrect authentication credentials.'], 401) + ->withHeader('Access-Control-Allow-Origin', '*'); } else { return $response - ->withJson(["detail" => "Incorrect authentication credentials."], 401) + ->withJson(['detail' => 'Incorrect authentication credentials.'], 401) ->withHeader('Access-Control-Allow-Origin', '*'); } return $response ->withStatus(404) ->withHeader('Access-Control-Allow-Origin', '*'); - } else { - return $response - ->withJson(["detail" => "Incorrect authentication credentials."], 401) - ->withHeader('Access-Control-Allow-Origin', '*'); } + + return $response + ->withJson(['detail' => 'Incorrect authentication credentials.'], 401) + ->withHeader('Access-Control-Allow-Origin', '*'); }); diff --git a/src/flextype/endpoints/delivery/registry.php b/src/flextype/endpoints/delivery/registry.php index e1510e76..697f44c4 100644 --- a/src/flextype/endpoints/delivery/registry.php +++ b/src/flextype/endpoints/delivery/registry.php @@ -12,6 +12,7 @@ namespace Flextype; use Flextype\Component\Filesystem\Filesystem; use Psr\Http\Message\ResponseInterface as Response; use Psr\Http\Message\ServerRequestInterface as Request; +use function array_replace_recursive; /** * Validate delivery registry token @@ -27,7 +28,6 @@ function validate_delivery_registry_token($request, $flextype) : bool * endpoint: /api/delivery/registry */ $app->get('/api/delivery/registry', function (Request $request, Response $response) use ($flextype) { - // Get Query Params $query = $request->getQueryParams(); @@ -35,41 +35,40 @@ $app->get('/api/delivery/registry', function (Request $request, Response $respon $id = $query['id']; if ($flextype['registry']->get('flextype.api.registry.enabled')) { - // Validate delivery token if (validate_delivery_registry_token($request, $flextype)) { $delivery_registry_token_file_path = PATH['tokens'] . '/delivery/registry/' . $request->getQueryParams()['token'] . '/token.yaml'; // Set delivery token file if ($delivery_registry_token_file_data = $flextype['parser']->decode(Filesystem::read($delivery_registry_token_file_path), 'yaml')) { - if ($delivery_registry_token_file_data['state'] == 'disabled' || - ($delivery_registry_token_file_data['limit_calls'] != 0 && $delivery_registry_token_file_data['calls'] >= $delivery_registry_token_file_data['limit_calls'])) { - return $response->withJson(["detail" => "Incorrect authentication credentials."], 401); - } else { - // Fetch registry - $data = $flextype['registry']->get($id); - - // Update calls counter - Filesystem::write($delivery_registry_token_file_path, $flextype['parser']->encode(array_replace_recursive($delivery_registry_token_file_data, ['calls' => $delivery_registry_token_file_data['calls'] + 1]), 'yaml')); - - // Return response - return $response - ->withJson($data, 200) - ->withHeader('Access-Control-Allow-Origin', '*'); + if ($delivery_registry_token_file_data['state'] === 'disabled' || + ($delivery_registry_token_file_data['limit_calls'] !== 0 && $delivery_registry_token_file_data['calls'] >= $delivery_registry_token_file_data['limit_calls'])) { + return $response->withJson(['detail' => 'Incorrect authentication credentials.'], 401); } - } else { + + // Fetch registry + $data = $flextype['registry']->get($id); + + // Update calls counter + Filesystem::write($delivery_registry_token_file_path, $flextype['parser']->encode(array_replace_recursive($delivery_registry_token_file_data, ['calls' => $delivery_registry_token_file_data['calls'] + 1]), 'yaml')); + + // Return response return $response - ->withJson(["detail" => "Incorrect authentication credentials."], 401) + ->withJson($data, 200) ->withHeader('Access-Control-Allow-Origin', '*'); } - } else { + return $response - ->withJson(["detail" => "Incorrect authentication credentials."], 401) + ->withJson(['detail' => 'Incorrect authentication credentials.'], 401) ->withHeader('Access-Control-Allow-Origin', '*'); } - } else { + return $response - ->withJson(["detail" => "Incorrect authentication credentials."], 401) + ->withJson(['detail' => 'Incorrect authentication credentials.'], 401) ->withHeader('Access-Control-Allow-Origin', '*'); } + + return $response + ->withJson(['detail' => 'Incorrect authentication credentials.'], 401) + ->withHeader('Access-Control-Allow-Origin', '*'); });