loophp / repository-monadic-helper
Monadic Doctrine repositories helper classes and services.
Fund package maintenance!
drupol
Requires
- php: >= 8.1
- marcosh/lamphpda: ^1
Requires (Dev)
- doctrine/doctrine-bundle: ^2.6
- doctrine/orm: ^2.12
- drupol/php-conventions: ^5
- phpunit/phpunit: ^10
README
Doctrine Repository Monadic Helper
Description
This project provides the necessary classes and services to use Doctrine repositories in a more functional way, by using monads.
This project also demonstrate that it's a nice and clean way to work with repositories and non-deterministic data store, in this case, a database.
There is no need to always check for the existence of an entity, so we are able to reduce the amount of conditions and cruft, while focusing on what's important and relevant only.
When using properly typed monads and callbacks, types inconsistencies will be instantly detected by static analysis tools. This provides a safer and better way to design functions and data transformation methods.
The monad in use in this project is the Either monad, provided by the contrib package Lamphpda from Marco Perone.
History
This project started as a proof-of-concept for my own needs and for many reasons.
I first wanted to get rid of all the checks and conditions that were needed in my code to test if an entity was existing or not.
Then, it turns out that this was a recurrent pattern that I was seeing the code of my friends, colleagues and issue queue.
A practical way to get rid of conditions is to use a more declarative way of programming and adopt a more functional programming style.
And lastly, willing to learn more about the monads which are some kind of "design patterns" for functional programming, I started to write this package.
This package does not have the pretention to become a de-facto standard on how to use Doctrine repositories, but it might help people understanding what monads are, how to use them, and hopefully reduce the amount of conditions in their code.
The monad package in use here is an arbitrary choice. I could have used some other packages, but marcosh/lamphpda seems to be the best option, especially when you analyse your code with static analysis tools to detect issues upfront, without running their unit tests.
Installation
composer require loophp/repository-monadic-helper
Usage
To use this package and have monadic repositories, there are 3 options available:
- Without alteration of existing Doctrine repositories
- Options 1: By using a service which is creating a monadic repository from an entity class name or an existing repository.
- With alteration of existing Doctrine repositories
- Option 2: By adding an interface, a trait and relevant typing
information like:
@implements MonadicServiceEntityRepositoryInterface<EntityClassName>
- Option 3: By replacing
extends ServiceEntityRepository
withextends MonadicServiceEntityRepository
and add relevant typing information like:@extends MonadicServiceEntityRepository<EntityClassName>
- Option 2: By adding an interface, a trait and relevant typing
information like:
In my own opinion, the best way to use this package is to use the first option.
It's paramount to replace EntityClassName
with the entity class in use in the
repository in order to let static analysis tools infer types properly.
For the option 2 and 3, let's use the following usual Doctrine repository as example:
<?php declare(strict_types=1); namespace App\Repository; use App\Entity\MyCustomEntity; use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository; use Doctrine\Persistence\ManagerRegistry; /** * @method MyCustomEntity|null find($id, $lockMode = null, $lockVersion = null) * @method MyCustomEntity|null findOneBy(array $criteria, array $orderBy = null) * @method MyCustomEntity[] findAll() * @method MyCustomEntity[] findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null) */ class MyCustomEntityRepository extends ServiceEntityRepository { public function __construct(ManagerRegistry $registry) { parent::__construct($registry, MyCustomEntity::class); } }
Option 1: Using a service
<?php declare(strict_types=1); namespace App\Controller; use App\Entity\MyCustomEntity; use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository; use Doctrine\Persistence\ManagerRegistry; use loophp\RepositoryMonadicHelper\MonadicRepositoryFactoryInterface; use Throwable; final class MyCustomController { public function __invoke( MonadicRepositoryFactoryInterface $monadicRepositoryFactory ) { $body = $monadicRepositoryFactory ->fromEntity(MyCustomEntity::class) ->eitherFind(123) // This returns a Either monad. ->map( static fn (MyCustomEntity $entity): string => $entity->getTitle() ) ->eval( static fn (Throwable $exception): string => $exception->getMessage(), static fn (string $entity): string => $entity ); return new Response($body); } }
Option 2: Using an interface and a trait on existing repositories
Upgrade your Doctrine repositories to:
<?php declare(strict_types=1); namespace App\Repository; use App\Entity\MyCustomEntity; use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository; use Doctrine\Persistence\ManagerRegistry; use loophp\RepositoryMonadicHelper\Doctrine\MonadicServiceEntityRepositoryInterface; use loophp\RepositoryMonadicHelper\MonadicServiceEntityRepositoryTrait; use Throwable; /** * @implements MonadicServiceEntityRepositoryInterface<Throwable, MyCustomEntity> */ class MyCustomEntityRepository extends ServiceEntityRepository implements MonadicServiceEntityRepositoryInterface { Use MonadicServiceEntityRepositoryTrait; public function __construct(ManagerRegistry $registry) { parent::__construct($registry, MyCustomEntity::class); } }
Option 3: Replace ServiceEntityRepository
with MonadicServiceEntityRepository
Upgrade your Doctrine repositories to:
<?php declare(strict_types=1); namespace App\Repository; use App\Entity\MyCustomEntity; use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository; use Doctrine\Persistence\ManagerRegistry; use loophp\RepositoryMonadicHelper\MonadicServiceEntityRepository; /** * @extends MonadicServiceEntityRepository<MyCustomEntity> */ class MyCustomEntityRepository extends MonadicServiceEntityRepository { public function __construct(ManagerRegistry $registry) { parent::__construct($registry, MyCustomEntity::class); } }
API
The following methods will be available when using the service (option 1) or upgrading your repositories (options 2 and 3).
For each API methods, the placeholder MyCustomEntity
should be replaced by the
entity you're referring to.
eitherFind
Signature:
eitherFind(int|string $id): Either<Throwable, MyCustomEntity>
An exception is generated and wrapped in the monad when the returned result is
null
.
eitherFindAll
Signature:
eitherFindAll(): Either<Throwable, list<MyCustomEntity>>
An exception is generated and wrapped in the monad when the returned result is empty.
eitherFindBy
Signature:
eitherFindBy(array $criteria, ?array $orderBy = null, ?int $limit = null, ?int $offset = null): Either<Throwable, list<MyCustomEntity>>
An exception is generated and wrapped in the monad when the returned result is empty.
eitherFindOneBy
Signature:
eitherFindOneBy(array $criteria): Either<Throwable, MyCustomEntity>
An exception is generated and wrapped in the monad when the returned result is
null
.
Todo
- Improve documentation and code examples.
Contributing
Feel free to contribute by sending pull requests. We are a usually very responsive team and we will help you going through your pull request from the beginning to the end.
For some reasons, if you can't contribute to the code and willing to help, sponsoring is a good, sound and safe way to show us some gratitude for the hours we invested in this package.
Sponsor me on Github and/or any of the contributors.
Changelog
See CHANGELOG.md for a changelog based on git commits.
For more detailed changelogs, please check the release changelogs.