8000 [FrameworkBundle] Allow to leverage autoconfiguration for DataCollectors with template by l-vo · Pull Request #37332 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[FrameworkBundle] Allow to leverage autoconfiguration for DataCollectors with template #37332

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
Aug 13, 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
[FrameworkBundle] Allow to leverage autoconfiguration for DataCollect…
…ors with template
  • Loading branch information
l-vo committed Aug 12, 2020
commit 986a0a221e21401c86f8d785d386ce79585e92d5
1 change: 1 addition & 0 deletions src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ CHANGELOG
* Added `framework.trusted_proxies` and `framework.trusted_headers` configuration options
* Deprecated the public `form.factory`, `form.type.file`, `translator`, `security.csrf.token_manager`, `serializer`,
`cache_clearer`, `filesystem` and `validator` services to private.
* Added `TemplateAwareDataCollectorInterface` and `AbstractDataCollector` to simplify custom data collector creation and leverage autoconfiguration

5.1.0
-----
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

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

namespace Symfony\Bundle\FrameworkBundle\DataCollector;

/**
* @author Laurent VOULLEMIER <laurent.voullemier@gmail.com>
*/
abstract class AbstractDataCollector implements TemplateAwareDataCollectorInterface
{
/**
* @var array
*/
protected $data = [];

public function getName(): string
{
return static::class;
}

public function reset(): void
{
$this->data = [];
}

public static function getTemplate(): ?string
{
return null;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

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

namespace Symfony\Bundle\FrameworkBundle\DataCollector;

use Symfony\Component\HttpKernel\DataCollector\DataCollectorInterface;

/**
* @author Laurent VOULLEMIER <laurent.voullemier@gmail.com>
*/
interface TemplateAwareDataCollectorInterface extends DataCollectorInterface
{
public static function getTemplate(): ?string;
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

namespace Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler;

use Symfony\Bundle\FrameworkBundle\DataCollector\TemplateAwareDataCollectorInterface;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
Expand All @@ -37,11 +38,14 @@ public function process(ContainerBuilder $container)
$priority = isset($attributes[0]['priority']) ? $attributes[0]['priority'] : 0;
$template = null;

if (isset($attributes[0]['template'])) {
if (!isset($attributes[0]['id'])) {
$collectorClass = $container->findDefinition($id)->getClass();
$isTemplateAware = is_subclass_of($collectorClass, TemplateAwareDataCollectorInterface::class);
if (isset($attributes[0]['template']) || $isTemplateAware) {
$idForTemplate = $attributes[0]['id'] ?? $collectorClass;
if (!$idForTemplate) {
throw new InvalidArgumentException(sprintf('Data collector service "%s" must have an id attribute in order to specify a template.', $id));
}
$template = [$attributes[0]['id'], $attributes[0]['template']];
$template = [$idForTemplate, $attributes[0]['template'] ?? $collectorClass::getTemplate()];
}

$collectors->insert([$id, $template], [$priority, --$order]);
Expand Down
50AA
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,15 @@
namespace Symfony\Bundle\FrameworkBundle\Tests\DependencyInjection\Compiler;

use PHPUnit\Framework\TestCase;
use Symfony\Bundle\FrameworkBundle\DataCollector\AbstractDataCollector;
use Symfony\Bundle\FrameworkBundle\DataCollector\TemplateAwareDataCollectorInterface;
use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\ProfilerPass;
use Symfony\Component\DependencyInjection\Compiler\ResolveChildDefinitionsPass;
use Symfony\Component\DependencyInjection\Compiler\ResolveInstanceofConditionalsPass;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\DataCollector\DataCollectorInterface;

class ProfilerPassTest extends TestCase
{
Expand Down Expand Up @@ -54,4 +61,64 @@ public function testValidCollector()
$this->assertCount(1, $methodCalls);
$this->assertEquals('add', $methodCalls[0][0]); // grab the method part of the first call
}

public function provideValidCollectorWithTemplateUsingAutoconfigure(): \Generator
{
yield [new class() implements TemplateAwareDataCollectorInterface {
public function collect(Request $request, Response $response, \Throwable $exception = null)
{
}

public function getName(): string
{
return static::class;
}

public function reset()
{
}

public static function getTemplate(): string
{
return 'foo';
}
}];

yield [new class() extends AbstractDataCollector {
public function collect(Request $request, Response $response, \Throwable $exception = null)
{
}

public static function getTemplate(): string
{
return 'foo';
}
}];
}

/**
* @dataProvider provideValidCollectorWithTemplateUsingAutoconfigure
*/
public function testValidCollectorWithTemplateUsingAutoconfigure(TemplateAwareDataCollectorInterface $dataCollector)
{
$container = new ContainerBuilder();
$profilerDefinition = $container->register('profiler', 'ProfilerClass');

$container->registerForAutoconfiguration(DataCollectorInterface::class)->addTag('data_collector');
$container->register('mydatacollector', \get_class($dataCollector))->setAutoconfigured(true);

(new ResolveInstanceofConditionalsPass())->process($container);
(new ProfilerPass())->process($container);

$idForTemplate = \get_class($dataCollector);
$this->assertSame(['mydatacollector' => [$idForTemplate, 'foo']], $container->getParameter('data_collector.templates'));

// grab the method calls off of the "profiler" definition
$methodCalls = $profilerDefinition->getMethodCalls();
$this->assertCount(1, $methodCalls);
$this->assertEquals('add', $methodCalls[0][0]); // grab the method part of the first call

(new ResolveChildDefinitionsPass())->process($container);
$this->assertSame($idForTemplate, $container->get('mydatacollector')->getName());
}
}
0