/**
  * {@inheritdoc}
  */
 public function alter(&$cache, &$context1 = NULL, &$context2 = NULL)
 {
     // Sort the registry alphabetically (for easier debugging).
     ksort($cache);
     // Ensure paths to templates are set properly. This allows templates to
     // be moved around in a theme without having to constantly ensuring that
     // the theme's hook_theme() definitions have the correct static "path" set.
     foreach ($this->currentTheme->getAncestry() as $ancestor) {
         $current_theme = $ancestor->getName() === $this->currentTheme->getName();
         $theme_path = $ancestor->getPath();
         foreach ($ancestor->fileScan('/\\.html\\.twig$/', 'templates') as $file) {
             $hook = str_replace('-', '_', str_replace('.html.twig', '', $file->filename));
             $path = dirname($file->uri);
             $incomplete = !isset($cache[$hook]) || strrpos($hook, '__');
             if (!isset($cache[$hook])) {
                 $cache[$hook] = [];
             }
             $cache[$hook]['path'] = $path;
             $cache[$hook]['type'] = $current_theme ? 'theme' : 'base_theme';
             $cache[$hook]['theme path'] = $theme_path;
             if ($incomplete) {
                 $cache[$hook]['incomplete preprocess functions'] = TRUE;
             }
         }
     }
     // Discover all the theme's preprocess plugins.
     $preprocess_manager = new PreprocessManager($this->currentTheme);
     $plugins = $preprocess_manager->getDefinitions();
     ksort($plugins, SORT_NATURAL);
     // Iterate over the preprocess plugins.
     foreach ($plugins as $plugin_id => $definition) {
         $incomplete = !isset($cache[$plugin_id]) || strrpos($plugin_id, '__');
         if (!isset($cache[$plugin_id])) {
             $cache[$plugin_id] = [];
         }
         array_walk($cache, function (&$info, $hook) use($plugin_id, $definition) {
             if ($hook === $plugin_id || strpos($hook, $plugin_id . '__') === 0) {
                 if (!isset($info['preprocess functions'])) {
                     $info['preprocess functions'] = [];
                 }
                 // Due to a limitation in \Drupal\Core\Theme\ThemeManager::render,
                 // callbacks must be functions and not classes. We always specify
                 // "materialize_preprocess" here and then assign the plugin ID to a
                 // separate property that we can later intercept and properly invoke.
                 // @todo Revisit if/when preprocess callbacks can be any callable.
                 Materialize::addCallback($info['preprocess functions'], 'materialize_preprocess', $definition['replace'], $definition['action']);
                 $info['preprocess functions'] = array_unique($info['preprocess functions']);
                 $info['materialize preprocess'] = $plugin_id;
             }
         });
         if ($incomplete) {
             $cache[$plugin_id]['incomplete preprocess functions'] = TRUE;
         }
     }
     // Allow core to post process.
     $this->postProcessExtension($cache, $this->theme);
 }