Skip to content

Commit

Permalink
Added compatibility mode to fall back to Symfony YAML 2.8 when needed
Browse files Browse the repository at this point in the history
  • Loading branch information
mahagr committed Apr 20, 2018
1 parent fae2aa4 commit 261ea62
Show file tree
Hide file tree
Showing 11 changed files with 69 additions and 31 deletions.
9 changes: 5 additions & 4 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 5 additions & 1 deletion system/config/system.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ twig:
cache: true # Set to true to enable Twig caching
debug: true # Enable Twig debug
auto_reload: true # Refresh cache on changes
autoescape: false # Autoescape Twig vars
autoescape: false # Autoescape Twig vars (DEPRECATED, always enabled in strict mode)
undefined_functions: true # Allow undefined functions
undefined_filters: true # Allow undefined filters
umask_fix: false # By default Twig creates cached files as 755, fix switches this to 775
Expand Down Expand Up @@ -146,3 +146,7 @@ gpm:
method: 'auto' # Either 'curl', 'fopen' or 'auto'. 'auto' will try fopen first and if not available cURL
verify_peer: true # Sometimes on some systems (Windows most commonly) GPM is unable to connect because the SSL certificate cannot be verified. Disabling this setting might help.
official_gpm_only: true # By default GPM direct-install will only allow URLs via the official GPM proxy to ensure security

strict_mode:
yaml_compat: true # Grav 1.5+: Enables YAML backwards compatibility
twig_compat: true # Grav 1.5+: Enables deprecated Twig autoescape setting (autoescape: false)
20 changes: 14 additions & 6 deletions system/src/Grav/Common/Data/Validation.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
use Grav\Common\Grav;
use Grav\Common\Utils;
use Symfony\Component\Yaml\Exception\ParseException;
use Symfony\Component\Yaml\Parser;
use Symfony\Component\Yaml\Yaml;
use RocketTheme\Toolbox\Compat\Yaml\Yaml as FallbackYaml;

class Validation
{
Expand Down Expand Up @@ -643,13 +643,21 @@ protected static function filterList($value, array $params, array $field)

public static function filterYaml($value, $params)
{
if (!is_string($value)) {
return $value;
}

try {
if (is_string($value)) {
return (array) Yaml::parse($value);
} else {
return $value;
}
return (array) Yaml::parse($value);
} catch (ParseException $e) {
// If YAML compatibility mode is set on, fall back to older YAML parser.
if (Grav::instance()['config']->get('system.strict_mode.yaml_compat', true)) {
try {
return (array) FallbackYaml::parse($value);
} catch (ParseException $e2) {
}
}

return $value;
}
}
Expand Down
3 changes: 0 additions & 3 deletions system/src/Grav/Common/File/CompiledFile.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,6 @@ trait CompiledFile
*/
public function content($var = null)
{
// Set some options
$this->settings(['native' => true, 'compat' => true]);

try {
// If nothing has been loaded, attempt to get pre-compiled version of the file first.
if ($var === null && $this->raw === null && $this->content === null) {
Expand Down
11 changes: 8 additions & 3 deletions system/src/Grav/Common/GPM/GPM.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
use Grav\Common\Inflector;
use Grav\Common\Iterator;
use Grav\Common\Utils;
use Symfony\Component\Yaml\Yaml;
use RocketTheme\Toolbox\File\YamlFile;

class GPM extends Iterator
{
Expand Down Expand Up @@ -624,7 +624,10 @@ public static function getBlueprints($source)
return false;
}

$blueprint = (array)Yaml::parse(file_get_contents($blueprint_file));
$file = YamlFile::instance($blueprint_file);
$blueprint = (array)$file->content();
$file->free();

return $blueprint;
}

Expand Down Expand Up @@ -873,7 +876,9 @@ public function getDependencies($packages)
// get currently installed version
$locator = Grav::instance()['locator'];
$blueprints_path = $locator->findResource('plugins://' . $dependency_slug . DS . 'blueprints.yaml');
$package_yaml = Yaml::parse(file_get_contents($blueprints_path));
$file = YamlFile::instance($blueprints_path);
$package_yaml = $file->content();
$file->free();
$currentlyInstalledVersion = $package_yaml['version'];

// if requirement is next significant release, check is compatible with currently installed version, might not be
Expand Down
10 changes: 5 additions & 5 deletions system/src/Grav/Common/Page/Page.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use Grav\Common\Cache;
use Grav\Common\Config\Config;
use Grav\Common\Data\Blueprint;
use Grav\Common\File\CompiledYamlFile;
use Grav\Common\Filesystem\Folder;
use Grav\Common\Grav;
use Grav\Common\Language\Language;
Expand Down Expand Up @@ -318,8 +319,6 @@ public function header($var = null)
if (!$this->header) {
$file = $this->file();
if ($file) {
// Set some options
$file->settings(['native' => true, 'compat' => true]);
try {
$this->raw_content = $file->markdown();
$this->frontmatter = $file->frontmatter();
Expand All @@ -328,11 +327,12 @@ public function header($var = null)
if (!Utils::isAdminPlugin()) {
// If there's a `frontmatter.yaml` file merge that in with the page header
// note page's own frontmatter has precedence and will overwrite any defaults
$frontmatter_file = $this->path . '/' . $this->folder . '/frontmatter.yaml';
if (file_exists($frontmatter_file)) {
$frontmatter_data = (array)Yaml::parse(file_get_contents($frontmatter_file));
$frontmatterFile = CompiledYamlFile::instance($this->path . '/' . $this->folder . '/frontmatter.yaml');
if ($frontmatterFile->exists()) {
$frontmatter_data = (array)$frontmatterFile->content();
$this->header = (object)array_replace_recursive($frontmatter_data,
(array)$this->header);
$frontmatterFile->free();
}
// Process frontmatter with Twig if enabled
if (Grav::instance()['config']->get('system.pages.frontmatter.process_twig') === true) {
Expand Down
14 changes: 13 additions & 1 deletion system/src/Grav/Common/Service/ConfigServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use Grav\Common\Config\Setup;
use Pimple\Container;
use Pimple\ServiceProviderInterface;
use RocketTheme\Toolbox\File\YamlFile;
use RocketTheme\Toolbox\ResourceLocator\UniformResourceLocator;

class ConfigServiceProvider implements ServiceProviderInterface
Expand All @@ -31,7 +32,14 @@ public function register(Container $container)
};

$container['config'] = function ($c) {
return static::load($c);
$config = static::load($c);

// After configuration has been loaded, we can disable YAML compatibility if strict mode has been enabled.
if (!$config->get('system.strict_mode.yaml_compat', true)) {
YamlFile::globalSettings(['compat' => false, 'native' => true]);
}

return $config;
};

$container['languages'] = function ($c) {
Expand Down Expand Up @@ -65,6 +73,10 @@ public static function blueprints(Container $container)
return $blueprints->name("master-{$setup->environment}")->load();
}

/**
* @param Container $container
* @return Config
*/
public static function load(Container $container)
{
/** Setup $setup */
Expand Down
5 changes: 4 additions & 1 deletion system/src/Grav/Common/Twig/Twig.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,10 @@ public function init()
$params['cache'] = new \Twig_Cache_Filesystem($cachePath, \Twig_Cache_Filesystem::FORCE_BYTECODE_INVALIDATION);
}

if (!empty($this->autoescape)) {
if (!$config->get('system.strict_mode.twig_compat', true)) {
// Force autoescape on for all files if in strict mode.
$params['autoescape'] = true;
} elseif (!empty($this->autoescape)) {
$params['autoescape'] = $this->autoescape;
}

Expand Down
9 changes: 6 additions & 3 deletions system/src/Grav/Console/Cli/InstallCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
namespace Grav\Console\Cli;

use Grav\Console\ConsoleCommand;
use RocketTheme\Toolbox\File\YamlFile;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Yaml\Yaml;

class InstallCommand extends ConsoleCommand
{
Expand Down Expand Up @@ -71,9 +71,9 @@ protected function serve()

// Look for dependencies file in ROOT and USER dir
if (file_exists($this->user_path . $dependencies_file)) {
$this->config = Yaml::parse(file_get_contents($this->user_path . $dependencies_file));
$file = YamlFile::instance($this->user_path . $dependencies_file);
} elseif (file_exists($this->destination . $dependencies_file)) {
$this->config = Yaml::parse(file_get_contents($this->destination . $dependencies_file));
$file = YamlFile::instance($this->destination . $dependencies_file);
} else {
$this->output->writeln('<red>ERROR</red> Missing .dependencies file in <cyan>user/</cyan> folder');
if ($this->input->getArgument('destination')) {
Expand All @@ -85,6 +85,9 @@ protected function serve()
return;
}

$this->config = $file->content();
$file->free();

// If yaml config, process
if ($this->config) {
if (!$this->input->getOption('symlink')) {
Expand Down
6 changes: 4 additions & 2 deletions system/src/Grav/Console/ConsoleTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@
use Grav\Common\Composer;
use Grav\Common\GravTrait;
use Grav\Console\Cli\ClearCacheCommand;
use RocketTheme\Toolbox\File\YamlFile;
use Symfony\Component\Console\Formatter\OutputFormatterStyle;
use Symfony\Component\Console\Input\ArrayInput;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Yaml\Yaml;

trait ConsoleTrait
{
Expand Down Expand Up @@ -123,7 +123,9 @@ public function loadLocalConfig()
$local_config_file = $home_folder . '/.grav/config';

if (file_exists($local_config_file)) {
$this->local_config = Yaml::parse(file_get_contents($local_config_file));
$file = YamlFile::instance($local_config_file);
$this->local_config = $file->content();
$file->free();
return $local_config_file;
}

Expand Down
7 changes: 5 additions & 2 deletions system/src/Grav/Console/Gpm/VersionCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@
use Grav\Common\GPM\GPM;
use Grav\Common\GPM\Upgrader;
use Grav\Console\ConsoleCommand;
use RocketTheme\Toolbox\File\YamlFile;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Yaml\Yaml;

class VersionCommand extends ConsoleCommand
{
Expand Down Expand Up @@ -84,7 +84,10 @@ protected function serve()
}
}

$package_yaml = Yaml::parse(file_get_contents($blueprints_path));
$file = YamlFile::instance($blueprints_path);
$package_yaml = $file->content();
$file->free();

$version = $package_yaml['version'];

if (!$version) {
Expand Down

0 comments on commit 261ea62

Please sign in to comment.