loophp/repository-monadic-helper
Composer 安装命令:
composer require loophp/repository-monadic-helper
包简介
Monadic Doctrine repositories helper classes and services.
README 文档
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 ServiceEntityRepositorywithextends MonadicServiceEntityRepositoryand 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.
loophp/repository-monadic-helper 适用场景与选型建议
loophp/repository-monadic-helper 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 1.86k 次下载、GitHub Stars 达 17, 最近一次更新时间为 2021 年 12 月 03 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「doctrine」 「monad」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 loophp/repository-monadic-helper 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 loophp/repository-monadic-helper 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 loophp/repository-monadic-helper 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Option type (Some/None) replacing nullable types with explicit presence semantics
Result type capturing success or failure as a value for controlled error handling
A collection of functional programming data structures
Make pimcore migration simple
A functional programming library for PHP
Plugins for Tactician commands using Doctrine, like wrapping every command in a transaction
统计信息
- 总下载量: 1.86k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 17
- 点击次数: 14
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2021-12-03