retrofit-drupal / retrofit

Retrofit provides compatibility layers to run legacy Drupal code.
https://retrofit-drupal.com
MIT License
74 stars 3 forks source link

Provide class loader for .info files #170

Closed darrenoh closed 4 months ago

darrenoh commented 5 months ago

Drupal 7 parses files listed in .info and adds all classes discovered to a registry table so it knows which file to include when the class is used. Drupal 10 uses PSR4 class discovery. We could save a lot of work if we provide Drupal 10 with a way to include class files from Drupal 7 modules.

mglaman commented 5 months ago

Scratch notes from BoF

Handling files[] in .info

https://git.drupalcode.org/project/drupal/-/blob/7.x/includes/registry.inc?ref_type=heads#L53

      foreach ($module->info['files'] as $file) {
        $files["$dir/$file"] = array('module' => $module->name, 'weight' => $module->weight);
      }

goes through that, loads each file content and uses regex to find all class/interface/trait entries and dumps them into the table. Uses a LIKE query on names found and returns file path.

function _registry_parse_file($filename, $contents, $module = '', $weight = 0) {
  if (preg_match_all('/^\s*(?:abstract|final)?\s*(class|interface|trait)\s+([a-zA-Z0-9_]+)/m', $contents, $matches)) {
    foreach ($matches[2] as $key => $name) {
      db_merge('registry')
        ->key(array(
          'name' => $name,
          'type' => $matches[1][$key],
        ))
        ->fields(array(
          'filename' => $filename,
          'module' => $module,
          'weight' => $weight,
        ))
        ->execute();
    }
mglaman commented 4 months ago

Working on this. Starting with parsing from info.yml, then into .info

mglaman commented 4 months ago

https://github.com/retrofit-drupal/retrofit/pull/173 has a very rudimentary start, assuming info.yml has files array. Horrible for performance. But I want to get it working and then figure out how to cache it