1
0
mirror of https://github.com/flextype/flextype.git synced 2025-08-13 08:34:19 +02:00

refactor(core): total refactor from the core next round #414

This commit is contained in:
Awilum
2020-03-15 15:17:16 +03:00
parent 2bf9ae2eba
commit 678c4a1789
10 changed files with 98 additions and 103 deletions

View File

@@ -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

View File

@@ -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);
}

View File

@@ -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
{

View File

@@ -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
{

View File

@@ -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');

View File

@@ -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

View File

@@ -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);

View File

@@ -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', '*');
});

View File

@@ -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', '*');
});

View File

@@ -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', '*');
});