mirror of
https://github.com/shlinkio/shlink.git
synced 2025-02-25 18:45:27 -06:00
Simplified IpAddressMiddlewareFactory and decoupled from Core module
This commit is contained in:
parent
5fa4fa0225
commit
0323e0d17d
@ -6,6 +6,7 @@ namespace Shlinkio\Shlink\Common\Image;
|
|||||||
use mikehaertl\wkhtmlto\Image;
|
use mikehaertl\wkhtmlto\Image;
|
||||||
use Zend\ServiceManager\AbstractPluginManager;
|
use Zend\ServiceManager\AbstractPluginManager;
|
||||||
|
|
||||||
|
/** @deprecated */
|
||||||
class ImageBuilder extends AbstractPluginManager implements ImageBuilderInterface
|
class ImageBuilder extends AbstractPluginManager implements ImageBuilderInterface
|
||||||
{
|
{
|
||||||
protected $instanceOf = Image::class;
|
protected $instanceOf = Image::class;
|
||||||
|
@ -10,6 +10,7 @@ use Zend\ServiceManager\Exception\ServiceNotCreatedException;
|
|||||||
use Zend\ServiceManager\Exception\ServiceNotFoundException;
|
use Zend\ServiceManager\Exception\ServiceNotFoundException;
|
||||||
use Zend\ServiceManager\Factory\FactoryInterface;
|
use Zend\ServiceManager\Factory\FactoryInterface;
|
||||||
|
|
||||||
|
/** @deprecated */
|
||||||
class ImageBuilderFactory implements FactoryInterface
|
class ImageBuilderFactory implements FactoryInterface
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
|
@ -5,6 +5,7 @@ namespace Shlinkio\Shlink\Common\Image;
|
|||||||
|
|
||||||
use Zend\ServiceManager\ServiceLocatorInterface;
|
use Zend\ServiceManager\ServiceLocatorInterface;
|
||||||
|
|
||||||
|
/** @deprecated */
|
||||||
interface ImageBuilderInterface extends ServiceLocatorInterface
|
interface ImageBuilderInterface extends ServiceLocatorInterface
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
@ -10,6 +10,7 @@ use Zend\ServiceManager\Exception\ServiceNotCreatedException;
|
|||||||
use Zend\ServiceManager\Exception\ServiceNotFoundException;
|
use Zend\ServiceManager\Exception\ServiceNotFoundException;
|
||||||
use Zend\ServiceManager\Factory\FactoryInterface;
|
use Zend\ServiceManager\Factory\FactoryInterface;
|
||||||
|
|
||||||
|
/** @deprecated */
|
||||||
class ImageFactory implements FactoryInterface
|
class ImageFactory implements FactoryInterface
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
|
@ -5,29 +5,14 @@ namespace Shlinkio\Shlink\Common\Logger;
|
|||||||
|
|
||||||
use Cascade\Cascade;
|
use Cascade\Cascade;
|
||||||
use Interop\Container\ContainerInterface;
|
use Interop\Container\ContainerInterface;
|
||||||
use Interop\Container\Exception\ContainerException;
|
use Monolog\Logger;
|
||||||
use Zend\ServiceManager\Exception\ServiceNotCreatedException;
|
|
||||||
use Zend\ServiceManager\Exception\ServiceNotFoundException;
|
|
||||||
use Zend\ServiceManager\Factory\FactoryInterface;
|
|
||||||
|
|
||||||
use function count;
|
use function count;
|
||||||
use function explode;
|
use function explode;
|
||||||
|
|
||||||
class LoggerFactory implements FactoryInterface
|
class LoggerFactory
|
||||||
{
|
{
|
||||||
/**
|
public function __invoke(ContainerInterface $container, string $requestedName, ?array $options = null): Logger
|
||||||
* 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') : [];
|
$config = $container->has('config') ? $container->get('config') : [];
|
||||||
Cascade::fileConfig($config['logger'] ?? ['loggers' => []]);
|
Cascade::fileConfig($config['logger'] ?? ['loggers' => []]);
|
||||||
|
@ -3,28 +3,17 @@ declare(strict_types=1);
|
|||||||
|
|
||||||
namespace Shlinkio\Shlink\Common\Middleware;
|
namespace Shlinkio\Shlink\Common\Middleware;
|
||||||
|
|
||||||
use Interop\Container\ContainerInterface;
|
use Psr\Container\ContainerInterface;
|
||||||
use RKA\Middleware\IpAddress;
|
use RKA\Middleware\IpAddress;
|
||||||
use Shlinkio\Shlink\Core\Model\Visitor;
|
|
||||||
use Zend\ServiceManager\Exception\ServiceNotCreatedException;
|
|
||||||
use Zend\ServiceManager\Exception\ServiceNotFoundException;
|
|
||||||
use Zend\ServiceManager\Factory\FactoryInterface;
|
|
||||||
|
|
||||||
class IpAddressMiddlewareFactory implements FactoryInterface
|
class IpAddressMiddlewareFactory
|
||||||
{
|
{
|
||||||
/**
|
public const REQUEST_ATTR = 'remote_address';
|
||||||
* Create an object
|
|
||||||
*
|
public function __invoke(ContainerInterface $container): IpAddress
|
||||||
* @param ContainerInterface $container
|
|
||||||
* @param string $requestedName
|
|
||||||
* @param null|array $options
|
|
||||||
* @throws ServiceNotFoundException if unable to resolve the service.
|
|
||||||
* @throws ServiceNotCreatedException if an exception is raised when creating a service.
|
|
||||||
*/
|
|
||||||
public function __invoke(ContainerInterface $container, $requestedName, ?array $options = null): IpAddress
|
|
||||||
{
|
{
|
||||||
$config = $container->get('config');
|
$config = $container->get('config');
|
||||||
$headersToInspect = $config['ip_address_resolution']['headers_to_inspect'] ?? [];
|
$headersToInspect = $config['ip_address_resolution']['headers_to_inspect'] ?? [];
|
||||||
return new IpAddress(true, [], Visitor::REMOTE_ADDRESS_ATTR, $headersToInspect);
|
return new IpAddress(true, [], self::REQUEST_ATTR, $headersToInspect);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -6,7 +6,6 @@ namespace ShlinkioTest\Shlink\Common\Middleware;
|
|||||||
use PHPUnit\Framework\TestCase;
|
use PHPUnit\Framework\TestCase;
|
||||||
use ReflectionObject;
|
use ReflectionObject;
|
||||||
use Shlinkio\Shlink\Common\Middleware\IpAddressMiddlewareFactory;
|
use Shlinkio\Shlink\Common\Middleware\IpAddressMiddlewareFactory;
|
||||||
use Shlinkio\Shlink\Core\Model\Visitor;
|
|
||||||
use Zend\ServiceManager\ServiceManager;
|
use Zend\ServiceManager\ServiceManager;
|
||||||
|
|
||||||
class IpAddressMiddlewareFactoryTest extends TestCase
|
class IpAddressMiddlewareFactoryTest extends TestCase
|
||||||
@ -26,7 +25,7 @@ class IpAddressMiddlewareFactoryTest extends TestCase
|
|||||||
{
|
{
|
||||||
$instance = ($this->factory)(new ServiceManager(['services' => [
|
$instance = ($this->factory)(new ServiceManager(['services' => [
|
||||||
'config' => $config,
|
'config' => $config,
|
||||||
]]), '');
|
]]));
|
||||||
|
|
||||||
$ref = new ReflectionObject($instance);
|
$ref = new ReflectionObject($instance);
|
||||||
$checkProxyHeaders = $ref->getProperty('checkProxyHeaders');
|
$checkProxyHeaders = $ref->getProperty('checkProxyHeaders');
|
||||||
@ -40,7 +39,7 @@ class IpAddressMiddlewareFactoryTest extends TestCase
|
|||||||
|
|
||||||
$this->assertTrue($checkProxyHeaders->getValue($instance));
|
$this->assertTrue($checkProxyHeaders->getValue($instance));
|
||||||
$this->assertEquals([], $trustedProxies->getValue($instance));
|
$this->assertEquals([], $trustedProxies->getValue($instance));
|
||||||
$this->assertEquals(Visitor::REMOTE_ADDRESS_ATTR, $attributeName->getValue($instance));
|
$this->assertEquals(IpAddressMiddlewareFactory::REQUEST_ATTR, $attributeName->getValue($instance));
|
||||||
$this->assertEquals($expectedHeadersToInspect, $headersToInspect->getValue($instance));
|
$this->assertEquals($expectedHeadersToInspect, $headersToInspect->getValue($instance));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -4,11 +4,10 @@ declare(strict_types=1);
|
|||||||
namespace Shlinkio\Shlink\Core\Model;
|
namespace Shlinkio\Shlink\Core\Model;
|
||||||
|
|
||||||
use Psr\Http\Message\ServerRequestInterface;
|
use Psr\Http\Message\ServerRequestInterface;
|
||||||
|
use Shlinkio\Shlink\Common\Middleware\IpAddressMiddlewareFactory;
|
||||||
|
|
||||||
final class Visitor
|
final class Visitor
|
||||||
{
|
{
|
||||||
public const REMOTE_ADDRESS_ATTR = 'remote_address';
|
|
||||||
|
|
||||||
/** @var string */
|
/** @var string */
|
||||||
private $userAgent;
|
private $userAgent;
|
||||||
/** @var string */
|
/** @var string */
|
||||||
@ -28,7 +27,7 @@ final class Visitor
|
|||||||
return new self(
|
return new self(
|
||||||
$request->getHeaderLine('User-Agent'),
|
$request->getHeaderLine('User-Agent'),
|
||||||
$request->getHeaderLine('Referer'),
|
$request->getHeaderLine('Referer'),
|
||||||
$request->getAttribute(self::REMOTE_ADDRESS_ATTR)
|
$request->getAttribute(IpAddressMiddlewareFactory::REQUEST_ATTR)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user