diff --git a/src/DependencyInjection/MezcalitoFileManagerExtension.php b/src/DependencyInjection/MezcalitoFileManagerExtension.php index cf3d888..d81e60a 100644 --- a/src/DependencyInjection/MezcalitoFileManagerExtension.php +++ b/src/DependencyInjection/MezcalitoFileManagerExtension.php @@ -16,18 +16,20 @@ use Mezcalito\FileManagerBundle\Configurator\ConfiguratorInterface; use Mezcalito\FileManagerBundle\Twig\Components\Content; use Mezcalito\FileManagerBundle\Twig\Components\File; -use Mezcalito\FileManagerBundle\Twig\Components\FileManager; +use Mezcalito\FileManagerBundle\Twig\Components\FileSystem; use Mezcalito\FileManagerBundle\Twig\Components\Folder; use Mezcalito\FileManagerBundle\Twig\Components\Modal; use Mezcalito\FileManagerBundle\Twig\Components\Sidebar; +use Symfony\Component\AssetMapper\AssetMapperInterface; use Symfony\Component\Config\FileLocator; use Symfony\Component\DependencyInjection\ContainerBuilder; use Symfony\Component\DependencyInjection\ContainerInterface; use Symfony\Component\DependencyInjection\Extension\Extension; +use Symfony\Component\DependencyInjection\Extension\PrependExtensionInterface; use Symfony\Component\DependencyInjection\Loader\PhpFileLoader; use Symfony\Component\DependencyInjection\Reference; -class MezcalitoFileManagerExtension extends Extension +class MezcalitoFileManagerExtension extends Extension implements PrependExtensionInterface { public function load(array $configs, ContainerBuilder $container): void { @@ -52,7 +54,7 @@ public function load(array $configs, ContainerBuilder $container): void $container->registerForAutoconfiguration(ConfiguratorInterface::class) ->addTag('mezcalito_file_manager.configurator'); - $container->register(FileManager::class) + $container->register(FileSystem::class) ->setAutowired(true) ->setAutoconfigured(true); @@ -79,4 +81,43 @@ public function load(array $configs, ContainerBuilder $container): void new Reference('translator', ContainerInterface::IGNORE_ON_INVALID_REFERENCE), ]); } + + public function prepend(ContainerBuilder $container) + { + $container->prependExtensionConfig('twig_component', [ + 'defaults' => [ + 'Mezcalito\\FileManagerBundle\\Twig\\Components\\' => [ + 'template_directory' => '@MezcalitoFileManager/components/', + 'name_prefix' => 'Mezcalito:FileManager', + ], + ], + ]); + + if (!$this->isAssetMapperAvailable($container)) { + return; + } + + $container->prependExtensionConfig('framework', [ + 'asset_mapper' => [ + 'paths' => [ + __DIR__.'/../../assets/dist' => '@mezcalito/ux-filemanager', + ], + ], + ]); + } + + private function isAssetMapperAvailable(ContainerBuilder $container): bool + { + if (!interface_exists(AssetMapperInterface::class)) { + return false; + } + + // check that FrameworkBundle 6.3 or higher is installed + $bundlesMetadata = $container->getParameter('kernel.bundles_metadata'); + if (!isset($bundlesMetadata['FrameworkBundle'])) { + return false; + } + + return is_file($bundlesMetadata['FrameworkBundle']['path'].'/Resources/config/asset_mapper.php'); + } } diff --git a/src/Twig/Components/Content.php b/src/Twig/Components/Content.php index 9268232..d3686b0 100644 --- a/src/Twig/Components/Content.php +++ b/src/Twig/Components/Content.php @@ -25,7 +25,7 @@ use Symfony\UX\LiveComponent\DefaultActionTrait; use Symfony\UX\TwigComponent\Attribute\ExposeInTemplate; -#[AsLiveComponent('Mezcalito:Content', template: '@MezcalitoFileManager/components/content.html.twig')] +#[AsLiveComponent] class Content { use ComponentToolsTrait; diff --git a/src/Twig/Components/File.php b/src/Twig/Components/File.php index f914609..048fa51 100644 --- a/src/Twig/Components/File.php +++ b/src/Twig/Components/File.php @@ -18,7 +18,7 @@ use Symfony\UX\TwigComponent\Attribute\AsTwigComponent; use Symfony\UX\TwigComponent\Attribute\ExposeInTemplate; -#[AsTwigComponent('Mezcalito:File', template: '@MezcalitoFileManager/components/file.html.twig')] +#[AsTwigComponent] class File { use FilesystemToolsTrait; diff --git a/src/Twig/Components/FileManager.php b/src/Twig/Components/FileSystem.php similarity index 83% rename from src/Twig/Components/FileManager.php rename to src/Twig/Components/FileSystem.php index dfbd9c7..c4e7d7d 100644 --- a/src/Twig/Components/FileManager.php +++ b/src/Twig/Components/FileSystem.php @@ -18,8 +18,8 @@ use Symfony\UX\LiveComponent\Attribute\AsLiveComponent; use Symfony\UX\LiveComponent\DefaultActionTrait; -#[AsLiveComponent('Mezcalito:FileManager', template: '@MezcalitoFileManager/components/file_manager.html.twig')] -class FileManager +#[AsLiveComponent] +class FileSystem { use DefaultActionTrait; use FilesystemContextTrait; diff --git a/src/Twig/Components/Folder.php b/src/Twig/Components/Folder.php index ef4ee92..19e579f 100644 --- a/src/Twig/Components/Folder.php +++ b/src/Twig/Components/Folder.php @@ -18,7 +18,7 @@ use Symfony\UX\TwigComponent\Attribute\AsTwigComponent; use Symfony\UX\TwigComponent\Attribute\ExposeInTemplate; -#[AsTwigComponent('Mezcalito:Folder', template: '@MezcalitoFileManager/components/folder.html.twig')] +#[AsTwigComponent] class Folder { use FilesystemToolsTrait; diff --git a/src/Twig/Components/Modal.php b/src/Twig/Components/Modal.php index 433122d..5fa5520 100644 --- a/src/Twig/Components/Modal.php +++ b/src/Twig/Components/Modal.php @@ -27,7 +27,7 @@ use Symfony\UX\LiveComponent\DefaultActionTrait; use Symfony\UX\TwigComponent\Attribute\ExposeInTemplate; -#[AsLiveComponent('Mezcalito:Modal', template: '@MezcalitoFileManager/components/modal.html.twig')] +#[AsLiveComponent] class Modal { use ComponentToolsTrait; diff --git a/src/Twig/Components/Sidebar.php b/src/Twig/Components/Sidebar.php index 2c3237b..e631ae4 100644 --- a/src/Twig/Components/Sidebar.php +++ b/src/Twig/Components/Sidebar.php @@ -20,7 +20,7 @@ use Symfony\UX\LiveComponent\DefaultActionTrait; use Symfony\UX\TwigComponent\Attribute\ExposeInTemplate; -#[AsLiveComponent('Mezcalito:Sidebar', template: '@MezcalitoFileManager/components/sidebar.html.twig')] +#[AsLiveComponent] class Sidebar { use DefaultActionTrait; diff --git a/templates/components/content.html.twig b/templates/components/Content.html.twig similarity index 99% rename from templates/components/content.html.twig rename to templates/components/Content.html.twig index be9bfba..9cbcf45 100644 --- a/templates/components/content.html.twig +++ b/templates/components/Content.html.twig @@ -130,9 +130,9 @@