Skip to content

Commit

Permalink
Simplify response conversion configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
jsor committed Oct 11, 2014
1 parent f8cffc8 commit bc8b99c
Show file tree
Hide file tree
Showing 9 changed files with 25 additions and 81 deletions.
4 changes: 2 additions & 2 deletions src/Configurator/HttpKernelConfigurator.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ class HttpKernelConfigurator implements ConfiguratorInterface
{
public function configureResponseConversion(HttpKernelInterface $app, $prettyPrint = false)
{
return new HttpKernelDecorator($app, function () use ($prettyPrint) {
call_user_func(\Closure::bind(function () use ($prettyPrint) {
$this->dispatcher->addSubscriber(new ResponseConversionListener($prettyPrint));
});
}, $app, 'Symfony\Component\HttpKernel\HttpKernel'));
}
}
30 changes: 0 additions & 30 deletions src/Configurator/HttpKernelDecorator.php

This file was deleted.

4 changes: 2 additions & 2 deletions src/Configurator/KernelConfigurator.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ class KernelConfigurator implements ConfiguratorInterface
{
public function configureResponseConversion(HttpKernelInterface $app, $prettyPrint = false)
{
return new KernelDecorator($app, function () use ($prettyPrint) {
call_user_func(\Closure::bind(function () use ($prettyPrint) {
$this->dispatcher->addSubscriber(new ResponseConversionListener($prettyPrint));
});
}, $app->getContainer()->get('http_kernel'), 'Symfony\Component\HttpKernel\HttpKernel'));
}
}
33 changes: 0 additions & 33 deletions src/Configurator/KernelDecorator.php

This file was deleted.

2 changes: 0 additions & 2 deletions src/Configurator/LaravelConfigurator.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,5 @@ public function configureResponseConversion(HttpKernelInterface $app, $prettyPri

return $router;
});

return $app;
}
}
1 change: 0 additions & 1 deletion src/Configurator/NoopConfigurator.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,5 @@ class NoopConfigurator implements ConfiguratorInterface
{
public function configureResponseConversion(HttpKernelInterface $app, $prettyPrint = false)
{
return $app;
}
}
2 changes: 0 additions & 2 deletions src/Configurator/Silex1Configurator.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,5 @@ public function configureResponseConversion(HttpKernelInterface $app, $prettyPri

return $dispatcher;
}));

return $app;
}
}
2 changes: 0 additions & 2 deletions src/Configurator/Silex2Configurator.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,5 @@ public function configureResponseConversion(HttpKernelInterface $app, $prettyPri

return $dispatcher;
});

return $app;
}
}
28 changes: 21 additions & 7 deletions src/ResponseConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,23 +13,22 @@ class ResponseConverter implements HttpKernelInterface
{
private $app;
private $prettyPrint;
private $configurator;
private $isConfigured = false;

public function __construct(HttpKernelInterface $app,
$prettyPrint = false,
ConfiguratorInterface $configurator = null)
{
$this->app = $app;
$this->prettyPrint = (bool) $prettyPrint;

if (!$configurator) {
$guesser = new ConfiguratorGuesser();
$configurator = $guesser->guess($app);
}

$this->app = $configurator->configureResponseConversion($app, $this->prettyPrint);
$this->configurator = $configurator;
}

public function handle(Request $request, $type = HttpKernelInterface::MASTER_REQUEST, $catch = true)
{
$this->configure();

$hal = $this->app->handle($request, $type, $catch);

if (!$hal instanceof Hal) {
Expand All @@ -38,4 +37,19 @@ public function handle(Request $request, $type = HttpKernelInterface::MASTER_REQ

return new HalResponse($hal, 200, [], $this->prettyPrint);
}

protected function configure()
{
if ($this->isConfigured) {
return;
}

if (!$this->configurator) {
$guesser = new ConfiguratorGuesser();
$this->configurator = $guesser->guess($this->app);
}

$this->configurator->configureResponseConversion($this->app, $this->prettyPrint);
$this->isConfigured = true;
}
}

0 comments on commit bc8b99c

Please sign in to comment.