Skip to content

Commit

Permalink
[Live] override data collector & debug command
Browse files Browse the repository at this point in the history
  • Loading branch information
kbond committed Apr 11, 2024
1 parent a13900f commit e22474e
Show file tree
Hide file tree
Showing 10 changed files with 106 additions and 22 deletions.
12 changes: 12 additions & 0 deletions src/LiveComponent/src/Command/LiveComponentDebugCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

namespace Symfony\UX\LiveComponent\Command;

use Symfony\UX\TwigComponent\Command\TwigComponentDebugCommand;

/**
* @author Kevin Bond <kevinbond@gmail.com>
*/
final class LiveComponentDebugCommand extends TwigComponentDebugCommand
{
}
18 changes: 18 additions & 0 deletions src/LiveComponent/src/DataCollector/LiveComponentDataCollector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace Symfony\UX\LiveComponent\DataCollector;

use Symfony\UX\TwigComponent\DataCollector\TwigComponentDataCollector;

/**
* @author Kevin Bond <kevinbond@gmail.com>
*
* @internal
*/
final class LiveComponentDataCollector extends TwigComponentDataCollector
{
public function getName(): string
{
return 'live_component';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

namespace Symfony\UX\LiveComponent\DependencyInjection\Compiler;

use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\UX\LiveComponent\Command\LiveComponentDebugCommand;
use Symfony\UX\LiveComponent\DataCollector\LiveComponentDataCollector;

/**
* @author Kevin Bond <kevinbond@gmail.com>
*
* @internal
*/
final class DebugLiveComponentPass implements CompilerPassInterface
{
public function process(ContainerBuilder $container): void
{
if (!$container->hasDefinition('ux.twig_component.data_collector')) {
return;
}

$container->getDefinition('ux.twig_component.data_collector')
->setClass(LiveComponentDataCollector::class)
->clearTag('data_collector')
->addTag('data_collector', [
'template' => '@LiveComponent/Collector/live_component.html.twig',
'id' => 'live_component',
'priority' => 256,
]);

$container->getDefinition('ux.twig_component.command.debug')
->setClass(LiveComponentDebugCommand::class);
}
}
2 changes: 2 additions & 0 deletions src/LiveComponent/src/LiveComponentBundle.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\HttpKernel\Bundle\Bundle;
use Symfony\UX\LiveComponent\DependencyInjection\Compiler\ComponentDefaultActionPass;
use Symfony\UX\LiveComponent\DependencyInjection\Compiler\DebugLiveComponentPass;
use Symfony\UX\LiveComponent\DependencyInjection\Compiler\OptionalDependencyPass;

/**
Expand All @@ -29,6 +30,7 @@ public function build(ContainerBuilder $container): void
// must run before Symfony\Component\Serializer\DependencyInjection\SerializerPass
$container->addCompilerPass(new OptionalDependencyPass(), PassConfig::TYPE_BEFORE_OPTIMIZATION, 100);
$container->addCompilerPass(new ComponentDefaultActionPass());
$container->addCompilerPass(new DebugLiveComponentPass(), priority: 100);
}

public function getPath(): string
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{% extends '@TwigComponent/Collector/twig_component.html.twig' %}

{% block menu %}
<span class="label{{ collector.components is empty ? ' disabled' }}">
<span class="icon">{{ source('@TwigComponent/Collector/icon.svg') }}</span>
<strong>Twigsss Components</strong>
</span>
{% endblock %}
19 changes: 14 additions & 5 deletions src/TwigComponent/config/debug.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,12 @@
* file that was distributed with this source code.
*/

namespace Symfony\UX\TwigComponent\DependencyInjection\Loader\Configurator;
namespace Symfony\Component\DependencyInjection\Loader\Configurator;

use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
use Symfony\UX\TwigComponent\Command\TwigComponentDebugCommand;
use Symfony\UX\TwigComponent\DataCollector\TwigComponentDataCollector;
use Symfony\UX\TwigComponent\EventListener\TwigComponentLoggerListener;

use function Symfony\Component\DependencyInjection\Loader\Configurator\service;

return static function (ContainerConfigurator $container) {
$container->services()

Expand All @@ -35,5 +33,16 @@
'template' => '@TwigComponent/Collector/twig_component.html.twig',
'id' => 'twig_component',
'priority' => 256,
]);
])

->set('ux.twig_component.command.debug', TwigComponentDebugCommand::class)
->args([
param('twig.default_path'),
service('ux.twig_component.component_factory'),
service('twig'),
param('ux.twig_component.class_component_map'),
param('ux.twig_component.anonymous_template_directory'),
])
->tag('console.command')
;
};
12 changes: 10 additions & 2 deletions src/TwigComponent/src/Command/TwigComponentDebugCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@

namespace Symfony\UX\TwigComponent\Command;

use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Completion\CompletionInput;
use Symfony\Component\Console\Completion\CompletionSuggestions;
Expand All @@ -28,7 +27,6 @@
use Symfony\UX\TwigComponent\Twig\PropsNode;
use Twig\Environment;

#[AsCommand(name: 'debug:twig-component', description: 'Display components and them usages for an application')]
class TwigComponentDebugCommand extends Command
{
private readonly string $anonymousDirectory;
Expand All @@ -44,6 +42,16 @@ public function __construct(
$this->anonymousDirectory = $anonymousDirectory ?? 'components';
}

public static function getDefaultName(): ?string
{
return 'debug:twig-component';
}

public static function getDefaultDescription(): ?string
{
return 'Display components and their usages for an application';
}

protected function configure(): void
{
$this
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@

/**
* @author Simon André <smn.andre@gmail.com>
*
* @internal
*/
class TwigComponentDataCollector extends AbstractDataCollector implements LateDataCollectorInterface
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,9 @@ public function process(ContainerBuilder $container): void
$factoryDefinition->setArgument(4, $componentConfig);
$factoryDefinition->setArgument(5, $componentClassMap);

$debugCommandDefinition = $container->findDefinition('ux.twig_component.command.debug');
$debugCommandDefinition->setArgument(3, $componentClassMap);
if ($container->hasDefinition('ux.twig_component.command.debug')) {
$container->setParameter('ux.twig_component.class_component_map', $componentClassMap);
}
}

private function findMatchingDefaults(string $className, array $componentDefaults): ?array
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,8 @@
use Symfony\Component\DependencyInjection\Exception\LogicException;
use Symfony\Component\DependencyInjection\Extension\Extension;
use Symfony\Component\DependencyInjection\Loader\PhpFileLoader;
use Symfony\Component\DependencyInjection\Parameter;
use Symfony\Component\DependencyInjection\Reference;
use Symfony\UX\TwigComponent\Attribute\AsTwigComponent;
use Symfony\UX\TwigComponent\Command\TwigComponentDebugCommand;
use Symfony\UX\TwigComponent\ComponentFactory;
use Symfony\UX\TwigComponent\ComponentRenderer;
use Symfony\UX\TwigComponent\ComponentRendererInterface;
Expand Down Expand Up @@ -111,22 +109,13 @@ class_exists(AbstractArgument::class) ? new AbstractArgument(sprintf('Added in %
->setDecoratedService(new Reference('twig.configurator.environment'))
->setArguments([new Reference('ux.twig_component.twig.environment_configurator.inner')]);

$container->register('ux.twig_component.command.debug', TwigComponentDebugCommand::class)
->setArguments([
new Parameter('twig.default_path'),
new Reference('ux.twig_component.component_factory'),
new Reference('twig'),
class_exists(AbstractArgument::class) ? new AbstractArgument(sprintf('Added in %s.', TwigComponentPass::class)) : [],
$config['anonymous_template_directory'],
])
->addTag('console.command')
;

$container->setAlias('console.command.stimulus_component_debug', 'ux.twig_component.command.debug')
->setDeprecated('symfony/ux-twig-component', '2.13', '%alias_id%');

if ($container->getParameter('kernel.debug')) {
$loader->load('debug.php');

$container->setParameter('ux.twig_component.anonymous_template_directory', $config['anonymous_template_directory']);
}
}

Expand Down

0 comments on commit e22474e

Please sign in to comment.