diff --git a/src/ProjectInfoTrait.php b/src/ProjectInfoTrait.php index 979c066174c24c99df240385a62277c5a1c6485b..633f8acaf9a06514be8268e716943ee225113457 100644 --- a/src/ProjectInfoTrait.php +++ b/src/ProjectInfoTrait.php @@ -2,11 +2,20 @@ namespace Drupal\automatic_updates; +use Drupal\Core\Extension\ExtensionList; + /** * Provide a helper to get project info. */ trait ProjectInfoTrait { + /** + * The extension lists. + * + * @var \Drupal\Core\Extension\ExtensionList[] + */ + protected $extensionLists; + /** * Get extension list. * @@ -17,15 +26,46 @@ trait ProjectInfoTrait { * The extension list service. */ protected function getExtensionList($extension_type) { - if (isset($this->{$extension_type})) { - $list = $this->{$extension_type}; + if (isset($this->extensionLists[$extension_type])) { + $list = $this->extensionLists[$extension_type]; } else { + if (!in_array($extension_type, $this->getExtensionsTypes())) { + throw new \UnexpectedValueException("Invalid extension type: $extension_type"); + } $list = \Drupal::service("extension.list.$extension_type"); } return $list; } + /** + * Sets the extension lists. + * + * @param \Drupal\Core\Extension\ExtensionList $module_list + * The module extension list. + * @param \Drupal\Core\Extension\ExtensionList $theme_list + * The theme extension list. + * @param \Drupal\Core\Extension\ExtensionList $profile_list + * The profile extension list. + */ + protected function setExtensionLists(ExtensionList $module_list, ExtensionList $theme_list, ExtensionList $profile_list) { + $this->extensionLists = [ + 'module' => $module_list, + 'theme' => $theme_list, + 'profile' => $profile_list, + ]; + } + + /** + * Get the extension types. + * + * @return string[] + * The extension types. + */ + protected function getExtensionsTypes() { + return ['module', 'profile', 'theme']; + } + /** * Returns an array of info files information of available extensions. * diff --git a/src/ReadinessChecker/MissingProjectInfo.php b/src/ReadinessChecker/MissingProjectInfo.php index 9ac9a5f39391c019d0b98a57f10cf6dac3682ce9..049b603bc8accdcc15e57e05ab40fcade3a17530 100644 --- a/src/ReadinessChecker/MissingProjectInfo.php +++ b/src/ReadinessChecker/MissingProjectInfo.php @@ -15,27 +15,6 @@ class MissingProjectInfo implements ReadinessCheckerInterface { use ProjectInfoTrait; use StringTranslationTrait; - /** - * The module extension list. - * - * @var \Drupal\Core\Extension\ExtensionList - */ - protected $modules; - - /** - * The profile extension list. - * - * @var \Drupal\Core\Extension\ExtensionList - */ - protected $profiles; - - /** - * The theme extension list. - * - * @var \Drupal\Core\Extension\ExtensionList - */ - protected $themes; - /** * MissingProjectInfo constructor. * @@ -47,9 +26,7 @@ class MissingProjectInfo implements ReadinessCheckerInterface { * The theme extension list. */ public function __construct(ExtensionList $modules, ExtensionList $profiles, ExtensionList $themes) { - $this->modules = $modules; - $this->profiles = $profiles; - $this->themes = $themes; + $this->setExtensionLists($modules, $themes, $profiles); } /** @@ -80,14 +57,4 @@ class MissingProjectInfo implements ReadinessCheckerInterface { return $messages; } - /** - * Get the extension types. - * - * @return array - * The extension types. - */ - protected function getExtensionsTypes() { - return ['modules', 'profiles', 'themes']; - } - } diff --git a/src/ReadinessChecker/ModifiedFiles.php b/src/ReadinessChecker/ModifiedFiles.php index a4ede9dec96ae9ed2c5ba8916a6335aab033cb29..fd53e9ed44b8e3a3b33501ab86e72dfc5b20502a 100644 --- a/src/ReadinessChecker/ModifiedFiles.php +++ b/src/ReadinessChecker/ModifiedFiles.php @@ -22,27 +22,6 @@ class ModifiedFiles implements ReadinessCheckerInterface { */ protected $modifiedFiles; - /** - * The module extension list. - * - * @var \Drupal\Core\Extension\ExtensionList - */ - protected $module; - - /** - * The profile extension list. - * - * @var \Drupal\Core\Extension\ExtensionList - */ - protected $profile; - - /** - * The theme extension list. - * - * @var \Drupal\Core\Extension\ExtensionList - */ - protected $theme; - /** * An array of array of strings of extension paths. * @@ -65,9 +44,7 @@ class ModifiedFiles implements ReadinessCheckerInterface { */ public function __construct(ModifiedFilesInterface $modified_files, ExtensionList $modules, ExtensionList $profiles, ExtensionList $themes) { $this->modifiedFiles = $modified_files; - $this->module = $modules; - $this->profile = $profiles; - $this->theme = $themes; + $this->setExtensionLists($modules, $themes, $profiles); } /** @@ -97,14 +74,4 @@ class ModifiedFiles implements ReadinessCheckerInterface { return $messages; } - /** - * Get the extension types. - * - * @return array - * The extension types. - */ - protected function getExtensionsTypes() { - return ['module', 'profile', 'theme']; - } - } diff --git a/src/Services/AutomaticUpdatesPsa.php b/src/Services/AutomaticUpdatesPsa.php index 4160282a020bf3da479b94368982aa6432acbb5b..68585d0dbfa0020554e537da1d4919290c6e8e23 100644 --- a/src/Services/AutomaticUpdatesPsa.php +++ b/src/Services/AutomaticUpdatesPsa.php @@ -3,6 +3,7 @@ namespace Drupal\automatic_updates\Services; use Composer\Semver\VersionParser; +use Drupal\automatic_updates\ProjectInfoTrait; use Drupal\Component\Datetime\TimeInterface; use Drupal\Component\Render\FormattableMarkup; use Drupal\Core\Cache\CacheBackendInterface; @@ -20,6 +21,7 @@ use Psr\Log\LoggerInterface; class AutomaticUpdatesPsa implements AutomaticUpdatesPsaInterface { use StringTranslationTrait; use DependencySerializationTrait; + use ProjectInfoTrait; /** * This module's configuration. @@ -49,27 +51,6 @@ class AutomaticUpdatesPsa implements AutomaticUpdatesPsaInterface { */ protected $time; - /** - * The module extension list. - * - * @var \Drupal\Core\Extension\ExtensionList - */ - protected $module; - - /** - * The profile extension list. - * - * @var \Drupal\Core\Extension\ExtensionList - */ - protected $profile; - - /** - * The theme extension list. - * - * @var \Drupal\Core\Extension\ExtensionList - */ - protected $theme; - /** * The logger. * @@ -88,24 +69,22 @@ class AutomaticUpdatesPsa implements AutomaticUpdatesPsaInterface { * The time service. * @param \GuzzleHttp\Client $client * The HTTP client. - * @param \Drupal\Core\Extension\ExtensionList $module + * @param \Drupal\Core\Extension\ExtensionList $moduleList * The module extension list. - * @param \Drupal\Core\Extension\ExtensionList $profile + * @param \Drupal\Core\Extension\ExtensionList $profileList * The profile extension list. - * @param \Drupal\Core\Extension\ExtensionList $theme + * @param \Drupal\Core\Extension\ExtensionList $themeList * The theme extension list. * @param \Psr\Log\LoggerInterface $logger * The logger. */ - public function __construct(ConfigFactoryInterface $config_factory, CacheBackendInterface $cache, TimeInterface $time, Client $client, ExtensionList $module, ExtensionList $profile, ExtensionList $theme, LoggerInterface $logger) { + public function __construct(ConfigFactoryInterface $config_factory, CacheBackendInterface $cache, TimeInterface $time, Client $client, ExtensionList $moduleList, ExtensionList $profileList, ExtensionList $themeList, LoggerInterface $logger) { $this->config = $config_factory->get('automatic_updates.settings'); $this->cache = $cache; $this->time = $time; $this->httpClient = $client; - $this->module = $module; - $this->profile = $profile; - $this->theme = $theme; $this->logger = $logger; + $this->setExtensionLists($moduleList, $themeList, $profileList); } /** @@ -175,11 +154,14 @@ class AutomaticUpdatesPsa implements AutomaticUpdatesPsaInterface { * TRUE if extension exists, else FALSE. */ protected function isValidExtension($extension_type, $project_name) { - if (!property_exists($this, $extension_type)) { - $this->logger->error('Extension list of type "%extension" does not exist.', ['%extension' => $extension_type]); + try { + $extension_list = $this->getExtensionList($extension_type); + return $extension_list->exists($project_name) && !empty($extension_list->getAllAvailableInfo()[$project_name]['version']); + } + catch (\UnexpectedValueException $exception) { + $this->logger->error($exception->getMessage()); return FALSE; } - return $this->{$extension_type}->exists($project_name) && !empty($this->{$extension_type}->getAllAvailableInfo()[$project_name]['version']); } /** @@ -191,7 +173,7 @@ class AutomaticUpdatesPsa implements AutomaticUpdatesPsaInterface { * The JSON object. */ protected function contribParser(array &$messages, $json) { - $extension_version = $this->{$json->type}->getAllAvailableInfo()[$json->project]['version']; + $extension_version = $this->getExtensionList($json->type)->getAllAvailableInfo()[$json->project]['version']; $json->insecure = array_filter(array_map(static function ($version) { $version_array = explode('-', $version, 2); if ($version_array && $version_array[0] === \Drupal::CORE_COMPATIBILITY) {