stormsys/simplehal 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

stormsys/simplehal

Composer 安装命令:

composer require stormsys/simplehal

包简介

SimpleHal is an easy to use library for consuming Hal API's

README 文档

README

Total Downloads

SimpleHal is an easy to use library for consuming Hal API's.

Installation using Composer

Add the dependency:

composer require stormsys/simplehal

Limitations

Currently the library supports only GET requests, there are plans to add support for PUT, POST and DELETE in the future.

Usage

Setup Root Resource

This examples shows how to setup a root resource to begin nagivation.

<?php

use Guzzle\Http\Client;
use Stormsys\SimpleHal\Clients\GuzzleHalClient;
use Stormsys\SimpleHal\Resource;
use Stormsys\SimpleHal\Uri\GuzzleUriTemplateProcessor;
use Stormsys\SimpleHal\Uri\LeagueUriJoiner;

$client = new GuzzleHalClient(new Client());
$uriTemplateProcessor = new GuzzleUriTemplateProcessor();
$uriJoiner = new LeagueUriJoiner();
$apiRootUrl = 'http://haltalk.herokuapp.com';

$root = new Resource($client, $uriTemplateProcessor, $uriJoiner, $apiRootUrl);

Following Non-Templated Links

once you have obtained a resource, SimpleHal offers several ways to follow links.

$latestPosts = $root->follow('ht:latest-posts');

Simple hal offers powerful php overloading mechanisms(see note on magic methods below) for making the api more fluent, the line above can also be represented as such:

$latestPosts = $root->{'ht:latest-posts'};
$latestPosts = $root->{'ht:latest-posts'}();

Following Templated Links

Similar to the above, you can follow templated links by providing the template variables like so.

$john = $root->follow('ht:me', ['name' => 'john']);

and again with the overload(see note on magic methods below).

$john = $root->{'ht:me'}(['name' => 'john']);

Reading Embedded Resources

Sometimes partial, incomplete or full resources are embedded into the hal document, these are accessible using the embedded function like so.

$postsArray = $latestPosts->embedded('ht:post');

Magic acessors are also avaliable for embedded resources (see magic methods below).

$postsArray = $latestPosts->{'ht:post'};
$postsArray = $latestPosts->{'ht:post'}();
Reading Resource Properties

To access properties of a resource, you can use the prop method shown below.

$johnsRealName = $john->prop('real_name');

As eith following relations and embedded resources, magic acessors are avalible for properties (see magic methods below).

below are all equivlant ways to access the property.

$johnsRealName = $john->real_name;
$johnsRealName = $john->{'real_name'};
$johnsRealName = $john->{'real_name'}();
$johnsRealName = $john->real_name();

Refresh / Obtain Full Representations

You can update or refresh resources that you have already loaded by calling refresh()

$latestPosts = $latestPosts->refresh();

or in the event that you are using a embedded partial resource as long as a self link is present you can use, this is just an alias for ->refresh().

$firstPost = $postsArray[0]->full();

Chain Example

The example below shows how you might chain methods to obtain some data on a hal api.

$postBody = $root->{'ht:latest-posts'}->{'ht:post'}[0]->content;
$postBody = $root->follow('ht:latest-posts')->{'ht:post'}[0]->content;
$postBody = $root->follow('ht:latest-posts')->embedded('ht:post')[0]->content;
$postBody = $root->follow('ht:latest-posts')->embedded('ht:post')[0]->prop('content');

Magic Methods

You can access embedded resources, follow links and access proprties through the magic acessors and method overloads. the name of the field/method will be equal to the relation or the property name.

the order in which SimpleHal will attempt to resolve the request is:

  • Embedded Resources by Link Relation
  • Follow link to obtain Resource if link relation exists
  • Return property of resource
  • if none of the above, null

is it impoarant to understand the order which requests are resolved as the library will pick the first one that is found, in the event of duplicates an embedded resource will be picked over the others, and following a link before a property.

Interfaces

The library offer the following interfaces which can be custom implemented.

  • Stormsys\SimpleHal\Uri\UriTemplateProcessorInterface
  • Stormsys\SimpleHal\Uri\UriJoinerInterface
  • Stormsys\SimpleHal\Clients\HalClientInterface

by default the library has bundled the following implementations:

  • Stormsys\SimpleHal\Uri\GuzzleUriTemplateProcessor
  • Stormsys\SimpleHal\Uri\LeagueUriJoiner
  • Stormsys\SimpleHal\Clients\GuzzleHalClient
  • Stormsys\SimpleHal\Clients\FileGetContentsHalClient

TODO

  • Add Tests
  • Support for Persist (POST/PUT)
  • Support for Delete

stormsys/simplehal 适用场景与选型建议

stormsys/simplehal 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 188 次下载、GitHub Stars 达 7, 最近一次更新时间为 2015 年 02 月 15 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「hal」 「hypermedia」 「hal client」 「HalClient」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 stormsys/simplehal 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 stormsys/simplehal 我们能提供哪些服务?
定制开发 / 二次开发

基于 stormsys/simplehal 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 188
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 7
  • 点击次数: 10
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 7
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2015-02-15