Skip to content

Configurable services #14

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 5 commits into from
Nov 14, 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
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"php": "^5.5 || ^7.0",
"maximebf/debugbar": "^1.10",
"psr/http-message": "^1.0",
"container-interop/container-interop": "^1.1",
"zendframework/zend-diactoros": "^1.1.3"
},
"require-dev": {
Expand Down
1 change: 1 addition & 0 deletions config/dependency.config.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@
return [
'factories' => [
PhpMiddleware\PhpDebugBar\PhpDebugBarMiddleware::class => PhpMiddleware\PhpDebugBar\PhpDebugBarMiddlewareFactory::class,
DebugBar\DataCollector\ConfigCollector::class => PhpMiddleware\PhpDebugBar\ConfigCollectorFactory::class,
],
];
15 changes: 15 additions & 0 deletions config/phpdebugbar.config.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

return [
'phpmiddleware' => [
'phpdebugbar' => [
'javascript_renderer' => [
'base_url' => '/phpdebugbar',
],
'collectors' => [
DebugBar\DataCollector\ConfigCollector::class,
],
'storage' => null,
],
],
];
16 changes: 16 additions & 0 deletions src/ConfigCollectorFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace PhpMiddleware\PhpDebugBar;

use DebugBar\DataCollector\ConfigCollector;
use Interop\Container\ContainerInterface;

final class ConfigCollectorFactory
{
public function __invoke(ContainerInterface $container)
{
$data = $container->get('config');

return new ConfigCollector($data, 'Config');
}
}
9 changes: 5 additions & 4 deletions src/ConfigProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@ public static function getConfig()

public function __invoke()
{
return [
'dependencies' => include __DIR__ . '/../config/dependency.config.php',
'middleware_pipeline' => include __DIR__ . '/../config/zend-expressive.middleware_pipeline.config.php',
];
$config = include __DIR__ . '/../config/phpdebugbar.config.php';
$config['dependencies'] = include __DIR__ . '/../config/dependency.config.php';
$config['middleware_pipeline'] = include __DIR__ . '/../config/zend-expressive.middleware_pipeline.config.php';

return $config;
}
}
36 changes: 36 additions & 0 deletions src/JavascriptRendererFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

namespace PhpMiddleware\PhpDebugBar;

use DebugBar\DebugBar;
use DebugBar\JavascriptRenderer;
use Interop\Container\ContainerInterface;

final class JavascriptRendererFactory
{
public function __invoke(ContainerInterface $container = null)
{
if ($container === null || !$container->has(DebugBar::class)) {
$standardDebugBarFactory = new StandardDebugBarFactory();
$debugbar = $standardDebugBarFactory($container);
} else {
$debugbar = $container->get(DebugBar::class);
}

$renderer = new JavascriptRenderer($debugbar);

$config = $container !== null && $container->has('config') ? $container->get('config') : [];

if (isset($config['phpmiddleware']['phpdebugbar']['javascript_renderer'])) {
$rendererOptions = $config['phpmiddleware']['phpdebugbar']['javascript_renderer'];
} else {
$rendererOptions = [
'base_url' => '/phpdebugbar',
];
}

$renderer->setOptions($rendererOptions);

return $renderer;
}
}
19 changes: 9 additions & 10 deletions src/PhpDebugBarMiddlewareFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,19 @@

namespace PhpMiddleware\PhpDebugBar;

use DebugBar\StandardDebugBar;
use DebugBar\JavascriptRenderer;
use Interop\Container\ContainerInterface;

/**
* Default, simple factory for middleware
*
* @author Witold Wasiczko <witold@wasiczko.pl>
*/
final class PhpDebugBarMiddlewareFactory
{
public function __invoke()
public function __invoke(ContainerInterface $container = null)
{
$debugbar = new StandardDebugBar();
$renderer = $debugbar->getJavascriptRenderer('/phpdebugbar');

if ($container === null || !$container->has(JavascriptRenderer::class)) {
$rendererFactory = new JavascriptRendererFactory();
$renderer = $rendererFactory($container);
} else {
$renderer = $container->get(JavascriptRenderer::class);
}
return new PhpDebugBarMiddleware($renderer);
}
}
32 changes: 32 additions & 0 deletions src/StandardDebugBarFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

namespace PhpMiddleware\PhpDebugBar;

use DebugBar\StandardDebugBar;
use Interop\Container\ContainerInterface;

final class StandardDebugBarFactory
{
public function __invoke(ContainerInterface $container = null)
{
$debugBar = new StandardDebugBar();

if ($container !== null) {
$config = $container->has('config') ? $container->get('config') : [];

$collectors = isset($config['phpmiddleware']['phpdebugbar']['collectors']) ? $config['phpmiddleware']['phpdebugbar']['collectors'] : [];

foreach ($collectors as $collectorName) {
$collector = $container->get($collectorName);
$debugBar->addCollector($collector);
}

if (isset($config['phpmiddleware']['phpdebugbar']['storage']) && is_string($config['phpmiddleware']['phpdebugbar']['storage'])) {
$storage = $container->get($config['phpmiddleware']['phpdebugbar']['storage']);
$debugBar->setStorage($config['phpmiddleware']['phpdebugbar']['storage']);
}
}

return $debugBar;
}
}
20 changes: 20 additions & 0 deletions test/ZendExpressiveTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@

use Interop\Container\ContainerInterface;
use PhpMiddleware\PhpDebugBar\ConfigProvider;
use Psr\Http\Message\ServerRequestInterface;
use Zend\Diactoros\Response\EmitterInterface;
use Zend\Diactoros\ServerRequestFactory;
use Zend\Expressive\Container\ApplicationFactory;
use Zend\ServiceManager\ServiceManager;
use Zend\Stratigility\Http\ResponseInterface;

final class ZendExpressiveTest extends AbstractMiddlewareRunnerTest
{
Expand All @@ -20,6 +22,24 @@ protected function setUp()
$this->testEmitter = new TestEmitter();
}

final public function testContainsConfigCollectorOutput()
{
$response = $this->dispatchApplication([
'REQUEST_URI' => '/hello',
'REQUEST_METHOD' => 'GET',
'HTTP_ACCEPT' => 'text/html',
], [
'/hello' => function (ServerRequestInterface $request, ResponseInterface $response, $next) {
$response->getBody()->write('Hello!');
return $response;
},
]);

$responseBody = (string) $response->getBody();

$this->assertContains('DebugBar\\\DataCollector\\\ConfigCollector', $responseBody);
}

protected function dispatchApplication(array $server, array $pipe = [])
{
$container = $this->createContainer();
Expand Down