Skip to content

Commit

Permalink
Merge pull request #44 from aiglesiasn/main
Browse files Browse the repository at this point in the history
Mezzio installation files
  • Loading branch information
akrabat authored Jul 15, 2024
2 parents 5e3b94b + ce0e7f1 commit 651996c
Show file tree
Hide file tree
Showing 5 changed files with 95 additions and 2 deletions.
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ This library cannot by design ensure you get correct and trustworthy results if

`composer require akrabat/ip-address-middleware`

In Mezzio, copy `Mezzio/config/ip_address.global.php.dist` into your Mezzio Application `config/autoload` directory as `ip_address.global.php`

## Usage

In Slim 3:
Expand All @@ -78,12 +80,13 @@ $app->get('/', function ($request, $response, $args) {
});
```

In Laminas, add to your `pipeline.php` config at the correct stage, usually just before the `DispatchMiddleware`:
In Laminas or Mezzio, add to your `pipeline.php` config at the correct stage, usually just before the `DispatchMiddleware`:
```php
# config/pipeline.php
# using default config
$app->add(RKA\Middleware\IpAddress::class);
```
If required, update your `.env` file with the environmental variables found in `/config/autoload/ip_address.global.php`.

## Testing

Expand Down
8 changes: 7 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@
"require": {
"php": "^7.2 || ^8.0",
"psr/http-message": "^1.0 || ^2.0",
"psr/http-server-middleware": "^1.0"
"psr/http-server-middleware": "^1.0",
"psr/container": "^1.0 || ^2.0"
},
"require-dev": {
"laminas/laminas-diactoros": "^2.4 || ^3.0",
Expand All @@ -31,5 +32,10 @@
"psr-4": {
"RKA\\Middleware\\": "src"
}
},
"extra": {
"laminas": {
"config-provider": "RKA\\Middleware\\Mezzio\\ConfigProvider"
}
}
}
26 changes: 26 additions & 0 deletions src/Mezzio/ConfigProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

declare(strict_types=1);

namespace RKA\Middleware\Mezzio;

use RKA\Middleware\IpAddress;

class ConfigProvider
{
public function __invoke(): array
{
return [
'dependencies' => $this->getDependencies(),
];
}

private function getDependencies(): array
{
return [
'factories' => [
IpAddress::class => IpAddressFactory::class,
]
];
}
}
38 changes: 38 additions & 0 deletions src/Mezzio/IpAddressFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

declare(strict_types=1);

namespace RKA\Middleware\Mezzio;

use Psr\Container\ContainerExceptionInterface;
use Psr\Container\ContainerInterface;
use Psr\Container\NotFoundExceptionInterface;
use RKA\Middleware\IpAddress;

class IpAddressFactory
{
/**
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
*/
public function __invoke(ContainerInterface $container): IpAddress
{
$config = [];

if ($container->has('config')) {
$config = $container->get('config');
}

$checkProxyHeaders = $config['rka']['ip_address']['check_proxy_headers'] ?? false;
$trustedProxies = $config['rka']['ip_address']['trusted_proxies'] ?? null;
$attributeName = $config['rka']['ip_address']['attribute_name'] ?? null;
$headersToInspect = $config['rka']['ip_address']['headers_to_inspect'] ?? [];

return new IpAddress(
$checkProxyHeaders,
$trustedProxies,
$attributeName,
$headersToInspect
);
}
}
20 changes: 20 additions & 0 deletions src/Mezzio/config/ip_address.global.php.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

declare(strict_types=1);

// phpcs:disable PSR12.Files.FileHeader.IncorrectOrder

/**
* IpAddress Middleware Configuration
*/

return [
'rka' => [
'ip_address' => [
'check_proxy_headers' => (bool) ($_ENV['IP_ADDRESS_CHECK_PROXY_HEADERS'] ?? false),
'trusted_proxies' => $_ENV['IP_ADDRESS_TRUSTED_PROXIES'] ?? null,
'attribute_name' => $_ENV['IP_ADDRESS_ATTRIBUTE_NAME'] ?? null,
'headers_to_inspect' => explode(',', $_ENV['IP_ADDRESS_HEADERS_TO_INSPECT'] ?? ''),
],
],
];

0 comments on commit 651996c

Please sign in to comment.