Further modules included.
[yaffs-website] / web / modules / contrib / drupalmoduleupgrader / src / Plugin / DMU / Converter / InfoToYAML.php
1 <?php
2
3 namespace Drupal\drupalmoduleupgrader\Plugin\DMU\Converter;
4
5 use Drupal\drupalmoduleupgrader\ConverterBase;
6 use Drupal\drupalmoduleupgrader\TargetInterface;
7
8 /**
9  * @Converter(
10  *  id = "info",
11  *  description = @Translation("Converts Drupal 7 info files to Drupal 8.")
12  * )
13  */
14 class InfoToYAML extends ConverterBase {
15
16   /**
17    * {@inheritdoc}
18    */
19   public function convert(TargetInterface $target) {
20     $info_file = $target->getPath('.info');
21
22     $info = self::parseInfo($info_file);
23     $info['core'] = '8.x';
24     $info['type'] = 'module';
25
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']));
29     }
30
31     unset($info['files'], $info['configure'], $info['datestamp'], $info['version'], $info['project']);
32     $this->writeInfo($target, 'info', $info);
33   }
34
35   /**
36    * Parses a D7 info file. This is copied straight outta the D7 function 
37    * drupal_parse_info_format().
38    */
39   public static function parseInfo($file) {
40     $info = [];
41     $constants = get_defined_constants();
42     $data = file_get_contents($file);
43
44     if (preg_match_all('
45       @^\s*                           # Start at the beginning of a line, ignoring leading whitespace
46       ((?:
47         [^=;\[\]]|                    # Key names cannot contain equal signs, semi-colons or square brackets,
48         \[[^\[\]]*\]                  # unless they are balanced and not nested
49       )+?)
50       \s*=\s*                         # Key/value pairs are separated by equal signs (ignoring white-space)
51       (?:
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.
59         $i = 0;
60         foreach (array('key', 'value1', 'value2', 'value3') as $var) {
61           $$var = isset($match[++$i]) ? $match[$i] : '';
62         }
63         $value = stripslashes(substr($value1, 1, -1)) . stripslashes(substr($value2, 1, -1)) . $value3;
64
65         // Parse array syntax.
66         $keys = preg_split('/\]?\[/', rtrim($key, ']'));
67         $last = array_pop($keys);
68         $parent = &$info;
69
70         // Create nested arrays.
71         foreach ($keys as $key) {
72           if ($key == '') {
73             $key = count($parent);
74           }
75           if (!isset($parent[$key]) || !is_array($parent[$key])) {
76             $parent[$key] = array();
77           }
78           $parent = &$parent[$key];
79         }
80
81         // Handle PHP constants.
82         if (isset($constants[$value])) {
83           $value = $constants[$value];
84         }
85
86         // Insert actual value.
87         if ($last == '') {
88           $last = count($parent);
89         }
90         $parent[$last] = $value;
91       }
92     }
93
94     return $info;
95   }
96
97 }