Commit 7e54f17b authored by catch's avatar catch

Issue #1713564 by alexpott, sun: Make Config\FileStorage instantiate...

Issue #1713564 by alexpott, sun: Make Config\FileStorage instantiate Yaml\Dumper and Yaml\Parser only once.
parent bfc2352b
......@@ -116,7 +116,7 @@ public function rename($name, $new_name) {
/**
* Implements Drupal\Core\Config\StorageInterface::encode().
*/
public static function encode($data) {
public function encode($data) {
return serialize($data);
}
......@@ -126,7 +126,7 @@ public static function encode($data) {
* @throws ErrorException
* unserialize() triggers E_NOTICE if the string cannot be unserialized.
*/
public static function decode($raw) {
public function decode($raw) {
$data = @unserialize($raw);
return is_array($data) ? $data : FALSE;
}
......
......@@ -7,7 +7,8 @@
namespace Drupal\Core\Config;
use Symfony\Component\Yaml\Yaml;
use Symfony\Component\Yaml\Dumper;
use Symfony\Component\Yaml\Parser;
/**
* Defines the file storage controller.
......@@ -23,6 +24,20 @@ class FileStorage implements StorageInterface {
*/
protected $options;
/**
* A shared YAML dumper instance.
*
* @var Symfony\Component\Yaml\Dumper
*/
protected $dumper;
/**
* A shared YAML parser instance.
*
* @var Symfony\Component\Yaml\Parser
*/
protected $parser;
/**
* Implements Drupal\Core\Config\StorageInterface::__construct().
*/
......@@ -118,15 +133,39 @@ public function rename($name, $new_name) {
return TRUE;
}
/**
* Gets the YAML dumper instance.
*
* @return Symfony\Component\Yaml\Dumper
*/
protected function getDumper() {
if (!isset($this->dumper)) {
$this->dumper = new Dumper();
}
return $this->dumper;
}
/**
* Gets the YAML parser instance.
*
* @return Symfony\Component\Yaml\Parser
*/
protected function getParser() {
if (!isset($this->parser)) {
$this->parser = new Parser();
}
return $this->parser;
}
/**
* Implements Drupal\Core\Config\StorageInterface::encode().
*
* @throws Symfony\Component\Yaml\Exception\DumpException
*/
public static function encode($data) {
public function encode($data) {
// The level where you switch to inline YAML is set to PHP_INT_MAX to ensure
// this does not occur.
return Yaml::dump($data, PHP_INT_MAX);
return $this->getDumper()->dump($data, PHP_INT_MAX);
}
/**
......@@ -134,8 +173,8 @@ public static function encode($data) {
*
* @throws Symfony\Component\Yaml\Exception\ParseException
*/
public static function decode($raw) {
$data = Yaml::parse($raw);
public function decode($raw) {
$data = $this->getParser()->parse($raw);
// A simple string is valid YAML for any reason.
if (!is_array($data)) {
return FALSE;
......
......@@ -59,14 +59,14 @@ public function rename($name, $new_name) {
/**
* Implements Drupal\Core\Config\StorageInterface::encode().
*/
public static function encode($data) {
public function encode($data) {
return $data;
}
/**
* Implements Drupal\Core\Config\StorageInterface::decode().
*/
public static function decode($raw) {
public function decode($raw) {
return $raw;
}
......
......@@ -85,7 +85,7 @@ public function rename($name, $new_name);
* This is a publicly accessible static method to allow for alternative
* usages in data conversion scripts and also tests.
*/
public static function encode($data);
public function encode($data);
/**
* Decodes configuration data from the storage-specific format.
......@@ -99,7 +99,7 @@ public static function encode($data);
* This is a publicly accessible static method to allow for alternative
* usages in data conversion scripts and also tests.
*/
public static function decode($raw);
public function decode($raw);
/**
* Gets configuration object names starting with a given prefix.
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment