From 444f98bf7c41b735b4fe847f865e14bc21087da7 Mon Sep 17 00:00:00 2001 From: Robin Keet Date: Mon, 15 Oct 2018 17:03:17 +0200 Subject: [PATCH] Create LocaleOptionsManager which creates options for locales and stores them in cache, if available. Comes with Factory and config setup. --- config/module.config.php | 7 ++ .../Service/LocaleOptionsManagerFactory.php | 29 +++++ src/Service/LocaleOptionsManager.php | 112 ++++++++++++++++++ 3 files changed, 148 insertions(+) create mode 100644 src/Factory/Service/LocaleOptionsManagerFactory.php create mode 100644 src/Service/LocaleOptionsManager.php diff --git a/config/module.config.php b/config/module.config.php index 949338c..4df38a8 100644 --- a/config/module.config.php +++ b/config/module.config.php @@ -23,6 +23,7 @@ use Keet\Country\Factory\InputFilter\Form\CurrencyFormInputFilterFactory; use Keet\Country\Factory\InputFilter\Form\LanguageFormInputFilterFactory; use Keet\Country\Factory\InputFilter\Form\TimezoneFormInputFilterFactory; +use Keet\Country\Factory\Service\LocaleOptionsManagerFactory; use Keet\Country\Fieldset\CoordinatesFieldset; use Keet\Country\Fieldset\CountryFieldset; use Keet\Country\Fieldset\CurrencyFieldset; @@ -43,6 +44,7 @@ use Keet\Country\InputFilter\Form\CurrencyFormInputFilter; use Keet\Country\InputFilter\Form\LanguageFormInputFilter; use Keet\Country\InputFilter\Form\TimezoneFormInputFilter; +use Keet\Country\Service\LocaleOptionsManager; return [ 'doctrine' => [ @@ -98,4 +100,9 @@ TimezoneFieldsetInputFilter::class => TimezoneFieldsetInputFilterFactory::class, ], ], + 'service_manager' => [ + 'factories' => [ + LocaleOptionsManager::class => LocaleOptionsManagerFactory::class, + ], + ], ]; \ No newline at end of file diff --git a/src/Factory/Service/LocaleOptionsManagerFactory.php b/src/Factory/Service/LocaleOptionsManagerFactory.php new file mode 100644 index 0000000..57fe589 --- /dev/null +++ b/src/Factory/Service/LocaleOptionsManagerFactory.php @@ -0,0 +1,29 @@ +get('Config'); + + /** @var StorageInterface $cache */ + $cache = $container->get('FilesystemCache'); + + if ( + array_key_exists('translator', $config) + && array_key_exists('translation_file_patterns', $config['translator']) + && count($config['translator']['translation_file_patterns']) > 0 + ) { + return new LocaleOptionsManager($cache, $config['translator']['translation_file_patterns']); + } + + return new LocaleOptionsManager($cache, []); + } +} \ No newline at end of file diff --git a/src/Service/LocaleOptionsManager.php b/src/Service/LocaleOptionsManager.php new file mode 100644 index 0000000..25a2b12 --- /dev/null +++ b/src/Service/LocaleOptionsManager.php @@ -0,0 +1,112 @@ +setCache($cache); + $this->setConfig($config); + } + + /** + * @param bool $forceCreate + * + * @return array + */ + public function __invoke(bool $forceCreate = false) : array + { + if ($forceCreate) { + // Must recreate cache - remove current locale options + $this->getCache()->removeItem('locale_options'); + } + + // Loads locale options from cache into $cache. Result (bool) of whether action succeeded loaded into $result + $cache = $this->getCache()->getItem('locale_options', $result); + + if ($result) { + // Loading cache (above) succeeded, return cache contents + return $cache; + } + + // Above loading of cache didn't succeed or didn't exist, create new cache + + $options = []; + foreach ($this->getConfig() as $config) { + if ( + array_key_exists('base_dir', $config) + && isset($config['base_dir']) + && array_key_exists('pattern', $config) + && isset($config['pattern']) + ) { + // str_replace used to replace "%s" with "*" to make it a regex pattern accepted by glob() + foreach ( + glob( + str_replace('%s', '*', $config['base_dir'] . DIRECTORY_SEPARATOR . $config['pattern']) + ) as $fileName + ) { + // Specifically returns filename without extension - see: http://php.net/manual/en/function.pathinfo.php + $options[] = pathinfo($fileName, PATHINFO_FILENAME); + } + } + } + + // Save supported locales to cache + if ($this->getCache()->setItem('locale_options', $options)) { + + return $options; + } + + return []; + } + + /** + * @return StorageInterface + */ + public function getCache() : StorageInterface + { + return $this->cache; + } + + /** + * @param StorageInterface $cache + * + * @return LocaleOptionsManager + */ + public function setCache(StorageInterface $cache) : LocaleOptionsManager + { + $this->cache = $cache; + + return $this; + } + + /** + * @return array + */ + public function getConfig() : array + { + return $this->config; + } + + /** + * @param array $config + */ + public function setConfig(array $config) : void + { + $this->config = $config; + } + +} \ No newline at end of file