From 38108f0624acf90a788abde6e0ecea4057962548 Mon Sep 17 00:00:00 2001 From: "Nek (Maxime Veber)" Date: Wed, 19 Aug 2020 19:16:45 +0200 Subject: [PATCH] feat(deps): doctrine & twig optional deps Problem: while trying to make a recipe I figure out that doctrine and twig were hard requirements while I do not want them to be. Solution: I splitted services definition in many files and load them only if dependencies are present. --- docs/crud-controllers.md | 3 +++ docs/getting-started.md | 2 ++ src/DependencyInjection/MelodiiaExtension.php | 12 ++++++++++++ src/Resources/config/crud.yaml | 12 ++++-------- src/Resources/config/doctrine.yaml | 7 +++++++ src/Resources/config/services.yaml | 8 -------- src/Resources/config/twig.yaml | 6 ++++++ 7 files changed, 34 insertions(+), 16 deletions(-) create mode 100644 src/Resources/config/doctrine.yaml create mode 100644 src/Resources/config/twig.yaml diff --git a/docs/crud-controllers.md b/docs/crud-controllers.md index 518c606..492a8ff 100644 --- a/docs/crud-controllers.md +++ b/docs/crud-controllers.md @@ -1,6 +1,9 @@ About CRUD Controllers ====================== +⚠️ By default Melodiia provides a doctrine integration. But if you didn't install Doctrine, it will not register +CRUD controllers. Be sure Doctrine is installed. + Here are all the controllers for your CRUD. | CRUD action | Service name | diff --git a/docs/getting-started.md b/docs/getting-started.md index 6a73a17..8a9b3a9 100644 --- a/docs/getting-started.md +++ b/docs/getting-started.md @@ -125,6 +125,8 @@ documentation: documentation_file_path: '%kernel.project_dir%/config/documentation.yaml' ``` +⚠️ The render of the documentation cannot work without Twig. Be sure twig is installed. + Step 5: do what you want ------------------------ diff --git a/src/DependencyInjection/MelodiiaExtension.php b/src/DependencyInjection/MelodiiaExtension.php index 39bda9b..332dc05 100644 --- a/src/DependencyInjection/MelodiiaExtension.php +++ b/src/DependencyInjection/MelodiiaExtension.php @@ -4,6 +4,7 @@ namespace SwagIndustries\Melodiia\DependencyInjection; +use Doctrine\Persistence\AbstractManagerRegistry; use SwagIndustries\Melodiia\Crud\FilterInterface; use SwagIndustries\Melodiia\Serialization\Context\ContextBuilderInterface; use Symfony\Component\Config\FileLocator; @@ -11,6 +12,7 @@ use Symfony\Component\DependencyInjection\Extension\Extension; use Symfony\Component\DependencyInjection\Loader\XmlFileLoader; use Symfony\Component\DependencyInjection\Loader\YamlFileLoader; +use Twig\Environment; class MelodiiaExtension extends Extension { @@ -25,6 +27,16 @@ public function load(array $configs, ContainerBuilder $container) $xmlLoader = new XmlFileLoader($container, $configFileLocator); $xmlLoader->load('error-management.xml'); + if (class_exists(AbstractManagerRegistry::class)) { + $loader->load('doctrine.yaml'); + } + if ($container->hasAlias('melodiia.data_provider')) { + $loader->load('crud.yaml'); + } + if (class_exists(Environment::class)) { + $loader->load('twig.yaml'); + } + $configuration = new Configuration(); $config = $this->processConfiguration($configuration, $configs); diff --git a/src/Resources/config/crud.yaml b/src/Resources/config/crud.yaml index 1d7b7b2..0c426c6 100644 --- a/src/Resources/config/crud.yaml +++ b/src/Resources/config/crud.yaml @@ -1,8 +1,4 @@ services: - melodiia.doctrine.data_provider: - class: SwagIndustries\Melodiia\Doctrine\DoctrineDataStore - arguments: - $registry: '@doctrine' melodiia.crud.id_resolver: class: SwagIndustries\Melodiia\Crud\Tools\SimpleIdResolver @@ -24,7 +20,7 @@ services: melodiia.crud.controller.create: class: SwagIndustries\Melodiia\Crud\Controller\Create arguments: - $dataStore: '@melodiia.doctrine.data_provider' + $dataStore: '@melodiia.data_provider' $formFactory: '@form.factory' $dispatcher: '@event_dispatcher' $checker: '@?security.authorization_checker' @@ -33,7 +29,7 @@ services: melodiia.crud.controller.update: class: SwagIndustries\Melodiia\Crud\Controller\Update arguments: - $dataStore: '@melodiia.doctrine.data_provider' + $dataStore: '@melodiia.data_provider' $formFactory: '@form.factory' $dispatcher: '@event_dispatcher' $idResolver: '@melodiia.crud.id_resolver' @@ -43,7 +39,7 @@ services: melodiia.crud.controller.get_all: class: SwagIndustries\Melodiia\Crud\Controller\GetAll arguments: - $dataStore: '@melodiia.doctrine.data_provider' + $dataStore: '@melodiia.data_provider' $collectionFactory: '@melodiia.crud.filters.filter_collection_factory' $pagesRequestFactory: '@SwagIndustries\Melodiia\Crud\Pagination\PaginationRequestFactoryInterface' $checker: '@?security.authorization_checker' @@ -60,7 +56,7 @@ services: melodiia.crud.controller.delete: class: SwagIndustries\Melodiia\Crud\Controller\Delete arguments: - $dataStore: '@melodiia.doctrine.data_provider' + $dataStore: '@melodiia.data_provider' $dispatcher: '@event_dispatcher' $idResolver: '@melodiia.crud.id_resolver' $checker: '@?security.authorization_checker' diff --git a/src/Resources/config/doctrine.yaml b/src/Resources/config/doctrine.yaml new file mode 100644 index 0000000..5403488 --- /dev/null +++ b/src/Resources/config/doctrine.yaml @@ -0,0 +1,7 @@ +services: + melodiia.doctrine.data_provider: + class: SwagIndustries\Melodiia\Doctrine\DoctrineDataStore + arguments: + $registry: '@doctrine' + + melodiia.data_provider: '@melodiia.doctrine.data_provider' diff --git a/src/Resources/config/services.yaml b/src/Resources/config/services.yaml index 8361ca6..6921ac2 100644 --- a/src/Resources/config/services.yaml +++ b/src/Resources/config/services.yaml @@ -1,16 +1,9 @@ services: - melodiia.documentation: - class: SwagIndustries\Melodiia\Documentation\Controller\SwaggerUiController - tags: ['controller.service_arguments'] - arguments: - $templating: '@twig' - melodiia.configuration: class: SwagIndustries\Melodiia\MelodiiaConfiguration arguments: $config: '%melodiia.config%' - # Priority -1 is DX. It allow the user to implement its own normalizers # that will take over those. melodiia.serialization.created_normalizer: @@ -43,5 +36,4 @@ services: $builders: !tagged_iterator melodiia.context_builder imports: - - 'crud.yaml' - 'form.yaml' diff --git a/src/Resources/config/twig.yaml b/src/Resources/config/twig.yaml new file mode 100644 index 0000000..cee4019 --- /dev/null +++ b/src/Resources/config/twig.yaml @@ -0,0 +1,6 @@ +services: + melodiia.documentation: + class: SwagIndustries\Melodiia\Documentation\Controller\SwaggerUiController + tags: ['controller.service_arguments'] + arguments: + $templating: '@twig'