From 2732b8e0acc2ef4f0ccf3b662428c434a0ede194 Mon Sep 17 00:00:00 2001 From: Lee Rowlands <lee.rowlands@previousnext.com.au> Date: Fri, 4 Oct 2019 09:50:26 +1000 Subject: [PATCH] Issue #3052703 by voleger, Berdir, kim.pepper: Deprecate drupal_installation_attempted() --- core/includes/bootstrap.inc | 17 ++++++++------ core/includes/errors.inc | 5 ++-- core/includes/install.inc | 5 ++-- core/includes/theme.maintenance.inc | 3 ++- .../Core/Cache/ChainedFastBackendFactory.php | 3 ++- .../Drupal/Core/Config/ConfigInstaller.php | 15 +++++++++--- .../Config/Importer/ConfigImporterBatch.php | 3 ++- core/lib/Drupal/Core/DrupalKernel.php | 3 ++- .../ConfigImportSubscriber.php | 3 ++- core/lib/Drupal/Core/Form/FormSubmitter.php | 11 ++++++++- .../Drupal/Core/Installer/ConfigOverride.php | 2 +- .../Drupal/Core/Installer/InstallerKernel.php | 15 ++++++++++++ .../Core/Installer/InstallerRedirectTrait.php | 2 +- .../content_translation.install | 3 ++- core/modules/locale/locale.module | 3 ++- .../locale/src/LocaleConfigSubscriber.php | 5 ++-- .../Core/Installer/InstallerLegacyTest.php | 23 +++++++++++++++++++ 17 files changed, 95 insertions(+), 26 deletions(-) create mode 100644 core/tests/Drupal/KernelTests/Core/Installer/InstallerLegacyTest.php diff --git a/core/includes/bootstrap.inc b/core/includes/bootstrap.inc index 3f2232b55f94..33a439f8a25a 100644 --- a/core/includes/bootstrap.inc +++ b/core/includes/bootstrap.inc @@ -10,6 +10,7 @@ use Drupal\Component\Render\FormattableMarkup; use Drupal\Component\Utility\Unicode; use Drupal\Core\Config\BootstrapConfigStorageFactory; +use Drupal\Core\Installer\InstallerKernel; use Drupal\Core\Logger\RfcLogLevel; use Drupal\Core\Test\TestDatabase; use Drupal\Core\Session\AccountInterface; @@ -750,14 +751,16 @@ function drupal_maintenance_theme() { /** * Returns TRUE if a Drupal installation is currently being attempted. + * + * @deprecated in drupal:8.8.0 and is removed from drupal:9.0.0. + * Use \Drupal\Core\Installer\InstallerKernel::installationAttempted() + * instead. + * + * @see https://www.drupal.org/node/3035275 */ function drupal_installation_attempted() { - // This cannot rely on the MAINTENANCE_MODE constant, since that would prevent - // tests from using the non-interactive installer, in which case Drupal - // only happens to be installed within the same request, but subsequently - // executed code does not involve the installer at all. - // @see install_drupal() - return isset($GLOBALS['install_state']) && empty($GLOBALS['install_state']['installation_finished']); + @trigger_error('drupal_installation_attempted() is deprecated in drupal:8.8.0 and is removed from drupal:9.0.0. Use \Drupal\Core\Installer\InstallerKernel::installationAttempted() instead. See https://www.drupal.org/node/3035275', E_USER_DEPRECATED); + return InstallerKernel::installationAttempted(); } /** @@ -788,7 +791,7 @@ function drupal_get_profile() { @trigger_error('drupal_get_profile() is deprecated in drupal:8.3.0 and is removed from drupal:9.0.0. Use the install_profile container parameter or \Drupal::installProfile() instead. If you are accessing the value before it is written to configuration during the installer use the $install_state global. If you need to access the value before container is available you can use BootstrapConfigStorageFactory to load the value directly from configuration. See https://www.drupal.org/node/2538996', E_USER_DEPRECATED); - if (drupal_installation_attempted()) { + if (InstallerKernel::installationAttempted()) { // If the profile has been selected return it. if (isset($install_state['parameters']['profile'])) { $profile = $install_state['parameters']['profile']; diff --git a/core/includes/errors.inc b/core/includes/errors.inc index 674fbc7559a8..8fc4f39a4c5c 100644 --- a/core/includes/errors.inc +++ b/core/includes/errors.inc @@ -7,6 +7,7 @@ use Drupal\Component\Render\FormattableMarkup; use Drupal\Component\Utility\Xss; +use Drupal\Core\Installer\InstallerKernel; use Drupal\Core\Logger\RfcLogLevel; use Drupal\Core\Render\Markup; use Drupal\Core\Utility\Error; @@ -140,7 +141,7 @@ function error_displayable($error = NULL) { * Non-recoverable fatal errors cannot be logged by Drupal. */ function _drupal_log_error($error, $fatal = FALSE) { - $is_installer = drupal_installation_attempted(); + $is_installer = InstallerKernel::installationAttempted(); // Backtrace array is not a valid replacement value for t(). $backtrace = $error['backtrace']; @@ -301,7 +302,7 @@ function _drupal_get_error_level() { // 'config.factory' service, which reads the default 'error_level' value from // System module's default configuration and the default value is not verbose. // @see error_displayable() - if (drupal_installation_attempted()) { + if (InstallerKernel::installationAttempted()) { return ERROR_REPORTING_DISPLAY_VERBOSE; } $error_level = NULL; diff --git a/core/includes/install.inc b/core/includes/install.inc index 9922166cb65d..2834e918f535 100644 --- a/core/includes/install.inc +++ b/core/includes/install.inc @@ -12,6 +12,7 @@ use Drupal\Core\Extension\Dependency; use Drupal\Core\Extension\ExtensionDiscovery; use Drupal\Core\File\FileSystemInterface; +use Drupal\Core\Installer\InstallerKernel; use Drupal\Core\Site\Settings; use Symfony\Component\HttpFoundation\RedirectResponse; @@ -99,7 +100,7 @@ function drupal_install_profile_distribution_name() { // During installation, the profile information is stored in the global // installation state (it might not be saved anywhere yet). $info = []; - if (drupal_installation_attempted()) { + if (InstallerKernel::installationAttempted()) { global $install_state; if (isset($install_state['profile_info'])) { $info = $install_state['profile_info']; @@ -126,7 +127,7 @@ function drupal_install_profile_distribution_name() { function drupal_install_profile_distribution_version() { // During installation, the profile information is stored in the global // installation state (it might not be saved anywhere yet). - if (drupal_installation_attempted()) { + if (InstallerKernel::installationAttempted()) { global $install_state; return isset($install_state['profile_info']['version']) ? $install_state['profile_info']['version'] : \Drupal::VERSION; } diff --git a/core/includes/theme.maintenance.inc b/core/includes/theme.maintenance.inc index 2982f845421f..aa43f080bfff 100644 --- a/core/includes/theme.maintenance.inc +++ b/core/includes/theme.maintenance.inc @@ -5,6 +5,7 @@ * Theming for maintenance pages. */ +use Drupal\Core\Installer\InstallerKernel; use Drupal\Core\Site\Settings; /** @@ -31,7 +32,7 @@ function _drupal_maintenance_theme() { // Install and update pages are treated differently to prevent theming overrides. if (defined('MAINTENANCE_MODE') && (MAINTENANCE_MODE == 'install' || MAINTENANCE_MODE == 'update')) { - if (drupal_installation_attempted()) { + if (InstallerKernel::installationAttempted()) { $custom_theme = $GLOBALS['install_state']['theme']; } else { diff --git a/core/lib/Drupal/Core/Cache/ChainedFastBackendFactory.php b/core/lib/Drupal/Core/Cache/ChainedFastBackendFactory.php index 1016e5a3ec6b..b04ee5c55cfd 100644 --- a/core/lib/Drupal/Core/Cache/ChainedFastBackendFactory.php +++ b/core/lib/Drupal/Core/Cache/ChainedFastBackendFactory.php @@ -2,6 +2,7 @@ namespace Drupal\Core\Cache; +use Drupal\Core\Installer\InstallerKernel; use Drupal\Core\Site\Settings; use Symfony\Component\DependencyInjection\ContainerAwareTrait; @@ -60,7 +61,7 @@ public function __construct(Settings $settings = NULL, $consistent_service_name // Do not use the fast chained backend during installation. In those cases, // we expect many cache invalidations and writes, the fast chained cache // backend performs badly in such a scenario. - if (!drupal_installation_attempted()) { + if (!InstallerKernel::installationAttempted()) { $this->fastServiceName = $fast_service_name; } } diff --git a/core/lib/Drupal/Core/Config/ConfigInstaller.php b/core/lib/Drupal/Core/Config/ConfigInstaller.php index 66ebcedd5e16..2d8e4d52c012 100644 --- a/core/lib/Drupal/Core/Config/ConfigInstaller.php +++ b/core/lib/Drupal/Core/Config/ConfigInstaller.php @@ -4,6 +4,7 @@ use Drupal\Component\Utility\Crypt; use Drupal\Core\Config\Entity\ConfigDependencyManager; +use Drupal\Core\Installer\InstallerKernel; use Symfony\Component\EventDispatcher\EventDispatcherInterface; class ConfigInstaller implements ConfigInstallerInterface { @@ -137,7 +138,7 @@ public function installDefaultConfig($type, $name) { // During a drupal installation optional configuration is installed at the // end of the installation process. // @see install_install_profile() - if (!$this->isSyncing() && !$this->drupalInstallationAttempted()) { + if (!$this->isSyncing() && !InstallerKernel::installationAttempted()) { $optional_install_path = $extension_path . '/' . InstallStorage::CONFIG_OPTIONAL_DIRECTORY; if (is_dir($optional_install_path)) { // Install any optional config the module provides. @@ -356,7 +357,7 @@ protected function createConfiguration($collection, array $config_to_create) { * {@inheritdoc} */ public function installCollectionDefaultConfig($collection) { - $storage = new ExtensionInstallStorage($this->getActiveStorages(StorageInterface::DEFAULT_COLLECTION), InstallStorage::CONFIG_INSTALL_DIRECTORY, $collection, $this->drupalInstallationAttempted(), $this->installProfile); + $storage = new ExtensionInstallStorage($this->getActiveStorages(StorageInterface::DEFAULT_COLLECTION), InstallStorage::CONFIG_INSTALL_DIRECTORY, $collection, InstallerKernel::installationAttempted(), $this->installProfile); // Only install configuration for enabled extensions. $enabled_extensions = $this->getEnabledExtensions(); $config_to_install = array_filter($storage->listAll(), function ($config_name) use ($enabled_extensions) { @@ -701,9 +702,17 @@ protected function drupalGetProfile() { * * @return bool * TRUE if a Drupal installation is currently being attempted. + * + * @deprecated in drupal:8.8.0 and is removed from drupal:9.0.0. + * Use \Drupal\Core\Installer\InstallerKernel::installationAttempted() + * instead. + * + * @see https://www.drupal.org/node/3035275 + * @see \Drupal\Core\Installer\InstallerKernel::installationAttempted() */ protected function drupalInstallationAttempted() { - return drupal_installation_attempted(); + @trigger_error(__METHOD__ . '() is deprecated in drupal:8.8.0 and is removed from drupal:9.0.0. Use \Drupal\Core\Installer\InstallerKernel::installationAttempted() instead. See https://www.drupal.org/node/3035275', E_USER_DEPRECATED); + return InstallerKernel::installationAttempted(); } } diff --git a/core/lib/Drupal/Core/Config/Importer/ConfigImporterBatch.php b/core/lib/Drupal/Core/Config/Importer/ConfigImporterBatch.php index 8aee289e0d14..274e68173750 100644 --- a/core/lib/Drupal/Core/Config/Importer/ConfigImporterBatch.php +++ b/core/lib/Drupal/Core/Config/Importer/ConfigImporterBatch.php @@ -3,6 +3,7 @@ namespace Drupal\Core\Config\Importer; use Drupal\Core\Config\ConfigImporter; +use Drupal\Core\Installer\InstallerKernel; /** * Methods for running the ConfigImporter in a batch. @@ -60,7 +61,7 @@ public static function finish($success, $results, $operations) { } $messenger->addWarning(t('The configuration was imported with errors.')); } - elseif (!drupal_installation_attempted()) { + elseif (!InstallerKernel::installationAttempted()) { // Display a success message when not installing Drupal. $messenger->addStatus(t('The configuration was imported successfully.')); } diff --git a/core/lib/Drupal/Core/DrupalKernel.php b/core/lib/Drupal/Core/DrupalKernel.php index d80e87270be8..b9f7b306138f 100644 --- a/core/lib/Drupal/Core/DrupalKernel.php +++ b/core/lib/Drupal/Core/DrupalKernel.php @@ -17,6 +17,7 @@ use Drupal\Core\Extension\ExtensionDiscovery; use Drupal\Core\File\MimeType\MimeTypeGuesser; use Drupal\Core\Http\TrustedHostsRequestFactory; +use Drupal\Core\Installer\InstallerKernel; use Drupal\Core\Installer\InstallerRedirectTrait; use Drupal\Core\Language\Language; use Drupal\Core\Security\PharExtensionInterceptor; @@ -685,7 +686,7 @@ public function handle(Request $request, $type = self::MASTER_REQUEST, $catch = // Redirect the user to the installation script if Drupal has not been // installed yet (i.e., if no $databases array has been defined in the // settings.php file) and we are not already installing. - if (!Database::getConnectionInfo() && !drupal_installation_attempted() && PHP_SAPI !== 'cli') { + if (!Database::getConnectionInfo() && !InstallerKernel::installationAttempted() && PHP_SAPI !== 'cli') { $response = new RedirectResponse($request->getBasePath() . '/core/install.php', 302, ['Cache-Control' => 'no-cache']); } else { diff --git a/core/lib/Drupal/Core/EventSubscriber/ConfigImportSubscriber.php b/core/lib/Drupal/Core/EventSubscriber/ConfigImportSubscriber.php index 7f2f35c32ba4..f521bb165586 100644 --- a/core/lib/Drupal/Core/EventSubscriber/ConfigImportSubscriber.php +++ b/core/lib/Drupal/Core/EventSubscriber/ConfigImportSubscriber.php @@ -9,6 +9,7 @@ use Drupal\Core\Config\ConfigNameException; use Drupal\Core\Extension\ModuleExtensionList; use Drupal\Core\Extension\ThemeHandlerInterface; +use Drupal\Core\Installer\InstallerKernel; /** * Config import subscriber for config import events. @@ -95,7 +96,7 @@ protected function validateModules(ConfigImporter $config_importer) { // Ensure the profile is not changing. if ($install_profile !== $core_extension['profile']) { - if (drupal_installation_attempted()) { + if (InstallerKernel::installationAttempted()) { $config_importer->logError($this->t('The selected installation profile %install_profile does not match the profile stored in configuration %config_profile.', [ '%install_profile' => $install_profile, '%config_profile' => $core_extension['profile'], diff --git a/core/lib/Drupal/Core/Form/FormSubmitter.php b/core/lib/Drupal/Core/Form/FormSubmitter.php index 4a4275ef8d0e..8591edaa2bbc 100644 --- a/core/lib/Drupal/Core/Form/FormSubmitter.php +++ b/core/lib/Drupal/Core/Form/FormSubmitter.php @@ -2,6 +2,7 @@ namespace Drupal\Core\Form; +use Drupal\Core\Installer\InstallerKernel; use Drupal\Core\Url; use Symfony\Component\HttpFoundation\RedirectResponse; use Symfony\Component\HttpFoundation\RequestStack; @@ -147,9 +148,17 @@ public function redirectForm(FormStateInterface $form_state) { * Wraps drupal_installation_attempted(). * * @return bool + * + * @deprecated in drupal:8.8.0 and is removed from drupal:9.0.0. + * Use \Drupal\Core\Installer\InstallerKernel::installationAttempted() + * instead. + * + * @see https://www.drupal.org/node/3035275 + * @see \Drupal\Core\Installer\InstallerKernel::installationAttempted() */ protected function drupalInstallationAttempted() { - return drupal_installation_attempted(); + @trigger_error(__METHOD__ . '() is deprecated in drupal:8.8.0 and is removed from drupal:9.0.0. Use \Drupal\Core\Installer\InstallerKernel::installationAttempted() instead. See https://www.drupal.org/node/3035275', E_USER_DEPRECATED); + return InstallerKernel::installationAttempted(); } /** diff --git a/core/lib/Drupal/Core/Installer/ConfigOverride.php b/core/lib/Drupal/Core/Installer/ConfigOverride.php index ff8d068b2fec..b343b23c8d8d 100644 --- a/core/lib/Drupal/Core/Installer/ConfigOverride.php +++ b/core/lib/Drupal/Core/Installer/ConfigOverride.php @@ -28,7 +28,7 @@ public function register(ContainerBuilder $container) { */ public function loadOverrides($names) { $overrides = []; - if (drupal_installation_attempted() && function_exists('drupal_install_profile_distribution_name')) { + if (InstallerKernel::installationAttempted() && function_exists('drupal_install_profile_distribution_name')) { // Early in the installer the site name is unknown. In this case we need // to fallback to the distribution's name. $overrides['system.site'] = [ diff --git a/core/lib/Drupal/Core/Installer/InstallerKernel.php b/core/lib/Drupal/Core/Installer/InstallerKernel.php index adeb5c53eed6..501d1f27de45 100644 --- a/core/lib/Drupal/Core/Installer/InstallerKernel.php +++ b/core/lib/Drupal/Core/Installer/InstallerKernel.php @@ -66,4 +66,19 @@ public function getInstallProfile() { return $profile; } + /** + * Returns TRUE if a Drupal installation is currently being attempted. + * + * @return bool + * TRUE if the installation is currently being attempted. + */ + public static function installationAttempted() { + // This cannot rely on the MAINTENANCE_MODE constant, since that would + // prevent tests from using the non-interactive installer, in which case + // Drupal only happens to be installed within the same request, but + // subsequently executed code does not involve the installer at all. + // @see install_drupal() + return isset($GLOBALS['install_state']) && empty($GLOBALS['install_state']['installation_finished']); + } + } diff --git a/core/lib/Drupal/Core/Installer/InstallerRedirectTrait.php b/core/lib/Drupal/Core/Installer/InstallerRedirectTrait.php index 067802d2bbbf..73001e545ab1 100644 --- a/core/lib/Drupal/Core/Installer/InstallerRedirectTrait.php +++ b/core/lib/Drupal/Core/Installer/InstallerRedirectTrait.php @@ -44,7 +44,7 @@ protected function shouldRedirectToInstaller(\Exception $exception, Connection $ } // Never redirect if we're already in the installer. - if (drupal_installation_attempted()) { + if (InstallerKernel::installationAttempted()) { return FALSE; } diff --git a/core/modules/content_translation/content_translation.install b/core/modules/content_translation/content_translation.install index 80488f6bdfb9..96859faaebde 100644 --- a/core/modules/content_translation/content_translation.install +++ b/core/modules/content_translation/content_translation.install @@ -6,6 +6,7 @@ */ use Drupal\Core\Entity\Sql\SqlEntityStorageInterface; +use Drupal\Core\Installer\InstallerKernel; use Drupal\Core\Language\LanguageInterface; use Drupal\Core\Url; @@ -19,7 +20,7 @@ function content_translation_install() { // Skip the guidance messages about enabling translation features if the // module was installed in the Drupal installation process. - if (drupal_installation_attempted()) { + if (InstallerKernel::installationAttempted()) { return; } diff --git a/core/modules/locale/locale.module b/core/modules/locale/locale.module index db37cec3b5ef..40cd9d2491bf 100644 --- a/core/modules/locale/locale.module +++ b/core/modules/locale/locale.module @@ -17,6 +17,7 @@ use Drupal\Component\Utility\Xss; use Drupal\Core\File\Exception\FileException; use Drupal\Core\File\FileSystemInterface; +use Drupal\Core\Installer\InstallerKernel; use Drupal\Core\Link; use Drupal\Core\Url; use Drupal\Core\Asset\AttachedAssetsInterface; @@ -408,7 +409,7 @@ function locale_system_update(array $components) { // Skip running the translation imports if in the installer, // because it would break out of the installer flow. We have // built-in support for translation imports in the installer. - if (!drupal_installation_attempted() && locale_translatable_language_list()) { + if (!InstallerKernel::installationAttempted() && locale_translatable_language_list()) { if (\Drupal::config('locale.settings')->get('translation.import_enabled')) { module_load_include('compare.inc', 'locale'); diff --git a/core/modules/locale/src/LocaleConfigSubscriber.php b/core/modules/locale/src/LocaleConfigSubscriber.php index 7d7ff393e2f4..289e838e0545 100644 --- a/core/modules/locale/src/LocaleConfigSubscriber.php +++ b/core/modules/locale/src/LocaleConfigSubscriber.php @@ -6,6 +6,7 @@ use Drupal\Core\Config\ConfigEvents; use Drupal\Core\Config\ConfigFactoryInterface; use Drupal\Core\Config\StorableConfigBase; +use Drupal\Core\Installer\InstallerKernel; use Drupal\language\Config\LanguageConfigOverrideCrudEvent; use Drupal\language\Config\LanguageConfigOverrideEvents; use Symfony\Component\EventDispatcher\EventSubscriberInterface; @@ -78,7 +79,7 @@ public static function getSubscribedEvents() { public function onConfigSave(ConfigCrudEvent $event) { // Only attempt to feed back configuration translation changes to locale if // the update itself was not initiated by locale data changes. - if (!drupal_installation_attempted() && !$this->localeConfigManager->isUpdatingTranslationsFromLocale()) { + if (!InstallerKernel::installationAttempted() && !$this->localeConfigManager->isUpdatingTranslationsFromLocale()) { $config = $event->getConfig(); $langcode = $config->get('langcode') ?: 'en'; $this->updateLocaleStorage($config, $langcode); @@ -94,7 +95,7 @@ public function onConfigSave(ConfigCrudEvent $event) { public function onOverrideChange(LanguageConfigOverrideCrudEvent $event) { // Only attempt to feed back configuration override changes to locale if // the update itself was not initiated by locale data changes. - if (!drupal_installation_attempted() && !$this->localeConfigManager->isUpdatingTranslationsFromLocale()) { + if (!InstallerKernel::installationAttempted() && !$this->localeConfigManager->isUpdatingTranslationsFromLocale()) { $translation_config = $event->getLanguageConfigOverride(); $langcode = $translation_config->getLangcode(); $reference_config = $this->configFactory->getEditable($translation_config->getName())->get(); diff --git a/core/tests/Drupal/KernelTests/Core/Installer/InstallerLegacyTest.php b/core/tests/Drupal/KernelTests/Core/Installer/InstallerLegacyTest.php new file mode 100644 index 000000000000..fea14b1d1d8e --- /dev/null +++ b/core/tests/Drupal/KernelTests/Core/Installer/InstallerLegacyTest.php @@ -0,0 +1,23 @@ +<?php + +namespace Drupal\KernelTests\Core\Installer; + +use Drupal\KernelTests\KernelTestBase; + +/** + * Tests for installer related legacy API. + * + * @group legacy + */ +class InstallerLegacyTest extends KernelTestBase { + + /** + * Tests drupal_installation_attempted(). + * + * @expectedDeprecation drupal_installation_attempted() is deprecated in drupal:8.8.0 and is removed from drupal:9.0.0. Use \Drupal\Core\Installer\InstallerKernel::installationAttempted() instead. See https://www.drupal.org/node/3035275 + */ + public function testDrupalInstallationAttempted() { + $this->assertFalse(drupal_installation_attempted()); + } + +} -- GitLab