Mercurial > hg > isophonics-drupal-site
annotate core/lib/Drupal/Core/Extension/InfoParserDynamic.php @ 14:1fec387a4317
Update Drupal core to 8.5.2 via Composer
author | Chris Cannam |
---|---|
date | Mon, 23 Apr 2018 09:46:53 +0100 |
parents | 4c8ae668cc8c |
children | 129ea1e6d783 |
rev | line source |
---|---|
Chris@0 | 1 <?php |
Chris@0 | 2 |
Chris@0 | 3 namespace Drupal\Core\Extension; |
Chris@0 | 4 |
Chris@0 | 5 use Drupal\Component\Serialization\Exception\InvalidDataTypeException; |
Chris@0 | 6 use Drupal\Core\Serialization\Yaml; |
Chris@0 | 7 |
Chris@0 | 8 /** |
Chris@0 | 9 * Parses dynamic .info.yml files that might change during the page request. |
Chris@0 | 10 */ |
Chris@0 | 11 class InfoParserDynamic implements InfoParserInterface { |
Chris@0 | 12 |
Chris@0 | 13 /** |
Chris@0 | 14 * {@inheritdoc} |
Chris@0 | 15 */ |
Chris@0 | 16 public function parse($filename) { |
Chris@0 | 17 if (!file_exists($filename)) { |
Chris@0 | 18 $parsed_info = []; |
Chris@0 | 19 } |
Chris@0 | 20 else { |
Chris@0 | 21 try { |
Chris@0 | 22 $parsed_info = Yaml::decode(file_get_contents($filename)); |
Chris@0 | 23 } |
Chris@0 | 24 catch (InvalidDataTypeException $e) { |
Chris@0 | 25 throw new InfoParserException("Unable to parse $filename " . $e->getMessage()); |
Chris@0 | 26 } |
Chris@0 | 27 $missing_keys = array_diff($this->getRequiredKeys(), array_keys($parsed_info)); |
Chris@0 | 28 if (!empty($missing_keys)) { |
Chris@0 | 29 throw new InfoParserException('Missing required keys (' . implode(', ', $missing_keys) . ') in ' . $filename); |
Chris@0 | 30 } |
Chris@0 | 31 if (isset($parsed_info['version']) && $parsed_info['version'] === 'VERSION') { |
Chris@0 | 32 $parsed_info['version'] = \Drupal::VERSION; |
Chris@0 | 33 } |
Chris@0 | 34 } |
Chris@0 | 35 return $parsed_info; |
Chris@0 | 36 } |
Chris@0 | 37 |
Chris@0 | 38 /** |
Chris@0 | 39 * Returns an array of keys required to exist in .info.yml file. |
Chris@0 | 40 * |
Chris@0 | 41 * @return array |
Chris@0 | 42 * An array of required keys. |
Chris@0 | 43 */ |
Chris@0 | 44 protected function getRequiredKeys() { |
Chris@0 | 45 return ['type', 'core', 'name']; |
Chris@0 | 46 } |
Chris@0 | 47 |
Chris@0 | 48 } |