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

Flextype Core: Add ability to work with different types of content #212 #186

This commit is contained in:
Awilum
2019-08-10 18:34:47 +03:00
parent 3a4cc1b1ae
commit 6da11ddb37
5 changed files with 191 additions and 16 deletions

View File

@@ -51,7 +51,8 @@
"cocur/slugify": "^3.2",
"oscarotero/psr7-middlewares": "^3.21",
"thunderer/shortcode": "^0.7.2",
"ramsey/uuid": "^3.8"
"ramsey/uuid": "^3.8",
"symfony/yaml": "^4.3.3"
},
"suggest": {
"ext-zend-opcache": "Recommended for better performance",

View File

@@ -5,8 +5,6 @@ declare(strict_types=1);
/**
* Flextype (http://flextype.org)
* Founded by Sergey Romanenko and maintained Flextype Community.
*
* @license https://github.com/flextype/flextype/blob/master/LICENSE.txt (MIT License)
*/
namespace Flextype;
@@ -72,13 +70,13 @@ class Entries
$entry_file = $this->_file_location($id);
// If requested entry founded then process it
if (Filesystem::has($entry_file)) {
if ($entry_file) {
// Create unique entry cache_id
// Entry Cache ID = entry + entry file + entry file time stamp
if ($timestamp = Filesystem::getTimestamp($entry_file)) {
$entry_cache_id = md5('entry' . $entry_file . $timestamp);
if ($timestamp = Filesystem::getTimestamp($entry_file['file'])) {
$entry_cache_id = md5('entry' . $entry_file['file'] . $timestamp);
} else {
$entry_cache_id = md5('entry' . $entry_file);
$entry_cache_id = md5('entry' . $entry_file['file']);
}
// Try to get the requested entry from cache
@@ -98,16 +96,16 @@ class Entries
}
// Try to get requested entry body content
if ($entry_body = Filesystem::read($entry_file)) {
if ($entry_body = Filesystem::read($entry_file['file'])) {
// Try to decode requested entry body content
if ($entry_decoded = JsonParser::decode($entry_body)) {
if ($entry_decoded = Parser::decode($entry_body, $entry_file['driver'])) {
// Add predefined entry items
// Entry Date
$entry_decoded['published_at'] = $entry_decoded['published_at'] ? $entry_decoded['published_at'] : Filesystem::getTimestamp($entry_file);
$entry_decoded['created_at'] = $entry_decoded['created_at'] ? $entry_decoded['created_at'] : Filesystem::getTimestamp($entry_file);
$entry_decoded['published_at'] = $entry_decoded['published_at'] ? $entry_decoded['published_at'] : Filesystem::getTimestamp($entry_file['file']);
$entry_decoded['created_at'] = $entry_decoded['created_at'] ? $entry_decoded['created_at'] : Filesystem::getTimestamp($entry_file['file']);
// Entry Timestamp
$entry_decoded['modified_at'] = Filesystem::getTimestamp($entry_file);
$entry_decoded['modified_at'] = Filesystem::getTimestamp($entry_file['file']);
// Entry Slug
$entry_decoded['slug'] = $entry_decoded['slug'] ?? ltrim(rtrim($id, '/'), '/');
@@ -466,9 +464,20 @@ class Entries
*
* @access private
*/
private function _file_location(string $id) : string
private function _file_location(string $id)
{
return PATH['entries'] . '/' . $id . '/entry.json';
$json_file = PATH['entries'] . '/' . $id . '/entry.json';
$yaml_file = PATH['entries'] . '/' . $id . '/entry.yaml';
if (Filesystem::has($json_file)) {
return ['file' => $json_file, 'driver' => 'json'];
}
if (Filesystem::has($yaml_file)) {
return ['file' => $yaml_file, 'driver' => 'yaml'];
}
return false;
}
/**

View File

@@ -5,8 +5,6 @@ declare(strict_types=1);
/**
* Flextype (http://flextype.org)
* Founded by Sergey Romanenko and maintained Flextype Community.
*
* @license https://github.com/flextype/flextype/blob/master/LICENSE.txt (MIT License)
*/
namespace Flextype;

View File

@@ -0,0 +1,47 @@
<?php
declare(strict_types=1);
/**
* Flextype (http://flextype.org)
* Founded by Sergey Romanenko and maintained Flextype Community.
*/
namespace Flextype;
class Parser
{
public static function encode($input, string $driver) : string
{
switch ($driver) {
case 'json':
return JsonParser::encode($input);
break;
case 'yaml':
return JsonParser::encode($input);
break;
default:
// code...
break;
}
}
public static function decode(string $input, string $driver)
{
switch ($driver) {
case 'json':
return JsonParser::decode($input);
break;
case 'yaml':
return YamlParser::decode($input);
break;
default:
// code...
break;
}
}
}

View File

@@ -0,0 +1,120 @@
<?php
declare(strict_types=1);
/**
* @link http://flextype.org
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Flextype;
use RuntimeException;
use Symfony\Component\Yaml\Exception\DumpException;
use Symfony\Component\Yaml\Exception\ParseException;
use Symfony\Component\Yaml\Yaml;
use function function_exists;
use function ini_get;
use function ini_set;
class YamlParser
{
/**
* Inline
*
* The level where you switch to inline YAML
*
* @var int
*/
public static $inline = 5;
/**
* Ident
*
* The amount of spaces to use for indentation of nested nodes
*
* @var int
*/
public static $indent = 2;
/**
* Native
*
* Use native parser or symfony
*
* @var bool
*/
public static $native = true;
/**
* Flag
*
* A bit field of PARSE_* constants to customize the YAML parser behavior
*
* @var int
*/
public static $flag = 16;
/**
* Dumps a PHP value to a YAML string.
*
* The dump method, when supplied with an array, will do its best
* to convert the array into friendly YAML.
*
* @param mixed $input The PHP value
* @param int $inline The level where you switch to inline YAML
* @param int $indent The amount of spaces to use for indentation of nested nodes
* @param int $flags A bit field of DUMP_* constants to customize the dumped YAML string
*
* @return string A YAML string representing the original PHP value
*/
public static function encode($input, int $inline = 5, int $indent = 2, int $flags = 16) : string
{
try {
return Yaml::dump(
$input,
$inline ? $inline : self::$inline,
$indent ? $indent : self::$indent,
$flags ? $flags : self::$flag
);
} catch (DumpException $e) {
throw new RuntimeException('Encoding YAML failed: ' . $e->getMessage(), 0, $e);
}
}
/**
* Parses YAML into a PHP value.
*
* $array = YamlParser::decode($yaml_file_content);
*
* @param string $input A string containing YAML
* @param int $flags A bit field of PARSE_* constants to customize the YAML parser behavior
*
* @return mixed The YAML converted to a PHP value
*
* @throws ParseException If the YAML is not valid
*/
public static function decode(string $input, int $flags = 0)
{
// Try native PECL YAML PHP extension first if available.
if (self::$native && function_exists('yaml_parse')) {
// Safely decode YAML.
$saved = @ini_get('yaml.decode_php');
@ini_set('yaml.decode_php', 0);
$decoded = @yaml_parse($input);
@ini_set('yaml.decode_php', $saved);
if ($decoded !== false) {
return (array) $decoded;
}
}
try {
return (array) Yaml::parse($input);
} catch (ParseException $e) {
throw new RuntimeException('Decoding YAML failed: ' . $e->getMessage(), 0, $e);
}
}
}