This repository has been archived by the owner on Sep 8, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create LocaleOptionsManager which creates options for locales and sto…
…res them in cache, if available. Comes with Factory and config setup.
- Loading branch information
Showing
3 changed files
with
148 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
<?php | ||
|
||
namespace Keet\Country\Factory\Service; | ||
|
||
use Interop\Container\ContainerInterface; | ||
use Keet\Country\Service\LocaleOptionsManager; | ||
use Zend\Cache\Storage\StorageInterface; | ||
use Zend\ServiceManager\Factory\FactoryInterface; | ||
|
||
class LocaleOptionsManagerFactory implements FactoryInterface | ||
{ | ||
public function __invoke(ContainerInterface $container, $requestedName, array $options = null) | ||
{ | ||
$config = $container->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, []); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
<?php | ||
|
||
namespace Keet\Country\Service; | ||
|
||
use Zend\Cache\Storage\StorageInterface; | ||
|
||
class LocaleOptionsManager | ||
{ | ||
/** | ||
* @var StorageInterface | ||
*/ | ||
protected $cache; | ||
|
||
/** | ||
* @var array | ||
*/ | ||
protected $config; | ||
|
||
public function __construct(StorageInterface $cache, array $config) | ||
{ | ||
$this->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; | ||
} | ||
|
||
} |