Skip to content

Commit 1f02a97

Browse files
authored
Merge pull request #13 from php-middleware/feature/config-provider
Config provider for zend expressive
2 parents d3db972 + 733645e commit 1f02a97

5 files changed

+85
-15
lines changed

Diff for: README.md

+14
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,20 @@ $app->run($request, $response);
2828

2929
You don't need to copy any static assets from phpdebugbar vendor!
3030

31+
### How to install on Zend Expressive?
32+
33+
Use [mtymek/expressive-config-manager](https://github.com/mtymek/expressive-config-manager) and add
34+
`PhpMiddleware\PhpDebugBar\ConfigProvider` class name:
35+
36+
```php
37+
$configManager = new \Zend\Expressive\ConfigManager\ConfigManager([
38+
\PhpMiddleware\PhpDebugBar\ConfigProvider::class,
39+
new \Zend\Expressive\ConfigManager\PhpFileProvider('config/autoload/{{,*.}global,{,*.}local}.php'),
40+
]);
41+
```
42+
43+
more [about config manager](https://zendframework.github.io/zend-expressive/cookbook/modular-layout/).
44+
3145
### How to install on Slim 3?
3246

3347
Add existing factory to container:

Diff for: config/dependency.config.php

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
3+
return [
4+
'factories' => [
5+
PhpMiddleware\PhpDebugBar\PhpDebugBarMiddleware::class => PhpMiddleware\PhpDebugBar\PhpDebugBarMiddlewareFactory::class,
6+
],
7+
];
+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
use PhpMiddleware\PhpDebugBar\PhpDebugBarMiddleware;
4+
5+
return [
6+
PhpDebugBarMiddleware::class => [
7+
'middleware' => [
8+
PhpDebugBarMiddleware::class,
9+
],
10+
'priority' => 1000,
11+
],
12+
];

Diff for: src/ConfigProvider.php

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
namespace PhpMiddleware\PhpDebugBar;
4+
5+
final class ConfigProvider
6+
{
7+
public static function getConfig()
8+
{
9+
$self = new self();
10+
return $self();
11+
}
12+
13+
public function __invoke()
14+
{
15+
return [
16+
'dependencies' => include __DIR__ . '/../config/dependency.config.php',
17+
'middleware_pipeline' => include __DIR__ . '/../config/zend-expressive.middleware_pipeline.config.php',
18+
];
19+
}
20+
}

Diff for: test/ZendExpressiveTest.php

+32-15
Original file line numberDiff line numberDiff line change
@@ -2,28 +2,30 @@
22

33
namespace PhpMiddlewareTest\PhpDebugBar;
44

5-
use PhpMiddleware\PhpDebugBar\PhpDebugBarMiddleware;
6-
use PhpMiddleware\PhpDebugBar\PhpDebugBarMiddlewareFactory;
5+
use Interop\Container\ContainerInterface;
6+
use PhpMiddleware\PhpDebugBar\ConfigProvider;
7+
use Zend\Diactoros\Response\EmitterInterface;
78
use Zend\Diactoros\ServerRequestFactory;
8-
use Zend\Expressive\Application;
9-
use Zend\Expressive\Router\FastRouteRouter;
9+
use Zend\Expressive\Container\ApplicationFactory;
1010
use Zend\ServiceManager\ServiceManager;
1111

1212
final class ZendExpressiveTest extends AbstractMiddlewareRunnerTest
1313
{
14-
protected function dispatchApplication(array $server, array $pipe = [])
14+
private $testEmitter;
15+
16+
protected function setUp()
1517
{
16-
$container = new ServiceManager([
17-
'factories' => [
18-
PhpDebugBarMiddleware::class => PhpDebugBarMiddlewareFactory::class,
19-
],
20-
]);
21-
$router = new FastRouteRouter();
22-
$emitter = new TestEmitter();
18+
parent::setUp();
19+
20+
$this->testEmitter = new TestEmitter();
21+
}
2322

24-
$app = new Application($router, $container, null, $emitter);
23+
protected function dispatchApplication(array $server, array $pipe = [])
24+
{
25+
$container = $this->createContainer();
2526

26-
$app->pipe(PhpDebugBarMiddleware::class);
27+
$appFactory = new ApplicationFactory();
28+
$app = $appFactory($container);
2729

2830
foreach ($pipe as $pattern => $middleware) {
2931
$app->get($pattern, $middleware);
@@ -36,6 +38,21 @@ protected function dispatchApplication(array $server, array $pipe = [])
3638

3739
$app->run($serverRequest);
3840

39-
return $emitter->getResponse();
41+
return $this->testEmitter->getResponse();
42+
}
43+
44+
/**
45+
*
46+
* @return ContainerInterface
47+
*/
48+
private function createContainer()
49+
{
50+
$config = ConfigProvider::getConfig();
51+
52+
$serviceManagerConfig = $config['dependencies'];
53+
$serviceManagerConfig['services']['config'] = $config;
54+
$serviceManagerConfig['services'][EmitterInterface::class] = $this->testEmitter;
55+
56+
return new ServiceManager($serviceManagerConfig);
4057
}
4158
}

0 commit comments

Comments
 (0)