Skip to content

Symfony bootstrap cleanup, the new 'kernel.reset' tag should do the job #144

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 1 commit into from
Jan 7, 2020
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
117 changes: 8 additions & 109 deletions Bootstraps/Symfony.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@

namespace PHPPM\Bootstraps;

use PHPPM\Symfony\StrongerNativeSessionStorage;
use PHPPM\Utils;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Config\Resource\FileResource;
use Symfony\Contracts\Service\ResetInterface;
use function PHPPM\register_file;
use Symfony\Component\HttpKernel\KernelInterface;

/**
* A default bootstrap for the Symfony framework
Expand Down Expand Up @@ -39,7 +39,7 @@ public function initialize($appenv, $debug)
/**
* Create a Symfony application
*
* @return \AppKernel
* @return KernelInterface
* @throws \Exception
*/
public function getApplication()
Expand Down Expand Up @@ -68,27 +68,6 @@ public function getApplication()
//since we need to change some services, we need to manually change some services
$app = new $class($this->appenv, $this->debug);

// We need to change some services, before the boot, because they would
// otherwise be instantiated and passed to other classes which makes it
// impossible to replace them.

Utils::bindAndCall(function () use ($app) {
// init bundles
$app->initializeBundles();

// init container
$app->initializeContainer();
}, $app);

Utils::bindAndCall(function () use ($app) {
foreach ($app->getBundles() as $bundle) {
$bundle->setContainer($app->container);
$bundle->boot();
}

$app->booted = true;
}, $app);

if ($this->debug) {
Utils::bindAndCall(function () use ($app) {
$container = $app->container;
Expand Down Expand Up @@ -149,25 +128,23 @@ protected function getVendorDir()
/**
* Does some necessary preparation before each request.
*
* @param \AppKernel $app
* @param KernelInterface $app
*/
public function preHandle($app)
{
//resets Kernels startTime, so Symfony can correctly calculate the execution time
Utils::hijackProperty($app, 'startTime', microtime(true));
}

/**
* Does some necessary clean up after each request.
*
* @param \AppKernel $app
* @param KernelInterface $app
*/
public function postHandle($app)
{
$container = $app->getContainer();

if ($container->has('doctrine')) {
$doctrineRegistry = $container->get("doctrine");
$doctrineRegistry = $container->get('doctrine');
if (!$doctrineRegistry instanceof ResetInterface) {
foreach ($doctrineRegistry->getManagers() as $curManagerName => $curManager) {
if (!$curManager->isOpen()) {
Expand All @@ -179,11 +156,6 @@ public function postHandle($app)
}
}

//resets stopwatch, so it can correctly calculate the execution time
if ($container->has('debug.stopwatch')) {
$container->get('debug.stopwatch')->__construct();
}

//Symfony\Bundle\TwigBundle\Loader\FilesystemLoader
//->Twig_Loader_Filesystem
if ($this->debug && $container->has('twig.loader')) {
Expand All @@ -195,83 +167,10 @@ public function postHandle($app)
}, $twigLoader);
}

//reset Webpack Encore file list
Utils::bindAndCall(function () use ($container) {
if (isset($container->privates['webpack_encore.entrypoint_lookup'])) {
$container->privates['webpack_encore.entrypoint_lookup']->reset();
}
}, $container);

//reset all profiler stuff currently supported
if ($container->has('profiler')) {
$profiler = $container->get('profiler');

// since Symfony does not reset Profiler::disable() calls after each request, we need to do it,
// so the profiler bar is visible after the second request as well.
$profiler->enable();

//PropelLogger
if ($container->has('propel.logger')) {
$propelLogger = $container->get('propel.logger');
Utils::hijackProperty($propelLogger, 'queries', []);
}

//Doctrine
//Doctrine\Bundle\DoctrineBundle\DataCollector\DoctrineDataCollector
if ($profiler->has('db')) {
Utils::bindAndCall(function () {
//$logger: \Doctrine\DBAL\Logging\DebugStack
foreach ($this->loggers as $logger) {
Utils::hijackProperty($logger, 'queries', []);
}
}, $profiler->get('db'), null, 'Symfony\Bridge\Doctrine\DataCollector\DoctrineDataCollector');
}

//EventDataCollector
if ($profiler->has('events')) {
Utils::hijackProperty($profiler->get('events'), 'data', [
'called_listeners' => [],
'not_called_listeners' => [],
]);
}

//TwigDataCollector
if ($profiler->has('twig')) {
Utils::bindAndCall(function () {
Utils::hijackProperty($this->profile, 'profiles', []);
}, $profiler->get('twig'));
}

//Logger
if ($container->has('logger')) {
$logger = $container->get('logger');
Utils::bindAndCall(function () {
if (\method_exists($this, 'getDebugLogger') && $debugLogger = $this->getDebugLogger()) {
//DebugLogger
Utils::hijackProperty($debugLogger, 'records', []);
}
}, $logger);
}

//SwiftMailer logger
//Symfony\Bundle\SwiftmailerBundle\DataCollector\MessageDataCollector
if ($container->hasParameter('swiftmailer.mailers')) {
$mailers = $container->getParameter('swiftmailer.mailers');
foreach ($mailers as $name => $mailer) {
$loggerName = sprintf('swiftmailer.mailer.%s.plugin.messagelogger', $name);
if ($container->has($loggerName)) {
/** @var \Swift_Plugins_MessageLogger $logger */
$logger = $container->get($loggerName);
$logger->clear();
}
}
}

//Symfony\Bridge\Swiftmailer\DataCollector\MessageDataCollector
if ($container->has('swiftmailer.plugin.messagelogger')) {
$logger = $container->get('swiftmailer.plugin.messagelogger');
$logger->clear();
}
if ($container->has('propel.logger')) {
$propelLogger = $container->get('propel.logger');
Utils::hijackProperty($propelLogger, 'queries', []);
}
}
}
3 changes: 1 addition & 2 deletions tests/Fixtures/Symfony/Controller/GetController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@

namespace PHPPM\Tests\Fixtures\Symfony\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;

class GetController extends Controller
class GetController
{
/**
* @Route("/get")
Expand Down
3 changes: 1 addition & 2 deletions tests/Fixtures/Symfony/Controller/PostJsonController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,11 @@

namespace PHPPM\Tests\Fixtures\Symfony\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;

class PostJsonController extends Controller
class PostJsonController
{
/**
* @Route("/json", methods={"POST"})
Expand Down
3 changes: 1 addition & 2 deletions tests/Fixtures/Symfony/Controller/StreamedController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@

namespace PHPPM\Tests\Fixtures\Symfony\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\StreamedResponse;
use Symfony\Component\Routing\Annotation\Route;

class StreamedController extends Controller
class StreamedController
{
/**
* @Route("/streamed")
Expand Down
3 changes: 1 addition & 2 deletions tests/Fixtures/Symfony/Controller/UploadController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,11 @@

namespace PHPPM\Tests\Fixtures\Symfony\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;

class UploadController extends Controller
class UploadController
{
/**
* @Route("/upload", methods={"POST"})
Expand Down
2 changes: 1 addition & 1 deletion tests/Fixtures/Symfony/config/bundles.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?php

return [
Symfony\Bundle\FrameworkBundle\FrameworkBundle::class => ['all' => true],
\Symfony\Bundle\FrameworkBundle\FrameworkBundle::class => ['all' => true],
];
14 changes: 14 additions & 0 deletions tests/Fixtures/Symfony/config/services.yaml
Original file line number Diff line number Diff line change
@@ -1,3 +1,17 @@
services:
# default configuration for services in *this* file
_defaults:
autowire: true # Automatically injects dependencies in your services.
autoconfigure: true # Automatically registers your services as commands, event subscribers, etc.
public: false # Allows optimizing the container by removing unused services; this also means
# fetching services directly from the container via $container->get() won't work.
# The best practice is to be explicit about your dependencies anyway.

# makes classes in src/ available to be used as services
# this creates a service per class whose id is the fully-qualified class name
PHPPM\Tests\Fixtures\Symfony\Controller\:
resource: '../Controller/*'
tags: ['controller.service_arguments']

framework:
secret: foobar