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 27f2f2a
Show file tree
Hide file tree
Showing 16 changed files with 92 additions and 49 deletions.
39 changes: 38 additions & 1 deletion config/services.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,49 @@
namespace Symfony\Component\DependencyInjection\Loader\Configurator;

use Mezcalito\FileManagerBundle\Factory\FilesystemFactory;
use Mezcalito\FileManagerBundle\Twig\Components\Content;
use Mezcalito\FileManagerBundle\Twig\Components\File;
use Mezcalito\FileManagerBundle\Twig\Components\FileSystem;
use Mezcalito\FileManagerBundle\Twig\Components\Folder;
use Mezcalito\FileManagerBundle\Twig\Components\Modal;
use Mezcalito\FileManagerBundle\Twig\Components\Sidebar;

return static function (ContainerConfigurator $container) {
$container->services()
->set(FilesystemFactory::class)
->set('.mezcalito_file_manager.filesystem_factory', FilesystemFactory::class)
->args([
service('parameter_bag'),
])

->set(Content::class)
->tag('controller.service_arguments')
->tag('twig.component', ['expose_public_props' => true, 'live' => true, 'csrf' => true, 'route' => 'ux_live_component', 'method' => 'post', 'url_reference_type' => 1])
->call('setFilesystemFactory', [service('.mezcalito_file_manager.filesystem_factory')])

->set(File::class)
->tag('twig.component', ['expose_public_props' => true])
->call('setFilesystemFactory', [service('.mezcalito_file_manager.filesystem_factory')])

->set(FileSystem::class)
->tag('controller.service_arguments')
->tag('twig.component', ['expose_public_props' => true, 'live' => true, 'csrf' => true, 'route' => 'ux_live_component', 'method' => 'post', 'url_reference_type' => 1])
->call('setFilesystemFactory', [service('.mezcalito_file_manager.filesystem_factory')])

->set(Folder::class)
->tag('twig.component', ['expose_public_props' => true])
->call('setFilesystemFactory', [service('.mezcalito_file_manager.filesystem_factory')])

->set(Modal::class)
->tag('controller.service_arguments')
->tag('twig.component', ['expose_public_props' => true, 'live' => true, 'csrf' => true, 'route' => 'ux_live_component', 'method' => 'post', 'url_reference_type' => 1])
->call('setFilesystemFactory', [service('.mezcalito_file_manager.filesystem_factory')])
->args([
service('translator')->ignoreOnInvalid()
])

->set(Sidebar::class)
->tag('controller.service_arguments')
->tag('twig.component', ['expose_public_props' => true, 'live' => true, 'csrf' => true, 'route' => 'ux_live_component', 'method' => 'post', 'url_reference_type' => 1])
->call('setFilesystemFactory', [service('.mezcalito_file_manager.filesystem_factory')])
;
};
68 changes: 37 additions & 31 deletions src/DependencyInjection/MezcalitoFileManagerExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,14 @@
namespace Mezcalito\FileManagerBundle\DependencyInjection;

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\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 @@ -51,32 +45,44 @@ public function load(array $configs, ContainerBuilder $container): void

$container->registerForAutoconfiguration(ConfiguratorInterface::class)
->addTag('mezcalito_file_manager.configurator');
}

$container->register(FileManager::class)
->setAutowired(true)
->setAutoconfigured(true);

$container->register(Sidebar::class)
->setAutowired(true)
->setAutoconfigured(true);
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->register(Content::class)
->setAutowired(true)
->setAutoconfigured(true);
$container->prependExtensionConfig('framework', [
'asset_mapper' => [
'paths' => [
__DIR__.'/../../assets/dist' => '@mezcalito/ux-filemanager',
],
],
]);
}

$container->register(File::class)
->setAutowired(true)
->setAutoconfigured(true);
private function isAssetMapperAvailable(ContainerBuilder $container): bool
{
if (!interface_exists(AssetMapperInterface::class)) {
return false;
}

$container->register(Folder::class)
->setAutowired(true)
->setAutoconfigured(true);
// check that FrameworkBundle 6.3 or higher is installed
$bundlesMetadata = $container->getParameter('kernel.bundles_metadata');
if (!isset($bundlesMetadata['FrameworkBundle'])) {
return false;
}

$container->register(Modal::class)
->setAutowired(true)
->setAutoconfigured(true)
->setArguments([
new Reference('translator', ContainerInterface::IGNORE_ON_INVALID_REFERENCE),
]);
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 27f2f2a

Please sign in to comment.