aeq/hal
Composer 安装命令:
composer require aeq/hal
包简介
README 文档
README
#PHP HAL (Hypertext Application Language) Explorer#
This library provides a full featured API to discover a HAL (Hypertext Application Language) API via an expressive interface.
$posts = $resource->getLink('posts')->follow();
Features
- GuzzleHttp as default HTTP Client
- integrate custom HTTP clients easily
- human readable API
- Shipped with a simple default serializer for JSON responses
- Possibility to integrate custom serializers
- Fully tested
- Used in production projects
##Usage##
###1. Initiate the Explorer###
$explorer = new Explorer(); $explorer->setClientAdapter(new GuzzleClientAdapter(new Client())); $explorer->setSerializer(new JsonSerializer());
If you want to use the the build in "GuzzleClientAdapter" you have to require guzzlehttp/guzzle manually in your composer project since guzzle is only suggested.
###2. Request your API###
$response = $explorer->request('GET', '/my/api/');
###3. Explore the response###
$resource = $explorer->explore($response);
###4. Discover your API###
$posts = $resource->getLink('posts')->follow(); foreach($posts as $post) { $publisher = $post->getLink('publisher')->follow(); $company = $publisher->getLink('company')->follow(); }
The result of following a link is that the resolved resource is appended to the parent „$resource“ object („_embedded“). This allows you to access the linked resource without resolving the link again:
$posts = $resource->getEmbedded('posts'); foreach($posts as $post) { $publisher = $post->getEmbedded('publisher'); $company = $publisher->getEmbedded('company'); }
##Custom HTTP Clients## Create a new class implementing the "ClientAdapterInterface" and return the response as PSR-7.
use Aeq\Hal\Client\ClientAdapterInterface; class MyCustomClientAdapter implements ClientAdapterInterface { public function request($method, $uri = null, array $options = []) { // call your custom client and return the response } }
Set the custom client adapter to your explorer before using it.
$explorer->setClientAdapter(new MyCustomClientAdapter());
##Custom Serializers## Create a new class implementing the "SerializerInterface" and return the response.
use Aeq\Hal\Serializer\SerializerInterface; class MyCustomSerializer implements SerializerInterface { public function serialize($data) { // serialize your data: $data } public function deserialize($str) { // deserialize the string: $str } }
Set the custom serializer to your explorer before using it.
$explorer->setSerializer(new MyCustomSerializer());
##Events## This library offers you some events to listen on. To use the event system you have to add the "EventManager" to your Explorer before using it.
$explorer->setEventManager(new EventManager());
To listen on a specific event you have to implement a Listener. A Listener is a PHP object with a public method "handle". As parameter your will get the triggered Event object.
class MyHandler { public function handle(EventInterface $event) { // process your stuff } }
###PostClientRequestEvent### This event is called on each executed request (after following a link or using the "request" method) and contains the complete PSR-7 response.
$explorer->listenOnEvent(PostClientRequestEvent::class, new MyHandler());
aeq/hal 适用场景与选型建议
aeq/hal 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 14.37k 次下载、GitHub Stars 达 2, 最近一次更新时间为 2016 年 04 月 18 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 aeq/hal 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 aeq/hal 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 14.37k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 2
- 点击次数: 0
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: Unknown
- 更新时间: 2016-04-18