This project is not covered by Drupal’s security advisory policy.

…And the idea also is pretty insane and silly anyway…

A PoC module which aim to "replace" PHP classes you define in your module's info.yml file.

Usage

  1. Add a new key class_replacements to the .info.yml of the module which contains the replacement modules:
    name: Migration Manager Replacement
    type: module
    class_replacements:
      # Key: the qualified name of the class which should be replaced.
      # Value: location of the replacement file, relative to the actual module's root.
      Drupal\migrate\Plugin\MigrationPluginManager: src/migrate/MigrationPluginManager.php
      Drupal\migrate_drupal\MigrationPluginManager: src/migrate_d/MigrationPluginManager.php
    
  2. Make sure that the module depends on crema; or have a good reason not doing so 🙃
  3. Create the replacement file at the declared location:
    • Its namespace must be the namespace of the original file with the leading Drupal replaced by Crema.
    • The class name must be the same as the replaced class name.
    • If you want to extend the original class (I'm pretty sure you want to), then you can do it by adding the proper (aliased) use declaration of the original class.
    # File content of <module_root>/src/migrate/MigrationPluginManager.php
    <?php
    
    namespace Crema\migrate\Plugin;
    
    use Drupal\migrate\Plugin\MigrationPluginManager as Original;
    
    /**
     * Replaces the default migration plugin manager.
     */
    class MigrationPluginManager extends Original {
    
      protected $defaults = [
        'class' => '\Drupal\my_module\Plugin\BetterMigration',
      ];
    
      public function foo(): string {
        return 'foo';
      }
    
    }
    

Limitations

  1. You cannot replace the same PHP class (or trait or interface) more than once.
  2. Due to the way how Crema works, you cannot use debugger breakpoint neither in the replacement nor in the replaced php file.
  3. The module only can replace files with Drupal\* namespace, and not all of them, but most of them. For example you cannot replace Drupal kernels, database driver classes etc.
  4. Crema uses PHP's temporary stream to store the replaced and the replacement files before it loads them. Because of this, (for now) the files must me smaller than 2MB: https://www.php.net/manual/en/wrappers.php.php#wrappers.php.memory

Project information

  • caution Minimally maintained
    Maintainers monitor issues, but fast responses are not guaranteed.
  • Project categories: Developer tools
  • Created by huzooka on , updated
  • shield alertThis project is not covered by the security advisory policy.
    Use at your own risk! It may have publicly disclosed vulnerabilities.

Releases