Patched to Drupal 8.4.8 level. See https://www.drupal.org/sa-core-2018-004 and patch...
[yaffs-website] / web / core / lib / Drupal / Core / Extension / InfoParserDynamic.php
1 <?php
2
3 namespace Drupal\Core\Extension;
4
5 use Drupal\Component\Serialization\Exception\InvalidDataTypeException;
6 use Drupal\Core\Serialization\Yaml;
7
8 /**
9  * Parses dynamic .info.yml files that might change during the page request.
10  */
11 class InfoParserDynamic implements InfoParserInterface {
12
13   /**
14    * {@inheritdoc}
15    */
16   public function parse($filename) {
17     if (!file_exists($filename)) {
18       $parsed_info = [];
19     }
20     else {
21       try {
22         $parsed_info = Yaml::decode(file_get_contents($filename));
23       }
24       catch (InvalidDataTypeException $e) {
25         throw new InfoParserException("Unable to parse $filename " . $e->getMessage());
26       }
27       $missing_keys = array_diff($this->getRequiredKeys(), array_keys($parsed_info));
28       if (!empty($missing_keys)) {
29         throw new InfoParserException('Missing required keys (' . implode(', ', $missing_keys) . ') in ' . $filename);
30       }
31       if (isset($parsed_info['version']) && $parsed_info['version'] === 'VERSION') {
32         $parsed_info['version'] = \Drupal::VERSION;
33       }
34     }
35     return $parsed_info;
36   }
37
38   /**
39    * Returns an array of keys required to exist in .info.yml file.
40    *
41    * @return array
42    *   An array of required keys.
43    */
44   protected function getRequiredKeys() {
45     return ['type', 'core', 'name'];
46   }
47
48 }