Skip to content

Commit 42b8489

Browse files
committed
[UX3] Remove TwigComponent deprecations
1 parent 28aee91 commit 42b8489

15 files changed

+41
-1101
lines changed

src/TwigComponent/src/ComponentAttributes.php

Lines changed: 6 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@
1111

1212
namespace Symfony\UX\TwigComponent;
1313

14+
use BadMethodCallException;
1415
use Symfony\UX\StimulusBundle\Dto\StimulusAttributes;
16+
use Symfony\UX\TwigComponent\Exception\RuntimeException;
1517
use Symfony\WebpackEncoreBundle\Dto\AbstractStimulusDto;
1618
use Twig\Runtime\EscaperRuntime;
1719

@@ -47,6 +49,10 @@ public function __toString(): string
4749
continue;
4850
}
4951

52+
if (null === $value) {
53+
throw new RuntimeException('Attribute values cannot be null. If you want to remove an attribute, use the "remove()" method.');
54+
}
55+
5056
if (false === $value) {
5157
continue;
5258
}
@@ -60,11 +66,6 @@ public function __toString(): string
6066
continue;
6167
}
6268

63-
if (null === $value) {
64-
trigger_deprecation('symfony/ux-twig-component', '2.8.0', 'Passing "null" as an attribute value is deprecated and will throw an exception in 3.0.');
65-
$value = true;
66-
}
67-
6869
if (!\is_scalar($value) && !($value instanceof \Stringable)) {
6970
throw new \LogicException(\sprintf('A "%s" prop was passed when creating the component. No matching "%s" property or mount() argument was found, so we attempted to use this as an HTML attribute. But, the value is not a scalar (it\'s a "%s"). Did you mean to pass this to your component or is there a typo on its name?', $key, $key, get_debug_type($value)));
7071
}
@@ -198,33 +199,6 @@ public function without(string ...$keys): self
198199
return $clone;
199200
}
200201

201-
public function add($stimulusDto): self
202-
{
203-
if ($stimulusDto instanceof AbstractStimulusDto) {
204-
trigger_deprecation('symfony/ux-twig-component', '2.9.0', 'Passing a StimulusDto to ComponentAttributes::add() is deprecated. Run "composer require symfony/stimulus-bundle" then use "attributes.defaults(stimulus_controller(\'...\'))".');
205-
} elseif ($stimulusDto instanceof StimulusAttributes) {
206-
trigger_deprecation('symfony/ux-twig-component', '2.9.0', 'Calling ComponentAttributes::add() is deprecated. Instead use "attributes.defaults(stimulus_controller(\'...\'))".');
207-
208-
return $this->defaults($stimulusDto);
209-
} else {
210-
throw new \InvalidArgumentException(\sprintf('Argument 1 passed to "%s()" must be an instance of "%s" or "%s", "%s" given.', __METHOD__, AbstractStimulusDto::class, StimulusAttributes::class, get_debug_type($stimulusDto)));
211-
}
212-
213-
$controllersAttributes = $stimulusDto->toArray();
214-
$attributes = $this->attributes;
215-
216-
$attributes['data-controller'] = trim(implode(' ', array_merge(
217-
explode(' ', $attributes['data-controller'] ?? ''),
218-
explode(' ', $controllersAttributes['data-controller'] ?? [])
219-
)));
220-
unset($controllersAttributes['data-controller']);
221-
222-
$clone = new self($attributes, $this->escaper);
223-
224-
// add the remaining attributes for values/classes
225-
return $clone->defaults($controllersAttributes);
226-
}
227-
228202
public function remove($key): self
229203
{
230204
$attributes = $this->attributes;

src/TwigComponent/src/ComponentTemplateFinder.php

Lines changed: 4 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -11,58 +11,25 @@
1111

1212
namespace Symfony\UX\TwigComponent;
1313

14-
use Twig\Environment;
1514
use Twig\Loader\LoaderInterface;
1615

1716
/**
1817
* @author Matheo Daninos <[email protected]>
1918
*/
2019
final class ComponentTemplateFinder implements ComponentTemplateFinderInterface
2120
{
22-
private readonly LoaderInterface $loader;
23-
2421
public function __construct(
25-
Environment|LoaderInterface $loader,
26-
private readonly ?string $directory = null,
22+
private readonly LoaderInterface $loader,
23+
private readonly string $directory,
2724
) {
28-
if ($loader instanceof Environment) {
29-
trigger_deprecation('symfony/ux-twig-component', '2.13', 'The "%s()" method will require "%s $loader" as first argument in 3.0. Passing an "Environment" instance is deprecated.', __METHOD__, LoaderInterface::class);
30-
$loader = $loader->getLoader();
31-
}
32-
$this->loader = $loader;
33-
if (null === $this->directory) {
34-
trigger_deprecation('symfony/ux-twig-component', '2.13', 'The "%s()" method will require "string $directory" argument in 3.0. Not defining it or passing null is deprecated.', __METHOD__);
35-
}
3625
}
3726

3827
public function findAnonymousComponentTemplate(string $name): ?string
3928
{
40-
$loader = $this->loader;
4129
$componentPath = rtrim(str_replace(':', '/', $name));
4230

43-
// Legacy auto-naming rules < 2.13
44-
if (null === $this->directory) {
45-
if ($loader->exists('components/'.$componentPath.'.html.twig')) {
46-
return 'components/'.$componentPath.'.html.twig';
47-
}
48-
49-
if ($loader->exists($componentPath.'.html.twig')) {
50-
return $componentPath.'.html.twig';
51-
}
52-
53-
if ($loader->exists('components/'.$componentPath)) {
54-
return 'components/'.$componentPath;
55-
}
56-
57-
if ($loader->exists($componentPath)) {
58-
return $componentPath;
59-
}
60-
61-
return null;
62-
}
63-
6431
$template = rtrim($this->directory, '/').'/'.$componentPath.'.html.twig';
65-
if ($loader->exists($template)) {
32+
if ($this->loader->exists($template)) {
6633
return $template;
6734
}
6835

@@ -72,7 +39,7 @@ public function findAnonymousComponentTemplate(string $name): ?string
7239
}
7340

7441
$template = '@'.$parts[0].'/components/'.$parts[1].'.html.twig';
75-
if ($loader->exists($template)) {
42+
if ($this->loader->exists($template)) {
7643
return $template;
7744
}
7845

src/TwigComponent/src/DependencyInjection/Compiler/TwigComponentPass.php

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,6 @@ public function process(ContainerBuilder $container): void
3535
$componentNames = [];
3636
$componentDefaults = $container->getParameter('ux.twig_component.component_defaults');
3737
$container->getParameterBag()->remove('ux.twig_component.component_defaults');
38-
$legacyAutoNaming = $container->hasParameter('ux.twig_component.legacy_autonaming');
39-
$container->getParameterBag()->remove('ux.twig_component.legacy_autonaming');
4038

4139
foreach ($container->findTaggedServiceIds('twig.component') as $id => $tags) {
4240
$definition = $container->findDefinition($id);
@@ -48,17 +46,13 @@ public function process(ContainerBuilder $container): void
4846

4947
foreach ($tags as $tag) {
5048
if (!\array_key_exists('key', $tag)) {
51-
if ($legacyAutoNaming) {
52-
$name = substr($fqcn, strrpos($fqcn, '\\') + 1);
53-
} else {
54-
if (null === $defaults) {
55-
throw new LogicException(\sprintf('Could not generate a component name for class "%s": no matching namespace found under the "twig_component.defaults" to use as a root. Check the config or give your component an explicit name.', $fqcn));
56-
}
57-
58-
$name = str_replace('\\', ':', substr($fqcn, \strlen($defaults['namespace'])));
59-
if ($defaults['name_prefix']) {
60-
$name = \sprintf('%s:%s', $defaults['name_prefix'], $name);
61-
}
49+
if (null === $defaults) {
50+
throw new LogicException(\sprintf('Could not generate a component name for class "%s": no matching namespace found under the "twig_component.defaults" to use as a root. Check the config or give your component an explicit name.', $fqcn));
51+
}
52+
53+
$name = str_replace('\\', ':', substr($fqcn, \strlen($defaults['namespace'])));
54+
if ($defaults['name_prefix']) {
55+
$name = \sprintf('%s:%s', $defaults['name_prefix'], $name);
6256
}
6357
if (\in_array($name, $componentNames, true)) {
6458
throw new LogicException(\sprintf('Failed creating the "%s" component with the automatic name "%s": another component already has this name. To fix this, give the component an explicit name (hint: using "%s" will override the existing component).', $fqcn, $name, $name));

src/TwigComponent/src/DependencyInjection/TwigComponentExtension.php

Lines changed: 2 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,6 @@
4949
*/
5050
final class TwigComponentExtension extends Extension implements ConfigurationInterface
5151
{
52-
private const DEPRECATED_DEFAULT_KEY = '__deprecated__use_old_naming_behavior';
53-
5452
public function load(array $configs, ContainerBuilder $container): void
5553
{
5654
$loader = new PhpFileLoader($container, new FileLocator(__DIR__.'/../../config'));
@@ -62,12 +60,6 @@ public function load(array $configs, ContainerBuilder $container): void
6260
$configuration = $this->getConfiguration($configs, $container);
6361
$config = $this->processConfiguration($configuration, $configs);
6462
$defaults = $config['defaults'];
65-
if ($defaults === [self::DEPRECATED_DEFAULT_KEY]) {
66-
trigger_deprecation('symfony/ux-twig-component', '2.13', 'Not setting the "twig_component.defaults" config option is deprecated. Check the documentation for an example configuration.');
67-
$container->setParameter('ux.twig_component.legacy_autonaming', true);
68-
69-
$defaults = [];
70-
}
7163
$container->setParameter('ux.twig_component.component_defaults', $defaults);
7264

7365
$container->register('ux.twig_component.component_template_finder', ComponentTemplateFinder::class)
@@ -147,9 +139,6 @@ static function (ChildDefinition $definition, AsTwigComponent $attribute) {
147139
->addTag('console.command')
148140
;
149141

150-
$container->setAlias('console.command.stimulus_component_debug', 'ux.twig_component.command.debug')
151-
->setDeprecated('symfony/ux-twig-component', '2.13', '%alias_id%');
152-
153142
if ($container->getParameter('kernel.debug') && $config['profiler']) {
154143
$loader->load('debug.php');
155144
}
@@ -170,19 +159,9 @@ public function getConfigTreeBuilder(): TreeBuilder
170159
\assert($rootNode instanceof ArrayNodeDefinition);
171160

172161
$rootNode
173-
->validate()
174-
->always(function ($v) {
175-
if (!isset($v['anonymous_template_directory'])) {
176-
trigger_deprecation('symfony/twig-component-bundle', '2.13', 'Not setting the "twig_component.anonymous_template_directory" config option is deprecated. It will default to "components" in 3.0.');
177-
$v['anonymous_template_directory'] = null;
178-
}
179-
180-
return $v;
181-
})
182-
->end()
183162
->children()
184163
->arrayNode('defaults')
185-
->defaultValue([self::DEPRECATED_DEFAULT_KEY])
164+
->isRequired()
186165
->useAttributeAsKey('namespace')
187166
->validate()
188167
->always(function ($v) {
@@ -213,16 +192,13 @@ public function getConfigTreeBuilder(): TreeBuilder
213192
->end()
214193
->end()
215194
->scalarNode('anonymous_template_directory')
195+
->isRequired()
216196
->info('Defaults to `components`')
217197
->end()
218198
->booleanNode('profiler')
219199
->info('Enables the profiler for Twig Component (in debug mode)')
220200
->defaultValue('%kernel.debug%')
221201
->end()
222-
->scalarNode('controllers_json')
223-
->setDeprecated('symfony/ux-twig-component', '2.18', 'The "twig_component.controllers_json" config option is deprecated, and will be removed in 3.0.')
224-
->defaultNull()
225-
->end()
226202
->end();
227203

228204
return $treeBuilder;

src/TwigComponent/src/Event/PostMountEvent.php

Lines changed: 2 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -19,31 +19,12 @@
1919
*/
2020
final class PostMountEvent extends Event
2121
{
22-
private ?ComponentMetadata $metadata;
23-
private array $extraMetadata;
24-
2522
public function __construct(
2623
private object $component,
2724
private array $data,
28-
array|ComponentMetadata $metadata = [],
29-
$extraMetadata = [],
25+
private ?ComponentMetadata $metadata = null,
26+
private array $extraMetadata = [],
3027
) {
31-
if (\is_array($metadata)) {
32-
trigger_deprecation('symfony/ux-twig-component', '2.13', 'In TwigComponent 3.0, the third argument of "%s()" will be a "%s" object and the "$extraMetadata" array should be passed as the fourth argument.', __METHOD__, ComponentMetadata::class);
33-
34-
$this->metadata = null;
35-
$this->extraMetadata = $metadata;
36-
} else {
37-
if (null !== $metadata && !$metadata instanceof ComponentMetadata) {
38-
throw new \InvalidArgumentException(\sprintf('Expecting "$metadata" to be null or an instance of "%s", given: "%s."', ComponentMetadata::class, get_debug_type($metadata)));
39-
}
40-
if (!\is_array($extraMetadata)) {
41-
throw new \InvalidArgumentException(\sprintf('Expecting "$extraMetadata" to be array, given: "%s".', get_debug_type($extraMetadata)));
42-
}
43-
44-
$this->metadata = $metadata;
45-
$this->extraMetadata = $extraMetadata;
46-
}
4728
}
4829

4930
public function getComponent(): object

src/TwigComponent/src/Event/PreMountEvent.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,11 @@
1919
*/
2020
final class PreMountEvent extends Event
2121
{
22-
public function __construct(private object $component, private array $data, private readonly ?ComponentMetadata $metadata = null)
23-
{
24-
if (null === $this->metadata) {
25-
trigger_deprecation('symfony/ux-twig-component', '2.13', 'In TwigComponent 3.0, "%s()" method will require a "%s $metadata" argument. Not passing it is deprecated.', __METHOD__, ComponentMetadata::class);
26-
}
22+
public function __construct(
23+
private object $component,
24+
private array $data,
25+
private readonly ComponentMetadata $metadata,
26+
) {
2727
}
2828

2929
public function getComponent(): object

src/TwigComponent/src/Twig/ComponentNode.php

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -51,15 +51,7 @@ public function compile(Compiler $compiler): void
5151

5252
$useYield = method_exists(Environment::class, 'useYield') && $compiler->getEnvironment()->useYield();
5353

54-
// since twig/twig 3.9.0: Using the internal "twig_to_array" function is deprecated.
55-
if (method_exists(CoreExtension::class, 'toArray')) {
56-
$twig_to_array = 'Twig\Extension\CoreExtension::toArray';
57-
} else {
58-
$twig_to_array = 'twig_to_array';
59-
}
60-
6154
$componentRuntime = $compiler->getVarName();
62-
6355
$compiler
6456
->write(\sprintf('$%s = $this->env->getRuntime(', $componentRuntime))
6557
->string(ComponentRuntime::class)
@@ -75,7 +67,7 @@ public function compile(Compiler $compiler): void
7567
->write(\sprintf('$preRendered = $%s->preRender(', $componentRuntime))
7668
->string($this->getAttribute('component'))
7769
->raw(', ')
78-
->raw($twig_to_array)
70+
->raw('Twig\Extension\CoreExtension::toArray')
7971
->raw('(');
8072
$this->writeProps($compiler)
8173
->raw(')')
@@ -107,7 +99,7 @@ public function compile(Compiler $compiler): void
10799
->write(\sprintf('$preRenderEvent = $%s->startEmbedComponent(', $componentRuntime))
108100
->string($this->getAttribute('component'))
109101
->raw(', ')
110-
->raw($twig_to_array)
102+
->raw('Twig\Extension\CoreExtension::toArray')
111103
->raw('(');
112104
$this->writeProps($compiler)
113105
->raw('), ')

src/TwigComponent/tests/Fixtures/Kernel.php

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -56,24 +56,20 @@ protected function configureContainer(ContainerConfigurator $c): void
5656
]);
5757

5858
$twigComponentConfig = [];
59-
if ('legacy_autonaming' != $this->environment) {
60-
$acmeDefaults = [
61-
'name_prefix' => 'AcmePrefix',
62-
];
63-
if ('no_template_directory' !== $this->environment) {
64-
$acmeDefaults['template_directory'] = 'acme_components';
65-
}
66-
$twigComponentConfig['defaults'] = [
67-
'Symfony\UX\TwigComponent\Tests\Fixtures\Component\\' => 'components/',
68-
'Symfony\UX\TwigComponent\Tests\Fixtures\AcmeComponent\\' => $acmeDefaults,
69-
];
59+
$acmeDefaults = [
60+
'name_prefix' => 'AcmePrefix',
61+
];
62+
if ('no_template_directory' !== $this->environment) {
63+
$acmeDefaults['template_directory'] = 'acme_components';
7064
}
65+
$twigComponentConfig['defaults'] = [
66+
'Symfony\UX\TwigComponent\Tests\Fixtures\Component\\' => 'components/',
67+
'Symfony\UX\TwigComponent\Tests\Fixtures\AcmeComponent\\' => $acmeDefaults,
68+
];
7169

72-
if ('legacy_anonymous' != $this->environment) {
73-
$twigComponentConfig['anonymous_template_directory'] = 'components';
74-
if ('anonymous_directory' == $this->environment) {
75-
$twigComponentConfig['anonymous_template_directory'] = 'anonymous';
76-
}
70+
$twigComponentConfig['anonymous_template_directory'] = 'components';
71+
if ('anonymous_directory' === $this->environment) {
72+
$twigComponentConfig['anonymous_template_directory'] = 'anonymous';
7773
}
7874

7975
$c->extension('twig_component', $twigComponentConfig);

0 commit comments

Comments
 (0)