YamlFileLoader.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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 method may throw an exception. Do not modify the class'
  43. // state before it completes
  44. if (false === ($classes = $this->parseFile($this->file))) {
  45. return false;
  46. }
  47. $this->classes = $classes;
  48. if (isset($this->classes['namespaces'])) {
  49. foreach ($this->classes['namespaces'] as $alias => $namespace) {
  50. $this->addNamespaceAlias($alias, $namespace);
  51. }
  52. unset($this->classes['namespaces']);
  53. }
  54. }
  55. if (isset($this->classes[$metadata->getClassName()])) {
  56. $classDescription = $this->classes[$metadata->getClassName()];
  57. $this->loadClassMetadataFromYaml($metadata, $classDescription);
  58. return true;
  59. }
  60. return false;
  61. }
  62. /**
  63. * Parses a collection of YAML nodes.
  64. *
  65. * @param array $nodes The YAML nodes
  66. *
  67. * @return array An array of values or Constraint instances
  68. */
  69. protected function parseNodes(array $nodes)
  70. {
  71. $values = array();
  72. foreach ($nodes as $name => $childNodes) {
  73. if (is_numeric($name) && is_array($childNodes) && 1 === count($childNodes)) {
  74. $options = current($childNodes);
  75. if (is_array($options)) {
  76. $options = $this->parseNodes($options);
  77. }
  78. $values[] = $this->newConstraint(key($childNodes), $options);
  79. } else {
  80. if (is_array($childNodes)) {
  81. $childNodes = $this->parseNodes($childNodes);
  82. }
  83. $values[$name] = $childNodes;
  84. }
  85. }
  86. return $values;
  87. }
  88. /**
  89. * Loads the YAML class descriptions from the given file.
  90. *
  91. * @param string $path The path of the YAML file
  92. *
  93. * @return array|null The class descriptions or null, if the file was empty
  94. *
  95. * @throws \InvalidArgumentException If the file could not be loaded or did
  96. * not contain a YAML array
  97. */
  98. private function parseFile($path)
  99. {
  100. try {
  101. $classes = $this->yamlParser->parse(file_get_contents($path));
  102. } catch (ParseException $e) {
  103. throw new \InvalidArgumentException(sprintf('The file "%s" does not contain valid YAML.', $path), 0, $e);
  104. }
  105. // empty file
  106. if (null === $classes) {
  107. return;
  108. }
  109. // not an array
  110. if (!is_array($classes)) {
  111. throw new \InvalidArgumentException(sprintf('The file "%s" must contain a YAML array.', $this->file));
  112. }
  113. return $classes;
  114. }
  115. /**
  116. * Loads the validation metadata from the given YAML class description.
  117. *
  118. * @param ClassMetadata $metadata The metadata to load
  119. * @param array $classDescription The YAML class description
  120. */
  121. private function loadClassMetadataFromYaml(ClassMetadata $metadata, array $classDescription)
  122. {
  123. if (isset($classDescription['group_sequence_provider'])) {
  124. $metadata->setGroupSequenceProvider(
  125. (bool) $classDescription['group_sequence_provider']
  126. );
  127. }
  128. if (isset($classDescription['group_sequence'])) {
  129. $metadata->setGroupSequence($classDescription['group_sequence']);
  130. }
  131. if (isset($classDescription['constraints']) && is_array($classDescription['constraints'])) {
  132. foreach ($this->parseNodes($classDescription['constraints']) as $constraint) {
  133. $metadata->addConstraint($constraint);
  134. }
  135. }
  136. if (isset($classDescription['properties']) && is_array($classDescription['properties'])) {
  137. foreach ($classDescription['properties'] as $property => $constraints) {
  138. if (null !== $constraints) {
  139. foreach ($this->parseNodes($constraints) as $constraint) {
  140. $metadata->addPropertyConstraint($property, $constraint);
  141. }
  142. }
  143. }
  144. }
  145. if (isset($classDescription['getters']) && is_array($classDescription['getters'])) {
  146. foreach ($classDescription['getters'] as $getter => $constraints) {
  147. if (null !== $constraints) {
  148. foreach ($this->parseNodes($constraints) as $constraint) {
  149. $metadata->addGetterConstraint($getter, $constraint);
  150. }
  151. }
  152. }
  153. }
  154. }
  155. }