Move into nested docroot

This commit is contained in:
Rob Davies 2017-02-13 15:31:17 +00:00
parent 83a0d3a149
commit c8b70abde9
13405 changed files with 0 additions and 0 deletions

View file

@ -0,0 +1,9 @@
<?php
namespace Drupal\Component\Serialization\Exception;
/**
* Exception thrown when a data type is invalid.
*/
class InvalidDataTypeException extends \InvalidArgumentException {
}

View file

@ -0,0 +1,36 @@
<?php
namespace Drupal\Component\Serialization;
/**
* Default serialization for JSON.
*
* @ingroup third_party
*/
class Json implements SerializationInterface {
/**
* {@inheritdoc}
*
* Uses HTML-safe strings, with several characters escaped.
*/
public static function encode($variable) {
// Encode <, >, ', &, and ".
return json_encode($variable, JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_AMP | JSON_HEX_QUOT);
}
/**
* {@inheritdoc}
*/
public static function decode($string) {
return json_decode($string, TRUE);
}
/**
* {@inheritdoc}
*/
public static function getFileExtension() {
return 'json';
}
}

View file

@ -0,0 +1,31 @@
<?php
namespace Drupal\Component\Serialization;
/**
* Default serialization for serialized PHP.
*/
class PhpSerialize implements SerializationInterface {
/**
* {@inheritdoc}
*/
public static function encode($data) {
return serialize($data);
}
/**
* {@inheritdoc}
*/
public static function decode($raw) {
return unserialize($raw);
}
/**
* {@inheritdoc}
*/
public static function getFileExtension() {
return 'serialized';
}
}

View file

@ -0,0 +1,12 @@
The Drupal Serialization Component
Thanks for using this Drupal component.
You can participate in its development on Drupal.org, through our issue system:
https://www.drupal.org/project/issues/drupal
You can get the full Drupal repo here:
https://www.drupal.org/project/drupal/git-instructions
You can browse the full Drupal repo here:
http://cgit.drupalcode.org/drupal

View file

@ -0,0 +1,40 @@
<?php
namespace Drupal\Component\Serialization;
/**
* Defines an interface for serialization formats.
*/
interface SerializationInterface {
/**
* Encodes data into the serialization format.
*
* @param mixed $data
* The data to encode.
*
* @return string
* The encoded data.
*/
public static function encode($data);
/**
* Decodes data from the serialization format.
*
* @param string $raw
* The raw data string to decode.
*
* @return mixed
* The decoded data.
*/
public static function decode($raw);
/**
* Gets the file extension for this serialization format.
*
* @return string
* The file extension, without leading dot.
*/
public static function getFileExtension();
}

View file

@ -0,0 +1,18 @@
HOW-TO: Test this Drupal component
In order to test this component, you'll need to get the entire Drupal repo and
run the tests there.
You'll find the tests under core/tests/Drupal/Tests/Component.
You can get the full Drupal repo here:
https://www.drupal.org/project/drupal/git-instructions
You can find more information about running PHPUnit tests with Drupal here:
https://www.drupal.org/node/2116263
Each component in the Drupal\Component namespace has its own annotated test
group. You can use this group to run only the tests for this component. Like
this:
$ ./vendor/bin/phpunit -c core --group Serialization

View file

@ -0,0 +1,64 @@
<?php
namespace Drupal\Component\Serialization;
/**
* Provides a YAML serialization implementation.
*
* Proxy implementation that will choose the best library based on availability.
*/
class Yaml implements SerializationInterface {
/**
* The YAML implementation to use.
*
* @var \Drupal\Component\Serialization\SerializationInterface
*/
protected static $serializer;
/**
* {@inheritdoc}
*/
public static function encode($data) {
// Instead of using \Drupal\Component\Serialization\Yaml::getSerializer(),
// always using Symfony for writing the data, to reduce the risk of having
// differences if different environments (like production and development)
// do not match in terms of what YAML implementation is available.
return YamlSymfony::encode($data);
}
/**
* {@inheritdoc}
*/
public static function decode($raw) {
$serializer = static::getSerializer();
return $serializer::decode($raw);
}
/**
* {@inheritdoc}
*/
public static function getFileExtension() {
return 'yml';
}
/**
* Determines which implementation to use for parsing YAML.
*/
protected static function getSerializer() {
if (!isset(static::$serializer)) {
// Use the PECL YAML extension if it is available. It has better
// performance for file reads and is YAML compliant.
if (extension_loaded('yaml')) {
static::$serializer = YamlPecl::class;
}
else {
// Otherwise, fallback to the Symfony implementation.
static::$serializer = YamlSymfony::class;
}
}
return static::$serializer;
}
}

View file

@ -0,0 +1,101 @@
<?php
namespace Drupal\Component\Serialization;
use Drupal\Component\Serialization\Exception\InvalidDataTypeException;
/**
* Provides default serialization for YAML using the PECL extension.
*/
class YamlPecl implements SerializationInterface {
/**
* {@inheritdoc}
*/
public static function encode($data) {
static $init;
if (!isset($init)) {
ini_set('yaml.output_indent', 2);
// Do not break lines at 80 characters.
ini_set('yaml.output_width', -1);
$init = TRUE;
}
return yaml_emit($data, YAML_UTF8_ENCODING, YAML_LN_BREAK);
}
/**
* {@inheritdoc}
*/
public static function decode($raw) {
// yaml_parse() will error with an empty value.
if (!trim($raw)) {
return NULL;
}
// @todo Use ErrorExceptions when https://drupal.org/node/1247666 is in.
// yaml_parse() will throw errors instead of raising an exception. Until
// such time as Drupal supports native PHP ErrorExceptions as the error
// handler, we need to temporarily set the error handler as ::errorHandler()
// and then restore it after decoding has occurred. This allows us to turn
// parsing errors into a throwable exception.
// @see Drupal\Component\Serialization\Exception\InvalidDataTypeException
// @see http://php.net/manual/en/class.errorexception.php
set_error_handler([__CLASS__, 'errorHandler']);
$ndocs = 0;
$data = yaml_parse($raw, 0, $ndocs, [
YAML_BOOL_TAG => '\Drupal\Component\Serialization\YamlPecl::applyBooleanCallbacks',
]);
restore_error_handler();
return $data;
}
/**
* Handles errors for \Drupal\Component\Serialization\YamlPecl::decode().
*
* @param int $severity
* The severity level of the error.
* @param string $message
* The error message to display.
*
* @see \Drupal\Component\Serialization\YamlPecl::decode()
*/
public static function errorHandler($severity, $message) {
restore_error_handler();
throw new InvalidDataTypeException($message, $severity);
}
/**
* {@inheritdoc}
*/
public static function getFileExtension() {
return 'yml';
}
/**
* Applies callbacks after parsing to ignore 1.1 style booleans.
*
* @param mixed $value
* Value from YAML file.
* @param string $tag
* Tag that triggered the callback.
* @param int $flags
* Scalar entity style flags.
*
* @return string|bool
* FALSE, false, TRUE and true are returned as booleans, everything else is
* returned as a string.
*/
public static function applyBooleanCallbacks($value, $tag, $flags) {
// YAML 1.1 spec dictates that 'Y', 'N', 'y' and 'n' are booleans. But, we
// want the 1.2 behavior, so we only consider 'false', 'FALSE', 'true' and
// 'TRUE' as booleans.
if (!in_array(strtolower($value), ['false', 'true'], TRUE)) {
return $value;
}
$map = [
'false' => FALSE,
'true' => TRUE,
];
return $map[strtolower($value)];
}
}

View file

@ -0,0 +1,50 @@
<?php
namespace Drupal\Component\Serialization;
use Drupal\Component\Serialization\Exception\InvalidDataTypeException;
use Symfony\Component\Yaml\Parser;
use Symfony\Component\Yaml\Dumper;
/**
* Default serialization for YAML using the Symfony component.
*/
class YamlSymfony implements SerializationInterface {
/**
* {@inheritdoc}
*/
public static function encode($data) {
try {
$yaml = new Dumper();
$yaml->setIndentation(2);
return $yaml->dump($data, PHP_INT_MAX, 0, TRUE, FALSE);
}
catch (\Exception $e) {
throw new InvalidDataTypeException($e->getMessage(), $e->getCode(), $e);
}
}
/**
* {@inheritdoc}
*/
public static function decode($raw) {
try {
$yaml = new Parser();
// Make sure we have a single trailing newline. A very simple config like
// 'foo: bar' with no newline will fail to parse otherwise.
return $yaml->parse($raw, TRUE, FALSE);
}
catch (\Exception $e) {
throw new InvalidDataTypeException($e->getMessage(), $e->getCode(), $e);
}
}
/**
* {@inheritdoc}
*/
public static function getFileExtension() {
return 'yml';
}
}

View file

@ -0,0 +1,16 @@
{
"name": "drupal/core-serialization",
"description": "Serialization.",
"keywords": ["drupal"],
"homepage": "https://www.drupal.org/project/drupal",
"license": "GPL-2.0+",
"require": {
"php": ">=5.5.9",
"symfony/yaml": "~2.7"
},
"autoload": {
"psr-4": {
"Drupal\\Component\\Serialization\\": ""
}
}
}