Skip to content
This repository has been archived by the owner on Sep 8, 2020. It is now read-only.

Commit

Permalink
Create LocaleOptionsManager which creates options for locales and sto…
Browse files Browse the repository at this point in the history
…res them in cache, if available. Comes with Factory and config setup.
  • Loading branch information
rkeet committed Oct 15, 2018
1 parent 4b9f905 commit 444f98b
Show file tree
Hide file tree
Showing 3 changed files with 148 additions and 0 deletions.
7 changes: 7 additions & 0 deletions config/module.config.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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' => [
Expand Down Expand Up @@ -98,4 +100,9 @@
TimezoneFieldsetInputFilter::class => TimezoneFieldsetInputFilterFactory::class,
],
],
'service_manager' => [
'factories' => [
LocaleOptionsManager::class => LocaleOptionsManagerFactory::class,
],
],
];
29 changes: 29 additions & 0 deletions src/Factory/Service/LocaleOptionsManagerFactory.php
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, []);
}
}
112 changes: 112 additions & 0 deletions src/Service/LocaleOptionsManager.php
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;
}

}

0 comments on commit 444f98b

Please sign in to comment.