Skip to content

Commit 1bb5884

Browse files
committed
Adding Asset Mapper support + new StimulusBundle
1 parent b339cda commit 1bb5884

File tree

10 files changed

+81
-118
lines changed

10 files changed

+81
-118
lines changed

CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
11
# CHANGELOG
22

3+
## 2.9.0
4+
5+
- Replace `symfony/webpack-encore-bundle` by `symfony/stimulus-bundle` in dependencies
6+
7+
- Add support for symfony/asset-mapper
8+
9+
- Minimum PHP version is now 8.1
10+
311
## 2.7.0
412

513
- Add `assets/src` to `.gitattributes` to exclude source TypeScript files from

assets/package.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@
1313
"fetch": "eager",
1414
"enabled": true
1515
}
16+
},
17+
"importmap": {
18+
"@hotwired/stimulus": "^3.0.0",
19+
"vue": "^3.0"
1620
}
1721
},
1822
"peerDependencies": {

composer.json

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,14 @@
3232
}
3333
},
3434
"require": {
35-
"symfony/webpack-encore-bundle": "^1.11"
35+
"php": ">=8.1",
36+
"symfony/stimulus-bundle": "^2.9"
3637
},
3738
"require-dev": {
38-
"symfony/framework-bundle": "^4.4|^5.0|^6.0",
39-
"symfony/phpunit-bridge": "^5.2|^6.0",
40-
"symfony/twig-bundle": "^4.4|^5.0|^6.0",
41-
"symfony/var-dumper": "^4.4|^5.0|^6.0"
39+
"symfony/framework-bundle": "^5.4|^6.0",
40+
"symfony/phpunit-bridge": "^5.4|^6.0",
41+
"symfony/twig-bundle": "^5.4|^6.0",
42+
"symfony/var-dumper": "^5.4|^6.0"
4243
},
4344
"extra": {
4445
"thanks": {

src/DependencyInjection/VueExtension.php

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,10 @@
1111

1212
namespace Symfony\UX\Vue\DependencyInjection;
1313

14+
use Symfony\Component\AssetMapper\AssetMapperInterface;
1415
use Symfony\Component\DependencyInjection\ContainerBuilder;
1516
use Symfony\Component\DependencyInjection\Definition;
17+
use Symfony\Component\DependencyInjection\Extension\PrependExtensionInterface;
1618
use Symfony\Component\DependencyInjection\Reference;
1719
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
1820
use Symfony\UX\Vue\Twig\VueComponentExtension;
@@ -23,15 +25,30 @@
2325
*
2426
* @internal
2527
*/
26-
class VueExtension extends Extension
28+
class VueExtension extends Extension implements PrependExtensionInterface
2729
{
2830
public function load(array $configs, ContainerBuilder $container)
2931
{
3032
$container
3133
->setDefinition('twig.extension.vue', new Definition(VueComponentExtension::class))
32-
->setArgument(0, new Reference('webpack_encore.twig_stimulus_extension'))
34+
->setArgument(0, new Reference('stimulus.helper'))
3335
->addTag('twig.extension')
3436
->setPublic(false)
3537
;
3638
}
39+
40+
public function prepend(ContainerBuilder $container)
41+
{
42+
if (!interface_exists(AssetMapperInterface::class)) {
43+
return;
44+
}
45+
46+
$container->prependExtensionConfig('framework', [
47+
'asset_mapper' => [
48+
'paths' => [
49+
__DIR__.'/../../assets/dist' => '@symfony/ux-vue',
50+
],
51+
],
52+
]);
53+
}
3754
}

src/Twig/VueComponentExtension.php

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@
1111

1212
namespace Symfony\UX\Vue\Twig;
1313

14+
use Symfony\UX\StimulusBundle\Helper\StimulusHelper;
1415
use Symfony\WebpackEncoreBundle\Twig\StimulusTwigExtension;
15-
use Twig\Environment;
1616
use Twig\Extension\AbstractExtension;
1717
use Twig\TwigFunction;
1818

@@ -24,27 +24,38 @@
2424
*/
2525
class VueComponentExtension extends AbstractExtension
2626
{
27-
private $stimulusExtension;
27+
private $stimulusHelper;
2828

29-
public function __construct(StimulusTwigExtension $stimulusExtension)
29+
/**
30+
* @param $stimulus StimulusHelper
31+
*/
32+
public function __construct(StimulusHelper|StimulusTwigExtension $stimulus)
3033
{
31-
$this->stimulusExtension = $stimulusExtension;
34+
if ($stimulus instanceof StimulusTwigExtension) {
35+
trigger_deprecation('symfony/ux-vue', '2.9', 'Passing an instance of "%s" to "%s" is deprecated, pass an instance of "%s" instead.', StimulusTwigExtension::class, __CLASS__, StimulusHelper::class);
36+
$stimulus = new StimulusHelper(null);
37+
}
38+
39+
$this->stimulusHelper = $stimulus;
3240
}
3341

3442
public function getFunctions(): array
3543
{
3644
return [
37-
new TwigFunction('vue_component', [$this, 'renderVueComponent'], ['needs_environment' => true, 'is_safe' => ['html_attr']]),
45+
new TwigFunction('vue_component', [$this, 'renderVueComponent'], ['is_safe' => ['html_attr']]),
3846
];
3947
}
4048

41-
public function renderVueComponent(Environment $env, string $componentName, array $props = []): string
49+
public function renderVueComponent(string $componentName, array $props = []): string
4250
{
4351
$params = ['component' => $componentName];
4452
if ($props) {
4553
$params['props'] = $props;
4654
}
4755

48-
return $this->stimulusExtension->renderStimulusController($env, '@symfony/ux-vue/vue', $params);
56+
$stimulusAttributes = $this->stimulusHelper->createStimulusAttributes();
57+
$stimulusAttributes->addController('@symfony/ux-vue/vue', $params);
58+
59+
return (string) $stimulusAttributes;
4960
}
5061
}

tests/Kernel/AppKernelTrait.php

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

tests/Kernel/FrameworkAppKernel.php

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

tests/Kernel/TwigAppKernel.php

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@
1616
use Symfony\Component\Config\Loader\LoaderInterface;
1717
use Symfony\Component\DependencyInjection\ContainerBuilder;
1818
use Symfony\Component\HttpKernel\Kernel;
19+
use Symfony\UX\StimulusBundle\StimulusBundle;
1920
use Symfony\UX\Vue\VueBundle;
20-
use Symfony\WebpackEncoreBundle\WebpackEncoreBundle;
2121

2222
/**
2323
* @author Titouan Galopin <galopintitouan@gmail.com>
@@ -27,22 +27,40 @@
2727
*/
2828
class TwigAppKernel extends Kernel
2929
{
30-
use AppKernelTrait;
31-
3230
public function registerBundles(): iterable
3331
{
34-
return [new WebpackEncoreBundle(), new FrameworkBundle(), new TwigBundle(), new VueBundle()];
32+
return [new StimulusBundle(), new FrameworkBundle(), new TwigBundle(), new VueBundle()];
3533
}
3634

3735
public function registerContainerConfiguration(LoaderInterface $loader)
3836
{
3937
$loader->load(function (ContainerBuilder $container) {
4038
$container->loadFromExtension('framework', ['secret' => '$ecret', 'test' => true]);
41-
$container->loadFromExtension('webpack_encore', ['output_path' => '%kernel.project_dir%/public/build']);
4239
$container->loadFromExtension('twig', ['default_path' => __DIR__.'/templates', 'strict_variables' => true, 'exception_controller' => null]);
4340

4441
$container->setAlias('test.twig', 'twig')->setPublic(true);
4542
$container->setAlias('test.twig.extension.vue', 'twig.extension.vue')->setPublic(true);
4643
});
4744
}
45+
46+
public function getCacheDir(): string
47+
{
48+
return $this->createTmpDir('cache');
49+
}
50+
51+
public function getLogDir(): string
52+
{
53+
return $this->createTmpDir('logs');
54+
}
55+
56+
private function createTmpDir(string $type): string
57+
{
58+
$dir = sys_get_temp_dir().'/vue_bundle/'.uniqid($type.'_', true);
59+
60+
if (!file_exists($dir)) {
61+
mkdir($dir, 0777, true);
62+
}
63+
64+
return $dir;
65+
}
4866
}

tests/Twig/VueComponentExtensionTest.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ public function testRenderComponent()
3232
$extension = $kernel->getContainer()->get('test.twig.extension.vue');
3333

3434
$rendered = $extension->renderVueComponent(
35-
$kernel->getContainer()->get('test.twig'),
3635
'SubDir/MyComponent',
3736
['fullName' => 'Titouan Galopin']
3837
);
@@ -51,7 +50,7 @@ public function testRenderComponentWithoutProps()
5150
/** @var VueComponentExtension $extension */
5251
$extension = $kernel->getContainer()->get('test.twig.extension.vue');
5352

54-
$rendered = $extension->renderVueComponent($kernel->getContainer()->get('test.twig'), 'SubDir/MyComponent');
53+
$rendered = $extension->renderVueComponent('SubDir/MyComponent');
5554

5655
$this->assertSame(
5756
'data-controller="symfony--ux-vue--vue" data-symfony--ux-vue--vue-component-value="SubDir&#x2F;MyComponent"',

tests/VueBundleTest.php

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@
1212
namespace Symfony\UX\Vue\Tests;
1313

1414
use PHPUnit\Framework\TestCase;
15-
use Symfony\Component\HttpKernel\Kernel;
16-
use Symfony\UX\Vue\Tests\Kernel\FrameworkAppKernel;
1715
use Symfony\UX\Vue\Tests\Kernel\TwigAppKernel;
1816

1917
/**
@@ -24,17 +22,9 @@
2422
*/
2523
class VueBundleTest extends TestCase
2624
{
27-
public function provideKernels()
28-
{
29-
yield 'framework' => [new FrameworkAppKernel('test', true)];
30-
yield 'twig' => [new TwigAppKernel('test', true)];
31-
}
32-
33-
/**
34-
* @dataProvider provideKernels
35-
*/
36-
public function testBootKernel(Kernel $kernel)
25+
public function testBootKernel()
3726
{
27+
$kernel = new TwigAppKernel('test', true);
3828
$kernel->boot();
3929
$this->assertArrayHasKey('VueBundle', $kernel->getBundles());
4030
}

0 commit comments

Comments
 (0)