diff --git a/core/modules/update/lib/Drupal/update/UpdateManager.php b/core/modules/update/lib/Drupal/update/UpdateManager.php index fbc8149..6129e76 100644 --- a/core/modules/update/lib/Drupal/update/UpdateManager.php +++ b/core/modules/update/lib/Drupal/update/UpdateManager.php @@ -8,6 +8,8 @@ use Drupal\Core\Config\ConfigFactory; use Drupal\Core\Extension\ModuleHandlerInterface; +use Drupal\Core\KeyValueStore\KeyValueFactoryInterface; +use Drupal\Core\StringTranslation\TranslationInterface; use Drupal\Core\Utility\ProjectInfo; /** @@ -44,20 +46,27 @@ class UpdateManager { protected $projects; /** - * Update key/value store + * The key/value store. * * @var \Drupal\Core\KeyValueStore\KeyValueStoreExpirableInterface */ - protected $tempStore; + protected $keyValueStore; /** - * Update avaiable releases store + * Update available releases key/value store. * * @var \Drupal\Core\KeyValueStore\KeyValueStoreExpirableInterface */ protected $availableReleasesTempStore; /** + * The translation service. + * + * @var \Drupal\Core\StringTranslation\TranslationInterface + */ + protected $translation; + + /** * Constructs a UpdateManager. * * @param \Drupal\Core\Config\ConfigFactory $config_factory @@ -65,14 +74,19 @@ class UpdateManager { * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler * The Module Handler service * @param \Drupal\update\UpdateProcessor $update_processor - * The Update Processor service + * The Update Processor service. + * @param \Drupal\Core\StringTranslation\TranslationInterface $translation + * The translation service. + * @param \Drupal\Core\KeyValueStore\KeyValueFactoryInterface $key_value_expirable_factory + * The expirable key/value factory. */ - public function __construct(ConfigFactory $config_factory, ModuleHandlerInterface $module_handler, UpdateProcessor $update_processor) { + public function __construct(ConfigFactory $config_factory, ModuleHandlerInterface $module_handler, UpdateProcessor $update_processor, TranslationInterface $translation, KeyValueFactoryInterface $key_value_expirable_factory) { $this->updateSettings = $config_factory->get('update.settings'); $this->moduleHandler = $module_handler; $this->updateProcessor = $update_processor; - $this->tempStore = \Drupal::keyValueExpirable('update'); - $this->availableReleasesTempStore = \Drupal::keyValueExpirable('update_available_releases'); + $this->translation = $translation; + $this->keyValueStore = $key_value_expirable_factory->get('update'); + $this->availableReleasesTempStore = $key_value_expirable_factory->get('update_available_releases'); $this->projects = array(); } @@ -87,8 +101,8 @@ public function refreshUpdateData() { // since that data (even if it's stale) can be useful during // update_get_projects(); for example, to modules that implement // hook_system_info_alter() such as cvs_deploy. - $this->tempStore->delete('update_project_projects'); - $this->tempStore->delete('update_project_data'); + $this->keyValueStore->delete('update_project_projects'); + $this->keyValueStore->delete('update_project_data'); $projects = $this->getProjects(); @@ -167,7 +181,7 @@ public function getProjects() { // Allow other modules to alter projects before fetching and comparing. $this->moduleHandler->alter('update_projects', $this->projects); // Store the site's project data for at most 1 hour. - $this->tempStore->setWithExpire('update_project_projects', $this->projects, 3600); + $this->keyValueStore->setWithExpire('update_project_projects', $this->projects, 3600); } } return $this->projects; @@ -217,10 +231,10 @@ public function projectStorage($key) { 'admin/reports/updates/check', ); if (in_array(current_path(), $paths)) { - $this->tempStore->delete($key); + $this->keyValueStore->delete($key); } else { - $projects = $this->tempStore->get($key); + $projects = $this->keyValueStore->get($key); } return $projects; } @@ -236,7 +250,7 @@ public function fetchDataBatch(&$context) { $context['finished'] = 0; $context['sandbox']['max'] = $this->updateProcessor->numberOfQueueItems(); $context['sandbox']['progress'] = 0; - $context['message'] = \Drupal::translation()->translate('Checking available update data ...'); + $context['message'] = $this->t('Checking available update data ...'); $context['results']['updated'] = 0; $context['results']['failures'] = 0; $context['results']['processed'] = 0; @@ -247,10 +261,10 @@ public function fetchDataBatch(&$context) { if ($item = $this->updateProcessor->claimQueueItem()) { if ($this->updateProcessor->processFetchTask($item->data)) { $context['results']['updated']++; - $context['message'] = \Drupal::translation()->translate('Checked available update data for %title.', array('%title' => $item->data['info']['name'])); + $context['message'] = $this->t('Checked available update data for %title.', array('%title' => $item->data['info']['name'])); } else { - $context['message'] = \Drupal::translation()->translate('Failed to check available update data for %title.', array('%title' => $item->data['info']['name'])); + $context['message'] = $this->t('Failed to check available update data for %title.', array('%title' => $item->data['info']['name'])); $context['results']['failures']++; } $context['sandbox']['progress']++; @@ -272,4 +286,13 @@ public function fetchDataBatch(&$context) { } } -} \ No newline at end of file + /** + * Translates a string to the current language or to a given language. + * + * See the t() documentation for details. + */ + protected function t($string, array $args = array(), array $options = array()) { + return $this->translation->translate($string, $args, $options); + } + +} diff --git a/core/modules/update/lib/Drupal/update/UpdateProcessor.php b/core/modules/update/lib/Drupal/update/UpdateProcessor.php index d40d7ba..0237fda 100644 --- a/core/modules/update/lib/Drupal/update/UpdateProcessor.php +++ b/core/modules/update/lib/Drupal/update/UpdateProcessor.php @@ -8,6 +8,9 @@ use Drupal\Component\Utility\Crypt; use Drupal\Core\Config\ConfigFactory; +use Drupal\Core\KeyValueStore\KeyValueFactoryInterface; +use Drupal\Core\KeyValueStore\StateInterface; +use Drupal\Core\PrivateKey; use Drupal\Core\Queue\QueueFactory; /** @@ -65,34 +68,57 @@ class UpdateProcessor { protected $failed; /** + * The state service. + * + * @var \Drupal\Core\KeyValueStore\StateInterface + */ + protected $stateStore; + + /** + * The private key. + * + * @var \Drupal\Core\PrivateKey + */ + protected $privateKey; + + /** * Constructs a UpdateProcessor. * * @param \Drupal\Core\Config\ConfigFactory $config_factory * The config factory. - * @param \Drupal\Core\Queue\QueueInterface $queue_factory + * @param \Drupal\Core\Queue\QueueFactory $queue_factory * The queue factory - * @param \Drupal\update\UpdateFetcher + * @param \Drupal\update\UpdateFetcher $update_fetcher * The update fetcher service + * @param \Drupal\Core\KeyValueStore\StateInterface $state_store + * The state service. + * @param \Drupal\Core\PrivateKey $private_key + * The private key factory service. + * @param \Drupal\Core\KeyValueStore\KeyValueFactoryInterface $key_value_factory + * The key/value factory. + * @param \Drupal\Core\KeyValueStore\KeyValueFactoryInterface $key_value_expirable_factory + * The expirable key/value factory. */ - public function __construct(ConfigFactory $config_factory, QueueFactory $queue_factory, UpdateFetcher $update_fetcher) { + public function __construct(ConfigFactory $config_factory, QueueFactory $queue_factory, UpdateFetcher $update_fetcher, StateInterface $state_store, PrivateKey $private_key, KeyValueFactoryInterface $key_value_factory, KeyValueFactoryInterface $key_value_expirable_factory) { $this->updateFetcher = $update_fetcher; $this->updateSettings = $config_factory->get('update.settings'); $this->fetchQueue = $queue_factory->get('update_fetch_tasks'); - $this->tempStore = \Drupal::keyValueExpirable('update'); - $this->fetchTaskStore = \Drupal::keyValue('update_fetch_task'); - $this->availableReleasesTempStore = \Drupal::keyValueExpirable('update_available_releases'); + $this->tempStore = $key_value_expirable_factory->get('update'); + $this->fetchTaskStore = $key_value_factory->get('update_fetch_task'); + $this->availableReleasesTempStore = $key_value_expirable_factory->get('update_available_releases'); + $this->stateStore = $state_store; + $this->privateKey = $private_key; $this->fetchTasks = array(); $this->failed = array(); } - /** * Adds a task to the queue for fetching release history data for a project. * * We only create a new fetch task if there's no task already in the queue for * this particular project (based on 'update_fetch_task' key-value collection). * - * @param $project + * @param array $project * Associative array of information about a project as created by * update_get_projects(), including keys such as 'name' (short name), and the * 'info' array with data from a .info.yml file for the project. @@ -128,13 +154,13 @@ public function fetchData() { /** * Processes a task to fetch available update data for a single project. * - * Once the release history XML data is downloaded, it is parsed and saved in an - * entry just for that project. + * Once the release history XML data is downloaded, it is parsed and saved in + * an entry just for that project. * - * @param $project + * @param array $project * Associative array of information about the project to fetch data for. * - * @return + * @return bool * TRUE if we fetched parsable XML, otherwise FALSE. */ public function processFetchTask($project) { @@ -153,7 +179,7 @@ public function processFetchTask($project) { $success = FALSE; $available = array(); - $site_key = Crypt::hmacBase64($base_url, \Drupal::service('private_key')->get()); + $site_key = Crypt::hmacBase64($base_url, $this->privateKey->get()); $fetch_url_base = $this->updateFetcher->getFetchBaseUrl($project); $project_name = $project['name']; @@ -186,7 +212,7 @@ public function processFetchTask($project) { $this->tempStore->setWithExpire('fetch_failures', $this->failed, $request_time_difference + (60 * 5)); // Whether this worked or not, we did just (try to) check for updates. - \Drupal::state()->set('update.last_check', REQUEST_TIME + $request_time_difference); + $this->stateStore->set('update.last_check', REQUEST_TIME + $request_time_difference); // Now that we processed the fetch task for this project, clear out the // record for this task so we're willing to fetch again. @@ -198,10 +224,10 @@ public function processFetchTask($project) { /** * Parses the XML of the Drupal release history info files. * - * @param $raw_xml + * @param string $raw_xml * A raw XML string of available release data for a given project. * - * @return + * @return array * Array of parsed data about releases for a given project, or NULL if there * was an error parsing the string. */ @@ -213,11 +239,11 @@ protected function parseXml($raw_xml) { // SimpleXMLElement::__construct produces an E_WARNING error message for // each error found in the XML data and throws an exception if errors // were detected. Catch any exception and return failure (NULL). - return; + return NULL; } // If there is no valid project data, the XML is invalid, so return failure. if (!isset($xml->short_name)) { - return; + return NULL; } $data = array(); foreach ($xml as $k => $v) { @@ -248,7 +274,7 @@ protected function parseXml($raw_xml) { /** * Retrieves the number of items in the update fetch queue. * - * @return + * @return int * An integer estimate of the number of items in the queue. * * @see \Drupal\Core\Queue\QueueInterface::numberOfItems() @@ -274,7 +300,7 @@ public function claimQueueItem() { * Deletes a finished item from the update fetch queue. * * @param $item - * The item returned by Drupal\Core\Queue\QueueInterface::claimItem(). + * The item returned by \Drupal\Core\Queue\QueueInterface::claimItem(). * * @see \Drupal\Core\Queue\QueueInterface::deleteItem() */ @@ -282,4 +308,4 @@ public function deleteQueueItem($item) { return $this->fetchQueue->deleteItem($item); } -} \ No newline at end of file +} diff --git a/core/modules/update/update.services.yml b/core/modules/update/update.services.yml index 176faab..e335631 100644 --- a/core/modules/update/update.services.yml +++ b/core/modules/update/update.services.yml @@ -6,10 +6,10 @@ services: - { name: access_check } update.manager: class: Drupal\update\UpdateManager - arguments: ['@config.factory', '@module_handler', '@update.processor'] + arguments: ['@config.factory', '@module_handler', '@update.processor', '@string_translation', '@keyvalue.expirable'] update.processor: class: Drupal\update\UpdateProcessor - arguments: ['@config.factory', '@queue', '@update.fetcher'] + arguments: ['@config.factory', '@queue', '@update.fetcher', '@state', '@private_key', '@keyvalue', '@keyvalue.expirable'] update.fetcher: class: Drupal\update\UpdateFetcher arguments: ['@config.factory', '@http_default_client']