Skip to content

Commit d274972

Browse files
committed
Adding convention to load anon components from bundles
1 parent 03a85b5 commit d274972

File tree

5 files changed

+44
-0
lines changed

5 files changed

+44
-0
lines changed

src/TwigComponent/src/ComponentTemplateFinder.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,16 @@ public function findAnonymousComponentTemplate(string $name): ?string
6666
return $template;
6767
}
6868

69+
$parts = explode('/', $componentPath, 2);
70+
if (\count($parts) < 2) {
71+
return null;
72+
}
73+
74+
$template = '@'.$parts[0].'/components/'.$parts[1].'.html.twig';
75+
if ($loader->exists($template)) {
76+
return $template;
77+
}
78+
6979
return null;
7080
}
7181
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\UX\TwigComponent\Tests\Fixtures\Bundle\AcmeBundle;
13+
14+
use Symfony\Component\HttpKernel\Bundle\Bundle;
15+
16+
class AcmeBundle extends Bundle
17+
{
18+
public function getPath(): string
19+
{
20+
return __DIR__;
21+
}
22+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<button class="btn btn-primary">{{ label }}</button>

src/TwigComponent/tests/Fixtures/Kernel.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use Symfony\Bundle\TwigBundle\TwigBundle;
1818
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
1919
use Symfony\Component\HttpKernel\Kernel as BaseKernel;
20+
use Symfony\UX\TwigComponent\Tests\Fixtures\Bundle\AcmeBundle\AcmeBundle;
2021
use Symfony\UX\TwigComponent\Tests\Fixtures\Component\ComponentB;
2122
use Symfony\UX\TwigComponent\TwigComponentBundle;
2223

@@ -32,6 +33,7 @@ public function registerBundles(): iterable
3233
yield new FrameworkBundle();
3334
yield new TwigBundle();
3435
yield new TwigComponentBundle();
36+
yield new AcmeBundle();
3537
}
3638

3739
protected function configureContainer(ContainerConfigurator $c): void

src/TwigComponent/tests/Integration/ComponentFactoryTest.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,15 @@ public function testAnonymous(): void
169169
$this->factory()->metadataFor('anonymous:AButton');
170170
}
171171

172+
public function testLoadingAnonymousComponentFromBundle(): void
173+
{
174+
$metadata = $this->factory()->metadataFor('Acme:Button');
175+
176+
$this->assertSame('@Acme/components/Button.html.twig', $metadata->getTemplate());
177+
$this->assertSame('Acme:Button', $metadata->getName());
178+
$this->assertNull($metadata->get('class'));
179+
}
180+
172181
public function testAutoNamingInSubDirectory(): void
173182
{
174183
$metadata = $this->factory()->metadataFor('SubDirectory:ComponentInSubDirectory');

0 commit comments

Comments
 (0)