W3cubDocs

/Drupal 8

protected function ModuleInstaller::removeCacheBins

protected ModuleInstaller::removeCacheBins($module)

Helper method for removing all cache bins registered by a given module.

Parameters

string $module: The name of the module for which to remove all registered cache bins.

File

core/lib/Drupal/Core/Extension/ModuleInstaller.php, line 496

Class

ModuleInstaller
Default implementation of the module installer.

Namespace

Drupal\Core\Extension

Code

protected function removeCacheBins($module) {
  // Remove any cache bins defined by a module.
  $service_yaml_file = drupal_get_path('module', $module) . "/$module.services.yml";
  if (file_exists($service_yaml_file)) {
    $definitions = Yaml::decode(file_get_contents($service_yaml_file));
    if (isset($definitions['services'])) {
      foreach ($definitions['services'] as $id => $definition) {
        if (isset($definition['tags'])) {
          foreach ($definition['tags'] as $tag) {
            // This works for the default cache registration and even in some
            // cases when a non-default "super" factory is used. That should
            // be extremely rare.
            if ($tag['name'] == 'cache.bin' && isset($definition['factory_service']) && isset($definition['factory_method']) && !empty($definition['arguments'])) {
              try {
                $factory = \Drupal::service($definition['factory_service']);
                if (method_exists($factory, $definition['factory_method'])) {
                  $backend = call_user_func_array(array($factory, $definition['factory_method']), $definition['arguments']);
                  if ($backend instanceof CacheBackendInterface) {
                    $backend->removeBin();
                  }
                }
              }
              catch (\Exception $e) {
                watchdog_exception('system', $e, 'Failed to remove cache bin defined by the service %id.', array('%id' => $id));
              }
            }
          }
        }
      }
    }
  }
}

© 2001–2016 by the original authors
Licensed under the GNU General Public License, version 2 and later.
Drupal is a registered trademark of Dries Buytaert.
https://api.drupal.org/api/drupal/core!lib!Drupal!Core!Extension!ModuleInstaller.php/function/ModuleInstaller::removeCacheBins/8.1.x