mirror of
https://github.com/symfony/ux-svelte.git
synced 2026-03-24 00:12:07 +01:00
Adding Asset Mapper support + new StimulusBundle
This commit is contained in:
13
CHANGELOG.md
Normal file
13
CHANGELOG.md
Normal file
@@ -0,0 +1,13 @@
|
||||
# CHANGELOG
|
||||
|
||||
## 2.9.0
|
||||
|
||||
- Add support for symfony/asset-mapper
|
||||
|
||||
- Replace `symfony/webpack-encore-bundle` by `symfony/stimulus-bundle` in dependencies
|
||||
|
||||
- Minimum PHP version is now 8.1
|
||||
|
||||
## 2.8.0
|
||||
|
||||
- Introduce the package
|
||||
@@ -12,6 +12,10 @@
|
||||
"fetch": "eager",
|
||||
"enabled": true
|
||||
}
|
||||
},
|
||||
"importmap": {
|
||||
"@hotwired/stimulus": "^3.0.0",
|
||||
"svelte": "^3.0"
|
||||
}
|
||||
},
|
||||
"peerDependencies": {
|
||||
|
||||
@@ -32,7 +32,8 @@
|
||||
}
|
||||
},
|
||||
"require": {
|
||||
"symfony/webpack-encore-bundle": "^1.15"
|
||||
"php": ">=8.1",
|
||||
"symfony/stimulus-bundle": "^2.9"
|
||||
},
|
||||
"require-dev": {
|
||||
"symfony/framework-bundle": "^5.4|^6.2",
|
||||
|
||||
@@ -11,8 +11,10 @@
|
||||
|
||||
namespace Symfony\UX\Svelte\DependencyInjection;
|
||||
|
||||
use Symfony\Component\AssetMapper\AssetMapperInterface;
|
||||
use Symfony\Component\DependencyInjection\ContainerBuilder;
|
||||
use Symfony\Component\DependencyInjection\Definition;
|
||||
use Symfony\Component\DependencyInjection\Extension\PrependExtensionInterface;
|
||||
use Symfony\Component\DependencyInjection\Reference;
|
||||
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
|
||||
use Symfony\UX\Svelte\Twig\SvelteComponentExtension;
|
||||
@@ -23,15 +25,30 @@ use Symfony\UX\Svelte\Twig\SvelteComponentExtension;
|
||||
*
|
||||
* @internal
|
||||
*/
|
||||
class SvelteExtension extends Extension
|
||||
class SvelteExtension extends Extension implements PrependExtensionInterface
|
||||
{
|
||||
public function load(array $configs, ContainerBuilder $container)
|
||||
{
|
||||
$container
|
||||
->setDefinition('twig.extension.svelte', new Definition(SvelteComponentExtension::class))
|
||||
->setArgument(0, new Reference('webpack_encore.twig_stimulus_extension'))
|
||||
->setArgument(0, new Reference('stimulus.helper'))
|
||||
->addTag('twig.extension')
|
||||
->setPublic(false)
|
||||
;
|
||||
}
|
||||
|
||||
public function prepend(ContainerBuilder $container)
|
||||
{
|
||||
if (!interface_exists(AssetMapperInterface::class)) {
|
||||
return;
|
||||
}
|
||||
|
||||
$container->prependExtensionConfig('framework', [
|
||||
'asset_mapper' => [
|
||||
'paths' => [
|
||||
__DIR__.'/../../assets/dist' => '@symfony/ux-svelte',
|
||||
],
|
||||
],
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -11,8 +11,8 @@
|
||||
|
||||
namespace Symfony\UX\Svelte\Twig;
|
||||
|
||||
use Symfony\UX\StimulusBundle\Helper\StimulusHelper;
|
||||
use Symfony\WebpackEncoreBundle\Twig\StimulusTwigExtension;
|
||||
use Twig\Environment;
|
||||
use Twig\Extension\AbstractExtension;
|
||||
use Twig\TwigFunction;
|
||||
|
||||
@@ -24,21 +24,29 @@ use Twig\TwigFunction;
|
||||
*/
|
||||
class SvelteComponentExtension extends AbstractExtension
|
||||
{
|
||||
private $stimulusExtension;
|
||||
private $stimulusHelper;
|
||||
|
||||
public function __construct(StimulusTwigExtension $stimulusExtension)
|
||||
/**
|
||||
* @param $stimulus StimulusHelper
|
||||
*/
|
||||
public function __construct(StimulusHelper|StimulusTwigExtension $stimulus)
|
||||
{
|
||||
$this->stimulusExtension = $stimulusExtension;
|
||||
if ($stimulus instanceof StimulusTwigExtension) {
|
||||
trigger_deprecation('symfony/ux-svelte', '2.9', 'Passing an instance of "%s" to "%s" is deprecated, pass an instance of "%s" instead.', StimulusTwigExtension::class, __CLASS__, StimulusHelper::class);
|
||||
$stimulus = new StimulusHelper(null);
|
||||
}
|
||||
|
||||
$this->stimulusHelper = $stimulus;
|
||||
}
|
||||
|
||||
public function getFunctions(): array
|
||||
{
|
||||
return [
|
||||
new TwigFunction('svelte_component', [$this, 'renderSvelteComponent'], ['needs_environment' => true, 'is_safe' => ['html_attr']]),
|
||||
new TwigFunction('svelte_component', [$this, 'renderSvelteComponent'], ['is_safe' => ['html_attr']]),
|
||||
];
|
||||
}
|
||||
|
||||
public function renderSvelteComponent(Environment $env, string $componentName, array $props = [], bool $intro = false): string
|
||||
public function renderSvelteComponent(string $componentName, array $props = [], bool $intro = false): string
|
||||
{
|
||||
$params = ['component' => $componentName];
|
||||
if ($props) {
|
||||
@@ -48,6 +56,9 @@ class SvelteComponentExtension extends AbstractExtension
|
||||
$params['intro'] = true;
|
||||
}
|
||||
|
||||
return $this->stimulusExtension->renderStimulusController($env, '@symfony/ux-svelte/svelte', $params);
|
||||
$stimulusAttributes = $this->stimulusHelper->createStimulusAttributes();
|
||||
$stimulusAttributes->addController('@symfony/ux-svelte/svelte', $params);
|
||||
|
||||
return (string) $stimulusAttributes;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,42 +0,0 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the Symfony package.
|
||||
*
|
||||
* (c) Fabien Potencier <fabien@symfony.com>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Symfony\UX\Svelte\Tests\Kernel;
|
||||
|
||||
/**
|
||||
* @author Titouan Galopin <galopintitouan@gmail.com>
|
||||
* @author Thomas Choquet <thomas.choquet.pro@gmail.com>
|
||||
*
|
||||
* @internal
|
||||
*/
|
||||
trait AppKernelTrait
|
||||
{
|
||||
public function getCacheDir(): string
|
||||
{
|
||||
return $this->createTmpDir('cache');
|
||||
}
|
||||
|
||||
public function getLogDir(): string
|
||||
{
|
||||
return $this->createTmpDir('logs');
|
||||
}
|
||||
|
||||
private function createTmpDir(string $type): string
|
||||
{
|
||||
$dir = sys_get_temp_dir().'/svelte_bundle/'.uniqid($type.'_', true);
|
||||
|
||||
if (!file_exists($dir)) {
|
||||
mkdir($dir, 0777, true);
|
||||
}
|
||||
|
||||
return $dir;
|
||||
}
|
||||
}
|
||||
@@ -1,43 +0,0 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the Symfony package.
|
||||
*
|
||||
* (c) Fabien Potencier <fabien@symfony.com>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Symfony\UX\Svelte\Tests\Kernel;
|
||||
|
||||
use Symfony\Bundle\FrameworkBundle\FrameworkBundle;
|
||||
use Symfony\Component\Config\Loader\LoaderInterface;
|
||||
use Symfony\Component\DependencyInjection\ContainerBuilder;
|
||||
use Symfony\Component\HttpKernel\Kernel;
|
||||
use Symfony\UX\Svelte\SvelteBundle;
|
||||
use Symfony\WebpackEncoreBundle\WebpackEncoreBundle;
|
||||
|
||||
/**
|
||||
* @author Titouan Galopin <galopintitouan@gmail.com>
|
||||
* @author Thomas Choquet <thomas.choquet.pro@gmail.com>
|
||||
*
|
||||
* @internal
|
||||
*/
|
||||
class FrameworkAppKernel extends Kernel
|
||||
{
|
||||
use AppKernelTrait;
|
||||
|
||||
public function registerBundles(): iterable
|
||||
{
|
||||
return [new WebpackEncoreBundle(), new FrameworkBundle(), new SvelteBundle()];
|
||||
}
|
||||
|
||||
public function registerContainerConfiguration(LoaderInterface $loader)
|
||||
{
|
||||
$loader->load(function (ContainerBuilder $container) {
|
||||
$container->loadFromExtension('framework', ['secret' => '$ecret', 'test' => true]);
|
||||
$container->loadFromExtension('webpack_encore', ['output_path' => '%kernel.project_dir%/public/build']);
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -16,8 +16,8 @@ use Symfony\Bundle\TwigBundle\TwigBundle;
|
||||
use Symfony\Component\Config\Loader\LoaderInterface;
|
||||
use Symfony\Component\DependencyInjection\ContainerBuilder;
|
||||
use Symfony\Component\HttpKernel\Kernel;
|
||||
use Symfony\UX\StimulusBundle\StimulusBundle;
|
||||
use Symfony\UX\Svelte\SvelteBundle;
|
||||
use Symfony\WebpackEncoreBundle\WebpackEncoreBundle;
|
||||
|
||||
/**
|
||||
* @author Titouan Galopin <galopintitouan@gmail.com>
|
||||
@@ -27,22 +27,40 @@ use Symfony\WebpackEncoreBundle\WebpackEncoreBundle;
|
||||
*/
|
||||
class TwigAppKernel extends Kernel
|
||||
{
|
||||
use AppKernelTrait;
|
||||
|
||||
public function registerBundles(): iterable
|
||||
{
|
||||
return [new WebpackEncoreBundle(), new FrameworkBundle(), new TwigBundle(), new SvelteBundle()];
|
||||
return [new StimulusBundle(), new FrameworkBundle(), new TwigBundle(), new SvelteBundle()];
|
||||
}
|
||||
|
||||
public function registerContainerConfiguration(LoaderInterface $loader)
|
||||
{
|
||||
$loader->load(function (ContainerBuilder $container) {
|
||||
$container->loadFromExtension('framework', ['secret' => '$ecret', 'test' => true]);
|
||||
$container->loadFromExtension('webpack_encore', ['output_path' => '%kernel.project_dir%/public/build']);
|
||||
$container->loadFromExtension('twig', ['default_path' => __DIR__.'/templates', 'strict_variables' => true, 'exception_controller' => null]);
|
||||
|
||||
$container->setAlias('test.twig', 'twig')->setPublic(true);
|
||||
$container->setAlias('test.twig.extension.svelte', 'twig.extension.svelte')->setPublic(true);
|
||||
});
|
||||
}
|
||||
|
||||
public function getCacheDir(): string
|
||||
{
|
||||
return $this->createTmpDir('cache');
|
||||
}
|
||||
|
||||
public function getLogDir(): string
|
||||
{
|
||||
return $this->createTmpDir('logs');
|
||||
}
|
||||
|
||||
private function createTmpDir(string $type): string
|
||||
{
|
||||
$dir = sys_get_temp_dir().'/svelte_bundle/'.uniqid($type.'_', true);
|
||||
|
||||
if (!file_exists($dir)) {
|
||||
mkdir($dir, 0777, true);
|
||||
}
|
||||
|
||||
return $dir;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -12,8 +12,6 @@
|
||||
namespace Symfony\UX\Symfony\Tests;
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Symfony\Component\HttpKernel\Kernel;
|
||||
use Symfony\UX\Svelte\Tests\Kernel\FrameworkAppKernel;
|
||||
use Symfony\UX\Svelte\Tests\Kernel\TwigAppKernel;
|
||||
|
||||
/**
|
||||
@@ -24,17 +22,9 @@ use Symfony\UX\Svelte\Tests\Kernel\TwigAppKernel;
|
||||
*/
|
||||
class SvelteBundleTest extends TestCase
|
||||
{
|
||||
public function provideKernels()
|
||||
{
|
||||
yield 'framework' => [new FrameworkAppKernel('test', true)];
|
||||
yield 'twig' => [new TwigAppKernel('test', true)];
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider provideKernels
|
||||
*/
|
||||
public function testBootKernel(Kernel $kernel)
|
||||
public function testBootKernel()
|
||||
{
|
||||
$kernel = new TwigAppKernel('test', true);
|
||||
$kernel->boot();
|
||||
$this->assertArrayHasKey('SvelteBundle', $kernel->getBundles());
|
||||
}
|
||||
|
||||
@@ -32,7 +32,6 @@ class SvelteComponentExtensionTest extends TestCase
|
||||
$extension = $kernel->getContainer()->get('test.twig.extension.svelte');
|
||||
|
||||
$rendered = $extension->renderSvelteComponent(
|
||||
$kernel->getContainer()->get('test.twig'),
|
||||
'SubDir/MyComponent',
|
||||
['fullName' => 'Titouan Galopin']
|
||||
);
|
||||
@@ -51,7 +50,7 @@ class SvelteComponentExtensionTest extends TestCase
|
||||
/** @var SvelteComponentExtension $extension */
|
||||
$extension = $kernel->getContainer()->get('test.twig.extension.svelte');
|
||||
|
||||
$rendered = $extension->renderSvelteComponent($kernel->getContainer()->get('test.twig'), 'SubDir/MyComponent');
|
||||
$rendered = $extension->renderSvelteComponent('SubDir/MyComponent');
|
||||
|
||||
$this->assertSame(
|
||||
'data-controller="symfony--ux-svelte--svelte" data-symfony--ux-svelte--svelte-component-value="SubDir/MyComponent"',
|
||||
@@ -68,7 +67,6 @@ class SvelteComponentExtensionTest extends TestCase
|
||||
$extension = $kernel->getContainer()->get('test.twig.extension.svelte');
|
||||
|
||||
$rendered = $extension->renderSvelteComponent(
|
||||
$kernel->getContainer()->get('test.twig'),
|
||||
'SubDir/MyComponent',
|
||||
['fullName' => 'Titouan Galopin'],
|
||||
true
|
||||
|
||||
Reference in New Issue
Block a user