Moved some elements in Common module to more proper locations

This commit is contained in:
Alejandro Celaya 2019-08-10 23:58:21 +02:00
parent 986c165815
commit 5fa4fa0225
17 changed files with 139 additions and 108 deletions

View file

@ -75,7 +75,8 @@
"Shlinkio\\Shlink\\Core\\": "module/Core/src",
"Shlinkio\\Shlink\\Common\\": "module/Common/src",
"Shlinkio\\Shlink\\EventDispatcher\\": "module/EventDispatcher/src",
"Shlinkio\\Shlink\\IpGeolocation\\": "module/IpGeolocation/src/"
"Shlinkio\\Shlink\\IpGeolocation\\": "module/IpGeolocation/src/",
"Shlinkio\\Shlink\\Integrations\\": "module/Integrations/src/"
},
"files": [
"module/Common/functions/functions.php",
@ -96,7 +97,8 @@
"module/Common/test-db"
],
"ShlinkioTest\\Shlink\\EventDispatcher\\": "module/EventDispatcher/test",
"ShlinkioTest\\Shlink\\IpGeolocation\\": "module/IpGeolocation/test"
"ShlinkioTest\\Shlink\\IpGeolocation\\": "module/IpGeolocation/test",
"ShlinkioTest\\Shlink\\Integrations\\": "module/Integrations/test"
}
},
"scripts": {

View file

@ -1,18 +1,12 @@
<?php
declare(strict_types=1);
use Shlinkio\Shlink\Common\Factory\EmptyResponseImplicitOptionsMiddlewareFactory;
use Zend\Expressive;
use Zend\Expressive\Container;
use Zend\Expressive\Router\Middleware\ImplicitOptionsMiddleware;
return [
'dependencies' => [
'factories' => [
ImplicitOptionsMiddleware::class => EmptyResponseImplicitOptionsMiddlewareFactory::class,
],
'delegators' => [
Expressive\Application::class => [
Container\ApplicationConfigInjectionDelegator::class,

View file

@ -18,6 +18,7 @@ return (new ConfigAggregator\ConfigAggregator([
ExpressiveErrorHandler\ConfigProvider::class,
Common\ConfigProvider::class,
IpGeolocation\ConfigProvider::class,
Integrations\ConfigProvider::class,
Core\ConfigProvider::class,
CLI\ConfigProvider::class,
Rest\ConfigProvider::class,

View file

@ -3,7 +3,6 @@ declare(strict_types=1);
namespace Shlinkio\Shlink\Common;
use GeoIp2\Database\Reader;
use GuzzleHttp\Client as GuzzleClient;
use Monolog\Logger;
use Psr\Log\LoggerInterface;
@ -12,7 +11,6 @@ use Symfony\Component\Filesystem\Filesystem;
use Zend\I18n\Translator\Translator;
use Zend\ServiceManager\AbstractFactory\ConfigAbstractFactory;
use Zend\ServiceManager\Factory\InvokableFactory;
use Zend\ServiceManager\Proxy\LazyServiceFactory;
return [
@ -20,9 +18,8 @@ return [
'factories' => [
GuzzleClient::class => InvokableFactory::class,
Filesystem::class => InvokableFactory::class,
Reader::class => ConfigAbstractFactory::class,
Translator::class => Factory\TranslatorFactory::class,
Translator::class => I18n\TranslatorFactory::class,
Template\Extension\TranslatorExtension::class => ConfigAbstractFactory::class,
Middleware\LocaleMiddleware::class => ConfigAbstractFactory::class,
@ -44,24 +41,9 @@ return [
'abstract_factories' => [
Factory\DottedAccessConfigAbstractFactory::class,
],
'delegators' => [
// The GeoLite2 db reader has to be lazy so that it does not try to load the DB file at app bootstrapping.
// By doing so, it would fail the first time shlink tries to download it.
Reader::class => [
LazyServiceFactory::class,
],
],
'lazy_services' => [
'class_map' => [
Reader::class => Reader::class,
],
],
],
ConfigAbstractFactory::class => [
Reader::class => ['config.geolite2.db_location'],
Template\Extension\TranslatorExtension::class => ['translator'],
Middleware\LocaleMiddleware::class => ['translator'],
Middleware\CloseDbConnectionMiddleware::class => ['em'],

View file

@ -22,11 +22,9 @@ class DottedAccessConfigAbstractFactory implements AbstractFactoryInterface
/**
* Can the factory create an instance for the service?
*
* @param ContainerInterface $container
* @param string $requestedName
* @return bool
*/
public function canCreate(ContainerInterface $container, $requestedName)
public function canCreate(ContainerInterface $container, $requestedName): bool
{
return substr_count($requestedName, '.') > 0;
}

View file

@ -1,34 +0,0 @@
<?php
declare(strict_types=1);
namespace Shlinkio\Shlink\Common\Factory;
use Interop\Container\ContainerInterface;
use Interop\Container\Exception\ContainerException;
use Zend\Diactoros\Response\EmptyResponse;
use Zend\Expressive\Router\Middleware\ImplicitOptionsMiddleware;
use Zend\ServiceManager\Exception\ServiceNotCreatedException;
use Zend\ServiceManager\Exception\ServiceNotFoundException;
use Zend\ServiceManager\Factory\FactoryInterface;
class EmptyResponseImplicitOptionsMiddlewareFactory 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)
{
return new ImplicitOptionsMiddleware(function () {
return new EmptyResponse();
});
}
}

View file

@ -1,32 +0,0 @@
<?php
declare(strict_types=1);
namespace Shlinkio\Shlink\Common\Factory;
use Interop\Container\ContainerInterface;
use Interop\Container\Exception\ContainerException;
use Zend\I18n\Translator\Translator;
use Zend\ServiceManager\Exception\ServiceNotCreatedException;
use Zend\ServiceManager\Exception\ServiceNotFoundException;
use Zend\ServiceManager\Factory\FactoryInterface;
class TranslatorFactory 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->get('config');
return Translator::factory($config['translator'] ?? []);
}
}

View file

@ -0,0 +1,16 @@
<?php
declare(strict_types=1);
namespace Shlinkio\Shlink\Common\I18n;
use Interop\Container\ContainerInterface;
use Zend\I18n\Translator\Translator;
class TranslatorFactory
{
public function __invoke(ContainerInterface $container): Translator
{
$config = $container->get('config');
return Translator::factory($config['translator'] ?? []);
}
}

View file

@ -1,10 +1,10 @@
<?php
declare(strict_types=1);
namespace ShlinkioTest\Shlink\Common\Factory;
namespace ShlinkioTest\Shlink\Common\I18n;
use PHPUnit\Framework\TestCase;
use Shlinkio\Shlink\Common\Factory\TranslatorFactory;
use Shlinkio\Shlink\Common\I18n\TranslatorFactory;
use Zend\I18n\Translator\Translator;
use Zend\ServiceManager\ServiceManager;
@ -19,11 +19,11 @@ class TranslatorFactoryTest extends TestCase
}
/** @test */
public function serviceIsCreated()
public function serviceIsCreated(): void
{
$instance = $this->factory->__invoke(new ServiceManager(['services' => [
$instance = ($this->factory)(new ServiceManager(['services' => [
'config' => [],
]]), '');
]]));
$this->assertInstanceOf(Translator::class, $instance);
}
}

View file

@ -23,6 +23,8 @@ return [
Psr\EventDispatcherInterface::class => Phly\EventDispatcher::class,
],
'delegators' => [
// The listener provider has to be lazy, because it uses the Swoole server to generate AsyncEventListeners
// Without making this lazy, CLI commands which depend on the EventDispatcher fail
Psr\ListenerProviderInterface::class => [
LazyServiceFactory::class,
],

View file

@ -0,0 +1,16 @@
<?php
declare(strict_types=1);
namespace Shlinkio\Shlink\Integrations;
use Zend\Expressive\Router\Middleware\ImplicitOptionsMiddleware;
return [
'dependencies' => [
'factories' => [
ImplicitOptionsMiddleware::class => Middleware\EmptyResponseImplicitOptionsMiddlewareFactory::class,
],
],
];

View file

@ -0,0 +1,14 @@
<?php
declare(strict_types=1);
namespace Shlinkio\Shlink\Integrations;
use function Shlinkio\Shlink\Common\loadConfigFromGlob;
class ConfigProvider
{
public function __invoke()
{
return loadConfigFromGlob(__DIR__ . '/../config/{,*.}config.php');
}
}

View file

@ -0,0 +1,17 @@
<?php
declare(strict_types=1);
namespace Shlinkio\Shlink\Integrations\Middleware;
use Zend\Diactoros\Response\EmptyResponse;
use Zend\Expressive\Router\Middleware\ImplicitOptionsMiddleware;
class EmptyResponseImplicitOptionsMiddlewareFactory
{
public function __invoke()
{
return new ImplicitOptionsMiddleware(function () {
return new EmptyResponse();
});
}
}

View file

@ -1,14 +1,13 @@
<?php
declare(strict_types=1);
namespace ShlinkioTest\Shlink\Common\Factory;
namespace ShlinkioTest\Shlink\Integrations\Middleware;
use PHPUnit\Framework\TestCase;
use ReflectionObject;
use Shlinkio\Shlink\Common\Factory\EmptyResponseImplicitOptionsMiddlewareFactory;
use Shlinkio\Shlink\Integrations\Middleware\EmptyResponseImplicitOptionsMiddlewareFactory;
use Zend\Diactoros\Response\EmptyResponse;
use Zend\Expressive\Router\Middleware\ImplicitOptionsMiddleware;
use Zend\ServiceManager\ServiceManager;
class EmptyResponseImplicitOptionsMiddlewareFactoryTest extends TestCase
{
@ -21,16 +20,16 @@ class EmptyResponseImplicitOptionsMiddlewareFactoryTest extends TestCase
}
/** @test */
public function serviceIsCreated()
public function serviceIsCreated(): void
{
$instance = $this->factory->__invoke(new ServiceManager(), '');
$instance = ($this->factory)();
$this->assertInstanceOf(ImplicitOptionsMiddleware::class, $instance);
}
/** @test */
public function responsePrototypeIsEmptyResponse()
public function responsePrototypeIsEmptyResponse(): void
{
$instance = $this->factory->__invoke(new ServiceManager(), '');
$instance = ($this->factory)();
$ref = new ReflectionObject($instance);
$prop = $ref->getProperty('responseFactory');

View file

@ -0,0 +1,18 @@
# IP Address Geolocation module
Shlink module with tools to locate an IP address suing different strategies.
```php
<?php
declare(strict_types=1);
return [
'geolite2' => [
'db_location' => __DIR__ . '/../../data/GeoLite2-City.mmdb',
'temp_dir' => sys_get_temp_dir(),
// 'download_from' => 'http://geolite.maxmind.com/download/geoip/database/GeoLite2-City.tar.gz',
],
];
```

View file

@ -0,0 +1,35 @@
<?php
declare(strict_types=1);
namespace Shlinkio\Shlink\IpGeolocation;
use GeoIp2\Database\Reader;
use Zend\ServiceManager\AbstractFactory\ConfigAbstractFactory;
use Zend\ServiceManager\Proxy\LazyServiceFactory;
return [
'dependencies' => [
'factories' => [
Reader::class => ConfigAbstractFactory::class,
],
'delegators' => [
// The GeoLite2 db reader has to be lazy so that it does not try to load the DB file at app bootstrapping.
// By doing so, it would fail the first time shlink tries to download it.
Reader::class => [
LazyServiceFactory::class,
],
],
'lazy_services' => [
'class_map' => [
Reader::class => Reader::class,
],
],
],
ConfigAbstractFactory::class => [
Reader::class => ['config.geolite2.db_location'],
],
];

View file

@ -24,6 +24,9 @@
<testsuite name="IpGeolocation">
<directory>./module/IpGeolocation/test</directory>
</testsuite>
<testsuite name="Integrations">
<directory>./module/Integrations/test</directory>
</testsuite>
</testsuites>
<filter>