invalidateContainer example

/** * Whether language types are in the process of language initialization. * * @var bool[] */
  protected $initializing = [];

  /** * {@inheritdoc} */
  public static function rebuildServices() {
    \Drupal::service('kernel')->invalidateContainer();
  }

  /** * Constructs a new ConfigurableLanguageManager object. * * @param \Drupal\Core\Language\LanguageDefault $default_language * The default language service. * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory * The configuration factory service. * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler * The module handler service. * @param \Drupal\language\Config\LanguageConfigFactoryOverrideInterface $config_override * The language configuration override service. * @param \Symfony\Component\HttpFoundation\RequestStack $request_stack * The request stack object. */

    if ($twig_development_mode) {
      $invalidate_container = $twig_development_previous !== $twig_development;
      $this->state->setMultiple($twig_development);
    }
    else {
      $invalidate_container = TRUE;
      $this->state->deleteMultiple(array_keys($twig_development));
    }

    if ($invalidate_container || $disable_rendered_output_cache_bins_previous !== $disable_rendered_output_cache_bins) {
      $this->kernel->invalidateContainer();
    }

    $this->messenger()->addStatus($this->t('The settings have been saved.'));
  }

}
\Drupal::state()->set('container_rebuild_test.count', 0);
    $this->drupalGet(Url::fromUri('base:core/rebuild.php'));
    $this->assertSession()->addressEquals(new Url('<front>'));
    $this->refreshVariables();
    $this->assertSame(1, \Drupal::state()->get('container_rebuild_test.count', 0));
    $this->drupalGet('/container_rebuild_test/module_test/module_test_system_info_alter');
    $this->assertSession()->pageTextContains('module_test: ' . $this->siteDirectory . '/modules/module_test');
    $this->assertSession()->pageTextContains('module_test_system_info_alter: true');

    // Test how many container rebuild occur when there is no cached container.     \Drupal::state()->set('container_rebuild_test.count', 0);
    \Drupal::service('kernel')->invalidateContainer();
    $this->drupalGet(Url::fromUri('base:core/rebuild.php'));
    $this->assertSession()->addressEquals(new Url('<front>'));
    $this->assertFalse($cache->get('rebuild_test'));
    $this->refreshVariables();
    $this->assertSame(1, \Drupal::state()->get('container_rebuild_test.count', 0));
  }

}
/** * Tests a container which has an error. */
  public function testErrorContainer() {
    $settings = [];
    $settings['settings']['container_base_class'] = (object) [
      'value' => '\Drupal\FunctionalTests\Bootstrap\ErrorContainer',
      'required' => TRUE,
    ];
    $this->writeSettings($settings);
    \Drupal::service('kernel')->invalidateContainer();

    $this->expectedExceptionMessage = 'Drupal\FunctionalTests\Bootstrap\ErrorContainer::Drupal\FunctionalTests\Bootstrap\{closure}(): Argument #1 ($container) must be of type Drupal\FunctionalTests\Bootstrap\ErrorContainer';
    $this->drupalGet('');
    $this->assertSession()->statusCodeEquals(500);

    $this->assertSession()->pageTextContains($this->expectedExceptionMessage);
    $this->assertErrorLogged($this->expectedExceptionMessage);
  }

  /** * Tests a container which has an exception really early. */
    $this->drupalGet('<front>');
    $assert->responseHeaderEquals('container_rebuild_test_parameter', NULL);

    // Ensure that after setting the parameter, without a container rebuild the     // parameter is still not set.     $this->writeSettings(['settings' => ['container_rebuild_test_parameter' => (object) ['value' => 'rebuild_me_please', 'required' => TRUE]]]);

    $this->drupalGet('<front>');
    $assert->responseHeaderEquals('container_rebuild_test_parameter', NULL);

    // Ensure that after container invalidation the parameter is set.     \Drupal::service('kernel')->invalidateContainer();
    $this->drupalGet('<front>');
    $assert->responseHeaderEquals('container_rebuild_test_parameter', 'rebuild_me_please');
  }

}
Home | Imprint | This part of the site doesn't use cookies.