MigrateBooster.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. <?php
  2. namespace Drupal\migrate_booster;
  3. use Consolidation\AnnotatedCommand\AnnotationData;
  4. use Symfony\Component\Console\Input\InputInterface;
  5. class MigrateBooster {
  6. protected static $alterActive;
  7. protected static $config;
  8. const CID = 'migrate_booster_enabled';
  9. // Startup hooks
  10. /**
  11. * Reacts on HOOK_drush_init().
  12. *
  13. * Enables/disables booster depending on a drush command invoked.
  14. */
  15. public static function bootDrush(InputInterface $input, AnnotationData $annotationData) {
  16. if (in_array($annotationData['command'], static::getConfig('commands'))) {
  17. static::enable();
  18. }
  19. else {
  20. static::disable();
  21. }
  22. }
  23. /**
  24. * Disables booster on Drupal and Drupal console boots.
  25. */
  26. public static function bootDrupal() {
  27. static::disable();
  28. }
  29. /**
  30. * Enables booster.
  31. *
  32. * Resets implementation cache and sets $alterActive class variable.
  33. *
  34. */
  35. public static function enable() {
  36. static::$alterActive = TRUE;
  37. static::reset();
  38. }
  39. /**
  40. * Disables booster.
  41. *
  42. * Resets implementation cache.
  43. */
  44. public static function disable() {
  45. static::reset();
  46. }
  47. /**
  48. * Resets implementations cache.
  49. */
  50. public static function reset() {
  51. $module_handler = \Drupal::moduleHandler();
  52. $module_handler->resetImplementations();
  53. }
  54. /** @noinspection PhpInconsistentReturnPointsInspection */
  55. /**
  56. * Implements hook_module_implementation_alter().
  57. *
  58. * Disables configured hooks.
  59. * @param $implementations
  60. * @param $hook
  61. * @return null
  62. */
  63. public static function alter(&$implementations, $hook) {
  64. if (!static::$alterActive) {
  65. return NULL;
  66. }
  67. if (!$implementations) {
  68. return NULL;
  69. }
  70. $hooks = static::getConfig('hooks');
  71. $modules = static::getConfig('modules');
  72. $disabled = [];
  73. // Disable by hook + module
  74. if (array_key_exists($hook, $hooks)) {
  75. $disabled = array_intersect_key($implementations, array_flip($hooks[$hook]));
  76. }
  77. // Disable by module
  78. $disabled += array_intersect_key($implementations, array_flip($modules));
  79. $implementations = array_diff_key($implementations, $disabled);
  80. /** @noinspection PhpUnusedParameterInspection */
  81. array_walk($disabled, function ($el, $key) use ($hook) {
  82. error_log('DISABLED: ' . $key . '_' . $hook);
  83. });
  84. }
  85. /**
  86. * Helper functions
  87. *
  88. * @param $key
  89. * @return array
  90. */
  91. protected static function getConfig($key) {
  92. if (!static::$config) {
  93. static::$config = \Drupal::config('migrate_booster.settings')->get();
  94. }
  95. if ($key && isset(static::$config[$key])) {
  96. return static::$config[$key];
  97. }
  98. return [];
  99. }
  100. }