Updated Drupal to 8.6. This goes with the following updates because it's possible...
[yaffs-website] / vendor / symfony / validator / Mapping / Loader / YamlFileLoader.php
index 16212833dc1696f88ab0baf1aba616222d7b1ed6..c1395bb2fccaa6637e589a5c7627706d413ead49 100644 (file)
@@ -14,6 +14,7 @@ namespace Symfony\Component\Validator\Mapping\Loader;
 use Symfony\Component\Validator\Mapping\ClassMetadata;
 use Symfony\Component\Yaml\Exception\ParseException;
 use Symfony\Component\Yaml\Parser as YamlParser;
+use Symfony\Component\Yaml\Yaml;
 
 /**
  * Loads validation metadata from a YAML file.
@@ -42,19 +43,7 @@ class YamlFileLoader extends FileLoader
     public function loadClassMetadata(ClassMetadata $metadata)
     {
         if (null === $this->classes) {
-            if (null === $this->yamlParser) {
-                $this->yamlParser = new YamlParser();
-            }
-
-            $this->classes = $this->parseFile($this->file);
-
-            if (isset($this->classes['namespaces'])) {
-                foreach ($this->classes['namespaces'] as $alias => $namespace) {
-                    $this->addNamespaceAlias($alias, $namespace);
-                }
-
-                unset($this->classes['namespaces']);
-            }
+            $this->loadClassesFromYaml();
         }
 
         if (isset($this->classes[$metadata->getClassName()])) {
@@ -68,6 +57,20 @@ class YamlFileLoader extends FileLoader
         return false;
     }
 
+    /**
+     * Return the names of the classes mapped in this file.
+     *
+     * @return string[] The classes names
+     */
+    public function getMappedClasses()
+    {
+        if (null === $this->classes) {
+            $this->loadClassesFromYaml();
+        }
+
+        return array_keys($this->classes);
+    }
+
     /**
      * Parses a collection of YAML nodes.
      *
@@ -80,16 +83,16 @@ class YamlFileLoader extends FileLoader
         $values = array();
 
         foreach ($nodes as $name => $childNodes) {
-            if (is_numeric($name) && is_array($childNodes) && 1 === count($childNodes)) {
+            if (is_numeric($name) && \is_array($childNodes) && 1 === \count($childNodes)) {
                 $options = current($childNodes);
 
-                if (is_array($options)) {
+                if (\is_array($options)) {
                     $options = $this->parseNodes($options);
                 }
 
                 $values[] = $this->newConstraint(key($childNodes), $options);
             } else {
-                if (is_array($childNodes)) {
+                if (\is_array($childNodes)) {
                     $childNodes = $this->parseNodes($childNodes);
                 }
 
@@ -112,10 +115,18 @@ class YamlFileLoader extends FileLoader
      */
     private function parseFile($path)
     {
+        $prevErrorHandler = set_error_handler(function ($level, $message, $script, $line) use ($path, &$prevErrorHandler) {
+            $message = E_USER_DEPRECATED === $level ? preg_replace('/ on line \d+/', ' in "'.$path.'"$0', $message) : $message;
+
+            return $prevErrorHandler ? $prevErrorHandler($level, $message, $script, $line) : false;
+        });
+
         try {
-            $classes = $this->yamlParser->parse(file_get_contents($path));
+            $classes = $this->yamlParser->parseFile($path, Yaml::PARSE_CONSTANT);
         } catch (ParseException $e) {
             throw new \InvalidArgumentException(sprintf('The file "%s" does not contain valid YAML.', $path), 0, $e);
+        } finally {
+            restore_error_handler();
         }
 
         // empty file
@@ -124,19 +135,30 @@ class YamlFileLoader extends FileLoader
         }
 
         // not an array
-        if (!is_array($classes)) {
+        if (!\is_array($classes)) {
             throw new \InvalidArgumentException(sprintf('The file "%s" must contain a YAML array.', $this->file));
         }
 
         return $classes;
     }
 
-    /**
-     * Loads the validation metadata from the given YAML class description.
-     *
-     * @param ClassMetadata $metadata         The metadata to load
-     * @param array         $classDescription The YAML class description
-     */
+    private function loadClassesFromYaml()
+    {
+        if (null === $this->yamlParser) {
+            $this->yamlParser = new YamlParser();
+        }
+
+        $this->classes = $this->parseFile($this->file);
+
+        if (isset($this->classes['namespaces'])) {
+            foreach ($this->classes['namespaces'] as $alias => $namespace) {
+                $this->addNamespaceAlias($alias, $namespace);
+            }
+
+            unset($this->classes['namespaces']);
+        }
+    }
+
     private function loadClassMetadataFromYaml(ClassMetadata $metadata, array $classDescription)
     {
         if (isset($classDescription['group_sequence_provider'])) {
@@ -149,13 +171,13 @@ class YamlFileLoader extends FileLoader
             $metadata->setGroupSequence($classDescription['group_sequence']);
         }
 
-        if (isset($classDescription['constraints']) && is_array($classDescription['constraints'])) {
+        if (isset($classDescription['constraints']) && \is_array($classDescription['constraints'])) {
             foreach ($this->parseNodes($classDescription['constraints']) as $constraint) {
                 $metadata->addConstraint($constraint);
             }
         }
 
-        if (isset($classDescription['properties']) && is_array($classDescription['properties'])) {
+        if (isset($classDescription['properties']) && \is_array($classDescription['properties'])) {
             foreach ($classDescription['properties'] as $property => $constraints) {
                 if (null !== $constraints) {
                     foreach ($this->parseNodes($constraints) as $constraint) {
@@ -165,7 +187,7 @@ class YamlFileLoader extends FileLoader
             }
         }
 
-        if (isset($classDescription['getters']) && is_array($classDescription['getters'])) {
+        if (isset($classDescription['getters']) && \is_array($classDescription['getters'])) {
             foreach ($classDescription['getters'] as $getter => $constraints) {
                 if (null !== $constraints) {
                     foreach ($this->parseNodes($constraints) as $constraint) {