Skip to content

Commit 651996c

Browse files
authored
Merge pull request #44 from aiglesiasn/main
Mezzio installation files
2 parents 5e3b94b + ce0e7f1 commit 651996c

File tree

5 files changed

+95
-2
lines changed

5 files changed

+95
-2
lines changed

README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,8 @@ This library cannot by design ensure you get correct and trustworthy results if
6262

6363
`composer require akrabat/ip-address-middleware`
6464

65+
In Mezzio, copy `Mezzio/config/ip_address.global.php.dist` into your Mezzio Application `config/autoload` directory as `ip_address.global.php`
66+
6567
## Usage
6668

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

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

8891
## Testing
8992

composer.json

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@
2020
"require": {
2121
"php": "^7.2 || ^8.0",
2222
"psr/http-message": "^1.0 || ^2.0",
23-
"psr/http-server-middleware": "^1.0"
23+
"psr/http-server-middleware": "^1.0",
24+
"psr/container": "^1.0 || ^2.0"
2425
},
2526
"require-dev": {
2627
"laminas/laminas-diactoros": "^2.4 || ^3.0",
@@ -31,5 +32,10 @@
3132
"psr-4": {
3233
"RKA\\Middleware\\": "src"
3334
}
35+
},
36+
"extra": {
37+
"laminas": {
38+
"config-provider": "RKA\\Middleware\\Mezzio\\ConfigProvider"
39+
}
3440
}
3541
}

src/Mezzio/ConfigProvider.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace RKA\Middleware\Mezzio;
6+
7+
use RKA\Middleware\IpAddress;
8+
9+
class ConfigProvider
10+
{
11+
public function __invoke(): array
12+
{
13+
return [
14+
'dependencies' => $this->getDependencies(),
15+
];
16+
}
17+
18+
private function getDependencies(): array
19+
{
20+
return [
21+
'factories' => [
22+
IpAddress::class => IpAddressFactory::class,
23+
]
24+
];
25+
}
26+
}

src/Mezzio/IpAddressFactory.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace RKA\Middleware\Mezzio;
6+
7+
use Psr\Container\ContainerExceptionInterface;
8+
use Psr\Container\ContainerInterface;
9+
use Psr\Container\NotFoundExceptionInterface;
10+
use RKA\Middleware\IpAddress;
11+
12+
class IpAddressFactory
13+
{
14+
/**
15+
* @throws ContainerExceptionInterface
16+
* @throws NotFoundExceptionInterface
17+
*/
18+
public function __invoke(ContainerInterface $container): IpAddress
19+
{
20+
$config = [];
21+
22+
if ($container->has('config')) {
23+
$config = $container->get('config');
24+
}
25+
26+
$checkProxyHeaders = $config['rka']['ip_address']['check_proxy_headers'] ?? false;
27+
$trustedProxies = $config['rka']['ip_address']['trusted_proxies'] ?? null;
28+
$attributeName = $config['rka']['ip_address']['attribute_name'] ?? null;
29+
$headersToInspect = $config['rka']['ip_address']['headers_to_inspect'] ?? [];
30+
31+
return new IpAddress(
32+
$checkProxyHeaders,
33+
$trustedProxies,
34+
$attributeName,
35+
$headersToInspect
36+
);
37+
}
38+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
// phpcs:disable PSR12.Files.FileHeader.IncorrectOrder
6+
7+
/**
8+
* IpAddress Middleware Configuration
9+
*/
10+
11+
return [
12+
'rka' => [
13+
'ip_address' => [
14+
'check_proxy_headers' => (bool) ($_ENV['IP_ADDRESS_CHECK_PROXY_HEADERS'] ?? false),
15+
'trusted_proxies' => $_ENV['IP_ADDRESS_TRUSTED_PROXIES'] ?? null,
16+
'attribute_name' => $_ENV['IP_ADDRESS_ATTRIBUTE_NAME'] ?? null,
17+
'headers_to_inspect' => explode(',', $_ENV['IP_ADDRESS_HEADERS_TO_INSPECT'] ?? ''),
18+
],
19+
],
20+
];

0 commit comments

Comments
 (0)