annotate core/lib/Drupal/Core/Extension/InfoParserDynamic.php @ 19:fa3358dc1485 tip

Add ndrum files
author Chris Cannam
date Wed, 28 Aug 2019 13:14:47 +0100
parents 129ea1e6d783
children
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@17 34 // Special backwards compatible handling profiles and their 'dependencies'
Chris@17 35 // key.
Chris@17 36 if ($parsed_info['type'] === 'profile' && isset($parsed_info['dependencies']) && !array_key_exists('install', $parsed_info)) {
Chris@17 37 // Only trigger the deprecation message if we are actually using the
Chris@17 38 // profile with the missing 'install' key. This avoids triggering the
Chris@17 39 // deprecation when scanning all the available install profiles.
Chris@17 40 global $install_state;
Chris@17 41 if (isset($install_state['parameters']['profile'])) {
Chris@17 42 $pattern = '@' . preg_quote(DIRECTORY_SEPARATOR . $install_state['parameters']['profile'] . '.info.yml') . '$@';
Chris@17 43 if (preg_match($pattern, $filename)) {
Chris@17 44 @trigger_error("The install profile $filename only implements a 'dependencies' key. As of Drupal 8.6.0 profile's support a new 'install' key for modules that should be installed but not depended on. See https://www.drupal.org/node/2952947.", E_USER_DEPRECATED);
Chris@17 45 }
Chris@17 46 }
Chris@17 47 // Move dependencies to install so that if a profile has both
Chris@17 48 // dependencies and install then dependencies are real.
Chris@17 49 $parsed_info['install'] = $parsed_info['dependencies'];
Chris@17 50 $parsed_info['dependencies'] = [];
Chris@17 51 }
Chris@0 52 }
Chris@0 53 return $parsed_info;
Chris@0 54 }
Chris@0 55
Chris@0 56 /**
Chris@0 57 * Returns an array of keys required to exist in .info.yml file.
Chris@0 58 *
Chris@0 59 * @return array
Chris@0 60 * An array of required keys.
Chris@0 61 */
Chris@0 62 protected function getRequiredKeys() {
Chris@0 63 return ['type', 'core', 'name'];
Chris@0 64 }
Chris@0 65
Chris@0 66 }