Drupal investigation

Yaml.php 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\Yaml;
  11. use Symfony\Component\Yaml\Exception\ParseException;
  12. /**
  13. * Yaml offers convenience methods to load and dump YAML.
  14. *
  15. * @author Fabien Potencier <fabien@symfony.com>
  16. */
  17. class Yaml
  18. {
  19. /**
  20. * Parses YAML into a PHP value.
  21. *
  22. * Usage:
  23. * <code>
  24. * $array = Yaml::parse(file_get_contents('config.yml'));
  25. * print_r($array);
  26. * </code>
  27. *
  28. * As this method accepts both plain strings and file names as an input,
  29. * you must validate the input before calling this method. Passing a file
  30. * as an input is a deprecated feature and will be removed in 3.0.
  31. *
  32. * Note: the ability to pass file names to the Yaml::parse method is deprecated since version 2.2 and will be removed in 3.0. Pass the YAML contents of the file instead.
  33. *
  34. * @param string $input Path to a YAML file or a string containing YAML
  35. * @param bool $exceptionOnInvalidType True if an exception must be thrown on invalid types false otherwise
  36. * @param bool $objectSupport True if object support is enabled, false otherwise
  37. * @param bool $objectForMap True if maps should return a stdClass instead of array()
  38. *
  39. * @return mixed The YAML converted to a PHP value
  40. *
  41. * @throws ParseException If the YAML is not valid
  42. */
  43. public static function parse($input, $exceptionOnInvalidType = false, $objectSupport = false, $objectForMap = false)
  44. {
  45. // if input is a file, process it
  46. $file = '';
  47. if (strpos($input, "\n") === false && is_file($input)) {
  48. @trigger_error('The ability to pass file names to the '.__METHOD__.' method is deprecated since version 2.2 and will be removed in 3.0. Pass the YAML contents of the file instead.', E_USER_DEPRECATED);
  49. if (false === is_readable($input)) {
  50. throw new ParseException(sprintf('Unable to parse "%s" as the file is not readable.', $input));
  51. }
  52. $file = $input;
  53. $input = file_get_contents($file);
  54. }
  55. $yaml = new Parser();
  56. try {
  57. return $yaml->parse($input, $exceptionOnInvalidType, $objectSupport, $objectForMap);
  58. } catch (ParseException $e) {
  59. if ($file) {
  60. $e->setParsedFile($file);
  61. }
  62. throw $e;
  63. }
  64. }
  65. /**
  66. * Dumps a PHP value to a YAML string.
  67. *
  68. * The dump method, when supplied with an array, will do its best
  69. * to convert the array into friendly YAML.
  70. *
  71. * @param mixed $input The PHP value
  72. * @param int $inline The level where you switch to inline YAML
  73. * @param int $indent The amount of spaces to use for indentation of nested nodes
  74. * @param bool $exceptionOnInvalidType true if an exception must be thrown on invalid types (a PHP resource or object), false otherwise
  75. * @param bool $objectSupport true if object support is enabled, false otherwise
  76. *
  77. * @return string A YAML string representing the original PHP value
  78. */
  79. public static function dump($input, $inline = 2, $indent = 4, $exceptionOnInvalidType = false, $objectSupport = false)
  80. {
  81. if ($indent < 1) {
  82. throw new \InvalidArgumentException('The indentation must be greater than zero.');
  83. }
  84. $yaml = new Dumper();
  85. $yaml->setIndentation($indent);
  86. return $yaml->dump($input, $inline, 0, $exceptionOnInvalidType, $objectSupport);
  87. }
  88. }