diff --git a/drush.services.yml b/drush.services.yml index 23d69bb30df90bfae0b6dbc89af2c884b6ce555d..16cd3c43c37bd62519c12db49ad776089a63d5e7 100644 --- a/drush.services.yml +++ b/drush.services.yml @@ -4,6 +4,7 @@ services: arguments: - '@extension.list.profile' - '%install_profile%' + - '@plugin.cache_clearer' tags: - { name: drush.command } diff --git a/src/Commands/Hooks.php b/src/Commands/Hooks.php index 04aa041353f3b63578edb9fabcb46011d58a0cb2..a56d8e5bdef9ecbd7430e037132aa2de09d52006 100644 --- a/src/Commands/Hooks.php +++ b/src/Commands/Hooks.php @@ -5,6 +5,7 @@ namespace Drupal\lightning_core\Commands; use Consolidation\AnnotatedCommand\CommandData; use Consolidation\OutputFormatters\Options\FormatterOptions; use Drupal\Core\Extension\ProfileExtensionList; +use Drupal\Core\Plugin\CachedDiscoveryClearerInterface; use Drush\Commands\DrushCommands; use Symfony\Component\Console\Event\ConsoleCommandEvent; @@ -27,6 +28,13 @@ class Hooks extends DrushCommands { */ private $installProfile; + /** + * The plugin cache clearer service. + * + * @var \Drupal\Core\Plugin\CachedDiscoveryClearerInterface + */ + private $pluginCacheClearer; + /** * Hooks constructor. * @@ -34,25 +42,32 @@ class Hooks extends DrushCommands { * The profile extension list service. * @param string $install_profile * The install_profile parameter. + * @param \Drupal\Core\Plugin\CachedDiscoveryClearerInterface $plugin_cache_clearer + * The plugin cache clearer service. */ - public function __construct(ProfileExtensionList $profile_list, $install_profile) { + public function __construct(ProfileExtensionList $profile_list, $install_profile, CachedDiscoveryClearerInterface $plugin_cache_clearer) { $this->profileList = $profile_list; $this->installProfile = $install_profile; + $this->pluginCacheClearer = $plugin_cache_clearer; } /** - * Clears all caches before database updates begin. + * Clears all plugin caches before database updates begin. * * A common cause of errors during database updates is update hooks * inadvertently using stale data from the myriad caches in Drupal core and - * contributed modules. Clearing all caches before updates begin ensures that - * the system always has the freshest and most accurate data to work with, - * which is especially helpful during major surgery like a database update. + * contributed modules. To migitate this, we do a bit of cache pruning before + * database updates begin. + * + * drupal_flush_all_caches() is extremely aggressive because it rebuilds the + * router and other things, but it's a bit too much of a sledgehammer for our + * purposes. A good compromise is to clear all plugin discovery caches (which + * will include entity type definitions). * * @hook pre-command updatedb */ public function preUpdate() { - drupal_flush_all_caches(); + $this->pluginCacheClearer->clearCachedDefinitions(); } /**