Drupal investigation

YamlFileLoader.php 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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\Serializer\Mapping\Loader;
  11. use Symfony\Component\Serializer\Exception\MappingException;
  12. use Symfony\Component\Serializer\Mapping\AttributeMetadata;
  13. use Symfony\Component\Serializer\Mapping\ClassMetadataInterface;
  14. use Symfony\Component\Yaml\Parser;
  15. /**
  16. * YAML File Loader.
  17. *
  18. * @author Kévin Dunglas <dunglas@gmail.com>
  19. */
  20. class YamlFileLoader extends FileLoader
  21. {
  22. private $yamlParser;
  23. /**
  24. * An array of YAML class descriptions.
  25. *
  26. * @var array
  27. */
  28. private $classes = null;
  29. /**
  30. * {@inheritdoc}
  31. */
  32. public function loadClassMetadata(ClassMetadataInterface $classMetadata)
  33. {
  34. if (null === $this->classes) {
  35. if (!stream_is_local($this->file)) {
  36. throw new MappingException(sprintf('This is not a local file "%s".', $this->file));
  37. }
  38. if (null === $this->yamlParser) {
  39. $this->yamlParser = new Parser();
  40. }
  41. $classes = $this->yamlParser->parse(file_get_contents($this->file));
  42. if (empty($classes)) {
  43. return false;
  44. }
  45. // not an array
  46. if (!is_array($classes)) {
  47. throw new MappingException(sprintf('The file "%s" must contain a YAML array.', $this->file));
  48. }
  49. $this->classes = $classes;
  50. }
  51. if (!isset($this->classes[$classMetadata->getName()])) {
  52. return false;
  53. }
  54. $yaml = $this->classes[$classMetadata->getName()];
  55. if (isset($yaml['attributes']) && is_array($yaml['attributes'])) {
  56. $attributesMetadata = $classMetadata->getAttributesMetadata();
  57. foreach ($yaml['attributes'] as $attribute => $data) {
  58. if (isset($attributesMetadata[$attribute])) {
  59. $attributeMetadata = $attributesMetadata[$attribute];
  60. } else {
  61. $attributeMetadata = new AttributeMetadata($attribute);
  62. $classMetadata->addAttributeMetadata($attributeMetadata);
  63. }
  64. if (isset($data['groups'])) {
  65. if (!is_array($data['groups'])) {
  66. throw new MappingException(sprintf('The "groups" key must be an array of strings in "%s" for the attribute "%s" of the class "%s".', $this->file, $attribute, $classMetadata->getName()));
  67. }
  68. foreach ($data['groups'] as $group) {
  69. if (!is_string($group)) {
  70. throw new MappingException(sprintf('Group names must be strings in "%s" for the attribute "%s" of the class "%s".', $this->file, $attribute, $classMetadata->getName()));
  71. }
  72. $attributeMetadata->addGroup($group);
  73. }
  74. }
  75. }
  76. }
  77. return true;
  78. }
  79. }