123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115 |
- <?php
- namespace Drupal\migrate_booster;
- use Consolidation\AnnotatedCommand\AnnotationData;
- use Symfony\Component\Console\Input\InputInterface;
- class MigrateBooster {
- protected static $alterActive;
- protected static $config;
- const CID = 'migrate_booster_enabled';
- // Startup hooks
- /**
- * Reacts on HOOK_drush_init().
- *
- * Enables/disables booster depending on a drush command invoked.
- */
- public static function bootDrush(InputInterface $input, AnnotationData $annotationData) {
- if (in_array($annotationData['command'], static::getConfig('commands'))) {
- static::enable();
- }
- else {
- static::disable();
- }
- }
- /**
- * Disables booster on Drupal and Drupal console boots.
- */
- public static function bootDrupal() {
- static::disable();
- }
- /**
- * Enables booster.
- *
- * Resets implementation cache and sets $alterActive class variable.
- *
- */
- public static function enable() {
- static::$alterActive = TRUE;
- static::reset();
- }
- /**
- * Disables booster.
- *
- * Resets implementation cache.
- */
- public static function disable() {
- static::reset();
- }
- /**
- * Resets implementations cache.
- */
- public static function reset() {
- $module_handler = \Drupal::moduleHandler();
- $module_handler->resetImplementations();
- }
- /** @noinspection PhpInconsistentReturnPointsInspection */
- /**
- * Implements hook_module_implementation_alter().
- *
- * Disables configured hooks.
- * @param $implementations
- * @param $hook
- * @return null
- */
- public static function alter(&$implementations, $hook) {
- if (!static::$alterActive) {
- return NULL;
- }
- if (!$implementations) {
- return NULL;
- }
- $hooks = static::getConfig('hooks');
- $modules = static::getConfig('modules');
- $disabled = [];
- // Disable by hook + module
- if (array_key_exists($hook, $hooks)) {
- $disabled = array_intersect_key($implementations, array_flip($hooks[$hook]));
- }
- // Disable by module
- $disabled += array_intersect_key($implementations, array_flip($modules));
- $implementations = array_diff_key($implementations, $disabled);
- /** @noinspection PhpUnusedParameterInspection */
- array_walk($disabled, function ($el, $key) use ($hook) {
- error_log('DISABLED: ' . $key . '_' . $hook);
- });
- }
- /**
- * Helper functions
- *
- * @param $key
- * @return array
- */
- protected static function getConfig($key) {
- if (!static::$config) {
- static::$config = \Drupal::config('migrate_booster.settings')->get();
- }
- if ($key && isset(static::$config[$key])) {
- return static::$config[$key];
- }
- return [];
- }
- }
|