DevelopmentLoader.php
6 years ago
JsonFileLoader.php
6 years ago
LoaderCache.php
6 years ago
LoaderInterface.php
6 years ago
LoaderCache.php
66 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Piwik - free/libre analytics platform |
| 4 | * |
| 5 | * @link https://matomo.org |
| 6 | * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later |
| 7 | */ |
| 8 | |
| 9 | namespace Piwik\Translation\Loader; |
| 10 | |
| 11 | use Piwik\Cache; |
| 12 | |
| 13 | /** |
| 14 | * Caches the translations loaded by another loader. |
| 15 | */ |
| 16 | class LoaderCache implements LoaderInterface |
| 17 | { |
| 18 | /** |
| 19 | * @var LoaderInterface |
| 20 | */ |
| 21 | private $loader; |
| 22 | |
| 23 | /** |
| 24 | * @var Cache\Lazy |
| 25 | */ |
| 26 | private $cache; |
| 27 | |
| 28 | public function __construct(LoaderInterface $loader, Cache\Lazy $cache) |
| 29 | { |
| 30 | $this->loader = $loader; |
| 31 | $this->cache = $cache; |
| 32 | } |
| 33 | |
| 34 | /** |
| 35 | * {@inheritdoc} |
| 36 | */ |
| 37 | public function load($language, array $directories) |
| 38 | { |
| 39 | if (empty($language)) { |
| 40 | return array(); |
| 41 | } |
| 42 | |
| 43 | $cacheKey = $this->getCacheKey($language, $directories); |
| 44 | |
| 45 | $translations = $this->cache->fetch($cacheKey); |
| 46 | |
| 47 | if (empty($translations) || !is_array($translations)) { |
| 48 | $translations = $this->loader->load($language, $directories); |
| 49 | |
| 50 | $this->cache->save($cacheKey, $translations, 43200); // ttl=12hours |
| 51 | } |
| 52 | |
| 53 | return $translations; |
| 54 | } |
| 55 | |
| 56 | private function getCacheKey($language, array $directories) |
| 57 | { |
| 58 | $cacheKey = 'Translations-' . $language . '-'; |
| 59 | |
| 60 | // in case loaded plugins change (ie Tests vs Tracker vs UI etc) |
| 61 | $cacheKey .= sha1(implode('', $directories)); |
| 62 | |
| 63 | return $cacheKey; |
| 64 | } |
| 65 | } |
| 66 |