UseCaseBundle
, (*1)
UseCaseBundle provides OpenClassrooms\UseCase Library in a Symfony2 context.
UseCase Library provides facilities to manage technical code over a Use Case in a Clean / Hexagonal / Use Case Architecture., (*2)
- Security access
- Cache management
- Transactional context
- Events
The goal is to have only functional code on the Use Case and manage technical code in an elegant way using annotations., (*3)
For usage of UseCase Library, please see the UseCase Library documentation., (*4)
Installation
This bundle can be installed using composer:, (*5)
composer require openclassrooms/use-case-bundle
or by adding the package to the composer.json file directly., (*6)
{
"require": {
"openclassrooms/use-case-bundle": "*"
}
}
After the package has been installed, add the bundle to the AppKernel.php file:, (*7)
// in AppKernel::registerBundles()
$bundles = array(
// ...
new OpenClassrooms\Bundle\OpenClassroomsUseCaseBundle(),
// ...
);
If cache facilities are needed, add the OpenClassrooms\CacheBundle to the AppKernel.php file:, (*8)
// in AppKernel::registerBundles()
$bundles = array(
// ...
new OpenClassrooms\Bundle\CacheBundle\OpenClassroomsCacheBundle(),
new OpenClassrooms\Bundle\UseCaseBundle\OpenClassroomsUseCaseBundle(),
// ...
);
Configuration
UseCaseBundle requires no initial configuration., (*9)
This is the default configuration:, (*10)
# app/config/config.yml
openclassrooms_use_case:
security: security_context
# an implementation of OpenClassrooms\UseCase\Application\Services\Security\Security
transaction: doctrine.orm.entity_manager
# an implementation of EntityManagerInterface or OpenClassrooms\UseCase\Application\Services\Transaction\Transaction
event_sender: event_dispatcher
# an implementation of EventDispatcherInterface or OpenClassrooms\UseCase\Application\Services\Event\EventSender
event_factory: openclassrooms.use_case.event_factory
# an implementation of OpenClassrooms\UseCase\Application\Services\Event\EventFactory
If cache facilities are needed, CacheBundle configuration MUST be set. See documentation for more details., (*11)
Furthermore, only needed services are used. It means, for example, if only security is used, the others services will never be called. Even if the services of the default configuration exist or not., (*12)
Usage
For usage of UseCase Library, please see the UseCase Library documentation., (*13)
Add the tag openclassrooms.use_case
to the use case declaration to enable UseCase Library facilities., (*14)
Resources/config/services.xml
<container xmlns="http://symfony.com/schema/dic/services"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
<parameters>
<parameter key="a_project.a_use_case.class">AProject\BusinessRules\UseCases\AUseCase</parameter>
</parameters>
<services>
<service id="a_project.a_use_case" class="a_project.a_use_case.class">
<tag name="openclassrooms.use_case"/>
</service>
</services>
</container>
The different services used are those defined in the configuration file.
For each tag and each facility, a specific service can be set:, (*15)
<service id="a_project.a_use_case" class="a_project.a_use_case.class">
<tag name="openclassrooms.use_case"
security="a.different.security_context"
cache="a.different.cache"
transaction="a.different.entity_manager"
event-sender="a.different.event_dipsatcher"
event-factory="a.different.event_factory"/>
</service>
-
security parameter MUST be an implementation of OpenClassrooms\UseCase\Application\Services\Security\Security
-
cache parameter MUST be an implementation of OpenClassrooms\Cache\Cache\Cache
-
transaction parameter MUST be an implementation of EntityManagerInterface or OpenClassrooms\UseCase\Application\Services\Transaction\Transaction
-
event-sender parameter MUST be an implementation of EventDispatcherInterface or OpenClassrooms\UseCase\Application\Services\Event\EventSender
-
event-factory parameter MUST be an implementation of OpenClassrooms\UseCase\Application\Services\Event\EventFactory