mirror of
https://github.com/shlinkio/shlink.git
synced 2026-03-10 09:13:11 +08:00
Created specific factory for AppOptions to prevent circular dependency with cache
This commit is contained in:
@@ -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,
|
||||
|
||||
@@ -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)
|
||||
{
|
||||
|
||||
29
module/Core/src/Options/AppOptionsFactory.php
Normal file
29
module/Core/src/Options/AppOptionsFactory.php
Normal 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'] : []);
|
||||
}
|
||||
}
|
||||
29
module/Core/test/Options/AppOptionsFactoryTest.php
Normal file
29
module/Core/test/Options/AppOptionsFactoryTest.php
Normal 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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user