Drupal investigation

YamlFileLoader.php 5.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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\Validator\Mapping\Loader;
  11. use Symfony\Component\Validator\Mapping\ClassMetadata;
  12. use Symfony\Component\Yaml\Exception\ParseException;
  13. use Symfony\Component\Yaml\Parser as YamlParser;
  14. /**
  15. * Loads validation metadata from a YAML file.
  16. *
  17. * @author Bernhard Schussek <bschussek@gmail.com>
  18. */
  19. class YamlFileLoader extends FileLoader
  20. {
  21. /**
  22. * An array of YAML class descriptions.
  23. *
  24. * @var array
  25. */
  26. protected $classes = null;
  27. /**
  28. * Caches the used YAML parser.
  29. *
  30. * @var YamlParser
  31. */
  32. private $yamlParser;
  33. /**
  34. * {@inheritdoc}
  35. */
  36. public function loadClassMetadata(ClassMetadata $metadata)
  37. {
  38. if (null === $this->classes) {
  39. if (null === $this->yamlParser) {
  40. $this->yamlParser = new YamlParser();
  41. }
  42. $this->classes = $this->parseFile($this->file);
  43. if (isset($this->classes['namespaces'])) {
  44. foreach ($this->classes['namespaces'] as $alias => $namespace) {
  45. $this->addNamespaceAlias($alias, $namespace);
  46. }
  47. unset($this->classes['namespaces']);
  48. }
  49. }
  50. if (isset($this->classes[$metadata->getClassName()])) {
  51. $classDescription = $this->classes[$metadata->getClassName()];
  52. $this->loadClassMetadataFromYaml($metadata, $classDescription);
  53. return true;
  54. }
  55. return false;
  56. }
  57. /**
  58. * Parses a collection of YAML nodes.
  59. *
  60. * @param array $nodes The YAML nodes
  61. *
  62. * @return array An array of values or Constraint instances
  63. */
  64. protected function parseNodes(array $nodes)
  65. {
  66. $values = array();
  67. foreach ($nodes as $name => $childNodes) {
  68. if (is_numeric($name) && is_array($childNodes) && 1 === count($childNodes)) {
  69. $options = current($childNodes);
  70. if (is_array($options)) {
  71. $options = $this->parseNodes($options);
  72. }
  73. $values[] = $this->newConstraint(key($childNodes), $options);
  74. } else {
  75. if (is_array($childNodes)) {
  76. $childNodes = $this->parseNodes($childNodes);
  77. }
  78. $values[$name] = $childNodes;
  79. }
  80. }
  81. return $values;
  82. }
  83. /**
  84. * Loads the YAML class descriptions from the given file.
  85. *
  86. * @param string $path The path of the YAML file
  87. *
  88. * @return array The class descriptions
  89. *
  90. * @throws \InvalidArgumentException If the file could not be loaded or did
  91. * not contain a YAML array
  92. */
  93. private function parseFile($path)
  94. {
  95. try {
  96. $classes = $this->yamlParser->parse(file_get_contents($path));
  97. } catch (ParseException $e) {
  98. throw new \InvalidArgumentException(sprintf('The file "%s" does not contain valid YAML.', $path), 0, $e);
  99. }
  100. // empty file
  101. if (null === $classes) {
  102. return array();
  103. }
  104. // not an array
  105. if (!is_array($classes)) {
  106. throw new \InvalidArgumentException(sprintf('The file "%s" must contain a YAML array.', $this->file));
  107. }
  108. return $classes;
  109. }
  110. /**
  111. * Loads the validation metadata from the given YAML class description.
  112. *
  113. * @param ClassMetadata $metadata The metadata to load
  114. * @param array $classDescription The YAML class description
  115. */
  116. private function loadClassMetadataFromYaml(ClassMetadata $metadata, array $classDescription)
  117. {
  118. if (isset($classDescription['group_sequence_provider'])) {
  119. $metadata->setGroupSequenceProvider(
  120. (bool) $classDescription['group_sequence_provider']
  121. );
  122. }
  123. if (isset($classDescription['group_sequence'])) {
  124. $metadata->setGroupSequence($classDescription['group_sequence']);
  125. }
  126. if (isset($classDescription['constraints']) && is_array($classDescription['constraints'])) {
  127. foreach ($this->parseNodes($classDescription['constraints']) as $constraint) {
  128. $metadata->addConstraint($constraint);
  129. }
  130. }
  131. if (isset($classDescription['properties']) && is_array($classDescription['properties'])) {
  132. foreach ($classDescription['properties'] as $property => $constraints) {
  133. if (null !== $constraints) {
  134. foreach ($this->parseNodes($constraints) as $constraint) {
  135. $metadata->addPropertyConstraint($property, $constraint);
  136. }
  137. }
  138. }
  139. }
  140. if (isset($classDescription['getters']) && is_array($classDescription['getters'])) {
  141. foreach ($classDescription['getters'] as $getter => $constraints) {
  142. if (null !== $constraints) {
  143. foreach ($this->parseNodes($constraints) as $constraint) {
  144. $metadata->addGetterConstraint($getter, $constraint);
  145. }
  146. }
  147. }
  148. }
  149. }
  150. }