Skip to content

Commit d07eef3

Browse files
committed
feature #3040 [TwigComponent][UX3] Remove deprecations (smnandre)
This PR was squashed before being merged into the 3.x branch. Discussion ---------- [TwigComponent][UX3] Remove deprecations | Q | A | ------------- | --- | Bug fix? | no | New feature? | yes | Docs? | no | Issues | Fix # | License | MIT Commits ------- 4daa323 [TwigComponent][UX3] Remove deprecations
2 parents dedcaa6 + 4daa323 commit d07eef3

18 files changed

+43
-1291
lines changed

src/TwigComponent/src/CVA.php

Lines changed: 0 additions & 140 deletions
This file was deleted.

src/TwigComponent/src/ComponentAttributes.php

Lines changed: 5 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
namespace Symfony\UX\TwigComponent;
1313

1414
use Symfony\UX\StimulusBundle\Dto\StimulusAttributes;
15-
use Symfony\WebpackEncoreBundle\Dto\AbstractStimulusDto;
15+
use Symfony\UX\TwigComponent\Exception\RuntimeException;
1616
use Twig\Runtime\EscaperRuntime;
1717

1818
/**
@@ -47,6 +47,10 @@ public function __toString(): string
4747
continue;
4848
}
4949

50+
if (null === $value) {
51+
throw new RuntimeException('Attribute values cannot be null. If you want to remove an attribute, use the "remove()" method.');
52+
}
53+
5054
if (false === $value) {
5155
continue;
5256
}
@@ -60,11 +64,6 @@ public function __toString(): string
6064
continue;
6165
}
6266

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-
6867
if (!\is_scalar($value) && !($value instanceof \Stringable)) {
6968
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)));
7069
}
@@ -198,33 +197,6 @@ public function without(string ...$keys): self
198197
return $clone;
199198
}
200199

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-
228200
public function remove($key): self
229201
{
230202
$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;

0 commit comments

Comments
 (0)