Skip to content

Introduce Symfony UX Turbo #61

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 6 commits into from
Closed
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
16 changes: 12 additions & 4 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ jobs:
- uses: shivammathur/setup-php@v2
with:
php-version: '7.2'
extensions: gd
- name: Chartjs
run: |
cd src/Chartjs
Expand All @@ -54,6 +55,11 @@ jobs:
cd src/LazyImage
composer update --prefer-lowest --prefer-dist --no-interaction --no-ansi --no-progress
php vendor/bin/simple-phpunit
- name: Turbo
run: |
cd src/Turbo
composer update --prefer-lowest --prefer-dist --no-interaction --no-ansi --no-progress
php vendor/bin/simple-phpunit

tests-php-high-deps:
runs-on: ubuntu-latest
Expand All @@ -62,28 +68,30 @@ jobs:
- uses: shivammathur/setup-php@v2
with:
php-version: '8.0'
extensions: gd
- name: Chartjs
run: |
cd src/Chartjs
composer config platform.php 7.4.99
composer update --prefer-dist --no-interaction --no-ansi --no-progress
php vendor/bin/simple-phpunit
- name: Cropperjs
run: |
cd src/Cropperjs
composer config platform.php 7.4.99
composer update --prefer-dist --no-interaction --no-ansi --no-progress
php vendor/bin/simple-phpunit
- name: Dropzone
run: |
cd src/Dropzone
composer config platform.php 7.4.99
composer update --prefer-dist --no-interaction --no-ansi --no-progress
php vendor/bin/simple-phpunit
- name: LazyImage
run: |
cd src/LazyImage
composer config platform.php 7.4.99
composer update --prefer-dist --no-interaction --no-ansi --no-progress
php vendor/bin/simple-phpunit
- name: Turbo
run: |
cd src/Turbo
composer update --prefer-dist --no-interaction --no-ansi --no-progress
php vendor/bin/simple-phpunit

Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ integrating it into Webpack Encore.
Improve image loading performances through lazy-loading and data-uri thumbnails
- [UX Swup](https://github.com/symfony/ux-swup):
[Swup](https://swup.js.org/) page transition library integration for Symfony
- [UX Turbo](https://github.com/symfony/ux-turbo):
[Turbo](https://turbo.hotwire.dev) interactive and real-time UI library integration for Symfony

## Stimulus Tools around the World

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
"overrides": [
{
"files": [
"src/*/Resources/assets/test/*.js"
"src/*/Resources/assets/test/**/*.js"
],
"extends": [
"plugin:jest/recommended"
Expand Down
5 changes: 5 additions & 0 deletions src/Turbo/.gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/.gitattributes export-ignore
/.gitignore export-ignore
/phpunit.xml.dist export-ignore
/assets/test export-ignore
/tests export-ignore
4 changes: 4 additions & 0 deletions src/Turbo/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/vendor/
.phpunit.result.cache
.php_cs.cache
composer.lock
51 changes: 51 additions & 0 deletions src/Turbo/DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\UX\Turbo\DependencyInjection;

use Symfony\Component\Config\Definition\Builder\TreeBuilder;
use Symfony\Component\Config\Definition\ConfigurationInterface;

/**
* @author Titouan Galopin <[email protected]>
*
* @internal
*/
class Configuration implements ConfigurationInterface
{
public function getConfigTreeBuilder()
{
$treeBuilder = new TreeBuilder('turbo');
$rootNode = $treeBuilder->getRootNode();

$rootNode
->children()
->arrayNode('streams')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe we could remove this nesting level and rename adapter to stream_adapter? (same for options)

->info('Enable this section to use Turbo Streams')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

there is nothing to enable in this config :)
maybe this should be removed? I don't see similar comments elsewhere.

->children()
->scalarNode('adapter')
->defaultNull()
->info('The service to use to create Turbo Streams')
->end()
->arrayNode('options')
->info('Key/value options passed to the adapter to create Turbo Streams')
->normalizeKeys(false)
->variablePrototype()
->end()
->end()
->end()
->end()
->end()
;

return $treeBuilder;
}
}
71 changes: 71 additions & 0 deletions src/Turbo/DependencyInjection/TurboExtension.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\UX\Turbo\DependencyInjection;

use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
use Symfony\UX\Turbo\Streams\MercureStreamAdapter;
use Symfony\UX\Turbo\Twig\TurboFrameExtension;
use Symfony\UX\Turbo\Twig\TurboStreamExtension;
use Symfony\WebpackEncoreBundle\Twig\StimulusTwigExtension;
use Twig\Environment;

/**
* @author Titouan Galopin <[email protected]>
*
* @internal
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we need all those @internal annotations, do we?
What about @experimental also? Some have it, not all.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I may be a bit conservative regarding these annotations indeed :) . Other UX packages have these annotations as well, not sure what our policy should be on them.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm often adding @internal when the class should not be used in both framework and standalone usage. E.g. when someone is using this package outside the Symfony framework, they probably end up needing to initialize MercureStreamAdapter. So I wouldn't add @internal there.

As for @experimental, I'm often not adding it to @internal classes (as there is no BC promise in those anyway). Not sure if these are "Symfony policies" or only my personal policy regarding them :)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree with @wouterj here, we should remove @internal imho.
Maybe we can make the class final also?

*/
class TurboExtension extends Extension
{
public function load(array $configs, ContainerBuilder $container)
{
$configuration = new Configuration();
$config = $this->processConfiguration($configuration, $configs);

// Register Turbo Streams config
if (!empty($config['streams']['adapter'])) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we try to avoid using empty as much as possible

$this->registerStreamsConfig($config, $container);
}

// Add Twig extension
if (class_exists(Environment::class)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could use an early return statement

$container
->setDefinition('turbo.twig.frame_extension', new Definition(TurboFrameExtension::class))
->addTag('twig.extension')
->setPublic(false)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not needed

;

if (!empty($config['streams']['adapter']) && class_exists(StimulusTwigExtension::class)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

empty + early return?

$container
->setDefinition('turbo.twig.stream_extension', new Definition(TurboStreamExtension::class))
->addArgument(new Reference('webpack_encore.twig_stimulus_extension'))
->addArgument(new Reference('turbo.streams.adapter'))
->addArgument($config['streams']['options'] ?? [])
->addTag('twig.extension')
->setPublic(false)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should be removed

;
}
}
}

private function registerStreamsConfig(array $config, ContainerBuilder $container)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe we can inline this method? I don't think it provides much

{
// Register native stream adapters
$container->setDefinition('turbo.streams.adapter.mercure', new Definition(MercureStreamAdapter::class))
->setPublic(false);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should be removed


// Wire configured adapter
$container->setAlias('turbo.streams.adapter', $config['streams']['adapter'])->setPublic(false);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

setPublic

}
}
19 changes: 19 additions & 0 deletions src/Turbo/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
Copyright (c) 2020-2021 Fabien Potencier
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

2021 only :)


Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is furnished
to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
Loading