Created specific factory for AppOptions to prevent circular dependency with cache

This commit is contained in:
Alejandro Celaya 2016-08-15 23:40:49 +02:00
parent ffa6c0d2ca
commit 5ca4bc928d
6 changed files with 77 additions and 6 deletions

View file

@ -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)
{

View file

@ -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(),
]]);
}
}

View file

@ -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,

View file

@ -1,7 +1,6 @@
<?php
namespace Shlinkio\Shlink\Core\Options;
use Acelaya\ZsmAnnotatedServices\Annotation\Inject;
use Shlinkio\Shlink\Common\Util\StringUtilsTrait;
use Zend\Stdlib\AbstractOptions;
@ -25,8 +24,6 @@ class AppOptions extends AbstractOptions
/**
* AppOptions constructor.
* @param array|null|\Traversable $options
*
* @Inject({"config.app_options"})
*/
public function __construct($options = null)
{

View file

@ -0,0 +1,29 @@
<?php
namespace Shlinkio\Shlink\Core\Options;
use Interop\Container\ContainerInterface;
use Interop\Container\Exception\ContainerException;
use Zend\ServiceManager\Exception\ServiceNotCreatedException;
use Zend\ServiceManager\Exception\ServiceNotFoundException;
use Zend\ServiceManager\Factory\FactoryInterface;
class AppOptionsFactory implements FactoryInterface
{
/**
* Create an object
*
* @param ContainerInterface $container
* @param string $requestedName
* @param null|array $options
* @return object
* @throws ServiceNotFoundException if unable to resolve the service.
* @throws ServiceNotCreatedException if an exception is raised when
* creating a service.
* @throws ContainerException if any other error occurs
*/
public function __invoke(ContainerInterface $container, $requestedName, array $options = null)
{
$config = $container->has('config') ? $container->get('config') : [];
return new AppOptions(isset($config['app_options']) ? $config['app_options'] : []);
}
}

View file

@ -0,0 +1,29 @@
<?php
namespace ShlinkioTest\Shlink\Core\Options;
use PHPUnit_Framework_TestCase as TestCase;
use Shlinkio\Shlink\Core\Options\AppOptions;
use Shlinkio\Shlink\Core\Options\AppOptionsFactory;
use Zend\ServiceManager\ServiceManager;
class AppOptionsFactoryTest extends TestCase
{
/**
* @var AppOptionsFactory
*/
protected $factory;
public function setUp()
{
$this->factory = new AppOptionsFactory();
}
/**
* @test
*/
public function serviceIsCreated()
{
$instance = $this->factory->__invoke(new ServiceManager([]), '');
$this->assertInstanceOf(AppOptions::class, $instance);
}
}