diff --git a/module/Common/src/Factory/CacheFactory.php b/module/Common/src/Factory/CacheFactory.php index 905ebf56..73cc9d2e 100644 --- a/module/Common/src/Factory/CacheFactory.php +++ b/module/Common/src/Factory/CacheFactory.php @@ -4,6 +4,7 @@ namespace Shlinkio\Shlink\Common\Factory; use Doctrine\Common\Cache; use Interop\Container\ContainerInterface; use Interop\Container\Exception\ContainerException; +use Shlinkio\Shlink\Core\Options\AppOptions; use Zend\ServiceManager\Exception\ServiceNotCreatedException; use Zend\ServiceManager\Exception\ServiceNotFoundException; use Zend\ServiceManager\Factory\FactoryInterface; @@ -31,6 +32,19 @@ class CacheFactory implements FactoryInterface * @throws ContainerException if any other error occurs */ public function __invoke(ContainerInterface $container, $requestedName, array $options = null) + { + $appOptions = $container->get(AppOptions::class); + $adapter = $this->getAdapter($container); + $adapter->setNamespace($appOptions->__toString()); + + return $adapter; + } + + /** + * @param ContainerInterface $container + * @return Cache\CacheProvider + */ + protected function getAdapter(ContainerInterface $container) { // Try to get the adapter from config $config = $container->get('config'); @@ -47,7 +61,7 @@ class CacheFactory implements FactoryInterface /** * @param array $cacheConfig - * @return Cache\Cache + * @return Cache\CacheProvider */ protected function resolveCacheAdapter(array $cacheConfig) { diff --git a/module/Common/test/Factory/CacheFactoryTest.php b/module/Common/test/Factory/CacheFactoryTest.php index b7fc49a8..0d50aab6 100644 --- a/module/Common/test/Factory/CacheFactoryTest.php +++ b/module/Common/test/Factory/CacheFactoryTest.php @@ -8,6 +8,7 @@ use Doctrine\Common\Cache\MemcachedCache; use Doctrine\Common\Cache\RedisCache; use PHPUnit_Framework_TestCase as TestCase; use Shlinkio\Shlink\Common\Factory\CacheFactory; +use Shlinkio\Shlink\Core\Options\AppOptions; use Zend\ServiceManager\ServiceManager; class CacheFactoryTest extends TestCase @@ -109,6 +110,7 @@ class CacheFactoryTest extends TestCase 'options' => $options, ], ] : [], + AppOptions::class => new AppOptions(), ]]); } } diff --git a/module/Core/config/dependencies.config.php b/module/Core/config/dependencies.config.php index 6a5596ee..9f2c9cc9 100644 --- a/module/Core/config/dependencies.config.php +++ b/module/Core/config/dependencies.config.php @@ -2,14 +2,14 @@ use Acelaya\ZsmAnnotatedServices\Factory\V3\AnnotatedFactory; use Shlinkio\Shlink\Core\Action; use Shlinkio\Shlink\Core\Middleware; -use Shlinkio\Shlink\Core\Options\AppOptions; +use Shlinkio\Shlink\Core\Options; use Shlinkio\Shlink\Core\Service; return [ 'dependencies' => [ 'factories' => [ - AppOptions::class => AnnotatedFactory::class, + Options\AppOptions::class => Options\AppOptionsFactory::class, // Services Service\UrlShortener::class => AnnotatedFactory::class, diff --git a/module/Core/src/Options/AppOptions.php b/module/Core/src/Options/AppOptions.php index 6ee1322c..cce854a2 100644 --- a/module/Core/src/Options/AppOptions.php +++ b/module/Core/src/Options/AppOptions.php @@ -1,7 +1,6 @@ has('config') ? $container->get('config') : []; + return new AppOptions(isset($config['app_options']) ? $config['app_options'] : []); + } +} diff --git a/module/Core/test/Options/AppOptionsFactoryTest.php b/module/Core/test/Options/AppOptionsFactoryTest.php new file mode 100644 index 00000000..a3d9ac1a --- /dev/null +++ b/module/Core/test/Options/AppOptionsFactoryTest.php @@ -0,0 +1,29 @@ +factory = new AppOptionsFactory(); + } + + /** + * @test + */ + public function serviceIsCreated() + { + $instance = $this->factory->__invoke(new ServiceManager([]), ''); + $this->assertInstanceOf(AppOptions::class, $instance); + } +}