3 namespace Drupal\drupalmoduleupgrader\Plugin\DMU\Converter;
5 use Drupal\drupalmoduleupgrader\ConverterBase;
6 use Drupal\drupalmoduleupgrader\TargetInterface;
11 * description = @Translation("Converts Drupal 7 info files to Drupal 8.")
14 class InfoToYAML extends ConverterBase {
19 public function convert(TargetInterface $target) {
20 $info_file = $target->getPath('.info');
22 $info = self::parseInfo($info_file);
23 $info['core'] = '8.x';
24 $info['type'] = 'module';
26 if (isset($info['dependencies'])) {
27 // array_values() is called in order to reindex the array. Issue #2340207
28 $info['dependencies'] = array_values(array_diff($info['dependencies'], ['ctools', 'list']));
31 unset($info['files'], $info['configure'], $info['datestamp'], $info['version'], $info['project']);
32 $this->writeInfo($target, 'info', $info);
36 * Parses a D7 info file. This is copied straight outta the D7 function
37 * drupal_parse_info_format().
39 public static function parseInfo($file) {
41 $constants = get_defined_constants();
42 $data = file_get_contents($file);
45 @^\s* # Start at the beginning of a line, ignoring leading whitespace
47 [^=;\[\]]| # Key names cannot contain equal signs, semi-colons or square brackets,
48 \[[^\[\]]*\] # unless they are balanced and not nested
50 \s*=\s* # Key/value pairs are separated by equal signs (ignoring white-space)
52 ("(?:[^"]|(?<=\\\\)")*")| # Double-quoted string, which may contain slash-escaped quotes/slashes
53 (\'(?:[^\']|(?<=\\\\)\')*\')| # Single-quoted string, which may contain slash-escaped quotes/slashes
54 ([^\r\n]*?) # Non-quoted string
55 )\s*$ # Stop at the next end of a line, ignoring trailing whitespace
56 @msx', $data, $matches, PREG_SET_ORDER)) {
57 foreach ($matches as $match) {
58 // Fetch the key and value string.
60 foreach (array('key', 'value1', 'value2', 'value3') as $var) {
61 $$var = isset($match[++$i]) ? $match[$i] : '';
63 $value = stripslashes(substr($value1, 1, -1)) . stripslashes(substr($value2, 1, -1)) . $value3;
65 // Parse array syntax.
66 $keys = preg_split('/\]?\[/', rtrim($key, ']'));
67 $last = array_pop($keys);
70 // Create nested arrays.
71 foreach ($keys as $key) {
73 $key = count($parent);
75 if (!isset($parent[$key]) || !is_array($parent[$key])) {
76 $parent[$key] = array();
78 $parent = &$parent[$key];
81 // Handle PHP constants.
82 if (isset($constants[$value])) {
83 $value = $constants[$value];
86 // Insert actual value.
88 $last = count($parent);
90 $parent[$last] = $value;