Skip to content

Commit

Permalink
feat(deps): doctrine & twig optional deps
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Nek- committed Aug 19, 2020
1 parent 688fae8 commit 38108f0
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 16 deletions.
3 changes: 3 additions & 0 deletions docs/crud-controllers.md
Original file line number Diff line number Diff line change
@@ -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 |
Expand Down
2 changes: 2 additions & 0 deletions docs/getting-started.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
------------------------

Expand Down
12 changes: 12 additions & 0 deletions src/DependencyInjection/MelodiiaExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@

namespace SwagIndustries\Melodiia\DependencyInjection;

use Doctrine\Persistence\AbstractManagerRegistry;
use SwagIndustries\Melodiia\Crud\FilterInterface;
use SwagIndustries\Melodiia\Serialization\Context\ContextBuilderInterface;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\DependencyInjection\ContainerBuilder;
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
{
Expand All @@ -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);

Expand Down
12 changes: 4 additions & 8 deletions src/Resources/config/crud.yaml
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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'
Expand All @@ -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'
Expand All @@ -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'
Expand All @@ -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'
Expand Down
7 changes: 7 additions & 0 deletions src/Resources/config/doctrine.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
services:
melodiia.doctrine.data_provider:
class: SwagIndustries\Melodiia\Doctrine\DoctrineDataStore
arguments:
$registry: '@doctrine'

melodiia.data_provider: '@melodiia.doctrine.data_provider'
8 changes: 0 additions & 8 deletions src/Resources/config/services.yaml
Original file line number Diff line number Diff line change
@@ -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:
Expand Down Expand Up @@ -43,5 +36,4 @@ services:
$builders: !tagged_iterator melodiia.context_builder

imports:
- 'crud.yaml'
- 'form.yaml'
6 changes: 6 additions & 0 deletions src/Resources/config/twig.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
services:
melodiia.documentation:
class: SwagIndustries\Melodiia\Documentation\Controller\SwaggerUiController
tags: ['controller.service_arguments']
arguments:
$templating: '@twig'

0 comments on commit 38108f0

Please sign in to comment.