maj+ header
This commit is contained in:
+5
-6
@@ -48,7 +48,7 @@ class File implements FileInterface
|
||||
/**
|
||||
* @var array|File[]
|
||||
*/
|
||||
static protected $instances = array();
|
||||
static protected $instances = [];
|
||||
|
||||
/**
|
||||
* Get file instance.
|
||||
@@ -97,8 +97,6 @@ class File implements FileInterface
|
||||
|
||||
/**
|
||||
* Prevent constructor from being used.
|
||||
*
|
||||
* @internal
|
||||
*/
|
||||
protected function __construct()
|
||||
{
|
||||
@@ -106,8 +104,6 @@ class File implements FileInterface
|
||||
|
||||
/**
|
||||
* Prevent cloning.
|
||||
*
|
||||
* @internal
|
||||
*/
|
||||
protected function __clone()
|
||||
{
|
||||
@@ -224,7 +220,7 @@ class File implements FileInterface
|
||||
public function unlock()
|
||||
{
|
||||
if (!$this->handle) {
|
||||
return;
|
||||
return false;
|
||||
}
|
||||
if ($this->locked) {
|
||||
flock($this->handle, LOCK_UN);
|
||||
@@ -232,6 +228,8 @@ class File implements FileInterface
|
||||
}
|
||||
fclose($this->handle);
|
||||
$this->handle = null;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -282,6 +280,7 @@ class File implements FileInterface
|
||||
*
|
||||
* @param mixed $var
|
||||
* @return string|array
|
||||
* @throws \RuntimeException
|
||||
*/
|
||||
public function content($var = null)
|
||||
{
|
||||
|
||||
+4
-4
@@ -18,7 +18,7 @@ class IniFile extends File
|
||||
/**
|
||||
* @var array|File[]
|
||||
*/
|
||||
static protected $instances = array();
|
||||
static protected $instances = [];
|
||||
|
||||
/**
|
||||
* Check contents and make sure it is in correct format.
|
||||
@@ -65,12 +65,12 @@ class IniFile extends File
|
||||
*/
|
||||
protected function decode($var)
|
||||
{
|
||||
$var = file_exists($this->filename) ? @parse_ini_file($this->filename) : [];
|
||||
$decoded = file_exists($this->filename) ? @parse_ini_file($this->filename) : [];
|
||||
|
||||
if ($var === false) {
|
||||
if ($decoded === false) {
|
||||
throw new \RuntimeException("Decoding file '{$this->filename}' failed'");
|
||||
}
|
||||
|
||||
return $var;
|
||||
return $decoded;
|
||||
}
|
||||
}
|
||||
|
||||
+1
-1
@@ -18,7 +18,7 @@ class JsonFile extends File
|
||||
/**
|
||||
* @var array|File[]
|
||||
*/
|
||||
static protected $instances = array();
|
||||
static protected $instances = [];
|
||||
|
||||
/**
|
||||
* Check contents and make sure it is in correct format.
|
||||
|
||||
+3
-3
@@ -13,7 +13,7 @@ class LogFile extends File
|
||||
/**
|
||||
* @var array|File[]
|
||||
*/
|
||||
static protected $instances = array();
|
||||
static protected $instances = [];
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
@@ -41,11 +41,11 @@ class LogFile extends File
|
||||
*
|
||||
* @param string $var
|
||||
* @return string|void
|
||||
* @throws \Exception
|
||||
* @throws \BadMethodCallException
|
||||
*/
|
||||
protected function encode($var)
|
||||
{
|
||||
throw new \Exception('Saving log file is forbidden.');
|
||||
throw new \BadMethodCallException('Saving log file is forbidden.');
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
+15
-11
@@ -1,7 +1,9 @@
|
||||
<?php
|
||||
namespace RocketTheme\Toolbox\File;
|
||||
|
||||
use \Symfony\Component\Yaml\Yaml as YamlParser;
|
||||
use Symfony\Component\Yaml\Exception\ParseException;
|
||||
use Symfony\Component\Yaml\Yaml as YamlParser;
|
||||
use RocketTheme\Toolbox\Compat\Yaml\Yaml as FallbackYamlParser;
|
||||
|
||||
/**
|
||||
* Implements Markdown File reader.
|
||||
@@ -20,7 +22,7 @@ class MarkdownFile extends File
|
||||
/**
|
||||
* @var array|File[]
|
||||
*/
|
||||
static protected $instances = array();
|
||||
static protected $instances = [];
|
||||
|
||||
/**
|
||||
* Get/set file header.
|
||||
@@ -136,26 +138,28 @@ class MarkdownFile extends File
|
||||
// Parse header.
|
||||
preg_match($frontmatter_regex, ltrim($var), $m);
|
||||
if(!empty($m)) {
|
||||
$content['frontmatter'] = preg_replace("/\n\t/", "\n ", $m[1]);
|
||||
// Normalize frontmatter.
|
||||
$content['frontmatter'] = $frontmatter = preg_replace("/\n\t/", "\n ", $m[1]);
|
||||
|
||||
// Try native PECL YAML PHP extension first if available.
|
||||
if ($this->setting('native') && function_exists('yaml_parse')) {
|
||||
$data = $content['frontmatter'];
|
||||
if ($this->setting('compat', true)) {
|
||||
// Fix illegal @ start character.
|
||||
$data = preg_replace('/ (@[\w\.\-]*)/', " '\${1}'", $data);
|
||||
}
|
||||
|
||||
// Safely decode YAML.
|
||||
$saved = @ini_get('yaml.decode_php');
|
||||
@ini_set('yaml.decode_php', 0);
|
||||
$content['header'] = @yaml_parse("---\n" . $data . "\n...");
|
||||
$content['header'] = @yaml_parse("---\n" . $frontmatter . "\n...");
|
||||
@ini_set('yaml.decode_php', $saved);
|
||||
}
|
||||
|
||||
if ($content['header'] === false) {
|
||||
// YAML hasn't been parsed yet (error or extension isn't available). Fall back to Symfony parser.
|
||||
$content['header'] = (array) YamlParser::parse($content['frontmatter']);
|
||||
try {
|
||||
$content['header'] = (array) YamlParser::parse($frontmatter);
|
||||
} catch (ParseException $e) {
|
||||
if (!$this->setting('compat', true)) {
|
||||
throw $e;
|
||||
}
|
||||
$content['header'] = (array) FallbackYamlParser::parse($frontmatter);
|
||||
}
|
||||
}
|
||||
$content['markdown'] = $m[2];
|
||||
} else {
|
||||
|
||||
+1
-1
@@ -23,7 +23,7 @@ class MoFile extends File
|
||||
/**
|
||||
* @var array|File[]
|
||||
*/
|
||||
static protected $instances = array();
|
||||
static protected $instances = [];
|
||||
|
||||
/**
|
||||
* File can never be written.
|
||||
|
||||
+7
-8
@@ -18,7 +18,7 @@ class PhpFile extends File
|
||||
/**
|
||||
* @var array|File[]
|
||||
*/
|
||||
static protected $instances = array();
|
||||
static protected $instances = [];
|
||||
|
||||
/**
|
||||
* Saves PHP file and invalidates opcache.
|
||||
@@ -43,7 +43,7 @@ class PhpFile extends File
|
||||
/**
|
||||
* Check contents and make sure it is in correct format.
|
||||
*
|
||||
* @param array $var
|
||||
* @param array|object $var
|
||||
* @return array
|
||||
* @throws \RuntimeException
|
||||
*/
|
||||
@@ -75,20 +75,20 @@ class PhpFile extends File
|
||||
* @param array $a The array to get as a string.
|
||||
* @param int $level Used internally to indent rows.
|
||||
*
|
||||
* @return array
|
||||
* @return string
|
||||
*/
|
||||
protected function encodeArray(array $a, $level = 0)
|
||||
{
|
||||
$r = [];
|
||||
foreach ($a as $k => $v) {
|
||||
if (is_array($v) || is_object($v)) {
|
||||
$r[] = var_export($k, true) . " => " . $this->encodeArray((array) $v, $level + 1);
|
||||
$r[] = var_export($k, true) . ' => ' . $this->encodeArray((array) $v, $level + 1);
|
||||
} else {
|
||||
$r[] = var_export($k, true) . " => " . var_export($v, true);
|
||||
$r[] = var_export($k, true) . ' => ' . var_export($v, true);
|
||||
}
|
||||
}
|
||||
|
||||
$space = str_repeat(" ", $level);
|
||||
$space = str_repeat(' ', $level);
|
||||
return "[\n {$space}" . implode(",\n {$space}", $r) . "\n{$space}]";
|
||||
}
|
||||
|
||||
@@ -100,7 +100,6 @@ class PhpFile extends File
|
||||
*/
|
||||
protected function decode($var)
|
||||
{
|
||||
$var = (array) include $this->filename;
|
||||
return $var;
|
||||
return (array) include $this->filename;
|
||||
}
|
||||
}
|
||||
|
||||
+71
-15
@@ -3,7 +3,8 @@ namespace RocketTheme\Toolbox\File;
|
||||
|
||||
use Symfony\Component\Yaml\Exception\DumpException;
|
||||
use Symfony\Component\Yaml\Exception\ParseException;
|
||||
use \Symfony\Component\Yaml\Yaml as YamlParser;
|
||||
use Symfony\Component\Yaml\Yaml as YamlParser;
|
||||
use RocketTheme\Toolbox\Compat\Yaml\Yaml as FallbackYamlParser;
|
||||
|
||||
/**
|
||||
* Implements YAML File reader.
|
||||
@@ -17,7 +18,27 @@ class YamlFile extends File
|
||||
/**
|
||||
* @var array|File[]
|
||||
*/
|
||||
static protected $instances = array();
|
||||
static protected $instances = [];
|
||||
|
||||
static protected $globalSettings = [
|
||||
'compat' => true,
|
||||
'native' => true
|
||||
];
|
||||
|
||||
/**
|
||||
* Set/get settings.
|
||||
*
|
||||
* @param array $settings
|
||||
* @return array
|
||||
*/
|
||||
public static function globalSettings(array $settings = null)
|
||||
{
|
||||
if ($settings !== null) {
|
||||
static::$globalSettings = $settings;
|
||||
}
|
||||
|
||||
return static::$globalSettings;
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
@@ -29,6 +50,38 @@ class YamlFile extends File
|
||||
$this->extension = '.yaml';
|
||||
}
|
||||
|
||||
/**
|
||||
* Set/get settings.
|
||||
*
|
||||
* @param array $settings
|
||||
* @return array
|
||||
*/
|
||||
public function settings(array $settings = null)
|
||||
{
|
||||
if ($settings !== null) {
|
||||
$this->settings = $settings;
|
||||
}
|
||||
|
||||
return $this->settings + static::$globalSettings;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get setting.
|
||||
*
|
||||
* @param string $setting
|
||||
* @param mixed $default
|
||||
* @return mixed
|
||||
*/
|
||||
public function setting($setting, $default = null)
|
||||
{
|
||||
$value = parent::setting($setting);
|
||||
if (null === $value) {
|
||||
$value = isset(static::$globalSettings[$setting]) ? static::$globalSettings[$setting] : $default;
|
||||
}
|
||||
|
||||
return $value;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check contents and make sure it is in correct format.
|
||||
*
|
||||
@@ -43,7 +96,7 @@ class YamlFile extends File
|
||||
/**
|
||||
* Encode contents into RAW string.
|
||||
*
|
||||
* @param string $var
|
||||
* @param array $var
|
||||
* @return string
|
||||
* @throws DumpException
|
||||
*/
|
||||
@@ -61,24 +114,27 @@ class YamlFile extends File
|
||||
*/
|
||||
protected function decode($var)
|
||||
{
|
||||
$data = false;
|
||||
|
||||
// Try native PECL YAML PHP extension first if available.
|
||||
if ($this->setting('native') && function_exists('yaml_parse')) {
|
||||
if ($this->setting('compat', true)) {
|
||||
// Fix illegal @ start character.
|
||||
$data = preg_replace('/ (@[\w\.\-]*)/', " '\${1}'", $var);
|
||||
} else {
|
||||
$data = $var;
|
||||
}
|
||||
|
||||
if ($this->setting('native', true) && function_exists('yaml_parse')) {
|
||||
// Safely decode YAML.
|
||||
$saved = @ini_get('yaml.decode_php');
|
||||
@ini_set('yaml.decode_php', 0);
|
||||
$data = @yaml_parse($data);
|
||||
$data = @yaml_parse($var);
|
||||
@ini_set('yaml.decode_php', $saved);
|
||||
|
||||
if ($data !== false) {
|
||||
return (array) $data;
|
||||
}
|
||||
}
|
||||
|
||||
return $data !== false ? $data : (array) YamlParser::parse($var);
|
||||
try {
|
||||
return (array) YamlParser::parse($var);
|
||||
} catch (ParseException $e) {
|
||||
if ($this->setting('compat', true)) {
|
||||
return (array) FallbackYamlParser::parse($var);
|
||||
}
|
||||
|
||||
throw $e;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user