Skip to content

Commit

Permalink
Dependency injection rework
Browse files Browse the repository at this point in the history
  • Loading branch information
loicsapone committed Aug 12, 2024
1 parent 9544a2d commit 9cfe65b
Show file tree
Hide file tree
Showing 15 changed files with 61 additions and 20 deletions.
47 changes: 44 additions & 3 deletions src/DependencyInjection/MezcalitoFileManagerExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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);

Expand All @@ -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');
}
}
2 changes: 1 addition & 1 deletion src/Twig/Components/Content.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion src/Twig/Components/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion src/Twig/Components/Folder.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion src/Twig/Components/Modal.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion src/Twig/Components/Sidebar.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,9 +130,9 @@
<div class="fm-c-content__cards" data-mezcalito--ux-filemanager--filemanager-target="list">
{% for node in content %}
{% if node.isDir() %}
<twig:Mezcalito:Folder :storage="storage" :id="node.id"/>
<twig:Mezcalito:FileManager:Folder :storage="storage" :id="node.id"/>
{% else %}
<twig:Mezcalito:File :storage="storage" :id="node.id"/>
<twig:Mezcalito:FileManager:File :storage="storage" :id="node.id"/>
{% endif %}
{% endfor %}
</div>
Expand Down
File renamed without changes.
7 changes: 7 additions & 0 deletions templates/components/FileSystem.html.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{# @var filesystem \Mezcalito\FileManagerBundle\Filesystem\Filesystem #}

<div {{ attributes.defaults({class: 'fm-c-layout', 'data-controller': 'mezcalito--ux-filemanager--filemanager'}) }}>
<twig:Mezcalito:FileManager:Sidebar :storage="storage" loading="defer"/>
<twig:Mezcalito:FileManager:Content :storage="storage" loading="defer"/>
<twig:Mezcalito:FileManager:Modal :storage="storage"/>
</div>
File renamed without changes.
File renamed without changes.
File renamed without changes.
7 changes: 0 additions & 7 deletions templates/components/file_manager.html.twig

This file was deleted.

2 changes: 1 addition & 1 deletion tests/TestApplication/templates/filemanager.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@
</head>

<body>
<twig:Mezcalito:FileManager storage="local"/>
<twig:Mezcalito:FileManager:FileSystem storage="local"/>
</body>
</html>

0 comments on commit 9cfe65b

Please sign in to comment.