Skip to content

[Twig][Live] Require Symfony 5.4+ #252

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 28, 2022
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
24 changes: 15 additions & 9 deletions src/LiveComponent/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,18 +30,24 @@
"symfony/ux-twig-component": "^2.0"
},
"require-dev": {
"symfony/framework-bundle": "^4.4|^5.0|^6.0",
"symfony/dependency-injection": "^4.4.2|^5.0|^6.0",
"symfony/twig-bundle": "^4.4|^5.0|^6.0",
"symfony/security-csrf": "^4.4|^5.0|^6.0",
"symfony/serializer": "^4.4|^5.0|^6.0",
"symfony/validator": "^4.4|^5.0|^6.0",
"symfony/phpunit-bridge": "^5.2|^6.0",
"doctrine/annotations": "^1.0",
"doctrine/doctrine-bundle": "^2.0",
"doctrine/orm": "^2.7",
"zenstruck/foundry": "^1.10",
"zenstruck/browser": "^0.9.1"
"symfony/dependency-injection": "^5.4|^6.0",
"symfony/framework-bundle": "^5.4|^6.0",
"symfony/phpunit-bridge": "^6.0",
"symfony/security-csrf": "^5.4|^6.0",
"symfony/serializer": "^5.4|^6.0",
"symfony/twig-bundle": "^5.4|^6.0",
"symfony/validator": "^5.4|^6.0",
"zenstruck/browser": "^0.9.1",
"zenstruck/foundry": "^1.10"
},
"conflict": {
"symfony/config": "<5.4.0"
},
"config": {
"sort-packages": true
},
"extra": {
"thanks": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,21 +38,19 @@ final class LiveComponentExtension extends Extension
{
public function load(array $configs, ContainerBuilder $container): void
{
if (method_exists($container, 'registerAttributeForAutoconfiguration')) {
$container->registerAttributeForAutoconfiguration(
AsLiveComponent::class,
function (ChildDefinition $definition, AsLiveComponent $attribute) {
$definition
->addTag('twig.component', array_filter([
'key' => $attribute->name,
'template' => $attribute->template,
'default_action' => $attribute->defaultAction,
]))
->addTag('controller.service_arguments')
;
}
);
}
$container->registerAttributeForAutoconfiguration(
AsLiveComponent::class,
function (ChildDefinition $definition, AsLiveComponent $attribute) {
$definition
->addTag('twig.component', array_filter([
'key' => $attribute->name,
'template' => $attribute->template,
'default_action' => $attribute->defaultAction,
]))
->addTag('controller.service_arguments')
;
}
);

$container->registerForAutoconfiguration(PropertyHydratorInterface::class)
->addTag('twig.component.property_hydrator')
Expand Down
26 changes: 0 additions & 26 deletions src/LiveComponent/tests/ContainerBC.php

This file was deleted.

37 changes: 8 additions & 29 deletions src/LiveComponent/tests/Fixture/Kernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Kernel as BaseKernel;
use Symfony\Component\Routing\Loader\Configurator\RoutingConfigurator;
use Symfony\Component\Routing\RouteCollectionBuilder;
use Symfony\UX\LiveComponent\LiveComponentBundle;
use Symfony\UX\LiveComponent\Tests\Fixture\Component\Component1;
use Symfony\UX\LiveComponent\Tests\Fixture\Component\Component2;
Expand Down Expand Up @@ -63,27 +62,17 @@ protected function configureContainer(ContainerBuilder $c, LoaderInterface $load
// disable logging errors to the console
$c->register('logger', NullLogger::class);

$componentA = $c->register(Component1::class)->setAutoconfigured(true)->setAutowired(true);
$componentB = $c->register(Component2::class)->setAutoconfigured(true)->setAutowired(true);
$componentC = $c->register(Component3::class)->setAutoconfigured(true)->setAutowired(true);
$componentF = $c->register(Component6::class)->setAutoconfigured(true)->setAutowired(true);

if (self::VERSION_ID < 50300) {
// add tag manually
$componentA->addTag('twig.component', ['key' => 'component1'])->addTag('controller.service_arguments');
$componentB->addTag('twig.component', ['key' => 'component2', 'default_action' => 'defaultAction'])->addTag('controller.service_arguments');
$componentC->addTag('twig.component', ['key' => 'component3'])->addTag('controller.service_arguments');
$componentF->addTag('twig.component', ['key' => 'component6'])->addTag('controller.service_arguments');
}

$sessionConfig = self::VERSION_ID < 50300 ? ['storage_id' => 'session.storage.mock_file'] : ['storage_factory_id' => 'session.storage.factory.mock_file'];
$c->register(Component1::class)->setAutoconfigured(true)->setAutowired(true);
$c->register(Component2::class)->setAutoconfigured(true)->setAutowired(true);
$c->register(Component3::class)->setAutoconfigured(true)->setAutowired(true);
$c->register(Component6::class)->setAutoconfigured(true)->setAutowired(true);

$c->loadFromExtension('framework', [
'secret' => 'S3CRET',
'test' => true,
'router' => ['utf8' => true],
'secrets' => false,
'session' => $sessionConfig,
'session' => ['storage_factory_id' => 'session.storage.factory.mock_file'],
]);

$c->loadFromExtension('twig', [
Expand All @@ -108,21 +97,11 @@ protected function configureContainer(ContainerBuilder $c, LoaderInterface $load
]);
}

/**
* @param RoutingConfigurator|RouteCollectionBuilder $routes
*/
protected function configureRoutes($routes): void
protected function configureRoutes(RoutingConfigurator $routes): void
{
$routes->import('@LiveComponentBundle/Resources/config/routing/live_component.xml');

if ($routes instanceof RoutingConfigurator) {
$routes->add('template', '/render-template/{template}')->controller('kernel::renderTemplate');
$routes->add('homepage', '/')->controller('kernel::index');

return;
}

$routes->add('/render-template/{template}', 'kernel::renderTemplate', 'template');
$routes->add('/', 'kernel::index', 'homepage');
$routes->add('template', '/render-template/{template}')->controller('kernel::renderTemplate');
$routes->add('homepage', '/')->controller('kernel::index');
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
use Symfony\UX\LiveComponent\LiveComponentHydrator;
use Symfony\UX\LiveComponent\Tests\ContainerBC;
use Symfony\UX\LiveComponent\Tests\Fixture\Component\Component1;
use Symfony\UX\LiveComponent\Tests\Fixture\Component\Component2;
use Symfony\UX\LiveComponent\Tests\Fixture\Component\Component6;
Expand All @@ -31,7 +30,6 @@
*/
final class LiveComponentSubscriberTest extends KernelTestCase
{
use ContainerBC;
use Factories;
use HasBrowser;
use ResetDatabase;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
use Symfony\UX\LiveComponent\Attribute\LiveProp;
use Symfony\UX\LiveComponent\LiveComponentHydrator;
use Symfony\UX\LiveComponent\Tests\ContainerBC;
use Symfony\UX\LiveComponent\Tests\Fixture\Component\Component1;
use Symfony\UX\LiveComponent\Tests\Fixture\Component\Component2;
use Symfony\UX\LiveComponent\Tests\Fixture\Component\Component3;
Expand All @@ -29,7 +28,6 @@
*/
final class LiveComponentHydratorTest extends KernelTestCase
{
use ContainerBC;
use Factories;
use ResetDatabase;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,12 @@
namespace Symfony\UX\LiveComponent\Tests\Integration;

use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
use Symfony\UX\LiveComponent\Tests\ContainerBC;

/**
* @author Kevin Bond <[email protected]>
*/
final class LiveComponentExtensionTest extends KernelTestCase
{
use ContainerBC;

public function testGetComponentUrl(): void
{
$rendered = self::getContainer()->get('twig')->render('component_url.html.twig', [
Expand Down
17 changes: 10 additions & 7 deletions src/TwigComponent/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,20 @@
},
"require": {
"php": ">=8.0",
"twig/twig": "^2.0|^3.0",
"symfony/property-access": "^4.4|^5.0|^6.0",
"symfony/dependency-injection": "^4.4|^5.0|^6.0"
"symfony/dependency-injection": "^5.4|^6.0",
"symfony/property-access": "^5.4|^6.0",
"twig/twig": "^2.0|^3.0"
},
"require-dev": {
"symfony/framework-bundle": "^4.4|^5.0|^6.0",
"symfony/twig-bundle": "^4.4|^5.0|^6.0",
"symfony/phpunit-bridge": "^5.2|^6.0"
"symfony/framework-bundle": "^5.4|^6.0",
"symfony/phpunit-bridge": "^6.0",
"symfony/twig-bundle": "^5.4|^6.0"
},
"conflict": {
"symfony/dependency-injection": "<4.4.18,<5.1.10,<5.2.1"
"symfony/config": "<5.4.0"
},
"config": {
"sort-packages": true
},
"extra": {
"thanks": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,17 +34,15 @@ final class TwigComponentExtension extends Extension
{
public function load(array $configs, ContainerBuilder $container): void
{
if (method_exists($container, 'registerAttributeForAutoconfiguration')) {
$container->registerAttributeForAutoconfiguration(
AsTwigComponent::class,
static function (ChildDefinition $definition, AsTwigComponent $attribute) {
$definition->addTag('twig.component', array_filter([
'key' => $attribute->name,
'template' => $attribute->template,
]));
}
);
}
$container->registerAttributeForAutoconfiguration(
AsTwigComponent::class,
static function (ChildDefinition $definition, AsTwigComponent $attribute) {
$definition->addTag('twig.component', array_filter([
'key' => $attribute->name,
'template' => $attribute->template,
]));
}
);

$container->register('ux.twig_component.component_factory', ComponentFactory::class)
->setArguments([
Expand Down
11 changes: 0 additions & 11 deletions src/TwigComponent/src/Resources/doc/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -79,11 +79,6 @@ as simple as possible::
{
}

**Note:** If this class is auto-configured, *and* you're using Symfony
5.3+, then you're all set. Otherwise, register the service and tag it
with ``twig.component`` and with a ``key`` tag attribute for the
component's name (``alert``).

Step 2 is to create a template for this component. By default, templates
live in ``templates/components/{Component Name}.html.twig``, where
``{Component Name}`` is whatever you passed as the first argument to the
Expand Down Expand Up @@ -184,12 +179,6 @@ as the second argument to the ``AsTwigComponent`` attribute:
// ...
}

**Note:** If this class is auto-configured, *and* you're using Symfony
5.3+, then you're all set. Otherwise, register the service and tag it
with ``twig.component`` and with a ``key`` tag attribute for the
component's name (``alert``) and a ``template`` tag attribute
(``my/custom/template.html.twig``).

The mount() Method
~~~~~~~~~~~~~~~~~~

Expand Down
20 changes: 3 additions & 17 deletions src/TwigComponent/tests/Fixture/Kernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
use Symfony\Component\Config\Loader\LoaderInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\HttpKernel\Kernel as BaseKernel;
use Symfony\Component\Routing\RouteCollectionBuilder;
use Symfony\UX\TwigComponent\Tests\Fixture\Component\ComponentA;
use Symfony\UX\TwigComponent\Tests\Fixture\Component\ComponentB;
use Symfony\UX\TwigComponent\Tests\Fixture\Component\ComponentC;
Expand Down Expand Up @@ -51,29 +50,16 @@ protected function configureContainer(ContainerBuilder $c, LoaderInterface $load
]);

$c->register(ServiceA::class)->setAutoconfigured(true)->setAutowired(true);

$componentA = $c->register(ComponentA::class)->setAutoconfigured(true)->setAutowired(true);
$componentB = $c->register('component_b', ComponentB::class)->setAutoconfigured(true)->setAutowired(true);
$componentC = $c->register(ComponentC::class)->setAutoconfigured(true)->setAutowired(true);

$c->register(ComponentA::class)->setAutoconfigured(true)->setAutowired(true);
$c->register('component_b', ComponentB::class)->setAutoconfigured(true)->setAutowired(true);
$c->register(ComponentC::class)->setAutoconfigured(true)->setAutowired(true);
$c->register('component_d', ComponentB::class)->addTag('twig.component', [
'key' => 'component_d',
'template' => 'components/custom2.html.twig',
]);

if (self::VERSION_ID < 50300) {
// add tag manually
$componentA->addTag('twig.component', ['key' => 'component_a']);
$componentB->addTag('twig.component', ['key' => 'component_b', 'template' => 'components/custom1.html.twig']);
$componentC->addTag('twig.component', ['key' => 'component_c']);
}

if ('missing_key' === $this->environment) {
$c->register('missing_key', ComponentB::class)->setAutowired(true)->addTag('twig.component');
}
}

protected function configureRoutes(RouteCollectionBuilder $routes): void
{
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@
*/
final class ComponentExtensionTest extends KernelTestCase
{
use ContainerBC;

public function testCanRenderComponent(): void
{
$output = self::getContainer()->get(Environment::class)->render('template_a.html.twig');
Expand Down
2 changes: 0 additions & 2 deletions src/TwigComponent/tests/Integration/ComponentFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@
*/
final class ComponentFactoryTest extends KernelTestCase
{
use ContainerBC;

public function testCreatedComponentsAreNotShared(): void
{
/** @var ComponentFactory $factory */
Expand Down
26 changes: 0 additions & 26 deletions src/TwigComponent/tests/Integration/ContainerBC.php

This file was deleted.