awwar / symfony-http-entity-manager
类似于Doctrine的http客户端
2.0.0-rc
2023-10-28 17:20 UTC
Requires
- php: ^8.0
- awwar/php-http-entity-manager: 1.0.0-rc
- symfony/config: ~5.4|^6.0
- symfony/dependency-injection: ~5.4|^6.0
- symfony/finder: ~5.4|^6.0
- symfony/http-client-contracts: ^2.0|^3.0
- symfony/http-kernel: ~5.4|^6.0
This package is not auto-updated.
Last update: 2024-09-28 22:51:37 UTC
README
一个库,允许您以与Symfony Doctrine ORM处理数据库相同的方式处理外部API。
例如
$users = $this->httpEntityManager->getRepository(User::class); $admins = $this->httpEntityManager->getRepository(Admin::class); $sasha = $users->find(124); $alex = $admins->filterOne(['filter' => ['specialization' => 'cinema']]); $deal = Deal::create($sasha, $alex, "buying a movie ticket", 30); $sasha->getDeals()->add($deal); $alex->setSpecialization('cinema'); $this->httpEntityManager->persist($deal); $this->httpEntityManager->flush();
真实请求
# get customer curl --request GET \ --url 'https://api.mysite.com/users/124' # get responsible curl --request GET \ --url 'https://api.mysite.com/admins?filter[specialization]=cinema' # create deal curl --request POST \ --url https://api.mysite.com/deals/ \ --data '{ "amount": 30, "title": "buying a movie ticket", "relations": [ {"name": "customer", "id": 124} {"name": "responsible", "id": 555} ] ' # add created deal to customer curl --request PATCH \ --url https://api.mysite.com/users/124 \ --data '{ "relations": [ {"name": "deal", "id": 322} ] '
正如我们所见,所有更改都按正确顺序应用,Alex的更改并未应用,因为我们没有更改他的专业化的值。这都归功于UnitOfWork,它跟踪更改并以正确的顺序执行它们,不允许可能影响系统性能的不必要更改。
看起来很简单?查看更多!
待办事项
- 实现除"json"之外格式的处理
- 在删除与实体相关的连接时,不会发送删除此关系的请求
- 延迟代理。如果有自定义实体,并且它有一个交易集合,要获取代理交易集合,至少需要传递它们的ID数组。然而,已知有这样的API,用户可以通过
GET /users/123
获取,其交易通过GET /users/123/deals
获取。为此,需要实现延迟代理。