YamlFileLoader.php 5.4 KB

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