class CurrentImportStorage
Provides the locale current import state service.
Handles the current import state for language imports per project and language.
@internal
Hierarchy
- class \Drupal\locale\CurrentImportStorage
Expanded class hierarchy of CurrentImportStorage
See also
6 files declare their use of CurrentImportStorage
- locale.module in core/
modules/ locale/ locale.module - LocaleCronHooks.php in core/
modules/ locale/ src/ Hook/ LocaleCronHooks.php - LocaleFileHashTest.php in core/
modules/ locale/ tests/ src/ Functional/ LocaleFileHashTest.php - LocaleFileManager.php in core/
modules/ locale/ src/ File/ LocaleFileManager.php - LocaleUpdateCronTest.php in core/
modules/ locale/ tests/ src/ Functional/ LocaleUpdateCronTest.php
File
-
core/
modules/ locale/ src/ CurrentImportStorage.php, line 22
Namespace
Drupal\localeView source
class CurrentImportStorage {
/**
* The cache tag for current import info.
*/
protected const LOCALE_CURRENT_IMPORT_CACHE_TAG = 'locale_current_import';
public function __construct(protected readonly Connection $database, #[Autowire(service: 'cache.memory')] protected CacheBackendInterface $memoryCache, protected CacheTagsInvalidatorInterface $cacheTagsInvalidator) {
}
/**
* Get current import information for a given project and language.
*
* @param string $project
* Project to get.
* @param string $langcode
* Langcode to get.
*
* @return \Drupal\locale\CurrentImport|null
* A CurrentImport object if one exists for this project/language.
*/
public function get(string $project, string $langcode) : ?CurrentImport {
$currentImport = NULL;
$cid = $this->getCacheId($project, $langcode);
if ($cached = $this->memoryCache
->get($cid)) {
$currentImport = $cached->data;
}
else {
$result = $this->database
->select('locale_file')
->fields('locale_file', [
'project',
'langcode',
'version',
'timestamp',
'hash',
'last_checked',
])
->condition('project', $project)
->condition('langcode', $langcode)
->execute()
->fetch(FetchAs::Associative);
if ($result) {
$currentImport = CurrentImport::createFromArray($result);
$this->memoryCache
->set($this->getCacheId($currentImport->project, $currentImport->langcode), $currentImport, Cache::PERMANENT, [
self::LOCALE_CURRENT_IMPORT_CACHE_TAG,
]);
}
}
return $currentImport;
}
/**
* Saves the current import information to persistent storage.
*
* @param \Drupal\locale\CurrentImport $currentImport
* CurrentImport representing the project just imported.
*/
public function save(CurrentImport $currentImport) : void {
$this->database
->merge('locale_file')
->keys([
'project' => $currentImport->project,
'langcode' => $currentImport->langcode,
])
->fields([
'version' => $currentImport->version,
'timestamp' => $currentImport->timestamp,
'hash' => $currentImport->hash,
'last_checked' => $currentImport->last_checked,
])
->execute();
$this->memoryCache
->set($this->getCacheId($currentImport->project, $currentImport->langcode), $currentImport, Cache::PERMANENT, [
self::LOCALE_CURRENT_IMPORT_CACHE_TAG,
]);
}
/**
* Deletes the information for the given projects and languages.
*
* @param array $projects
* Project name(s) to be deleted from the import state storage. If both
* project(s) and language code(s) are specified the conditions will be
* ANDed.
* @param array $langcodes
* Language code(s) to be deleted from the import state storage.
*/
public function delete(array $projects, array $langcodes = []) : void {
$query = $this->database
->delete('locale_file');
if (!empty($projects)) {
$query->condition('project', $projects, 'IN');
}
if (!empty($langcodes)) {
$query->condition('langcode', $langcodes, 'IN');
}
$query->execute();
$this->cacheTagsInvalidator
->invalidateTags([
self::LOCALE_CURRENT_IMPORT_CACHE_TAG,
]);
}
/**
* Get projects due for an import check.
*
* @param array $projects
* Projects to get.
* @param int $requestTime
* The timestamp to check since.
* @param int $checkTime
* The timestamp the check was queued.
*
* @return array
* Array of projects and their languages.
*/
public function getOutdatedImports(array $projects, int $requestTime, int $checkTime) : ?array {
$outdatedImports = [];
$results = $this->database
->select('locale_file', 'f')
->condition('f.project', $projects, 'IN')
->condition('f.last_checked', $checkTime, '<')
->fields('f', [
'project',
'langcode',
])
->execute()
->fetchAll();
foreach ($results as $result) {
$outdatedImports[$result->project][] = $result->langcode;
$this->updateLastChecked($result->project, $result->langcode, $requestTime);
}
return $outdatedImports;
}
/**
* Updates the last checked time for an import.
*
* @param string $project
* Project to update.
* @param string $langcode
* Langcode to update.
* @param int $requestTime
* The timestamp to check since.
*/
protected function updateLastChecked(string $project, string $langcode, int $requestTime) : void {
$this->database
->update('locale_file')
->fields([
'last_checked' => $requestTime,
])
->condition('project', $project)
->condition('langcode', $langcode)
->execute();
$this->memoryCache
->delete($this->getCacheId($project, $langcode));
}
/**
* Get CacheId for a given project and language.
*
* @param string $project
* Project to get CacheId for.
* @param string $langcode
* Langcode to get CacheId for.
*
* @return string
* The CacheId.
*/
protected function getCacheId(string $project, string $langcode) {
return "locale_current_import:{$project}:{$langcode}";
}
}
Members
| Title Sort descending | Modifiers | Object type | Summary |
|---|---|---|---|
| CurrentImportStorage::delete | public | function | Deletes the information for the given projects and languages. |
| CurrentImportStorage::get | public | function | Get current import information for a given project and language. |
| CurrentImportStorage::getCacheId | protected | function | Get CacheId for a given project and language. |
| CurrentImportStorage::getOutdatedImports | public | function | Get projects due for an import check. |
| CurrentImportStorage::LOCALE_CURRENT_IMPORT_CACHE_TAG | protected | constant | The cache tag for current import info. |
| CurrentImportStorage::save | public | function | Saves the current import information to persistent storage. |
| CurrentImportStorage::updateLastChecked | protected | function | Updates the last checked time for an import. |
| CurrentImportStorage::__construct | public | function |
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.