Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[HttpKernel][HttpFoundation] Prototype: Service reset as middleware #24697

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

Closed
wants to merge 1 commit into from
Closed
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: 7 additions & 7 deletions src/Symfony/Bundle/FrameworkBundle/Resources/config/services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,18 @@
</service>
<service id="Symfony\Component\EventDispatcher\EventDispatcherInterface" alias="event_dispatcher" />

<service id="http_kernel" class="Symfony\Component\HttpKernel\HttpKernel" public="true">
<service id="Symfony\Component\HttpKernel\HttpKernel">
<argument type="service" id="event_dispatcher" />
<argument type="service" id="controller_resolver" />
<argument type="service" id="request_stack" />
<argument type="service" id="argument_resolver" />
</service>
<service id="Symfony\Component\HttpKernel\Middleware\ServiceResetMiddleware">
<argument key="$httpKernel" type="service" id="Symfony\Component\HttpKernel\HttpKernel" />
<argument key="$services" /> <!-- ResettableServicePass will inject an iterator of initialized services here ($serviceId => $serviceInstance) -->
<argument key="$resetMethods" type="collection" /> <!-- ResettableServicePass will inject an array of reset methods here ($serviceId => $method) -->
</service>
<service id="http_kernel" alias="Symfony\Component\HttpKernel\Middleware\ServiceResetMiddleware" public="true" />
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rather than doing this (which makes the HttpKernel typehint autowirable btw), I would rather keep the http_kernel as it is currently, and then use service decoration when the reset middleware is needed

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since we do not implement a switch for turning off that feature (which was the whole point of moving the reset to the beginning), the middleware would technically be always present.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But I agree that enabling HttpKernel for autowiring could cause accidents. ;-)

<service id="Symfony\Component\HttpKernel\HttpKernelInterface" alias="http_kernel" />

<service id="request_stack" class="Symfony\Component\HttpFoundation\RequestStack" public="true" />
Expand Down Expand Up @@ -74,11 +80,5 @@
<service id="Symfony\Component\Config\Resource\SelfCheckingResourceChecker">
<tag name="config_cache.resource_checker" priority="-990" />
</service>

<service id="Symfony\Component\HttpKernel\EventListener\ServiceResetListener">
<argument /> <!-- ResettableServicePass will inject an iterator of initialized services here ($serviceId => $serviceInstance) -->
<argument type="collection" /> <!-- ResettableServicePass will inject an array of reset methods here ($serviceId => $method) -->
<tag name="kernel.event_subscriber" />
</service>
</services>
</container>
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ public function testBasicFunctionality()
$tester = new ApplicationTester($application);
$tester->run(array('command' => 'debug:autowiring'));

$this->assertContains('Symfony\Component\HttpKernel\HttpKernelInterface', $tester->getDisplay());
$this->assertContains('alias to http_kernel', $tester->getDisplay());
$this->assertContains('Symfony\Component\DependencyInjection\ContainerInterface', $tester->getDisplay());
$this->assertContains('alias to service_container', $tester->getDisplay());
}

public function testSearchArgument()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\DependencyInjection\Exception\RuntimeException;
use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\HttpKernel\EventListener\ServiceResetListener;
use Symfony\Component\HttpKernel\Middleware\ServiceResetMiddleware;

/**
* @author Alexander M. Turek <[email protected]>
Expand All @@ -39,7 +39,7 @@ public function __construct($tagName = 'kernel.reset')
*/
public function process(ContainerBuilder $container)
{
if (!$container->has(ServiceResetListener::class)) {
if (!$container->has(ServiceResetMiddleware::class)) {
return;
}

Expand All @@ -56,14 +56,8 @@ public function process(ContainerBuilder $container)
$methods[$id] = $attributes['method'];
}

if (empty($services)) {
$container->removeDefinition(ServiceResetListener::class);

return;
}

$container->findDefinition(ServiceResetListener::class)
->replaceArgument(0, new IteratorArgument($services))
->replaceArgument(1, $methods);
$container->findDefinition(ServiceResetMiddleware::class)
->replaceArgument('$services', new IteratorArgument($services))
->replaceArgument('$resetMethods', $methods);
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\HttpKernel\Middleware;

use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\HttpKernelInterface;
use Symfony\Component\HttpKernel\TerminableInterface;

/**
* Clean up services between requests.
*
* @author Alexander M. Turek <[email protected]>
*/
class ServiceResetMiddleware implements HttpKernelInterface, TerminableInterface
{
private $httpKernel;
private $services;
private $resetMethods;

public function __construct(HttpKernelInterface $httpKernel, \Traversable $services, array $resetMethods)
{
$this->services = $services;
$this->resetMethods = $resetMethods;
$this->httpKernel = $httpKernel;
}

/**
* {@inheritdoc}
*/
public function handle(Request $request, $type = self::MASTER_REQUEST, $catch = true)
{
if (self::MASTER_REQUEST === $type) {
$this->resetServices();
}

return $this->httpKernel->handle($request);
}

/**
* {@inheritdoc}
*/
public function terminate(Request $request, Response $response)
{
if ($this->httpKernel instanceof TerminableInterface) {
$this->httpKernel->terminate($request, $response);
}
}

private function resetServices()
{
foreach ($this->services as $id => $service) {
$method = $this->resetMethods[$id];
$service->$method();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\HttpKernel\DependencyInjection\ResettableServicePass;
use Symfony\Component\HttpKernel\EventListener\ServiceResetListener;
use Symfony\Component\HttpKernel\HttpKernel;
use Symfony\Component\HttpKernel\Middleware\ServiceResetMiddleware;
use Symfony\Component\HttpKernel\Tests\Fixtures\ClearableService;
use Symfony\Component\HttpKernel\Tests\Fixtures\ResettableService;

Expand All @@ -17,24 +18,30 @@ class ResettableServicePassTest extends TestCase
public function testCompilerPass()
{
$container = new ContainerBuilder();
$container->register(HttpKernel::class)->setSynthetic(true);
$container->register('one', ResettableService::class)
->setPublic(true)
->addTag('kernel.reset', array('method' => 'reset'));
$container->register('two', ClearableService::class)
->setPublic(true)
->addTag('kernel.reset', array('method' => 'clear'));

$container->register(ServiceResetListener::class)
$container->register(ServiceResetMiddleware::class)
->setPublic(true)
->setArguments(array(null, array()));
->setArguments(array(
'$httpKernel' => new Reference(HttpKernel::class),
'$services' => null,
'$resetMethods' => array()
));
$container->addCompilerPass(new ResettableServicePass('kernel.reset'));

$container->compile();

$definition = $container->getDefinition(ServiceResetListener::class);
$definition = $container->getDefinition(ServiceResetMiddleware::class);

$this->assertEquals(
array(
new Reference(HttpKernel::class),
new IteratorArgument(array(
'one' => new Reference('one', ContainerInterface::IGNORE_ON_UNINITIALIZED_REFERENCE),
'two' => new Reference('two', ContainerInterface::IGNORE_ON_UNINITIALIZED_REFERENCE),
Expand All @@ -55,34 +62,27 @@ public function testCompilerPass()
public function testMissingMethod()
{
$container = new ContainerBuilder();
$container->register(HttpKernel::class)->setSynthetic(true);
$container->register(ResettableService::class)
->addTag('kernel.reset');
$container->register(ServiceResetListener::class)
->setArguments(array(null, array()));
$container->register(ServiceResetMiddleware::class)
->setArguments(array(
'$httpKernel' => new Reference(HttpKernel::class),
'$services' => null,
'$resetMethods' => array()
));
$container->addCompilerPass(new ResettableServicePass('kernel.reset'));

$container->compile();
}

public function testCompilerPassWithoutResetters()
{
$container = new ContainerBuilder();
$container->register(ServiceResetListener::class)
->setArguments(array(null, array()));
$container->addCompilerPass(new ResettableServicePass());

$container->compile();

$this->assertFalse($container->has(ServiceResetListener::class));
}

public function testCompilerPassWithoutListener()
{
$container = new ContainerBuilder();
$container->addCompilerPass(new ResettableServicePass());

$container->compile();

$this->assertFalse($container->has(ServiceResetListener::class));
$this->assertFalse($container->has(ServiceResetMiddleware::class));
}
}
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
<?php

namespace Symfony\Component\HttpKernel\Tests\EventListener;
namespace Symfony\Component\HttpKernel\Tests\Middleware;

use PHPUnit\Framework\TestCase;
use Symfony\Component\DependencyInjection\Argument\IteratorArgument;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\HttpKernel\EventListener\ServiceResetListener;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\HttpKernel;
use Symfony\Component\HttpKernel\HttpKernelInterface;
use Symfony\Component\HttpKernel\Middleware\ServiceResetMiddleware;
use Symfony\Component\HttpKernel\Tests\Fixtures\ClearableService;
use Symfony\Component\HttpKernel\Tests\Fixtures\ResettableService;

class ServiceResetListenerTest extends TestCase
class ServiceResetMiddlewareTest extends TestCase
{
protected function setUp()
{
Expand All @@ -22,7 +26,17 @@ protected function setUp()
public function testResetServicesNoOp()
{
$container = $this->buildContainer();
$container->get('reset_subscriber')->onKernelTerminate();
$container->get('reset_middleware')->handle(new Request());

$this->assertEquals(0, ResettableService::$counter);
$this->assertEquals(0, ClearableService::$counter);
}

public function testNoResetOnSubRequests()
{
$container = $this->buildContainer();
$container->get('one');
$container->get('reset_middleware')->handle(new Request(), HttpKernelInterface::SUB_REQUEST);

$this->assertEquals(0, ResettableService::$counter);
$this->assertEquals(0, ClearableService::$counter);
Expand All @@ -32,7 +46,7 @@ public function testResetServicesPartially()
{
$container = $this->buildContainer();
$container->get('one');
$container->get('reset_subscriber')->onKernelTerminate();
$container->get('reset_middleware')->handle(new Request());

$this->assertEquals(1, ResettableService::$counter);
$this->assertEquals(0, ClearableService::$counter);
Expand All @@ -42,9 +56,9 @@ public function testResetServicesTwice()
{
$container = $this->buildContainer();
$container->get('one');
$container->get('reset_subscriber')->onKernelTerminate();
$container->get('reset_middleware')->handle(new Request());
$container->get('two');
$container->get('reset_subscriber')->onKernelTerminate();
$container->get('reset_middleware')->handle(new Request());

$this->assertEquals(2, ResettableService::$counter);
$this->assertEquals(1, ClearableService::$counter);
Expand All @@ -58,9 +72,12 @@ private function buildContainer()
$container = new ContainerBuilder();
$container->register('one', ResettableService::class)->setPublic(true);
$container->register('two', ClearableService::class)->setPublic(true);
$container->register(HttpKernel::class, HttpKernelInterface::class)
->setSynthetic(true);

$container->register('reset_subscriber', ServiceResetListener::class)
$container->register('reset_middleware', ServiceResetMiddleware::class)
->setPublic(true)
->addArgument(new Reference(HttpKernel::class))
->addArgument(new IteratorArgument(array(
'one' => new Reference('one', ContainerInterface::IGNORE_ON_UNINITIALIZED_REFERENCE),
'two' => new Reference('two', ContainerInterface::IGNORE_ON_UNINITIALIZED_REFERENCE),
Expand All @@ -72,6 +89,13 @@ private function buildContainer()

$container->compile();

$kernelMock = $this->createMock(HttpKernelInterface::class);
$kernelMock->expects($this->any())
->method('handle')
->willReturn(new Response());

$container->set(HttpKernel::class, $kernelMock);

return $container;
}
}