Skip to content

Commit ecb6c82

Browse files
committed
Integrating StimulusBundle for Stimulus helpers instead of Encore
1 parent 2c221c6 commit ecb6c82

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

47 files changed

+280
-568
lines changed

.github/build-packages.php

Lines changed: 37 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,43 @@
11
<?php
22

3-
$dir = __DIR__.'/../src/LiveComponent';
4-
$flags = JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE;
3+
require __DIR__.'/../vendor/autoload.php';
54

6-
$json = ltrim(file_get_contents($dir.'/composer.json'));
7-
if (null === $package = json_decode($json)) {
8-
passthru("composer validate $dir/composer.json");
9-
exit(1);
10-
}
5+
use Symfony\Component\Finder\Finder;
6+
7+
$finder = (new Finder())
8+
->in(__DIR__.'/../src/*/')
9+
->depth(0)
10+
->name('composer.json')
11+
;
12+
13+
foreach ($finder as $composerFile) {
14+
$json = file_get_contents($composerFile->getPathname());
15+
if (null === $packageData = json_decode($json, true)) {
16+
passthru(sprintf('composer validate %s', $composerFile->getPathname()));
17+
exit(1);
18+
}
1119

12-
$package->repositories[] = [
13-
'type' => 'path',
14-
'url' => '../TwigComponent',
15-
];
20+
$repositories = [];
1621

17-
$json = preg_replace('/\n "repositories": \[\n.*?\n \],/s', '', $json);
18-
$json = rtrim(json_encode(['repositories' => $package->repositories], $flags), "\n}").','.substr($json, 1);
19-
$json = preg_replace('/"symfony\/ux-twig-component": "(\^[\d]+\.[\d]+)"/s', '"symfony/ux-twig-component": "@dev"', $json);
20-
file_put_contents($dir.'/composer.json', $json);
22+
if (isset($packageData['require']['symfony/ux-twig-component'])) {
23+
$repositories[] = [
24+
'type' => 'path',
25+
'url' => '../TwigComponent',
26+
];
27+
$packageData['require']['symfony/ux-twig-component'] = '@dev';
28+
}
2129

30+
if (isset($packageData['require']['symfony/stimulus-bundle'])) {
31+
$repositories[] = [
32+
'type' => 'path',
33+
'url' => '../StimulusBundle',
34+
];
35+
$packageData['require']['symfony/stimulus-bundle'] = '@dev';
36+
}
37+
if (count($repositories) > 0) {
38+
$packageData['repositories'] = $repositories;
39+
}
40+
41+
$json = json_encode($packageData, \JSON_PRETTY_PRINT | \JSON_UNESCAPED_SLASHES | \JSON_UNESCAPED_UNICODE);
42+
file_put_contents($composerFile->getPathname(), $json."\n");
43+
}

README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,9 @@ to build the chart in PHP. The JavaScript is handled for you automatically.
1515
**That's Symfony UX.**
1616

1717
Symfony UX leverages [Stimulus](https://stimulus.hotwired.dev/) for JavaScript
18-
and the [Stimulus Bridge](https://github.com/symfony/stimulus-bridge) for
19-
integrating it into [Webpack Encore](https://github.com/symfony/webpack-encore).
18+
and can integrate with [Webpack Encore](https://github.com/symfony/webpack-encore)
19+
(with the help of [Stimulus Bridge](https://github.com/symfony/stimulus-bridge))
20+
or with [AssetMapper](https://symfony.com/doc/current/frontend/asset-mapper.html)
2021

2122
## Resources
2223

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
"license": "MIT",
44
"require-dev": {
55
"symfony/filesystem": "^5.2|^6.0",
6+
"symfony/finder": "^5.4|^6.0",
67
"php-cs-fixer/shim": "^3.13"
78
}
89
}

src/Chartjs/composer.json

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,21 +28,20 @@
2828
}
2929
},
3030
"require": {
31-
"php": ">=7.2.5",
31+
"php": ">=8.0",
3232
"symfony/config": "^4.4.17|^5.0|^6.0",
3333
"symfony/dependency-injection": "^4.4.17|^5.0|^6.0",
34-
"symfony/http-kernel": "^4.4.17|^5.0|^6.0"
34+
"symfony/http-kernel": "^4.4.17|^5.0|^6.0",
35+
"symfony/stimulus-bundle": "^2.9"
3536
},
3637
"require-dev": {
3738
"symfony/framework-bundle": "^4.4.17|^5.0|^6.0",
3839
"symfony/phpunit-bridge": "^5.2|^6.0",
3940
"symfony/twig-bundle": "^4.4.17|^5.0|^6.0",
40-
"symfony/var-dumper": "^4.4.17|^5.0|^6.0",
41-
"symfony/webpack-encore-bundle": "^1.11"
41+
"symfony/var-dumper": "^4.4.17|^5.0|^6.0"
4242
},
4343
"conflict": {
44-
"symfony/flex": "<1.13",
45-
"symfony/webpack-encore-bundle": "<1.11"
44+
"symfony/flex": "<1.13"
4645
},
4746
"extra": {
4847
"thanks": {

src/Chartjs/doc/index.rst

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,7 @@ and create charts in PHP::
7474
All options and data are provided as-is to Chart.js. You can read
7575
`Chart.js documentation`_ to discover them all.
7676

77-
Once created in PHP, a chart can be displayed using Twig if installed
78-
(requires `Symfony Webpack Encore`_):
77+
Once created in PHP, a chart can be displayed using Twig:
7978

8079
.. code-block:: html+twig
8180

@@ -244,7 +243,6 @@ the Symfony framework: https://symfony.com/doc/current/contributing/code/bc.html
244243
.. _`the Symfony UX initiative`: https://symfony.com/ux
245244
.. _`@symfony/stimulus-bridge`: https://github.com/symfony/stimulus-bridge
246245
.. _`Chart.js documentation`: https://www.chartjs.org/docs/latest/
247-
.. _`Symfony Webpack Encore`: https://symfony.com/doc/current/frontend/encore/installation.html
248246
.. _`Symfony UX configured in your app`: https://symfony.com/doc/current/frontend/ux.html
249247
.. _`a lot of plugins`: https://github.com/chartjs/awesome#plugins
250248
.. _`zoom plugin`: https://www.chartjs.org/chartjs-plugin-zoom/latest/

src/Chartjs/src/DependencyInjection/ChartjsExtension.php

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
use Symfony\UX\Chartjs\Builder\ChartBuilder;
2121
use Symfony\UX\Chartjs\Builder\ChartBuilderInterface;
2222
use Symfony\UX\Chartjs\Twig\ChartExtension;
23-
use Symfony\WebpackEncoreBundle\Twig\StimulusTwigExtension;
23+
use Symfony\UX\StimulusBundle\Dto\StimulusAttributes;
2424
use Twig\Environment;
2525

2626
/**
@@ -42,14 +42,12 @@ public function load(array $configs, ContainerBuilder $container)
4242
->setPublic(false)
4343
;
4444

45-
if (class_exists(Environment::class) && class_exists(StimulusTwigExtension::class)) {
46-
$container
47-
->setDefinition('chartjs.twig_extension', new Definition(ChartExtension::class))
48-
->addArgument(new Reference('webpack_encore.twig_stimulus_extension'))
49-
->addTag('twig.extension')
50-
->setPublic(false)
51-
;
52-
}
45+
$container
46+
->setDefinition('chartjs.twig_extension', new Definition(ChartExtension::class))
47+
->addArgument(new Reference('stimulus.helper'))
48+
->addTag('twig.extension')
49+
->setPublic(false)
50+
;
5351
}
5452

5553
public function prepend(ContainerBuilder $container)

src/Chartjs/src/Twig/ChartExtension.php

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,7 @@
1212
namespace Symfony\UX\Chartjs\Twig;
1313

1414
use Symfony\UX\Chartjs\Model\Chart;
15-
use Symfony\WebpackEncoreBundle\Dto\StimulusControllersDto;
16-
use Symfony\WebpackEncoreBundle\Twig\StimulusTwigExtension;
15+
use Symfony\UX\StimulusBundle\Helper\StimulusHelper;
1716
use Twig\Environment;
1817
use Twig\Extension\AbstractExtension;
1918
use Twig\TwigFunction;
@@ -27,7 +26,7 @@ class ChartExtension extends AbstractExtension
2726
{
2827
private $stimulus;
2928

30-
public function __construct(StimulusTwigExtension $stimulus)
29+
public function __construct(StimulusHelper $stimulus)
3130
{
3231
$this->stimulus = $stimulus;
3332
}
@@ -49,15 +48,9 @@ public function renderChart(Environment $env, Chart $chart, array $attributes =
4948
}
5049
$controllers['@symfony/ux-chartjs/chart'] = ['view' => $chart->createView()];
5150

52-
if (class_exists(StimulusControllersDto::class)) {
53-
$dto = new StimulusControllersDto($env);
54-
foreach ($controllers as $name => $controllerValues) {
55-
$dto->addController($name, $controllerValues);
56-
}
57-
58-
$html = '<canvas '.$dto.' ';
59-
} else {
60-
$html = '<canvas '.$this->stimulus->renderStimulusController($env, $controllers).' ';
51+
$stimulusAttributes = $this->stimulus->createStimulusAttributes();
52+
foreach ($controllers as $name => $controllerValues) {
53+
$stimulusAttributes->addController($name, $controllerValues);
6154
}
6255

6356
foreach ($chart->getAttributes() as $name => $value) {
@@ -66,12 +59,12 @@ public function renderChart(Environment $env, Chart $chart, array $attributes =
6659
}
6760

6861
if (true === $value) {
69-
$html .= $name.'="'.$name.'" ';
62+
$stimulusAttributes->addAttribute($name, $name);
7063
} elseif (false !== $value) {
71-
$html .= $name.'="'.$value.'" ';
64+
$stimulusAttributes->addAttribute($name, $value);
7265
}
7366
}
7467

75-
return trim($html).'></canvas>';
68+
return sprintf('<canvas %s></canvas>', $stimulusAttributes);
7669
}
7770
}

src/Chartjs/tests/ChartjsBundleTest.php

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -24,18 +24,9 @@
2424
*/
2525
class ChartjsBundleTest extends TestCase
2626
{
27-
public function provideKernels()
28-
{
29-
yield 'empty' => [new EmptyAppKernel('test', true)];
30-
yield 'framework' => [new FrameworkAppKernel('test', true)];
31-
yield 'twig' => [new TwigAppKernel('test', true)];
32-
}
33-
34-
/**
35-
* @dataProvider provideKernels
36-
*/
37-
public function testBootKernel(Kernel $kernel)
27+
public function testBootKernel()
3828
{
29+
$kernel = new TwigAppKernel('test', true);
3930
$kernel->boot();
4031
$this->assertArrayHasKey('ChartjsBundle', $kernel->getBundles());
4132
}

src/Chartjs/tests/Kernel/AppKernelTrait.php

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

src/Chartjs/tests/Kernel/EmptyAppKernel.php

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

src/Chartjs/tests/Kernel/FrameworkAppKernel.php

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

0 commit comments

Comments
 (0)