Skip to content

Commit f7c6e63

Browse files
committed
Introduce Symfony UX Turbo
1 parent 299200e commit f7c6e63

32 files changed

+1627
-0
lines changed

.github/workflows/test.yaml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ jobs:
3434
- uses: shivammathur/setup-php@v2
3535
with:
3636
php-version: '7.2'
37+
extensions: gd
3738
- name: Chartjs
3839
run: |
3940
cd src/Chartjs
@@ -54,6 +55,11 @@ jobs:
5455
cd src/LazyImage
5556
composer update --prefer-lowest --prefer-dist --no-interaction --no-ansi --no-progress
5657
php vendor/bin/simple-phpunit
58+
- name: Turbo
59+
run: |
60+
cd src/Turbo
61+
composer update --prefer-lowest --prefer-dist --no-interaction --no-ansi --no-progress
62+
php vendor/bin/simple-phpunit
5763
5864
tests-php-high-deps:
5965
runs-on: ubuntu-latest
@@ -62,6 +68,7 @@ jobs:
6268
- uses: shivammathur/setup-php@v2
6369
with:
6470
php-version: '8.0'
71+
extensions: gd
6572
- name: Chartjs
6673
run: |
6774
cd src/Chartjs
@@ -86,6 +93,12 @@ jobs:
8693
composer config platform.php 7.4.99
8794
composer update --prefer-dist --no-interaction --no-ansi --no-progress
8895
php vendor/bin/simple-phpunit
96+
- name: Turbo
97+
run: |
98+
cd src/Turbo
99+
composer config platform.php 7.4.99
100+
composer update --prefer-dist --no-interaction --no-ansi --no-progress
101+
php vendor/bin/simple-phpunit
89102
90103
tests-js:
91104
runs-on: ubuntu-latest

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ integrating it into Webpack Encore.
3434
Improve image loading performances through lazy-loading and data-uri thumbnails
3535
- [UX Swup](https://github.com/symfony/ux-swup):
3636
[Swup](https://swup.js.org/) page transition library integration for Symfony
37+
- [UX Turbo](https://github.com/symfony/ux-turbo):
38+
[Turbo](https://turbo.hotwire.dev) interactive and real-time UI library integration for Symfony
3739

3840
## Stimulus Tools around the World
3941

src/Turbo/.gitattributes

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
/.gitattributes export-ignore
2+
/.gitignore export-ignore
3+
/phpunit.xml.dist export-ignore
4+
/assets/test export-ignore
5+
/tests export-ignore

src/Turbo/.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
/vendor/
2+
.phpunit.result.cache
3+
.php_cs.cache
4+
composer.lock
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
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\Turbo\DependencyInjection;
13+
14+
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
15+
use Symfony\Component\Config\Definition\ConfigurationInterface;
16+
17+
/**
18+
* @author Titouan Galopin <[email protected]>
19+
*
20+
* @internal
21+
*/
22+
class Configuration implements ConfigurationInterface
23+
{
24+
public function getConfigTreeBuilder()
25+
{
26+
$treeBuilder = new TreeBuilder('turbo');
27+
$rootNode = $treeBuilder->getRootNode();
28+
29+
$rootNode
30+
->children()
31+
->arrayNode('streams')
32+
->info('Enable this section to use Turbo Streams')
33+
->children()
34+
->scalarNode('adapter')
35+
->defaultNull()
36+
->info('The service to use to create Turbo Streams')
37+
->end()
38+
->arrayNode('options')
39+
->info('Key/value options passed to the adapter to create Turbo Streams')
40+
->normalizeKeys(false)
41+
->variablePrototype()
42+
->end()
43+
->end()
44+
->end()
45+
->end()
46+
;
47+
48+
return $treeBuilder;
49+
}
50+
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
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\Turbo\DependencyInjection;
13+
14+
use Symfony\Component\DependencyInjection\ContainerBuilder;
15+
use Symfony\Component\DependencyInjection\Definition;
16+
use Symfony\Component\DependencyInjection\Reference;
17+
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
18+
use Symfony\UX\Turbo\Streams\MercureStreamAdapter;
19+
use Symfony\UX\Turbo\Twig\FrameTwigExtension;
20+
use Symfony\UX\Turbo\Twig\StreamTwigExtension;
21+
use Symfony\WebpackEncoreBundle\Twig\StimulusTwigExtension;
22+
use Twig\Environment;
23+
24+
/**
25+
* @author Titouan Galopin <[email protected]>
26+
*
27+
* @internal
28+
*/
29+
class TurboExtension extends Extension
30+
{
31+
public function load(array $configs, ContainerBuilder $container)
32+
{
33+
$configuration = new Configuration();
34+
$config = $this->processConfiguration($configuration, $configs);
35+
36+
// Register Turbo Streams config
37+
if (!empty($config['streams']['adapter'])) {
38+
$this->registerStreamsConfig($config, $container);
39+
}
40+
41+
// Add Twig extension
42+
if (class_exists(Environment::class)) {
43+
$container
44+
->setDefinition('turbo.twig.frame_extension', new Definition(FrameTwigExtension::class))
45+
->addTag('twig.extension')
46+
->setPublic(false)
47+
;
48+
49+
if (!empty($config['streams']['adapter']) && class_exists(StimulusTwigExtension::class)) {
50+
$container
51+
->setDefinition('turbo.twig.stream_extension', new Definition(StreamTwigExtension::class))
52+
->addArgument(new Reference('webpack_encore.twig_stimulus_extension'))
53+
->addArgument(new Reference('turbo.streams.adapter'))
54+
->addArgument($config['streams']['options'] ?? [])
55+
->addTag('twig.extension')
56+
->setPublic(false)
57+
;
58+
}
59+
}
60+
}
61+
62+
private function registerStreamsConfig(array $config, ContainerBuilder $container)
63+
{
64+
// Register native stream adapters
65+
$container->setDefinition('turbo.streams.adapter.mercure', new Definition(MercureStreamAdapter::class))
66+
->setPublic(false);
67+
68+
// Wire configured adapter
69+
$container->setAlias('turbo.streams.adapter', $config['streams']['adapter'])->setPublic(false);
70+
}
71+
}

0 commit comments

Comments
 (0)