Skip to content

Config provider for zend expressive #13

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Nov 13, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,20 @@ $app->run($request, $response);

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

### How to install on Zend Expressive?

Use [mtymek/expressive-config-manager](https://github.com/mtymek/expressive-config-manager) and add
`PhpMiddleware\PhpDebugBar\ConfigProvider` class name:

```php
$configManager = new \Zend\Expressive\ConfigManager\ConfigManager([
\PhpMiddleware\PhpDebugBar\ConfigProvider::class,
new \Zend\Expressive\ConfigManager\PhpFileProvider('config/autoload/{{,*.}global,{,*.}local}.php'),
]);
```

more [about config manager](https://zendframework.github.io/zend-expressive/cookbook/modular-layout/).

### How to install on Slim 3?

Add existing factory to container:
Expand Down
7 changes: 7 additions & 0 deletions config/dependency.config.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

return [
'factories' => [
PhpMiddleware\PhpDebugBar\PhpDebugBarMiddleware::class => PhpMiddleware\PhpDebugBar\PhpDebugBarMiddlewareFactory::class,
],
];
12 changes: 12 additions & 0 deletions config/zend-expressive.middleware_pipeline.config.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

use PhpMiddleware\PhpDebugBar\PhpDebugBarMiddleware;

return [
PhpDebugBarMiddleware::class => [
'middleware' => [
PhpDebugBarMiddleware::class,
],
'priority' => 1000,
],
];
20 changes: 20 additions & 0 deletions src/ConfigProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace PhpMiddleware\PhpDebugBar;

final class ConfigProvider
{
public static function getConfig()
{
$self = new self();
return $self();
}

public function __invoke()
{
return [
'dependencies' => include __DIR__ . '/../config/dependency.config.php',
'middleware_pipeline' => include __DIR__ . '/../config/zend-expressive.middleware_pipeline.config.php',
];
}
}
47 changes: 32 additions & 15 deletions test/ZendExpressiveTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,30 @@

namespace PhpMiddlewareTest\PhpDebugBar;

use PhpMiddleware\PhpDebugBar\PhpDebugBarMiddleware;
use PhpMiddleware\PhpDebugBar\PhpDebugBarMiddlewareFactory;
use Interop\Container\ContainerInterface;
use PhpMiddleware\PhpDebugBar\ConfigProvider;
use Zend\Diactoros\Response\EmitterInterface;
use Zend\Diactoros\ServerRequestFactory;
use Zend\Expressive\Application;
use Zend\Expressive\Router\FastRouteRouter;
use Zend\Expressive\Container\ApplicationFactory;
use Zend\ServiceManager\ServiceManager;

final class ZendExpressiveTest extends AbstractMiddlewareRunnerTest
{
protected function dispatchApplication(array $server, array $pipe = [])
private $testEmitter;

protected function setUp()
{
$container = new ServiceManager([
'factories' => [
PhpDebugBarMiddleware::class => PhpDebugBarMiddlewareFactory::class,
],
]);
$router = new FastRouteRouter();
$emitter = new TestEmitter();
parent::setUp();

$this->testEmitter = new TestEmitter();
}

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

$app->pipe(PhpDebugBarMiddleware::class);
$appFactory = new ApplicationFactory();
$app = $appFactory($container);

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

$app->run($serverRequest);

return $emitter->getResponse();
return $this->testEmitter->getResponse();
}

/**
*
* @return ContainerInterface
*/
private function createContainer()
{
$config = ConfigProvider::getConfig();

$serviceManagerConfig = $config['dependencies'];
$serviceManagerConfig['services']['config'] = $config;
$serviceManagerConfig['services'][EmitterInterface::class] = $this->testEmitter;

return new ServiceManager($serviceManagerConfig);
}
}