Skip to content
Open
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
140 changes: 0 additions & 140 deletions src/TwigComponent/src/CVA.php

This file was deleted.

38 changes: 5 additions & 33 deletions src/TwigComponent/src/ComponentAttributes.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
namespace Symfony\UX\TwigComponent;

use Symfony\UX\StimulusBundle\Dto\StimulusAttributes;
use Symfony\WebpackEncoreBundle\Dto\AbstractStimulusDto;
use Symfony\UX\TwigComponent\Exception\RuntimeException;
use Twig\Runtime\EscaperRuntime;

/**
Expand Down Expand Up @@ -47,6 +47,10 @@ public function __toString(): string
continue;
}

if (null === $value) {
throw new RuntimeException('Attribute values cannot be null. If you want to remove an attribute, use the "remove()" method.');
}

if (false === $value) {
continue;
}
Expand All @@ -60,11 +64,6 @@ public function __toString(): string
continue;
}

if (null === $value) {
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.');
$value = true;
}

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

public function add($stimulusDto): self
{
if ($stimulusDto instanceof AbstractStimulusDto) {
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(\'...\'))".');
} elseif ($stimulusDto instanceof StimulusAttributes) {
trigger_deprecation('symfony/ux-twig-component', '2.9.0', 'Calling ComponentAttributes::add() is deprecated. Instead use "attributes.defaults(stimulus_controller(\'...\'))".');

return $this->defaults($stimulusDto);
} else {
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)));
}

$controllersAttributes = $stimulusDto->toArray();
$attributes = $this->attributes;

$attributes['data-controller'] = trim(implode(' ', array_merge(
explode(' ', $attributes['data-controller'] ?? ''),
explode(' ', $controllersAttributes['data-controller'] ?? [])
)));
unset($controllersAttributes['data-controller']);

$clone = new self($attributes, $this->escaper);

// add the remaining attributes for values/classes
return $clone->defaults($controllersAttributes);
}

public function remove($key): self
{
$attributes = $this->attributes;
Expand Down
41 changes: 4 additions & 37 deletions src/TwigComponent/src/ComponentTemplateFinder.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,58 +11,25 @@

namespace Symfony\UX\TwigComponent;

use Twig\Environment;
use Twig\Loader\LoaderInterface;

/**
* @author Matheo Daninos <[email protected]>
*/
final class ComponentTemplateFinder implements ComponentTemplateFinderInterface
{
private readonly LoaderInterface $loader;

public function __construct(
Environment|LoaderInterface $loader,
private readonly ?string $directory = null,
private readonly LoaderInterface $loader,
private readonly string $directory,
) {
if ($loader instanceof Environment) {
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);
$loader = $loader->getLoader();
}
$this->loader = $loader;
if (null === $this->directory) {
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__);
}
}

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

// Legacy auto-naming rules < 2.13
if (null === $this->directory) {
if ($loader->exists('components/'.$componentPath.'.html.twig')) {
return 'components/'.$componentPath.'.html.twig';
}

if ($loader->exists($componentPath.'.html.twig')) {
return $componentPath.'.html.twig';
}

if ($loader->exists('components/'.$componentPath)) {
return 'components/'.$componentPath;
}

if ($loader->exists($componentPath)) {
return $componentPath;
}

return null;
}

$template = rtrim($this->directory, '/').'/'.$componentPath.'.html.twig';
if ($loader->exists($template)) {
if ($this->loader->exists($template)) {
return $template;
}

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

$template = '@'.$parts[0].'/components/'.$parts[1].'.html.twig';
if ($loader->exists($template)) {
if ($this->loader->exists($template)) {
return $template;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,6 @@ public function process(ContainerBuilder $container): void
$componentNames = [];
$componentDefaults = $container->getParameter('ux.twig_component.component_defaults');
$container->getParameterBag()->remove('ux.twig_component.component_defaults');
$legacyAutoNaming = $container->hasParameter('ux.twig_component.legacy_autonaming');
$container->getParameterBag()->remove('ux.twig_component.legacy_autonaming');

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

foreach ($tags as $tag) {
if (!\array_key_exists('key', $tag)) {
if ($legacyAutoNaming) {
$name = substr($fqcn, strrpos($fqcn, '\\') + 1);
} else {
if (null === $defaults) {
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));
}

$name = str_replace('\\', ':', substr($fqcn, \strlen($defaults['namespace'])));
if ($defaults['name_prefix']) {
$name = \sprintf('%s:%s', $defaults['name_prefix'], $name);
}
if (null === $defaults) {
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));
}

$name = str_replace('\\', ':', substr($fqcn, \strlen($defaults['namespace'])));
if ($defaults['name_prefix']) {
$name = \sprintf('%s:%s', $defaults['name_prefix'], $name);
}
if (\in_array($name, $componentNames, true)) {
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));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,6 @@
*/
final class TwigComponentExtension extends Extension implements ConfigurationInterface
{
private const DEPRECATED_DEFAULT_KEY = '__deprecated__use_old_naming_behavior';

public function load(array $configs, ContainerBuilder $container): void
{
$loader = new PhpFileLoader($container, new FileLocator(__DIR__.'/../../config'));
Expand All @@ -62,12 +60,6 @@ public function load(array $configs, ContainerBuilder $container): void
$configuration = $this->getConfiguration($configs, $container);
$config = $this->processConfiguration($configuration, $configs);
$defaults = $config['defaults'];
if ($defaults === [self::DEPRECATED_DEFAULT_KEY]) {
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.');
$container->setParameter('ux.twig_component.legacy_autonaming', true);

$defaults = [];
}
$container->setParameter('ux.twig_component.component_defaults', $defaults);

$container->register('ux.twig_component.component_template_finder', ComponentTemplateFinder::class)
Expand Down Expand Up @@ -147,9 +139,6 @@ static function (ChildDefinition $definition, AsTwigComponent $attribute) {
->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') && $config['profiler']) {
$loader->load('debug.php');
}
Expand All @@ -170,19 +159,9 @@ public function getConfigTreeBuilder(): TreeBuilder
\assert($rootNode instanceof ArrayNodeDefinition);

$rootNode
->validate()
->always(function ($v) {
if (!isset($v['anonymous_template_directory'])) {
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.');
$v['anonymous_template_directory'] = null;
}

return $v;
})
->end()
->children()
->arrayNode('defaults')
->defaultValue([self::DEPRECATED_DEFAULT_KEY])
->isRequired()
->useAttributeAsKey('namespace')
->validate()
->always(function ($v) {
Expand Down Expand Up @@ -213,16 +192,13 @@ public function getConfigTreeBuilder(): TreeBuilder
->end()
->end()
->scalarNode('anonymous_template_directory')
->isRequired()
->info('Defaults to `components`')
->end()
->booleanNode('profiler')
->info('Enables the profiler for Twig Component (in debug mode)')
->defaultValue('%kernel.debug%')
->end()
->scalarNode('controllers_json')
->setDeprecated('symfony/ux-twig-component', '2.18', 'The "twig_component.controllers_json" config option is deprecated, and will be removed in 3.0.')
->defaultNull()
->end()
->end();

return $treeBuilder;
Expand Down
Loading
Loading