chore: 跟踪范围改为黑名单,仅排除 node_modules/tmp/本地脚本/敏感配置

This commit is contained in:
liufumingren 2026-10-08 16:35:38 +08:00
parent 68df579a82
commit 29e149f777
3034 changed files with 536753 additions and 6 deletions

11
.bowerrc Normal file
View File

@ -0,0 +1,11 @@
{
"directory": "public/assets/libs",
"ignoredDependencies": [
"es6-promise",
"file-saver",
"html2canvas",
"jspdf",
"jspdf-autotable",
"pdfmake"
]
}

11
.env.sample Normal file
View File

@ -0,0 +1,11 @@
[app]
debug = false
trace = false
[database]
hostname = 127.0.0.1
database = fastadmin
username = root
password = root
hostport = 3306
prefix = fa_

18
.gitignore vendored
View File

@ -1,10 +1,16 @@
# ── 忽略根目录下所有文件/目录 ──
/*
# 例外:.gitignore 本身需要纳入版本控制
!/.gitignore
# ── 默认为黑名单:除下列内容外全部跟踪 ──
# ── 仅跟踪 application 目录 ──
!/application/
# ── 依赖与运行时残留(不入库、不部署) ──
/node_modules/
/tmp/
# ── 本地辅助脚本(不做 git 跟踪) ──
/scripts/
/package.json
/package-lock.json
# ── 环境变量(含密钥,只跟踪 .env.sample) ──
/.env
# ── application 中不跟踪的敏感配置 ──
/application/config.php

1
.htaccess Normal file
View File

@ -0,0 +1 @@

View File

@ -0,0 +1 @@
7nvtXkz8sBXPvilf8pgCFT92nB8BMSqJGE7QumvssGw.Uv4gmVNscdEPWr0XgQ5XqT5eSKxtyUHB8YlcxmaP-2A

View File

@ -0,0 +1 @@
202502140336431i4wbaezjofs55ioi2gzn12k1o2j93etxgaopxrsuat4j51epd

16
404.html Normal file
View File

@ -0,0 +1,16 @@
<html>
<style>
.btlink {
color: #20a53a;
text-decoration: none;
}
</style>
<meta charset="UTF-8">
<html>
<head><title>404 Not Found</title></head>
<body>
<center><h1>404 Not Found</h1></center>
<hr>
<div style="text-align: center;font-size: 15px" >Power by <a class="btlink" href="https://www.bt.cn/?from=404" target="_blank">堡塔 (免费,高效和安全的托管控制面板)</a></div>
</body>
</html>

191
LICENSE Normal file
View File

@ -0,0 +1,191 @@
Apache License
Version 2.0, January 2004
http://www.apache.org/licenses/
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
1. Definitions.
"License" shall mean the terms and conditions for use, reproduction, and
distribution as defined by Sections 1 through 9 of this document.
"Licensor" shall mean the copyright owner or entity authorized by the copyright
owner that is granting the License.
"Legal Entity" shall mean the union of the acting entity and all other entities
that control, are controlled by, or are under common control with that entity.
For the purposes of this definition, "control" means (i) the power, direct or
indirect, to cause the direction or management of such entity, whether by
contract or otherwise, or (ii) ownership of fifty percent (50%) or more of the
outstanding shares, or (iii) beneficial ownership of such entity.
"You" (or "Your") shall mean an individual or Legal Entity exercising
permissions granted by this License.
"Source" form shall mean the preferred form for making modifications, including
but not limited to software source code, documentation source, and configuration
files.
"Object" form shall mean any form resulting from mechanical transformation or
translation of a Source form, including but not limited to compiled object code,
generated documentation, and conversions to other media types.
"Work" shall mean the work of authorship, whether in Source or Object form, made
available under the License, as indicated by a copyright notice that is included
in or attached to the work (an example is provided in the Appendix below).
"Derivative Works" shall mean any work, whether in Source or Object form, that
is based on (or derived from) the Work and for which the editorial revisions,
annotations, elaborations, or other modifications represent, as a whole, an
original work of authorship. For the purposes of this License, Derivative Works
shall not include works that remain separable from, or merely link (or bind by
name) to the interfaces of, the Work and Derivative Works thereof.
"Contribution" shall mean any work of authorship, including the original version
of the Work and any modifications or additions to that Work or Derivative Works
thereof, that is intentionally submitted to Licensor for inclusion in the Work
by the copyright owner or by an individual or Legal Entity authorized to submit
on behalf of the copyright owner. For the purposes of this definition,
"submitted" means any form of electronic, verbal, or written communication sent
to the Licensor or its representatives, including but not limited to
communication on electronic mailing lists, source code control systems, and
issue tracking systems that are managed by, or on behalf of, the Licensor for
the purpose of discussing and improving the Work, but excluding communication
that is conspicuously marked or otherwise designated in writing by the copyright
owner as "Not a Contribution."
"Contributor" shall mean Licensor and any individual or Legal Entity on behalf
of whom a Contribution has been received by Licensor and subsequently
incorporated within the Work.
2. Grant of Copyright License.
Subject to the terms and conditions of this License, each Contributor hereby
grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free,
irrevocable copyright license to reproduce, prepare Derivative Works of,
publicly display, publicly perform, sublicense, and distribute the Work and such
Derivative Works in Source or Object form.
3. Grant of Patent License.
Subject to the terms and conditions of this License, each Contributor hereby
grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free,
irrevocable (except as stated in this section) patent license to make, have
made, use, offer to sell, sell, import, and otherwise transfer the Work, where
such license applies only to those patent claims licensable by such Contributor
that are necessarily infringed by their Contribution(s) alone or by combination
of their Contribution(s) with the Work to which such Contribution(s) was
submitted. If You institute patent litigation against any entity (including a
cross-claim or counterclaim in a lawsuit) alleging that the Work or a
Contribution incorporated within the Work constitutes direct or contributory
patent infringement, then any patent licenses granted to You under this License
for that Work shall terminate as of the date such litigation is filed.
4. Redistribution.
You may reproduce and distribute copies of the Work or Derivative Works thereof
in any medium, with or without modifications, and in Source or Object form,
provided that You meet the following conditions:
You must give any other recipients of the Work or Derivative Works a copy of
this License; and
You must cause any modified files to carry prominent notices stating that You
changed the files; and
You must retain, in the Source form of any Derivative Works that You distribute,
all copyright, patent, trademark, and attribution notices from the Source form
of the Work, excluding those notices that do not pertain to any part of the
Derivative Works; and
If the Work includes a "NOTICE" text file as part of its distribution, then any
Derivative Works that You distribute must include a readable copy of the
attribution notices contained within such NOTICE file, excluding those notices
that do not pertain to any part of the Derivative Works, in at least one of the
following places: within a NOTICE text file distributed as part of the
Derivative Works; within the Source form or documentation, if provided along
with the Derivative Works; or, within a display generated by the Derivative
Works, if and wherever such third-party notices normally appear. The contents of
the NOTICE file are for informational purposes only and do not modify the
License. You may add Your own attribution notices within Derivative Works that
You distribute, alongside or as an addendum to the NOTICE text from the Work,
provided that such additional attribution notices cannot be construed as
modifying the License.
You may add Your own copyright statement to Your modifications and may provide
additional or different license terms and conditions for use, reproduction, or
distribution of Your modifications, or for any such Derivative Works as a whole,
provided Your use, reproduction, and distribution of the Work otherwise complies
with the conditions stated in this License.
5. Submission of Contributions.
Unless You explicitly state otherwise, any Contribution intentionally submitted
for inclusion in the Work by You to the Licensor shall be under the terms and
conditions of this License, without any additional terms or conditions.
Notwithstanding the above, nothing herein shall supersede or modify the terms of
any separate license agreement you may have executed with Licensor regarding
such Contributions.
6. Trademarks.
This License does not grant permission to use the trade names, trademarks,
service marks, or product names of the Licensor, except as required for
reasonable and customary use in describing the origin of the Work and
reproducing the content of the NOTICE file.
7. Disclaimer of Warranty.
Unless required by applicable law or agreed to in writing, Licensor provides the
Work (and each Contributor provides its Contributions) on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied,
including, without limitation, any warranties or conditions of TITLE,
NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE. You are
solely responsible for determining the appropriateness of using or
redistributing the Work and assume any risks associated with Your exercise of
permissions under this License.
8. Limitation of Liability.
In no event and under no legal theory, whether in tort (including negligence),
contract, or otherwise, unless required by applicable law (such as deliberate
and grossly negligent acts) or agreed to in writing, shall any Contributor be
liable to You for damages, including any direct, indirect, special, incidental,
or consequential damages of any character arising as a result of this License or
out of the use or inability to use the Work (including but not limited to
damages for loss of goodwill, work stoppage, computer failure or malfunction, or
any and all other commercial damages or losses), even if such Contributor has
been advised of the possibility of such damages.
9. Accepting Warranty or Additional Liability.
While redistributing the Work or Derivative Works thereof, You may choose to
offer, and charge a fee for, acceptance of support, warranty, indemnity, or
other liability obligations and/or rights consistent with this License. However,
in accepting such obligations, You may act only on Your own behalf and on Your
sole responsibility, not on behalf of any other Contributor, and only if You
agree to indemnify, defend, and hold each Contributor harmless for any liability
incurred by, or claims asserted against, such Contributor by reason of your
accepting any such warranty or additional liability.
END OF TERMS AND CONDITIONS
APPENDIX: How to apply the Apache License to your work
To apply the Apache License to your work, attach the following boilerplate
notice, with the fields enclosed by brackets "{}" replaced with your own
identifying information. (Don't include the brackets!) The text should be
enclosed in the appropriate comment syntax for the file format. We also
recommend that a file or class name and description of purpose be included on
the same "printed page" as the copyright notice for easier identification within
third-party archives.
Copyright 2017 Karson
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

97
README.md Normal file
View File

@ -0,0 +1,97 @@
FastAdmin是一款基于ThinkPHP+Bootstrap的极速后台开发框架。
## 主要特性
* 基于`Auth`验证的权限管理系统
* 支持无限级父子级权限继承,父级的管理员可任意增删改子级管理员及权限设置
* 支持单管理员多角色
* 支持管理子级数据或个人数据
* 强大的一键生成功能
* 一键生成CRUD,包括控制器、模型、视图、JS、语言包、菜单、回收站等
* 一键压缩打包JS和CSS文件,一键CDN静态资源部署
* 一键生成控制器菜单和规则
* 一键生成API接口文档
* 完善的前端功能组件开发
* 基于`AdminLTE`二次开发
* 基于`Bootstrap`开发,自适应手机、平板、PC
* 基于`RequireJS`进行JS模块管理,按需加载
* 基于`Less`进行样式开发
* 强大的插件扩展功能,在线安装卸载升级插件
* 通用的会员模块和API模块
* 共用同一账号体系的Web端会员中心权限验证和API接口会员权限验证
* 二级域名部署支持,同时域名支持绑定到应用插件
* 多语言支持,服务端及客户端支持
* 支持大文件分片上传、剪切板粘贴上传、拖拽上传,进度条显示,图片上传前压缩
* 支持表格固定列、固定表头、跨页选择、Excel导出、模板渲染等功能
* 强大的第三方应用模块支持([CMS](https://www.fastadmin.net/store/cms.html)、[博客](https://www.fastadmin.net/store/blog.html)、[知识付费问答](https://www.fastadmin.net/store/ask.html)、[在线投票系统](https://www.fastadmin.net/store/vote.html)、[B2C商城](https://www.fastadmin.net/store/shopro.html)、[B2B2C商城](https://www.fastadmin.net/store/wanlshop.html))
* 支持CMS、博客、知识付费问答无缝整合[Xunsearch全文搜索](https://www.fastadmin.net/store/xunsearch.html)
* 第三方小程序支持([CMS小程序](https://www.fastadmin.net/store/cms.html)、[预订小程序](https://www.fastadmin.net/store/ball.html)、[问答小程序](https://www.fastadmin.net/store/ask.html)、[点餐小程序](https://www.fastadmin.net/store/unidrink.html)、[B2C小程序](https://www.fastadmin.net/store/shopro.html)、[B2B2C小程序](https://www.fastadmin.net/store/wanlshop.html)、[博客小程序](https://www.fastadmin.net/store/blog.html))
* 整合第三方短信接口(阿里云、腾讯云短信)
* 无缝整合第三方云存储(七牛云、阿里云OSS、又拍云)功能,支持云储存分片上传
* 第三方富文本编辑器支持(Summernote、Kindeditor、百度编辑器)
* 第三方登录(QQ、微信、微博)整合
* 第三方支付(微信、支付宝)无缝整合,微信支持PC端扫码支付
* 丰富的插件应用市场
## 安装使用
https://doc.fastadmin.net
## 在线演示
https://demo.fastadmin.net
用户名:admin
密 码:123456
提 示:演示站数据无法进行修改,请下载源码安装体验全部功能
## 界面截图
![控制台](https://images.gitee.com/uploads/images/2020/0929/202947_8db2d281_10933.gif "控制台")
## 问题反馈
在使用中有任何问题,请使用以下联系方式联系我们
交流社区: https://ask.fastadmin.net
QQ 1 群(满)、QQ 2 群(满)、QQ 3 群(满)、QQ 4 群(满)、QQ 5 群(满)、QQ 6 群(满)、[QQ 7 群](https://www.fastadmin.net/goto/qun)。
Github: https://github.com/karsonzhang/fastadmin
Gitee: https://gitee.com/karson/fastadmin
## 特别鸣谢
感谢以下的项目,排名不分先后
ThinkPHP:http://www.thinkphp.cn
AdminLTE:https://adminlte.io
Bootstrap:http://getbootstrap.com
jQuery:http://jquery.com
Bootstrap-table:https://github.com/wenzhixin/bootstrap-table
Nice-validator: https://validator.niceue.com
SelectPage: https://github.com/TerryZ/SelectPage
Layer: https://layuion.com/layer/
DropzoneJS: https://www.dropzonejs.com
## 版权信息
FastAdmin遵循Apache2开源协议发布,并提供免费使用。
本项目包含的第三方源码和二进制文件之版权信息另行标注。
版权所有Copyright © 2017-2022 by FastAdmin (https://www.fastadmin.net)
All rights reserved。

1
addons/.gitkeep Normal file
View File

@ -0,0 +1 @@

1
addons/.htaccess Normal file
View File

@ -0,0 +1 @@
deny from all

1
addons/alisms/.addonrc Normal file
View File

@ -0,0 +1 @@
{"files":[],"license":"extended","licenseto":"10734","licensekey":"rnCUHhXKZizGk38N h4EHylo\/0+VBqF5+AT00eQ==","domains":["gztbyc.com"],"licensecodes":[],"validations":["31553592567e773958541e0bc91b7e64"]}

86
addons/alisms/Alisms.php Normal file
View File

@ -0,0 +1,86 @@
<?php
namespace addons\alisms;
use think\Addons;
/**
* Alisms
*/
class Alisms extends Addons
{
/**
* 插件安装方法
* @return bool
*/
public function install()
{
return true;
}
/**
* 插件卸载方法
* @return bool
*/
public function uninstall()
{
return true;
}
/**
* 短信发送行为
* @param array $params 必须包含mobile,event,code
* @return boolean
*/
public function smsSend(&$params)
{
$config = get_addon_config('alisms');
if (!isset($config['template'][$params['event']])) {
return false;
}
$alisms = new \addons\alisms\library\Alisms();
$result = $alisms->mobile($params['mobile'])
->template($config['template'][$params['event']])
->param(['code' => $params['code']])
->send();
return $result;
}
/**
* 短信发送通知
* @param array $params 必须包含 mobile,event,msg
* @return boolean
*/
public function smsNotice(&$params)
{
$config = get_addon_config('alisms');
$alisms = \addons\alisms\library\Alisms::instance();
if (isset($params['msg'])) {
if (is_array($params['msg'])) {
$param = $params['msg'];
} else {
parse_str($params['msg'], $param);
}
} else {
$param = [];
}
$param = $param ? $param : [];
$params['template'] = $params['template'] ?? (isset($params['event']) && isset($config['template'][$params['event']]) ? $config['template'][$params['event']] : '');
$result = $alisms->mobile($params['mobile'])
->template($params['template'])
->param($param)
->send();
return $result;
}
/**
* 检测验证是否正确
* @param $params
* @return boolean
*/
public function smsCheck(&$params)
{
return true;
}
}

73
addons/alisms/config.php Normal file
View File

@ -0,0 +1,73 @@
<?php
return [
[
'name' => 'key',
'title' => '应用key',
'type' => 'string',
'content' => [],
'value' => 'your key',
'rule' => 'required',
'msg' => '',
'tip' => '',
'ok' => '',
'extend' => '',
],
[
'name' => 'secret',
'title' => '密钥secret',
'type' => 'string',
'content' => [],
'value' => 'your secret',
'rule' => 'required',
'msg' => '',
'tip' => '',
'ok' => '',
'extend' => '',
],
[
'name' => 'sign',
'title' => '签名',
'type' => 'string',
'content' => [],
'value' => 'your sign',
'rule' => 'required',
'msg' => '',
'tip' => '',
'ok' => '',
'extend' => '',
],
[
'name' => 'template',
'title' => '短信模板',
'type' => 'array',
'content' => [],
'value' => [
'register' => 'SMS_114000000',
'resetpwd' => 'SMS_114000000',
'changepwd' => 'SMS_114000000',
'changemobile' => 'SMS_114000000',
'profile' => 'SMS_114000000',
'notice' => 'SMS_114000000',
'mobilelogin' => 'SMS_114000000',
'bind' => 'SMS_114000000',
],
'rule' => 'required',
'msg' => '',
'tip' => '',
'ok' => '',
'extend' => '',
],
[
'name' => '__tips__',
'title' => '温馨提示',
'type' => 'string',
'content' => [],
'value' => '应用key和密钥你可以通过 https://ram.console.aliyun.com/manage/ak 获取',
'rule' => 'required',
'msg' => '',
'tip' => '',
'ok' => '',
'extend' => '',
],
];

View File

@ -0,0 +1,73 @@
<?php
namespace addons\alisms\controller;
use think\addons\Controller;
/**
* 阿里云短信
*/
class Index extends Controller
{
protected $model = null;
protected $templateList = [
'register' => '注册',
'resetpwd' => '重置密码',
'changepwd' => '修改密码',
'changemobile' => '修改手机号',
'profile' => '修改个人信息',
'notice' => '通知',
'mobilelogin' => '移动端登录',
'bind' => '绑定账号',
];
public function _initialize()
{
if (!\app\admin\library\Auth::instance()->id) {
$this->error('暂无权限浏览');
}
parent::_initialize();
}
//首页
public function index()
{
$this->view->assign('templateList', $this->templateList);
return $this->view->fetch();
}
//发送测试短信
public function send()
{
$config = get_addon_config('alisms');
$mobile = $this->request->post('mobile');
$template = $this->request->post('template');
$sign = $this->request->post('sign', '');
if (!$mobile) {
$this->error('手机号不能为空');
}
$templateArr = $config['template'] ?? [];
if (!isset($templateArr[$template]) || !$templateArr[$template]) {
$this->error('后台未配置对应的模板CODE');
}
$template = $templateArr[$template];
$sign = $sign ?: $config['sign'];
$param = (array)json_decode($this->request->post('param', '', 'trim'));
$param = ['code' => mt_rand(1000, 9999)];
$alisms = new \addons\alisms\library\Alisms();
$ret = $alisms->mobile($mobile)
->template($template)
->sign($sign)
->param($param)
->send();
if ($ret) {
$this->success("发送成功");
} else {
$this->error("发送失败!失败原因:" . $alisms->getError());
}
}
}

10
addons/alisms/info.ini Normal file
View File

@ -0,0 +1,10 @@
name = alisms
title = 阿里云短信发送
intro = 阿里云短信发送插件
author = FastAdmin
website = https://www.fastadmin.net
version = 1.0.11
state = 1
url = /addons/alisms
license = extended
licenseto = 10734

View File

@ -0,0 +1,170 @@
<?php
namespace addons\alisms\library;
/**
* 阿里云SMS短信发送
*/
class Alisms
{
private $_params = [];
public $error = '';
protected $config = [];
protected static $instance;
public function __construct($options = [])
{
if ($config = get_addon_config('alisms')) {
$this->config = array_merge($this->config, $config);
}
$this->config = array_merge($this->config, is_array($options) ? $options : []);
}
/**
* 单例
* @param array $options 参数
* @return Alisms
*/
public static function instance($options = [])
{
if (is_null(self::$instance)) {
self::$instance = new static($options);
}
return self::$instance;
}
/**
* 设置签名
* @param string $sign
* @return Alisms
*/
public function sign($sign = '')
{
$this->_params['SignName'] = $sign;
return $this;
}
/**
* 设置参数
* @param array $param
* @return Alisms
*/
public function param(array $param = [])
{
foreach ($param as $k => &$v) {
$v = (string)$v;
}
unset($v);
$param = array_filter($param);
$this->_params['TemplateParam'] = $param ? json_encode($param) : '{}';
return $this;
}
/**
* 设置模板
* @param string $code 短信模板
* @return Alisms
*/
public function template($code = '')
{
$this->_params['TemplateCode'] = $code;
return $this;
}
/**
* 接收手机
* @param string $mobile 手机号码
* @return Alisms
*/
public function mobile($mobile = '')
{
$this->_params['PhoneNumbers'] = $mobile;
return $this;
}
/**
* 立即发送
* @return boolean
*/
public function send()
{
$this->error = '';
$params = $this->_params();
$params['Signature'] = $this->_signed($params);
$response = $this->_curl($params);
if ($response !== false) {
$res = (array)json_decode($response, true);
if (isset($res['Code']) && $res['Code'] == 'OK') {
return true;
}
$this->error = $res['Message'] ?? 'InvalidResult';
} else {
$this->error = 'InvalidResult';
}
return false;
}
/**
* 获取错误信息
* @return string
*/
public function getError()
{
return $this->error;
}
private function _params()
{
return array_merge([
'AccessKeyId' => $this->config['key'],
'SignName' => $this->config['sign'] ?? '',
'Action' => 'SendSms',
'Format' => 'JSON',
'Version' => '2017-05-25',
'SignatureVersion' => '1.0',
'SignatureMethod' => 'HMAC-SHA1',
'SignatureNonce' => uniqid(),
'Timestamp' => gmdate('Y-m-d\TH:i:s\Z'),
], $this->_params);
}
private function percentEncode($string)
{
$string = urlencode($string);
$string = preg_replace('/\+/', '%20', $string);
$string = preg_replace('/\*/', '%2A', $string);
$string = preg_replace('/%7E/', '~', $string);
return $string;
}
private function _signed($params)
{
$sign = $this->config['secret'];
ksort($params);
$canonicalizedQueryString = '';
foreach ($params as $key => $value) {
$canonicalizedQueryString .= '&' . $this->percentEncode($key) . '=' . $this->percentEncode($value);
}
$stringToSign = 'GET&%2F&' . $this->percentencode(substr($canonicalizedQueryString, 1));
$signature = base64_encode(hash_hmac('sha1', $stringToSign, $sign . '&', true));
return $signature;
}
private function _curl($params)
{
$uri = 'http://dysmsapi.aliyuncs.com/?' . http_build_query($params);
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_URL, $uri);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.98 Safari/537.36");
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
$reponse = curl_exec($ch);
curl_close($ch);
return $reponse;
}
}

View File

@ -0,0 +1,57 @@
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8"/>
<title>阿里云短信发送示例 - {$site.name}</title>
<link href="__CDN__/assets/libs/bootstrap/dist/css/bootstrap.min.css" rel="stylesheet">
<!--[if lt IE 9]>
<script src="/assets/js/html5shiv.js"></script>
<script src="/assets/js/respond.min.js"></script>
<![endif]-->
</head>
<body>
<div class="container">
<div class="well" style="margin-top:30px;">
<div class="alert alert-danger">温馨提示:仅用于测试插件是否能正常发送短信</div>
<form class="form-horizontal" action="{:addon_url('alisms/index/send')}" method="POST">
<fieldset>
<legend style="padding-bottom:15px;">阿里云短信发送测试</legend>
<div class="form-group">
<label class="col-lg-2 control-label">手机号</label>
<div class="col-lg-10">
<input type="text" class="form-control" name="mobile" placeholder="手机号">
</div>
</div>
<div class="form-group">
<label class="col-lg-2 control-label">消息模板</label>
<div class="col-lg-10">
<select name="template" class="form-control">
{foreach name="templateList" id="item"}
<option value="{$key}">{$item} ({$key})</option>
{/foreach}
</select>
</div>
</div>
<div class="form-group">
<div class="col-lg-10 col-lg-offset-2">
<button type="submit" class="btn btn-primary">发送</button>
<button type="reset" class="btn btn-default">重置</button>
</div>
</div>
</fieldset>
</form>
</div>
</div>
<script src="__CDN__/assets/libs/jquery/dist/jquery.min.js"></script>
<script src="__CDN__/assets/libs/bootstrap/dist/js/bootstrap.min.js"></script>
<script type="text/javascript">
$(function () {
});
</script>
</body>
</html>

1
addons/barcode/.addonrc Normal file
View File

@ -0,0 +1 @@
{"files":[],"license":"regular","licenseto":"10734","licensekey":"ukaVAWbdc1elpsPZ qazzgb1hnZgbTE5BZbblVQ==","domains":["gzxunren.com"],"licensecodes":[],"validations":["9574f928a17b3a20a94b7909f58748f3"]}

View File

@ -0,0 +1,53 @@
<?php
namespace addons\barcode;
use think\Addons;
/**
* 条码生成
*/
class Barcode extends Addons {
/**
* 插件安装方法
* @return bool
*/
public function install() {
return true;
}
/**
* 插件卸载方法
* @return bool
*/
public function uninstall() {
return true;
}
/**
* 插件启用方法
* @return bool
*/
public function enable() {
return true;
}
/**
* 插件禁用方法
* @return bool
*/
public function disable() {
return true;
}
/**
* 应用初始化
*/
public function appInit()
{
if(!class_exists("\Picqer\Barcode")){
\think\Loader::addNamespace('Picqer\Barcode', ADDON_PATH . 'barcode' . DS . 'library' . DS . 'Barcode' . DS);
}
}
}

22
addons/barcode/config.php Normal file
View File

@ -0,0 +1,22 @@
<?php
return [
[
'name' => 'rewrite',
'title' => '伪静态',
'type' => 'array',
'content' =>
[
],
'value' =>
[
'index/index' => '/barcode$',
'index/build' => '/barcode/build$'
],
'rule' => 'required',
'msg' => '',
'tip' => '',
'ok' => '',
'extend' => '',
],
];

View File

@ -0,0 +1,66 @@
<?php
namespace addons\barcode\controller;
use think\addons\Controller;
use think\Response;
/**
* 条码生成
* @package addons\barcode\controller
*/
class Index extends Controller {
public function index() {
return $this->view->fetch();
}
// 生成条码
public function build() {
$text = $this->request->get('text', '1234567890');
$type = $this->request->get('type', 'C128');
$foreground = $this->request->get('foreground', "#000000");
$width = $this->request->get('width', 2);
$height = $this->request->get('height', 30);
$params = [
'text' => $text,
'type' => $type,
'foreground' => $foreground,
'width' => $width,
'height' => $height,
];
$barcode = \addons\barcode\library\Service::barcode($params);
// 直接显示条码
$response = Response::create()->header("Content-Type", "image/png");
header('Content-Type: image/png');
$response->content($barcode);
return $response;
}
// 生成条码
public function build2() {
$text = $this->request->get('text', '1234567890');
$type = $this->request->get('type', 'C128');
$foreground = $this->request->get('foreground', "#000000");
$width = $this->request->get('width', 2);
$height = $this->request->get('height', 30);
$params = [
'text' => $text,
'type' => $type,
'foreground' => $foreground,
'width' => $width,
'height' => $height,
];
$barcode = \addons\barcode\library\Service::barcode($params);
// 直接显示条码
$response = Response::create()->header("Content-Type", "image/png");
header('Content-Type: image/png');
$response->content($barcode);
return $response;
}
}

10
addons/barcode/info.ini Normal file
View File

@ -0,0 +1,10 @@
name = barcode
title = 条码生成
intro = 生成条码示例插件
author = aa820t
website = https://ask.fastadmin.net/u/26861
version = 1.0.0
state = 1
license = regular
licenseto = 10734
url = /addons/barcode

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,43 @@
<?php
namespace Picqer\Barcode;
class BarcodeGeneratorHTML extends BarcodeGenerator
{
/**
* Return an HTML representation of barcode.
*
* @param string $code code to print
* @param string $type type of barcode
* @param int $widthFactor Width of a single bar element in pixels.
* @param int $totalHeight Height of a single bar element in pixels.
* @param int|string $color Foreground color for bar elements (background is transparent).
* @return string HTML code.
* @public
*/
public function getBarcode($code, $type, $widthFactor = 2, $totalHeight = 30, $color = 'black')
{
$barcodeData = $this->getBarcodeData($code, $type);
$html = '<div style="font-size:0;position:relative;width:' . ($barcodeData['maxWidth'] * $widthFactor) . 'px;height:' . ($totalHeight) . 'px;">' . "\n";
$positionHorizontal = 0;
foreach ($barcodeData['bars'] as $bar) {
$barWidth = round(($bar['width'] * $widthFactor), 3);
$barHeight = round(($bar['height'] * $totalHeight / $barcodeData['maxHeight']), 3);
if ($bar['drawBar']) {
$positionVertical = round(($bar['positionVertical'] * $totalHeight / $barcodeData['maxHeight']), 3);
// draw a vertical bar
$html .= '<div style="background-color:' . $color . ';width:' . $barWidth . 'px;height:' . $barHeight . 'px;position:absolute;left:' . $positionHorizontal . 'px;top:' . $positionVertical . 'px;">&nbsp;</div>' . "\n";
}
$positionHorizontal += $barWidth;
}
$html .= '</div>' . "\n";
return $html;
}
}

View File

@ -0,0 +1,77 @@
<?php
namespace Picqer\Barcode;
use Picqer\Barcode\Exceptions\BarcodeException;
class BarcodeGeneratorJPG extends BarcodeGenerator
{
/**
* Return a JPG image representation of barcode (requires GD or Imagick library).
*
* @param string $code code to print
* @param string $type type of barcode:
* @param int $widthFactor Width of a single bar element in pixels.
* @param int $totalHeight Height of a single bar element in pixels.
* @param array $color RGB (0-255) foreground color for bar elements (background is transparent).
* @return string image data or false in case of error.
* @public
* @throws BarcodeException
*/
public function getBarcode($code, $type, $widthFactor = 2, $totalHeight = 30, $color = array(0, 0, 0))
{
$barcodeData = $this->getBarcodeData($code, $type);
// calculate image size
$width = ($barcodeData['maxWidth'] * $widthFactor);
$height = $totalHeight;
if (function_exists('imagecreate')) {
// GD library
$imagick = false;
$jpg = imagecreate($width, $height);
$colorBackground = imagecolorallocate($jpg, 255, 255, 255);
imagecolortransparent($jpg, $colorBackground);
$colorForeground = imagecolorallocate($jpg, $color[0], $color[1], $color[2]);
} elseif (extension_loaded('imagick')) {
$imagick = true;
$colorForeground = new \imagickpixel('rgb(' . $color[0] . ',' . $color[1] . ',' . $color[2] . ')');
$jpg = new \Imagick();
$jpg->newImage($width, $height, 'white', 'jpg');
$imageMagickObject = new \imagickdraw();
$imageMagickObject->setFillColor($colorForeground);
} else {
throw new BarcodeException('Neither gd-lib or imagick are installed!');
}
// print bars
$positionHorizontal = 0;
foreach ($barcodeData['bars'] as $bar) {
$bw = round(($bar['width'] * $widthFactor), 3);
$bh = round(($bar['height'] * $totalHeight / $barcodeData['maxHeight']), 3);
if ($bar['drawBar']) {
$y = round(($bar['positionVertical'] * $totalHeight / $barcodeData['maxHeight']), 3);
// draw a vertical bar
if ($imagick && isset($imageMagickObject)) {
$imageMagickObject->rectangle($positionHorizontal, $y, ($positionHorizontal + $bw), ($y + $bh));
} else {
imagefilledrectangle($jpg, $positionHorizontal, $y, ($positionHorizontal + $bw) - 1, ($y + $bh),
$colorForeground);
}
}
$positionHorizontal += $bw;
}
ob_start();
if ($imagick && isset($imageMagickObject)) {
$jpg->drawImage($imageMagickObject);
echo $jpg;
} else {
imagejpeg($jpg);
imagedestroy($jpg);
}
$image = ob_get_clean();
return $image;
}
}

View File

@ -0,0 +1,76 @@
<?php
namespace Picqer\Barcode;
use Picqer\Barcode\Exceptions\BarcodeException;
class BarcodeGeneratorPNG extends BarcodeGenerator
{
/**
* Return a PNG image representation of barcode (requires GD or Imagick library).
*
* @param string $code code to print
* @param string $type type of barcode:
* @param int $widthFactor Width of a single bar element in pixels.
* @param int $totalHeight Height of a single bar element in pixels.
* @param array $color RGB (0-255) foreground color for bar elements (background is transparent).
* @return string image data or false in case of error.
* @public
*/
public function getBarcode($code, $type, $widthFactor = 2, $totalHeight = 30, $color = array(0, 0, 0))
{
$barcodeData = $this->getBarcodeData($code, $type);
// calculate image size
$width = ($barcodeData['maxWidth'] * $widthFactor);
$height = $totalHeight;
if (function_exists('imagecreate')) {
// GD library
$imagick = false;
$png = imagecreate($width, $height);
$colorBackground = imagecolorallocate($png, 255, 255, 255);
imagecolortransparent($png, $colorBackground);
$colorForeground = imagecolorallocate($png, $color[0], $color[1], $color[2]);
} elseif (extension_loaded('imagick')) {
$imagick = true;
$colorForeground = new \imagickpixel('rgb(' . $color[0] . ',' . $color[1] . ',' . $color[2] . ')');
$png = new \Imagick();
$png->newImage($width, $height, 'none', 'png');
$imageMagickObject = new \imagickdraw();
$imageMagickObject->setFillColor($colorForeground);
} else {
throw new BarcodeException('Neither gd-lib or imagick are installed!');
}
// print bars
$positionHorizontal = 0;
foreach ($barcodeData['bars'] as $bar) {
$bw = round(($bar['width'] * $widthFactor), 3);
$bh = round(($bar['height'] * $totalHeight / $barcodeData['maxHeight']), 3);
if ($bar['drawBar']) {
$y = round(($bar['positionVertical'] * $totalHeight / $barcodeData['maxHeight']), 3);
// draw a vertical bar
if ($imagick && isset($imageMagickObject)) {
$imageMagickObject->rectangle($positionHorizontal, $y, ($positionHorizontal + $bw), ($y + $bh));
} else {
imagefilledrectangle($png, $positionHorizontal, $y, ($positionHorizontal + $bw) - 1, ($y + $bh),
$colorForeground);
}
}
$positionHorizontal += $bw;
}
ob_start();
if ($imagick && isset($imageMagickObject)) {
$png->drawImage($imageMagickObject);
echo $png;
} else {
imagepng($png);
imagedestroy($png);
}
$image = ob_get_clean();
return $image;
}
}

View File

@ -0,0 +1,50 @@
<?php
namespace Picqer\Barcode;
class BarcodeGeneratorSVG extends BarcodeGenerator
{
/**
* Return a SVG string representation of barcode.
*
* @param $code (string) code to print
* @param $type (const) type of barcode
* @param $widthFactor (int) Minimum width of a single bar in user units.
* @param $totalHeight (int) Height of barcode in user units.
* @param $color (string) Foreground color (in SVG format) for bar elements (background is transparent).
* @return string SVG code.
* @public
*/
public function getBarcode($code, $type, $widthFactor = 2, $totalHeight = 30, $color = 'black')
{
$barcodeData = $this->getBarcodeData($code, $type);
// replace table for special characters
$repstr = array("\0" => '', '&' => '&amp;', '<' => '&lt;', '>' => '&gt;');
$width = round(($barcodeData['maxWidth'] * $widthFactor), 3);
$svg = '<?xml version="1.0" standalone="no" ?>' . "\n";
$svg .= '<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">' . "\n";
$svg .= '<svg width="' . $width . '" height="' . $totalHeight . '" viewBox="0 0 ' . $width . ' ' . $totalHeight . '" version="1.1" xmlns="http://www.w3.org/2000/svg">' . "\n";
$svg .= "\t" . '<desc>' . strtr($barcodeData['code'], $repstr) . '</desc>' . "\n";
$svg .= "\t" . '<g id="bars" fill="' . $color . '" stroke="none">' . "\n";
// print bars
$positionHorizontal = 0;
foreach ($barcodeData['bars'] as $bar) {
$barWidth = round(($bar['width'] * $widthFactor), 3);
$barHeight = round(($bar['height'] * $totalHeight / $barcodeData['maxHeight']), 3);
if ($bar['drawBar']) {
$positionVertical = round(($bar['positionVertical'] * $totalHeight / $barcodeData['maxHeight']), 3);
// draw a vertical bar
$svg .= "\t\t" . '<rect x="' . $positionHorizontal . '" y="' . $positionVertical . '" width="' . $barWidth . '" height="' . $barHeight . '" />' . "\n";
}
$positionHorizontal += $barWidth;
}
$svg .= "\t" . '</g>' . "\n";
$svg .= '</svg>' . "\n";
return $svg;
}
}

View File

@ -0,0 +1,5 @@
<?php
namespace Picqer\Barcode\Exceptions;
class BarcodeException extends \Exception {}

View File

@ -0,0 +1,5 @@
<?php
namespace Picqer\Barcode\Exceptions;
class InvalidCharacterException extends BarcodeException {}

View File

@ -0,0 +1,5 @@
<?php
namespace Picqer\Barcode\Exceptions;
class InvalidCheckDigitException extends BarcodeException {}

View File

@ -0,0 +1,5 @@
<?php
namespace Picqer\Barcode\Exceptions;
class InvalidFormatException extends BarcodeException {}

View File

@ -0,0 +1,5 @@
<?php
namespace Picqer\Barcode\Exceptions;
class InvalidLengthException extends BarcodeException {}

View File

@ -0,0 +1,5 @@
<?php
namespace Picqer\Barcode\Exceptions;
class UnknownTypeException extends BarcodeException {}

View File

@ -0,0 +1,27 @@
<?php
namespace addons\barcode\library;
class Service
{
public static function barcode($params)
{
$params = is_array($params) ? $params : [$params];
$params['text'] = isset($params['text']) ? $params['text'] : 'Hello world!';
$params['type'] = isset($params['type']) ? $params['type'] : 'C128';
$params['width'] = isset($params['width']) ? $params['width'] : 2;
$params['height'] = isset($params['height']) ? $params['height'] : 30;
$params['foreground'] = isset($params['foreground']) ? $params['foreground'] : "#000000";
// 前景色
list($r, $g, $b) = sscanf($params['foreground'], "#%02x%02x%02x");
$foregroundcolor = [$r, $g, $b];
// 创建实例
$generator = new \Picqer\Barcode\BarcodeGeneratorPNG();
$barcode = $generator->getBarcode($params['text'], $params['type'], $params['width'], $params['height'], $foregroundcolor);
return $barcode;
}
}

View File

@ -0,0 +1,99 @@
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8" />
<title>条码生成 - {$site.name}</title>
<link href="https://cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
<!-- HTML5 shim and Respond.js IE8 support of HTML5 elements and media queries -->
<!--[if lt IE 9]>
<script src="http://apps.bdimg.com/libs/html5shiv/3.7/html5shiv.min.js"></script>
<script src="http://apps.bdimg.com/libs/respond.js/1.4.2/respond.js"></script>
<![endif]-->
</head>
<body>
<div class="container">
<h2>条码生成</h2>
<hr>
<div class="well">
<form action="" method="post">
<div class="row">
<div class="col-md-4">
<div class="form-group">
<label class="control-label">文本内容</label>
<input type="text" name="text" class="form-control" placeholder="" value="1234567890">
</div>
<div class="form-group">
<label class="control-label">条码类型</label>
<select name="type" class="form-control">
<option value="C128">C128</option>
<option value="C128A">C128A</option>
<option value="C128B">C128B</option>
<option value="C128C">C128C</option>
<option value="C39">C39</option>
<option value="C39+">C39+</option>
<option value="C39E">C39E</option>
<option value="C39E+">C39E+</option>
<option value="S25">S25</option>
<option value="I25">I25</option>
<option value="I25+">I25+</option>
<option value="EAN2">EAN2</option>
<option value="EAN5">EAN5</option>
<option value="EAN8">EAN8</option>
<option value="EAN13">EAN13</option>
<option value="UPCA">UPCA</option>
<option value="UPCE">UPCE</option>
<option value="MSI">MSI</option>
<option value="POSTNET">POSTNET</option>
<option value="PLANET">PLANET</option>
<option value="RMS4CC">RMS4CC</option>
<option value="KIX">KIX</option>
<option value="IMB">IMB</option>
<option value="CODABAR">CODABAR</option>
<option value="CODE11">CODE11</option>
<option value="PHARMA">PHARMA</option>
<option value="PHARMA2T">PHARMA2T</option>
</select>
</div>
<div class="form-group">
<input type="submit" class="btn btn-info" />
<input type="reset" class="btn btn-default" />
</div>
</div>
<div class="col-md-2">
<div class="form-group">
<div class="form-group">
<label class="control-label">前景色</label>
<input type="text" name="foreground" placeholder="" class="form-control" value="#000000">
</div>
<div class="form-group">
<label class="control-label">间距</label>
<input type="number" name="width" placeholder="" class="form-control" value="2">
</div>
<div class="form-group">
<label class="control-label">高度</label>
<input type="number" name="height" placeholder="" class="form-control" value="30">
</div>
</div>
</div>
</div>
</form>
</div>
<input type="text" class="form-control" id='barcodeurl' />
<img src="" alt="" id='barcodeimg' style="margin-top:15px;"/>
</div>
<script src="https://cdn.bootcss.com/jquery/2.2.4/jquery.min.js"></script>
<script type="text/javascript">
$(function () {
$("form").submit(function () {
$("#barcodeimg").prop("src", "{:addon_url('barcode/index/build',[],false)}?" + $(this).serialize());
$("#barcodeurl").val("{:addon_url('barcode/index/build',[],false,true)}?" + $(this).serialize());
return false;
});
$("form").trigger('submit');
});
</script>
</body>
</html>

1
addons/command/.addonrc Normal file
View File

@ -0,0 +1 @@
{"files":["application\/admin\/model\/Command.php","application\/admin\/validate\/Command.php","application\/admin\/view\/command\/detail.html","application\/admin\/view\/command\/index.html","application\/admin\/view\/command\/add.html","application\/admin\/lang\/zh-cn\/command.php","application\/admin\/controller\/Command.php","public\/assets\/js\/backend\/command.js"],"license":"regular","licenseto":"10734","licensekey":"9EXkzFP7hntW2SJo dAwnlvlK+Mb3SGkjEFux1A==","domains":["gzxunren.com"],"licensecodes":[],"validations":["9574f928a17b3a20a94b7909f58748f3"],"menus":["command","command\/index","command\/add","command\/detail","command\/command","command\/execute","command\/del","command\/multi"]}

View File

@ -0,0 +1,70 @@
<?php
namespace addons\command;
use app\common\library\Menu;
use think\Addons;
/**
* 在线命令插件
*/
class Command extends Addons
{
/**
* 插件安装方法
* @return bool
*/
public function install()
{
$menu = [
[
'name' => 'command',
'title' => '在线命令管理',
'icon' => 'fa fa-terminal',
'sublist' => [
['name' => 'command/index', 'title' => '查看'],
['name' => 'command/add', 'title' => '添加'],
['name' => 'command/detail', 'title' => '详情'],
['name' => 'command/command', 'title' => '生成并执行命令'],
['name' => 'command/execute', 'title' => '再次执行命令'],
['name' => 'command/del', 'title' => '删除'],
['name' => 'command/multi', 'title' => '批量更新'],
]
]
];
Menu::create($menu);
return true;
}
/**
* 插件卸载方法
* @return bool
*/
public function uninstall()
{
Menu::delete('command');
return true;
}
/**
* 插件启用方法
* @return bool
*/
public function enable()
{
Menu::enable('command');
return true;
}
/**
* 插件禁用方法
* @return bool
*/
public function disable()
{
Menu::disable('command');
return true;
}
}

View File

@ -0,0 +1,4 @@
<?php
return [
];

View File

@ -0,0 +1,15 @@
<?php
namespace addons\command\controller;
use think\addons\Controller;
class Index extends Controller
{
public function index()
{
$this->error("当前插件暂无前台页面");
}
}

10
addons/command/info.ini Normal file
View File

@ -0,0 +1,10 @@
name = command
title = 在线命令
intro = 可在线执行一键生成CRUD、一键生成菜单等相关命令
author = FastAdmin
website = https://www.fastadmin.net
version = 1.1.1
state = 1
url = /addons/command
license = regular
licenseto = 10734

View File

@ -0,0 +1,12 @@
CREATE TABLE IF NOT EXISTS `__PREFIX__command` (
`id` int(10) UNSIGNED NOT NULL AUTO_INCREMENT COMMENT 'ID',
`type` varchar(30) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL DEFAULT '' COMMENT '类型',
`params` varchar(1500) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL DEFAULT '' COMMENT '参数',
`command` varchar(1500) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL DEFAULT '' COMMENT '命令',
`content` text COMMENT '返回结果',
`executetime` bigint(16) UNSIGNED DEFAULT NULL COMMENT '执行时间',
`createtime` bigint(16) UNSIGNED DEFAULT NULL COMMENT '创建时间',
`updatetime` bigint(16) UNSIGNED DEFAULT NULL COMMENT '更新时间',
`status` enum('successed','failured') CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL DEFAULT 'failured' COMMENT '状态',
PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 1 CHARACTER SET = utf8 COLLATE = utf8_general_ci COMMENT = '在线命令表';

View File

@ -0,0 +1,28 @@
<?php
namespace addons\command\library;
/**
* Class Output
*/
class Output extends \think\console\Output
{
protected $message = [];
public function __construct($driver = 'console')
{
parent::__construct($driver);
}
protected function block($style, $message)
{
$this->message[] = $message;
}
public function getMessage()
{
return $this->message;
}
}

1
addons/cos/.addonrc Normal file
View File

@ -0,0 +1 @@
{"files":["public\/assets\/addons\/cos\/js\/spark.js"],"license":"regular","licenseto":"10734","licensekey":"f5e7LTsUu684QWtD +FV0nKCiYo\/gqhspqQGyEg==","domains":[],"licensecodes":[],"validations":[]}

153
addons/cos/Cos.php Normal file
View File

@ -0,0 +1,153 @@
<?php
namespace addons\cos;
use addons\cos\library\Auth;
use app\common\library\Menu;
use fast\Http;
use Qcloud\Cos\Client;
use think\Addons;
use think\App;
use think\Loader;
/**
* COS插件
*/
class Cos extends Addons
{
/**
* 插件安装方法
* @return bool
*/
public function install()
{
return true;
}
/**
* 插件卸载方法
* @return bool
*/
public function uninstall()
{
return true;
}
/**
* 插件启用方法
* @return bool
*/
public function enable()
{
return true;
}
/**
* 插件禁用方法
* @return bool
*/
public function disable()
{
return true;
}
/**
* 渲染命名空间配置信息
*/
public function appInit()
{
if (!class_exists('\Qcloud\Cos\Client')) {
Loader::addNamespace('Qcloud\Cos', $this->addons_path . str_replace('/', DS, 'library/Qcloud/Cos/'));
}
if (!class_exists('\GuzzleHttp\Command\Command')) {
Loader::addNamespace('GuzzleHttp\Command', $this->addons_path . str_replace('/', DS, 'library/Guzzle/command/src/'));
}
if (!class_exists('\GuzzleHttp\Command\Guzzle\Description')) {
Loader::addNamespace('GuzzleHttp\Command\Guzzle', $this->addons_path . str_replace('/', DS, 'library/Guzzle/guzzle-services/src/'));
}
}
/**
* 判断是否来源于API上传
*/
public function moduleInit($request)
{
$config = $this->getConfig();
$module = strtolower($request->module());
if ($module == 'api' && ($config['apiupload'] ?? 0) && in_array($module, explode(',', $config['uploadmodule'] ?? '')) &&
strtolower($request->controller()) == 'common' &&
strtolower($request->action()) == 'upload') {
request()->param('isApi', true);
App::invokeMethod(["\\addons\\cos\\controller\\Index", "upload"], ['isApi' => true]);
}
}
/**
*
*/
public function uploadConfigInit(&$upload)
{
$config = $this->getConfig();
$module = request()->module();
$module = $module ? strtolower($module) : 'index';
$uploadModule = array_filter(explode(',', $config['uploadmodule'] ?? ''));
if (!in_array($module, $uploadModule)) {
return $upload;
}
$data = ['deadline' => time() + $config['expire']];
$signature = hash_hmac('sha1', json_encode($data), $config['secretKey'], true);
$token = '';
$noNeedLogin = array_filter(explode(',', $config['noneedlogin'] ?? ''));
if (in_array($module, $noNeedLogin) || ($module == 'admin' && \app\admin\library\Auth::instance()->id) || ($module != 'admin' && \app\common\library\Auth::instance()->id)) {
$token = $config['appId'] . ':' . base64_encode($signature) . ':' . base64_encode(json_encode($data));
}
$multipart = [
'costoken' => $token
];
$upload = array_merge($upload, [
'cdnurl' => $config['cdnurl'],
'uploadurl' => $config['uploadmode'] == 'client' ? $config['uploadurl'] : addon_url('cos/index/upload', [], false, true),
'uploadmode' => $config['uploadmode'],
'bucket' => $config['bucket'],
'maxsize' => $config['maxsize'],
'mimetype' => $config['mimetype'],
'savekey' => $config['savekey'],
'chunking' => (bool)($config['chunking'] ?? $upload['chunking']),
'chunksize' => (int)($config['chunksize'] ?? $upload['chunksize']),
'multipart' => $multipart,
'storage' => $this->getName(),
'multiple' => $config['multiple'] ? true : false,
]);
}
/**
* 附件删除后
*/
public function uploadDelete($attachment)
{
$config = $this->getConfig();
if ($attachment['storage'] == 'cos' && isset($config['syncdelete']) && $config['syncdelete']) {
$cosConfig = array(
'region' => $config['region'],
'schema' => 'https', //协议头部,默认为http
'credentials' => array(
'secretId' => $config['secretId'],
'secretKey' => $config['secretKey']
)
);
$oss = new Client($cosConfig);
$ret = $oss->deleteObject(array('Bucket' => $config['bucket'], 'Key' => ltrim($attachment->url, '/')));
}
return true;
}
}

263
addons/cos/bootstrap.js vendored Normal file
View File

@ -0,0 +1,263 @@
if (typeof Config.upload.storage !== 'undefined' && Config.upload.storage === 'cos') {
require(['upload'], function (Upload) {
//获取文件MD5值
var getFileMd5 = function (file, cb) {
require(['../addons/cos/js/spark'], function (SparkMD5) {
var blobSlice = File.prototype.slice || File.prototype.mozSlice || File.prototype.webkitSlice,
chunkSize = 10 * 1024 * 1024,
chunks = Math.ceil(file.size / chunkSize),
currentChunk = 0,
spark = new SparkMD5.ArrayBuffer(),
fileReader = new FileReader();
fileReader.onload = function (e) {
spark.append(e.target.result);
currentChunk++;
if (currentChunk < chunks) {
loadNext();
} else {
cb && cb(spark.end());
}
};
fileReader.onerror = function () {
console.warn('文件读取错误');
};
function loadNext() {
var start = currentChunk * chunkSize,
end = ((start + chunkSize) >= file.size) ? file.size : start + chunkSize;
fileReader.readAsArrayBuffer(blobSlice.call(file, start, end));
}
loadNext();
});
};
var _onInit = Upload.events.onInit;
//初始化中完成判断
Upload.events.onInit = function () {
_onInit.apply(this, Array.prototype.slice.apply(arguments));
//如果上传接口不是COS,则不处理
if (this.options.url !== Config.upload.uploadurl) {
return;
}
$.extend(this.options, {
//关闭自动处理队列功能
autoQueue: false,
params: function (files, xhr, chunk) {
var params = Config.upload.multipart;
if (chunk) {
return $.extend({}, params, {
filesize: chunk.file.size,
filename: chunk.file.name,
chunkid: chunk.file.upload.uuid,
chunkindex: chunk.index,
chunkcount: chunk.file.upload.totalChunkCount,
chunkfilesize: chunk.dataBlock.data.size,
chunksize: this.options.chunkSize,
width: chunk.file.width || 0,
height: chunk.file.height || 0,
type: chunk.file.type,
uploadId: chunk.file.uploadId,
key: chunk.file.key,
});
} else {
params = $.extend({}, params, files[0].params);
params.category = files[0].category || '';
}
return params;
},
chunkSuccess: function (chunk, file, response) {
var etag = chunk.xhr.getResponseHeader("ETag").replace(/(^")|("$)/g, '');
this.etags = this.etags ? this.etags : [];
this.etags[chunk.index] = etag;
},
chunksUploaded: function (file, done) {
var that = this;
Fast.api.ajax({
url: "/addons/cos/index/upload",
data: {
action: 'merge',
filesize: file.size,
filename: file.name,
chunkid: file.upload.uuid,
chunkcount: file.upload.totalChunkCount,
md5: file.md5,
key: file.key,
uploadId: file.uploadId,
etags: this.etags,
category: file.category || '',
costoken: Config.upload.multipart.costoken,
},
}, function (data, ret) {
done(JSON.stringify(ret));
return false;
}, function (data, ret) {
file.accepted = false;
that._errorProcessing([file], ret.msg);
return false;
});
},
});
var _success = this.options.success;
//先移除已有的事件
this.off("success", _success).on("success", function (file, response) {
var ret = {code: 0, msg: response};
try {
if (response) {
ret = typeof response === 'string' ? JSON.parse(response) : response;
}
if (file.xhr.status === 200 || file.xhr.status === 204) {
if (Config.upload.uploadmode === 'client') {
ret = {code: 1, data: {url: '/' + file.key}};
}
if (ret.code == 1) {
var url = ret.data.url || '';
Fast.api.ajax({
url: "/addons/cos/index/notify",
data: {name: file.name, url: url, md5: file.md5, size: file.size, width: file.width || 0, height: file.height || 0, type: file.type, category: file.category || '', costoken: Config.upload.multipart.costoken}
}, function () {
return false;
}, function () {
return false;
});
} else {
console.error(ret);
}
} else {
console.error(file.xhr);
}
} catch (e) {
console.error(e);
}
_success.call(this, file, ret);
});
this.on("addedfile", function (file) {
var that = this;
setTimeout(function () {
if (file.status === 'error') {
return;
}
getFileMd5(file, function (md5) {
var chunk = that.options.chunking && file.size > that.options.chunkSize ? 1 : 0;
var params = $(that.element).data("params") || {};
var category = typeof params.category !== 'undefined' ? params.category : ($(that.element).data("category") || '');
category = typeof category === 'function' ? category.call(that, file) : category;
Fast.api.ajax({
url: "/addons/cos/index/params",
data: {method: 'POST', category: category, md5: md5, name: file.name, type: file.type, size: file.size, chunk: chunk, chunksize: that.options.chunkSize, costoken: Config.upload.multipart.costoken},
}, function (data) {
file.md5 = md5;
file.id = data.id;
file.key = data.key;
file.date = data.date;
file.uploadId = data.uploadId;
file.policy = data.policy;
file.signature = data.signature;
file.partsAuthorization = data.partsAuthorization;
file.params = data;
file.category = category;
if (file.status != 'error') {
//开始上传
that.enqueueFile(file);
} else {
that.removeFile(file);
}
return false;
}, function () {
that.removeFile(file);
});
});
}, 0);
});
if (Config.upload.uploadmode === 'client') {
var _method = this.options.method;
var _url = this.options.url;
this.options.method = function (files) {
if (files[0].upload.chunked) {
var chunk = null;
files[0].upload.chunks.forEach(function (item) {
if (item.status === 'uploading') {
chunk = item;
}
});
if (!chunk) {
return "POST";
} else {
return "PUT";
}
}
return _method;
};
this.options.url = function (files) {
if (files[0].upload.chunked) {
var chunk = null;
files[0].upload.chunks.forEach(function (item) {
if (item.status === 'uploading') {
chunk = item;
}
});
var index = chunk.dataBlock.chunkIndex;
// debugger;
this.options.headers = {"Authorization": files[0]['partsAuthorization'][index], "x-date": files[0]['date']};
if (!chunk) {
return Config.upload.uploadurl + "/" + files[0].key + "?uploadId=" + files[0].uploadId;
} else {
return Config.upload.uploadurl + "/" + files[0].key + "?partNumber=" + (index + 1) + "&uploadId=" + files[0].uploadId;
}
}
return _url;
};
this.options.params = function (files, xhr, chunk) {
var params = Config.upload.multipart;
if (chunk) {
return $.extend({}, params, {
filesize: chunk.file.size,
filename: chunk.file.name,
chunkid: chunk.file.upload.uuid,
chunkindex: chunk.index,
chunkcount: chunk.file.upload.totalChunkCount,
chunkfilesize: chunk.dataBlock.data.size,
width: chunk.file.width || 0,
height: chunk.file.height || 0,
type: chunk.file.type,
});
} else {
var retParams = $.extend({}, params, files[0].params || {});
delete retParams.costoken;
return retParams;
}
};
this.on("sending", function (file, xhr, formData) {
var that = this;
if (file.upload.chunked) {
var _send = xhr.send;
xhr.send = function () {
var chunk = null;
file.upload.chunks.forEach(function (item) {
if (item.status == 'uploading') {
chunk = item;
}
});
if (chunk) {
_send.call(xhr, chunk.dataBlock.data);
}
};
} else {
}
});
}
};
});
}

273
addons/cos/config.php Normal file
View File

@ -0,0 +1,273 @@
<?php
return [
[
'name' => 'appId',
'title' => 'AppID',
'type' => 'string',
'content' => [],
'value' => '1234567890',
'rule' => 'required',
'msg' => '',
'tip' => '请前往腾讯控制台 > 访问管理 > API密钥',
'ok' => '',
'extend' => '',
],
[
'name' => 'secretId',
'title' => 'SecretId',
'type' => 'string',
'content' => [],
'value' => 'AKIDeFhx1tQ5PYVYP9K8PXUFoazHRPBlpTXP',
'rule' => 'required',
'msg' => '',
'tip' => '请前往腾讯控制台 > 访问管理 > API密钥',
'ok' => '',
'extend' => '',
],
[
'name' => 'secretKey',
'title' => 'SecretKey',
'type' => 'string',
'content' => [],
'value' => 'pcHtKbhcKqDNLqEQRIxRjpjRHxTnvGQT',
'rule' => 'required',
'msg' => '',
'tip' => '请前往腾讯控制台 > 访问管理 > API密钥',
'ok' => '',
'extend' => '',
],
[
'name' => 'bucket',
'title' => '存储桶名称',
'type' => 'string',
'content' => [],
'value' => 'ye4s-1255377868',
'rule' => 'required;bucket',
'msg' => '',
'tip' => '存储空间名称',
'ok' => '',
'extend' => 'data-rule-bucket="[/^[0-9a-z_\\-]{3,63}$/, \'请输入正确的存储桶名称\']"',
],
[
'name' => 'region',
'title' => '地域名称',
'type' => 'string',
'content' => [],
'value' => 'ap-guangzhou',
'rule' => 'required',
'msg' => '',
'tip' => '请输入地域简称,请注意使用英文',
'ok' => '',
'extend' => '',
],
[
'name' => 'uploadmode',
'title' => '上传模式',
'type' => 'select',
'content' => [
'client' => '客户端直传(速度快,无备份)',
'server' => '服务器中转(占用服务器带宽,有备份)',
],
'value' => 'client',
'rule' => '',
'msg' => '',
'tip' => '',
'ok' => '',
'extend' => '',
],
[
'name' => 'serverbackup',
'title' => '服务器中转模式备份',
'type' => 'radio',
'content' => [
1 => '备份(附件管理将产生2条记录)',
0 => '不备份',
],
'value' => '0',
'rule' => '',
'msg' => '',
'tip' => '服务器中转模式下是否备份文件',
'ok' => '',
'extend' => '',
],
[
'name' => 'uploadurl',
'title' => '上传接口地址',
'type' => 'string',
'content' => [],
'value' => 'https://ye4s-1255377868.cos.ap-guangzhou.myqcloud.com',
'rule' => 'required;uploadurl',
'msg' => '',
'tip' => '请输入你的上传接口地址',
'ok' => '',
'extend' => 'data-rule-uploadurl="[/^http(s)?:\\/\\/.*$/, \'必需以http(s)://开头\']"',
],
[
'name' => 'cdnurl',
'title' => 'CDN地址',
'type' => 'string',
'content' => [],
'value' => 'http://ye4s.file.gzxunren.com',
'rule' => 'required;cdnurl',
'msg' => '',
'tip' => '请配置你的CDN地址或在存储桶基础配置中获取',
'ok' => '',
'extend' => 'data-rule-cdnurl="[/^http(s)?:\\/\\/.*$/, \'必需以http(s)://开头\']"',
],
[
'name' => 'savekey',
'title' => '保存文件名',
'type' => 'string',
'content' => [],
'value' => '/uploads/{year}{mon}{day}/{filemd5}{.suffix}',
'rule' => 'required',
'msg' => '',
'tip' => '',
'ok' => '',
'extend' => '',
],
[
'name' => 'expire',
'title' => '上传有效时长',
'type' => 'string',
'content' => [],
'value' => '600',
'rule' => 'required',
'msg' => '',
'tip' => '用户停留页面上传有效时长,单位秒',
'ok' => '',
'extend' => '',
],
[
'name' => 'maxsize',
'title' => '最大可上传',
'type' => 'string',
'content' => [],
'value' => '100M',
'rule' => 'required',
'msg' => '',
'tip' => '',
'ok' => '',
'extend' => '',
],
[
'name' => 'mimetype',
'title' => '可上传后缀格式',
'type' => 'string',
'content' => [],
'value' => 'jpg,png,bmp,jpeg,gif,zip,rar,xls,xlsx,apk',
'rule' => 'required',
'msg' => '',
'tip' => '',
'ok' => '',
'extend' => '',
],
[
'name' => 'multiple',
'title' => '多文件上传',
'type' => 'bool',
'content' => [],
'value' => '0',
'rule' => 'required',
'msg' => '',
'tip' => '',
'ok' => '',
'extend' => '',
],
[
'name' => 'thumbstyle',
'title' => '缩略图样式',
'type' => 'string',
'content' => [],
'value' => '',
'rule' => '',
'msg' => '',
'tip' => '用于后台列表缩略图样式,可使用:?imageMogr2/thumbnail/120x90',
'ok' => '',
'extend' => '',
],
[
'name' => 'chunking',
'title' => '分片上传',
'type' => 'radio',
'content' => [
1 => '开启',
0 => '关闭',
],
'value' => '0',
'rule' => 'required',
'msg' => '',
'tip' => '',
'ok' => '',
'extend' => '',
],
[
'name' => 'chunksize',
'title' => '分片大小',
'type' => 'number',
'content' => [],
'value' => '4194304',
'rule' => 'required',
'msg' => '',
'tip' => '',
'ok' => '',
'extend' => '',
],
[
'name' => 'syncdelete',
'title' => '附件删除时是否同步删除文件',
'type' => 'bool',
'content' => [],
'value' => '1',
'rule' => 'required',
'msg' => '',
'tip' => '',
'ok' => '',
'extend' => '',
],
[
'name' => 'apiupload',
'title' => 'API接口使用云存储',
'type' => 'bool',
'content' => [],
'value' => '1',
'rule' => 'required',
'msg' => '',
'tip' => '',
'ok' => '',
'extend' => '',
],
[
'name' => 'noneedlogin',
'title' => '免登录上传',
'type' => 'checkbox',
'content' => [
'api' => 'API',
'index' => '前台',
'admin' => '后台',
],
'value' => 'api,index,admin',
'rule' => '',
'msg' => '',
'tip' => '',
'ok' => '',
'extend' => '',
],
[
'name' => 'uploadmodule',
'title' => '上传开启模块',
'type' => 'checkbox',
'content' => [
'api' => 'API',
'index' => '前台',
'admin' => '后台',
],
'value' => 'api,index,admin',
'rule' => '',
'msg' => '',
'tip' => '',
'ok' => '',
'extend' => '',
],
];

View File

@ -0,0 +1,455 @@
<?php
namespace addons\cos\controller;
use app\common\exception\UploadException;
use app\common\library\Upload;
use app\common\model\Attachment;
use Qcloud\Cos\Client;
use Qcloud\Cos\Signature;
use think\addons\Controller;
use think\Config;
/**
* COS云储存
*
*/
class Index extends Controller
{
protected $cosConfig = [];
public function _initialize()
{
//跨域检测
check_cors_request();
parent::_initialize();
Config::set('default_return_type', 'json');
$config = get_addon_config('cos');
$this->cosConfig = array(
'region' => $config['region'],
'schema' => 'https', //协议头部,默认为http
'credentials' => array(
'secretId' => $config['secretId'],
'secretKey' => $config['secretKey']
)
);
}
public function index()
{
Config::set('default_return_type', 'html');
$this->error("当前插件暂无前台页面");
}
public function paramsApi($md5,$name,$type,$fileSize,$chunk,$partSize,$costoken){
$this->costoken = $costoken;
$this->check();
$config = get_addon_config('cos');
$key = (new Upload())->getSavekey($config['savekey'], $name, $md5);
$key = ltrim($key, "/");
$params = [
'key' => $key,
'md5' => $md5
];
if ($chunk) {
$oss = new Client($this->cosConfig);
$result = $oss->createMultipartUpload(array(
'Bucket' => $config['bucket'],
'Key' => $key,
));
$uploadId = $result['UploadId'];
$sig = new Signature($config['secretId'], $config['secretKey']);
$i = 0;
$size_count = $fileSize;
$values = array();
while ($size_count > 0) {
$size_count -= $partSize;
$values[] = array(
$partSize * $i,
($size_count > 0) ? $partSize : ($size_count + $partSize),
);
$i++;
}
$params['key'] = $key;
$params['uploadId'] = $uploadId;
$params['partsAuthorization'] = [];
$date = gmdate('D, d M Y H:i:s \G\M\T');
foreach ($values as $index => $part) {
$partNumber = $index + 1;
$options = array(
'Bucket' => $config['bucket'],
'Key' => $key,
'UploadId' => $uploadId,
'PartNumber' => $partNumber,
'Body' => ''
);
$command = $oss->getCommand('uploadPart', $options);
$request = $oss->commandToRequestTransformer($command);
$authorization = $sig->createAuthorization($request);
$params['partsAuthorization'][$index] = $authorization;
}
$params['date'] = $date;
} else {
if ($config['uploadmode'] == 'client') {
$expiretime = time() + $config['expire'];
$expiration = gmdate("Y-m-d\TH:i:s.414\Z", $expiretime);
$keytime = (time() - 60) . ';' . $expiretime;
$policy = json_encode([
'expiration' => $expiration,
'conditions' => [
['q-sign-algorithm' => 'sha1'],
['q-ak' => $config['secretId']],
['q-sign-time' => $keytime]
]
]);
$signature = hash_hmac('sha1', sha1($policy), hash_hmac('sha1', $keytime, $config['secretKey']));
$params = [
'key' => $key,
'policy' => base64_encode($policy),
'q-sign-algorithm' => 'sha1',
'q-ak' => $config['secretId'],
'q-key-time' => $keytime,
'q-sign-time' => $keytime,
'q-signature' => $signature
];
}
}
return $params;
}
public function params()
{
$this->check();
$config = get_addon_config('cos');
$name = $this->request->post('name');
$md5 = $this->request->post('md5');
$chunk = $this->request->post('chunk');
$key = (new Upload())->getSavekey($config['savekey'], $name, $md5);
$key = ltrim($key, "/");
$params = [
'key' => $key,
'md5' => $md5
];
if ($chunk) {
$fileSize = $this->request->post('size');
$oss = new Client($this->cosConfig);
$result = $oss->createMultipartUpload(array(
'Bucket' => $config['bucket'],
'Key' => $key,
));
$uploadId = $result['UploadId'];
$sig = new Signature($config['secretId'], $config['secretKey']);
$partSize = $this->request->post("chunksize");
$i = 0;
$size_count = $fileSize;
$values = array();
while ($size_count > 0) {
$size_count -= $partSize;
$values[] = array(
$partSize * $i,
($size_count > 0) ? $partSize : ($size_count + $partSize),
);
$i++;
}
$params['key'] = $key;
$params['uploadId'] = $uploadId;
$params['partsAuthorization'] = [];
$date = gmdate('D, d M Y H:i:s \G\M\T');
foreach ($values as $index => $part) {
$partNumber = $index + 1;
$options = array(
'Bucket' => $config['bucket'],
'Key' => $key,
'UploadId' => $uploadId,
'PartNumber' => $partNumber,
'Body' => ''
);
$command = $oss->getCommand('uploadPart', $options);
$request = $oss->commandToRequestTransformer($command);
$authorization = $sig->createAuthorization($request);
$params['partsAuthorization'][$index] = $authorization;
}
$params['date'] = $date;
} else {
if ($config['uploadmode'] == 'client') {
$expiretime = time() + $config['expire'];
$expiration = gmdate("Y-m-d\TH:i:s.414\Z", $expiretime);
$keytime = (time() - 60) . ';' . $expiretime;
$policy = json_encode([
'expiration' => $expiration,
'conditions' => [
['q-sign-algorithm' => 'sha1'],
['q-ak' => $config['secretId']],
['q-sign-time' => $keytime]
]
]);
$signature = hash_hmac('sha1', sha1($policy), hash_hmac('sha1', $keytime, $config['secretKey']));
$params = [
'key' => $key,
'policy' => base64_encode($policy),
'q-sign-algorithm' => 'sha1',
'q-ak' => $config['secretId'],
'q-key-time' => $keytime,
'q-sign-time' => $keytime,
'q-signature' => $signature
];
}
}
$this->success('', null, $params);
return;
}
/**
* 服务器中转上传文件
* 上传分片
* 合并分片
* @param bool $isApi
*/
public function upload($isApi = false)
{
if ($isApi === true) {
if (!$this->auth->isLogin()) {
$this->error("请登录后再进行操作");
}
} else {
$this->check();
}
$config = get_addon_config('cos');
$oss = new Client($this->cosConfig);
//检测删除文件或附件
$checkDeleteFile = function ($attachment, $upload, $force = false) use ($config) {
//如果设定为不备份则删除文件和记录 或 强制删除
if ((isset($config['serverbackup']) && !$config['serverbackup']) || $force) {
if ($attachment && !empty($attachment['id'])) {
$attachment->delete();
}
if ($upload) {
//文件绝对路径
$filePath = $upload->getFile()->getRealPath() ?: $upload->getFile()->getPathname();
@unlink($filePath);
}
}
};
$chunkid = $this->request->post("chunkid");
if ($chunkid) {
$action = $this->request->post("action");
$chunkindex = $this->request->post("chunkindex/d");
$chunkcount = $this->request->post("chunkcount/d");
$filesize = $this->request->post("filesize");
$filename = $this->request->post("filename");
$method = $this->request->method(true);
$key = $this->request->post("key");
$uploadId = $this->request->post("uploadId");
if ($action == 'merge') {
$attachment = null;
$upload = null;
//合并分片
if ($config['uploadmode'] == 'server') {
//合并分片文件
try {
$upload = new Upload();
$attachment = $upload->merge($chunkid, $chunkcount, $filename);
} catch (UploadException $e) {
$this->error($e->getMessage());
}
}
$etags = $this->request->post("etags/a", []);
if (count($etags) != $chunkcount) {
$checkDeleteFile($attachment, $upload, true);
$this->error("分片数据错误");
}
$listParts = [];
for ($i = 0; $i < $chunkcount; $i++) {
$listParts[] = array("PartNumber" => $i + 1, "ETag" => $etags[$i]);
}
try {
$result = $oss->completeMultipartUpload(array(
'Bucket' => $config['bucket'],
'Key' => $key,
'UploadId' => $uploadId,
'Parts' => $listParts
)
);
} catch (\Exception $e) {
$checkDeleteFile($attachment, $upload, true);
$this->error($e->getMessage());
}
if (!isset($result['Key'])) {
$checkDeleteFile($attachment, $upload, true);
$this->error("上传失败");
} else {
$checkDeleteFile($attachment, $upload);
$this->success("上传成功", '', ['url' => "/" . $key, 'fullurl' => cdnurl("/" . $key, true)]);
}
} else {
//默认普通上传文件
$file = $this->request->file('file');
try {
$upload = new Upload($file);
$file = $upload->chunk($chunkid, $chunkindex, $chunkcount);
} catch (UploadException $e) {
$this->error($e->getMessage());
}
try {
$params = array(
'Bucket' => $config['bucket'],
'Key' => $key,
'UploadId' => $uploadId,
'PartNumber' => $chunkindex + 1,
'Body' => $file->fread($file->getSize())
);
$ret = $oss->uploadPart($params);
$etag = $ret['ETag'];
} catch (\Exception $e) {
$this->error($e->getMessage());
}
$this->success("上传成功", "", [], 3, ['ETag' => $etag]);
}
} else {
$attachment = null;
//默认普通上传文件
$file = $this->request->file('file');
try {
$upload = new Upload($file);
$attachment = $upload->upload();
} catch (UploadException $e) {
$this->error($e->getMessage());
}
//文件绝对路径
$filePath = $upload->getFile()->getRealPath() ?: $upload->getFile()->getPathname();
$url = $attachment->url;
try {
$ret = $oss->upload($config['bucket'], ltrim($attachment->url, "/"), $upload->getFile());
//成功不做任何操作
} catch (\Exception $e) {
$checkDeleteFile($attachment, $upload, true);
$this->error("上传失败");
}
$checkDeleteFile($attachment, $upload);
$this->success("上传成功", '', ['url' => $url, 'fullurl' => cdnurl($url, true)]);
}
return;
}
public function noticeApi($size,$name,$md5,$type,$url,$width,$height,$category,$costoken,$uid){
$this->costoken = $costoken;
$this->check();
$category = array_key_exists($category, config('site.attachmentcategory') ?? []) ? $category : '';
$suffix = strtolower(pathinfo($name, PATHINFO_EXTENSION));
$suffix = $suffix && preg_match("/^[a-zA-Z0-9]+$/", $suffix) ? $suffix : 'file';
$attachment = Attachment::where('url', $url)->where('storage', 'cos')->find();
if (!$attachment) {
$params = array(
'category' => $category,
'admin_id' => 0,
'user_id' => $uid,
'filesize' => $size,
'filename' => $name,
'imagewidth' => $width,
'imageheight' => $height,
'imagetype' => $suffix,
'imageframes' => 0,
'mimetype' => $type,
'url' => $url,
'uploadtime' => time(),
'storage' => 'cos',
'sha1' => $md5,
);
Attachment::create($params, true);
}
$this->success();
return;
}
/**
* 回调
*/
public function notify()
{
$this->check();
$size = $this->request->post('size/d');
$name = $this->request->post('name', '');
$md5 = $this->request->post('md5', '');
$type = $this->request->post('type', '');
$url = $this->request->post('url', '');
$width = $this->request->post('width/d');
$height = $this->request->post('height/d');
$category = $this->request->post('category', '');
$category = array_key_exists($category, config('site.attachmentcategory') ?? []) ? $category : '';
$suffix = strtolower(pathinfo($name, PATHINFO_EXTENSION));
$suffix = $suffix && preg_match("/^[a-zA-Z0-9]+$/", $suffix) ? $suffix : 'file';
$attachment = Attachment::where('url', $url)->where('storage', 'cos')->find();
if (!$attachment) {
$params = array(
'category' => $category,
'admin_id' => (int)session('admin.id'),
'user_id' => (int)cookie('uid'),
'filesize' => $size,
'filename' => $name,
'imagewidth' => $width,
'imageheight' => $height,
'imagetype' => $suffix,
'imageframes' => 0,
'mimetype' => $type,
'url' => $url,
'uploadtime' => time(),
'storage' => 'cos',
'sha1' => $md5,
);
Attachment::create($params, true);
}
$this->success();
return;
}
/**
* 检查签名是否正确或过期
*/
protected function check()
{
$costoken = $this->request->post('costoken', '', 'trim');
$costoken = $costoken?$costoken:$this->costoken;
if (!$costoken) {
$this->error("参数不正确");
}
$config = get_addon_config('cos');
list($appId, $sign, $data) = explode(':', $costoken);
if (!$appId || !$sign || !$data) {
$this->error("参数不正确");
}
if ($appId !== $config['appId']) {
$this->error("参数不正确");
}
if ($sign !== base64_encode(hash_hmac('sha1', base64_decode($data), $config['secretKey'], true))) {
$this->error("签名不正确");
}
$json = json_decode(base64_decode($data), true);
if ($json['deadline'] < time()) {
$this->error("请求已经超时");
}
}
}

10
addons/cos/info.ini Normal file
View File

@ -0,0 +1,10 @@
name = cos
title = 腾讯COS云存储
intro = 使用腾讯COS云存储作为默认云储存
author = FastAdmin
website = https://www.fastadmin.net
version = 1.2.0
state = 1
url = /addons/cos
license = regular
licenseto = 10734

View File

@ -0,0 +1,19 @@
Copyright (c) 2015 Michael Dowling, https://github.com/mtdowling <mtdowling@gmail.com>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.

View File

@ -0,0 +1,134 @@
# Guzzle Commands
[![License](https://poser.pugx.org/guzzlehttp/command/license)](https://packagist.org/packages/guzzlehttp/command)
[![Build Status](https://travis-ci.org/guzzle/command.svg?branch=master)](https://travis-ci.org/guzzle/command)
[![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/guzzle/command/badges/quality-score.png?b=master)](https://scrutinizer-ci.com/g/guzzle/command/?branch=master)
[![Code Coverage](https://scrutinizer-ci.com/g/guzzle/command/badges/coverage.png?b=master)](https://scrutinizer-ci.com/g/guzzle/command/?branch=master)
[![SensioLabsInsight](https://insight.sensiolabs.com/projects/7a93338e-50cd-42f7-9299-17c44d92148f/mini.png)](https://insight.sensiolabs.com/projects/7a93338e-50cd-42f7-9299-17c44d92148f)
[![Latest Stable Version](https://poser.pugx.org/guzzlehttp/command/v/stable)](https://packagist.org/packages/guzzlehttp/command)
[![Latest Unstable Version](https://poser.pugx.org/guzzlehttp/command/v/unstable)](https://packagist.org/packages/guzzlehttp/command)
[![Total Downloads](https://poser.pugx.org/guzzlehttp/command/downloads)](https://packagist.org/packages/guzzlehttp/command)
This library uses Guzzle (``guzzlehttp/guzzle``, version 6.x) and provides the
foundations to create fully-featured web service clients by abstracting Guzzle
HTTP **requests** and **responses** into higher-level **commands** and
**results**. A **middleware** system, analogous to — but separate from — the one
in the HTTP layer may be used to customize client behavior when preparing
commands into requests and processing responses into results.
### Commands
Key-value pair objects representing an operation of a web service. Commands have a name and a set of parameters.
### Results
Key-value pair objects representing the processed result of executing an operation of a web service.
## Installing
This project can be installed using Composer:
``composer require guzzlehttp/command``
For **Guzzle 5**, use ``composer require guzzlehttp/command:0.8.*``. The source
code for the Guzzle 5 version is available on the
`0.8 branch <https://github.com/guzzle/command/tree/0.8>`_.
**Note:** If Composer is not
`installed globally <https://getcomposer.org/doc/00-intro.md#globally>`_,
then you may need to run the preceding Composer commands using
``php composer.phar`` (where ``composer.phar`` is the path to your copy of
Composer), instead of just ``composer``.
## Service Clients
Service Clients are web service clients that implement the
``GuzzleHttp\Command\ServiceClientInterface`` and use an underlying Guzzle HTTP
client (``GuzzleHttp\Client``) to communicate with the service. Service clients
create and execute **commands** (``GuzzleHttp\Command\CommandInterface``),
which encapsulate operations within the web service, including the operation
name and parameters. This library provides a generic implementation of a service
client: the ``GuzzleHttp\Command\ServiceClient`` class.
## Instantiating a Service Client
@TODO Add documentation
* ``ServiceClient``'s constructor
* Transformer functions (``$commandToRequestTransformer`` and ``$responseToResultTransformer``)
* The ``HandlerStack``
## Executing Commands
Service clients create command objects using the ``getCommand()`` method.
```php
$commandName = 'foo';
$arguments = ['baz' => 'bar'];
$command = $client->getCommand($commandName, $arguments);
```
After creating a command, you may execute the command using the ``execute()``
method of the client.
```php
$result = $client->execute($command);
```
The result of executing a command will be a ``GuzzleHttp\Command\ResultInterface``
object. Result objects are ``ArrayAccess``-ible and contain the data parsed from
HTTP response.
Service clients have magic methods that act as shortcuts to executing commands
by name without having to create the ``Command`` object in a separate step
before executing it.
```php
$result = $client->foo(['baz' => 'bar']);
```
## Asynchronous Commands
@TODO Add documentation
* ``-Async`` suffix for client methods
* Promises
```php
// Create and execute an asynchronous command.
$command = $command = $client->getCommand('foo', ['baz' => 'bar']);
$promise = $client->executeAsync($command);
// Use asynchronous commands with magic methods.
$promise = $client->fooAsync(['baz' => 'bar']);
```
@TODO Add documentation
* ``wait()``-ing on promises.
```php
$result = $promise->wait();
echo $result['fizz']; //> 'buzz'
```
## Concurrent Requests
@TODO Add documentation
* ``executeAll()``
* ``executeAllAsync()``.
* Options (``fulfilled``, ``rejected``, ``concurrency``)
## Middleware: Extending the Client
Middleware can be added to the service client or underlying HTTP client to
implement additional behavior and customize the ``Command``-to-``Result`` and
``Request``-to-``Response`` lifecycles, respectively.
## Todo
* Middleware system and command vs request layers
* The ``HandlerStack``

View File

@ -0,0 +1,36 @@
{
"name": "guzzlehttp/command",
"description": "Provides the foundation for building command-based web service clients",
"license": "MIT",
"authors": [
{
"name": "Michael Dowling",
"email": "mtdowling@gmail.com",
"homepage": "https://github.com/mtdowling"
},
{
"name": "Jeremy Lindblom",
"email": "jeremeamia@gmail.com",
"homepage": "https://github.com/jeremeamia"
}
],
"require": {
"php": ">=5.5.0",
"guzzlehttp/guzzle": "^6.2",
"guzzlehttp/promises": "~1.3",
"guzzlehttp/psr7": "~1.0"
},
"require-dev": {
"phpunit/phpunit": "~4.0|~5.0"
},
"autoload": {
"psr-4": {
"GuzzleHttp\\Command\\": "src/"
}
},
"extra": {
"branch-alias": {
"dev-master": "0.9-dev"
}
}
}

View File

@ -0,0 +1,55 @@
<?php
namespace GuzzleHttp\Command;
use GuzzleHttp\HandlerStack;
/**
* Default command implementation.
*/
class Command implements CommandInterface
{
use HasDataTrait;
/** @var string */
private $name;
/** @var HandlerStack */
private $handlerStack;
/**
* @param string $name Name of the command
* @param array $args Arguments to pass to the command
* @param HandlerStack $handlerStack Stack of middleware for the command
*/
public function __construct(
$name,
array $args = [],
HandlerStack $handlerStack = null
) {
$this->name = $name;
$this->data = $args;
$this->handlerStack = $handlerStack;
}
public function getHandlerStack()
{
return $this->handlerStack;
}
public function getName()
{
return $this->name;
}
public function hasParam($name)
{
return array_key_exists($name, $this->data);
}
public function __clone()
{
if ($this->handlerStack) {
$this->handlerStack = clone $this->handlerStack;
}
}
}

View File

@ -0,0 +1,39 @@
<?php
namespace GuzzleHttp\Command;
use GuzzleHttp\HandlerStack;
/**
* A command object encapsulates the input parameters used to control the
* creation of a HTTP request and processing of a HTTP response.
*
* Using the getParams() method will return the input parameters of the command
* as an associative array.
*/
interface CommandInterface extends \ArrayAccess, \IteratorAggregate, \Countable, ToArrayInterface
{
/**
* Retrieves the handler stack specific to this command's execution.
*
* This can be used to add middleware that is specific to the command instance.
*
* @return HandlerStack
*/
public function getHandlerStack();
/**
* Get the name of the command.
*
* @return string
*/
public function getName();
/**
* Check if the command has a parameter by name.
*
* @param string $name Name of the parameter to check.
*
* @return bool
*/
public function hasParam($name);
}

View File

@ -0,0 +1,7 @@
<?php
namespace GuzzleHttp\Command\Exception;
/**
* Exception encountered when a 4xx level response is received for a request
*/
class CommandClientException extends CommandException {}

View File

@ -0,0 +1,109 @@
<?php
namespace GuzzleHttp\Command\Exception;
use GuzzleHttp\Exception\GuzzleException;
use GuzzleHttp\Exception\RequestException;
use GuzzleHttp\Command\CommandInterface;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;
/**
* Exception encountered while executing a command.
*/
class CommandException extends \RuntimeException implements GuzzleException
{
/** @var CommandInterface */
private $command;
/** @var RequestInterface */
private $request;
/** @var ResponseInterface */
private $response;
/**
* @param CommandInterface $command
* @param \Exception $prev
* @return CommandException
*/
public static function fromPrevious(CommandInterface $command, \Exception $prev)
{
// If the exception is already a command exception, return it.
if ($prev instanceof self && $command === $prev->getCommand()) {
return $prev;
}
// If the exception is a RequestException, get the Request and Response.
$request = $response = null;
if ($prev instanceof RequestException) {
$request = $prev->getRequest();
$response = $prev->getResponse();
}
// Throw a more specific exception for 4XX or 5XX responses.
$class = self::class;
$statusCode = $response ? $response->getStatusCode() : 0;
if ($statusCode >= 400 && $statusCode < 500) {
$class = CommandClientException::class;
} elseif ($statusCode >= 500 && $statusCode < 600) {
$class = CommandServerException::class;
}
// Prepare the message.
$message = 'There was an error executing the ' . $command->getName()
. ' command: ' . $prev->getMessage();
// Create the exception.
return new $class($message, $command, $prev, $request, $response);
}
/**
* @param string $message Exception message
* @param CommandInterface $command
* @param \Exception $previous Previous exception (if any)
* @param RequestInterface $request
* @param ResponseInterface $response
*/
public function __construct(
$message,
CommandInterface $command,
\Exception $previous = null,
RequestInterface $request = null,
ResponseInterface $response = null
) {
$this->command = $command;
$this->request = $request;
$this->response = $response;
parent::__construct($message, 0, $previous);
}
/**
* Gets the command that failed.
*
* @return CommandInterface
*/
public function getCommand()
{
return $this->command;
}
/**
* Gets the request that caused the exception
*
* @return RequestInterface|null
*/
public function getRequest()
{
return $this->request;
}
/**
* Gets the associated response
*
* @return ResponseInterface|null
*/
public function getResponse()
{
return $this->response;
}
}

View File

@ -0,0 +1,7 @@
<?php
namespace GuzzleHttp\Command\Exception;
/**
* Exception encountered when a 5xx level response is received for a request
*/
class CommandServerException extends CommandException {}

View File

@ -0,0 +1,60 @@
<?php
namespace GuzzleHttp\Command;
/**
* Basic collection behavior for Command and Result objects.
*
* The methods in the class are primarily for implementing the ArrayAccess,
* Countable, and IteratorAggregate interfaces.
*/
trait HasDataTrait
{
/** @var array Data stored in the collection. */
protected $data;
public function __toString()
{
return print_r($this, true);
}
public function __debugInfo()
{
return $this->data;
}
public function offsetExists($offset)
{
return array_key_exists($offset, $this->data);
}
public function offsetGet($offset)
{
return isset($this->data[$offset]) ? $this->data[$offset] : null;
}
public function offsetSet($offset, $value)
{
$this->data[$offset] = $value;
}
public function offsetUnset($offset)
{
unset($this->data[$offset]);
}
public function count()
{
return count($this->data);
}
public function getIterator()
{
return new \ArrayIterator($this->data);
}
public function toArray()
{
return $this->data;
}
}

View File

@ -0,0 +1,18 @@
<?php
namespace GuzzleHttp\Command;
/**
* Default command implementation.
*/
class Result implements ResultInterface
{
use HasDataTrait;
/**
* @param array $data
*/
public function __construct(array $data = [])
{
$this->data = $data;
}
}

View File

@ -0,0 +1,9 @@
<?php
namespace GuzzleHttp\Command;
/**
* An array-like object that represents the result of executing a command.
*/
interface ResultInterface extends \ArrayAccess, \IteratorAggregate, \Countable, ToArrayInterface
{
}

View File

@ -0,0 +1,217 @@
<?php
namespace GuzzleHttp\Command;
use GuzzleHttp\ClientInterface as HttpClient;
use GuzzleHttp\Command\Exception\CommandException;
use GuzzleHttp\HandlerStack;
use GuzzleHttp\Promise;
use GuzzleHttp\Promise\PromiseInterface;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;
/**
* The Guzzle ServiceClient serves as the foundation for creating web service
* clients that interact with RPC-style APIs.
*/
class ServiceClient implements ServiceClientInterface
{
/** @var HttpClient HTTP client used to send requests */
private $httpClient;
/** @var HandlerStack */
private $handlerStack;
/** @var callable */
private $commandToRequestTransformer;
/** @var callable */
private $responseToResultTransformer;
/**
* Instantiates a Guzzle ServiceClient for making requests to a web service.
*
* @param HttpClient $httpClient A fully-configured Guzzle HTTP client that
* will be used to perform the underlying HTTP requests.
* @param callable $commandToRequestTransformer A callable that transforms
* a Command into a Request. The function should accept a
* `GuzzleHttp\Command\CommandInterface` object and return a
* `Psr\Http\Message\RequestInterface` object.
* @param callable $responseToResultTransformer A callable that transforms a
* Response into a Result. The function should accept a
* `Psr\Http\Message\ResponseInterface` object (and optionally a
* `Psr\Http\Message\RequestInterface` object) and return a
* `GuzzleHttp\Command\ResultInterface` object.
* @param HandlerStack $commandHandlerStack A Guzzle HandlerStack, which can
* be used to add command-level middleware to the service client.
*/
public function __construct(
HttpClient $httpClient,
callable $commandToRequestTransformer,
callable $responseToResultTransformer,
HandlerStack $commandHandlerStack = null
) {
$this->httpClient = $httpClient;
$this->commandToRequestTransformer = $commandToRequestTransformer;
$this->responseToResultTransformer = $responseToResultTransformer;
$this->handlerStack = $commandHandlerStack ?: new HandlerStack();
$this->handlerStack->setHandler($this->createCommandHandler());
}
public function getHttpClient()
{
return $this->httpClient;
}
public function getHandlerStack()
{
return $this->handlerStack;
}
public function getCommand($name, array $params = [])
{
return new Command($name, $params, clone $this->handlerStack);
}
public function execute(CommandInterface $command)
{
return $this->executeAsync($command)->wait();
}
public function executeAsync(CommandInterface $command)
{
$stack = $command->getHandlerStack() ?: $this->handlerStack;
$handler = $stack->resolve();
return $handler($command);
}
public function executeAll($commands, array $options = [])
{
// Modify provided callbacks to track results.
$results = [];
$options['fulfilled'] = function ($v, $k) use (&$results, $options) {
if (isset($options['fulfilled'])) {
$options['fulfilled']($v, $k);
}
$results[$k] = $v;
};
$options['rejected'] = function ($v, $k) use (&$results, $options) {
if (isset($options['rejected'])) {
$options['rejected']($v, $k);
}
$results[$k] = $v;
};
// Execute multiple commands synchronously, then sort and return the results.
return $this->executeAllAsync($commands, $options)
->then(function () use (&$results) {
ksort($results);
return $results;
})
->wait();
}
public function executeAllAsync($commands, array $options = [])
{
// Apply default concurrency.
if (!isset($options['concurrency'])) {
$options['concurrency'] = 25;
}
// Convert the iterator of commands to a generator of promises.
$commands = Promise\iter_for($commands);
$promises = function () use ($commands) {
foreach ($commands as $key => $command) {
if (!$command instanceof CommandInterface) {
throw new \InvalidArgumentException('The iterator must '
. 'yield instances of ' . CommandInterface::class);
}
yield $key => $this->executeAsync($command);
}
};
// Execute the commands using a pool.
return (new Promise\EachPromise($promises(), $options))->promise();
}
/**
* Creates and executes a command for an operation by name.
*
* @param string $name Name of the command to execute.
* @param array $args Arguments to pass to the getCommand method.
*
* @return ResultInterface|PromiseInterface
* @see \GuzzleHttp\Command\ServiceClientInterface::getCommand
*/
public function __call($name, array $args)
{
$args = isset($args[0]) ? $args[0] : [];
if (substr($name, -5) === 'Async') {
$command = $this->getCommand(substr($name, 0, -5), $args);
return $this->executeAsync($command);
} else {
return $this->execute($this->getCommand($name, $args));
}
}
/**
* Defines the main handler for commands that uses the HTTP client.
*
* @return callable
*/
private function createCommandHandler()
{
return function (CommandInterface $command) {
return Promise\coroutine(function () use ($command) {
// Prepare the HTTP options.
$opts = $command['@http'] ?: [];
unset($command['@http']);
try {
// Prepare the request from the command and send it.
$request = $this->transformCommandToRequest($command);
$promise = $this->httpClient->sendAsync($request, $opts);
// Create a result from the response.
$response = (yield $promise);
yield $this->transformResponseToResult($response, $request, $command);
} catch (\Exception $e) {
throw CommandException::fromPrevious($command, $e);
}
});
};
}
/**
* Transforms a Command object into a Request object.
*
* @param CommandInterface $command
* @return RequestInterface
*/
private function transformCommandToRequest(CommandInterface $command)
{
$transform = $this->commandToRequestTransformer;
return $transform($command);
}
/**
* Transforms a Response object, also using data from the Request object,
* into a Result object.
*
* @param ResponseInterface $response
* @param RequestInterface $request
* @param CommandInterface $command
* @return ResultInterface
*/
private function transformResponseToResult(
ResponseInterface $response,
RequestInterface $request,
CommandInterface $command
) {
$transform = $this->responseToResultTransformer;
return $transform($response, $request, $command);
}
}

View File

@ -0,0 +1,92 @@
<?php
namespace GuzzleHttp\Command;
use GuzzleHttp\ClientInterface;
use GuzzleHttp\Command\Exception\CommandException;
use GuzzleHttp\HandlerStack;
use GuzzleHttp\Promise\PromiseInterface;
/**
* Web service client interface.
*/
interface ServiceClientInterface
{
/**
* Create a command for an operation name.
*
* Special keys may be set on the command to control how it behaves.
* Implementations SHOULD be able to utilize the following keys or throw
* an exception if unable.
*
* @param string $name Name of the operation to use in the command
* @param array $args Arguments to pass to the command
*
* @return CommandInterface
* @throws \InvalidArgumentException if no command can be found by name
*/
public function getCommand($name, array $args = []);
/**
* Execute a single command.
*
* @param CommandInterface $command Command to execute
*
* @return ResultInterface The result of the executed command
* @throws CommandException
*/
public function execute(CommandInterface $command);
/**
* Execute a single command asynchronously
*
* @param CommandInterface $command Command to execute
*
* @return PromiseInterface A Promise that resolves to a Result.
*/
public function executeAsync(CommandInterface $command);
/**
* Executes multiple commands concurrently using a fixed pool size.
*
* @param array|\Iterator $commands Array or iterator that contains
* CommandInterface objects to execute with the client.
* @param array $options Associative array of options to apply.
* - concurrency: (int) Max number of commands to execute concurrently.
* - fulfilled: (callable) Function to invoke when a command completes.
* - rejected: (callable) Function to invoke when a command fails.
*
* @return array
* @see GuzzleHttp\Command\ServiceClientInterface::createPool for options.
*/
public function executeAll($commands, array $options = []);
/**
* Executes multiple commands concurrently and asynchronously using a
* fixed pool size.
*
* @param array|\Iterator $commands Array or iterator that contains
* CommandInterface objects to execute with the client.
* @param array $options Associative array of options to apply.
* - concurrency: (int) Max number of commands to execute concurrently.
* - fulfilled: (callable) Function to invoke when a command completes.
* - rejected: (callable) Function to invoke when a command fails.
*
* @return PromiseInterface
* @see GuzzleHttp\Command\ServiceClientInterface::createPool for options.
*/
public function executeAllAsync($commands, array $options = []);
/**
* Get the HTTP client used to send requests for the web service client
*
* @return ClientInterface
*/
public function getHttpClient();
/**
* Get the HandlerStack which can be used to add middleware to the client.
*
* @return HandlerStack
*/
public function getHandlerStack();
}

View File

@ -0,0 +1,16 @@
<?php
namespace GuzzleHttp\Command;
/**
* An object that can be represented as an array
*/
interface ToArrayInterface
{
/**
* Get the array representation of an object
*
* @return array
*/
public function toArray();
}

View File

@ -0,0 +1,4 @@
phpunit.xml
composer.lock
vendor/
artifacts/

View File

@ -0,0 +1,23 @@
sudo: false
language: php
php:
- 5.5
- 5.6
- 7.0
- 7.1
- hhvm
- nightly
matrix:
fast_finish: true
allow_failures:
- php: 7.1
- php: hhvm
- php: nightly
before_script:
- composer self-update
- composer install --no-interaction --prefer-source --dev
script: make test

View File

@ -0,0 +1,351 @@
# Change Log
## [1.1.3](https://github.com/guzzle/guzzle-services/tree/1.1.3) (2017-10-06)
[Full Changelog](https://github.com/guzzle/guzzle-services/compare/1.1.2...HEAD)
**Closed issues:**
- Parameter type configuration causes issue when filters change input type [\#147](https://github.com/guzzle/guzzle-services/issues/147)
**Merged pull requests:**
- Use wire name when visiting array [\#152](https://github.com/guzzle/guzzle-services/pull/152) ([my2ter](https://github.com/my2ter))
- Adding descriptive error message on parameter failure [\#144](https://github.com/guzzle/guzzle-services/pull/144) ([igorsantos07](https://github.com/igorsantos07))
## [1.1.2](https://github.com/guzzle/guzzle-services/tree/1.1.2) (2017-05-19)
[Full Changelog](https://github.com/guzzle/guzzle-services/compare/1.1.1...1.1.2)
**Closed issues:**
- Default values ignored in 1.1 [\#146](https://github.com/guzzle/guzzle-services/issues/146)
- Operations extends is broken in 1.1.1 [\#145](https://github.com/guzzle/guzzle-services/issues/145)
## [1.1.1](https://github.com/guzzle/guzzle-services/tree/1.1.1) (2017-05-15)
[Full Changelog](https://github.com/guzzle/guzzle-services/compare/1.1.0...1.1.1)
**Closed issues:**
- Filters are applied twice [\#134](https://github.com/guzzle/guzzle-services/issues/134)
- Is it possible to NOT urlencode a specific uri parameter value? [\#97](https://github.com/guzzle/guzzle-services/issues/97)
**Merged pull requests:**
- Fix minor typos in documentation. [\#139](https://github.com/guzzle/guzzle-services/pull/139) ([forevermatt](https://github.com/forevermatt))
- Do not mutate command at validation [\#135](https://github.com/guzzle/guzzle-services/pull/135) ([danizord](https://github.com/danizord))
- Added tests for JSON array of arrays and array of objects [\#131](https://github.com/guzzle/guzzle-services/pull/131) ([selfcatering](https://github.com/selfcatering))
- Allow filters on response model [\#138](https://github.com/guzzle/guzzle-services/pull/138) ([danizord](https://github.com/danizord))
- Exposing properties to a parent class [\#136](https://github.com/guzzle/guzzle-services/pull/136) ([Napas](https://github.com/Napas))
## [1.1.0](https://github.com/guzzle/guzzle-services/tree/1.1.0) (2017-01-31)
[Full Changelog](https://github.com/guzzle/guzzle-services/compare/1.0.1...1.1.0)
**Closed issues:**
- Grab a list of objects when they are not located at top level of a json response \(HATEOAS\) [\#90](https://github.com/guzzle/guzzle-services/issues/90)
- Regression of Issue \#51 - XmlLocation response not handling multiple tags of the same name correctly [\#82](https://github.com/guzzle/guzzle-services/issues/82)
- PUT requests with parameters with location of "postField" result in Exception [\#78](https://github.com/guzzle/guzzle-services/issues/78)
- Allow to provide Post Body as an Array [\#77](https://github.com/guzzle/guzzle-services/issues/77)
**Merged pull requests:**
- Bring more flexibility to query params serialization [\#132](https://github.com/guzzle/guzzle-services/pull/132) ([bakura10](https://github.com/bakura10))
- Allow to fix validation for parameters with a format [\#130](https://github.com/guzzle/guzzle-services/pull/130) ([bakura10](https://github.com/bakura10))
## [1.0.1](https://github.com/guzzle/guzzle-services/tree/1.0.1) (2017-01-13)
[Full Changelog](https://github.com/guzzle/guzzle-services/compare/1.0.0...1.0.1)
**Implemented enhancements:**
- Set a name when pushing ValidatedDescriptionHandler to stack [\#127](https://github.com/guzzle/guzzle-services/issues/127)
**Fixed bugs:**
- combine method in Uri [\#101](https://github.com/guzzle/guzzle-services/issues/101)
- Undefined Variable [\#88](https://github.com/guzzle/guzzle-services/issues/88)
- Regression in array parameter serialization [\#128](https://github.com/guzzle/guzzle-services/issues/128)
- Unable to POST multiple multipart parameters [\#123](https://github.com/guzzle/guzzle-services/issues/123)
**Closed issues:**
- Tag pre 1.0.0 release [\#121](https://github.com/guzzle/guzzle-services/issues/121)
- Adjust inline documentation of Parameter [\#120](https://github.com/guzzle/guzzle-services/issues/120)
- postField location not recognized after upgrading to 1.0 [\#119](https://github.com/guzzle/guzzle-services/issues/119)
- Create a new release for the guzzle6 branch [\#118](https://github.com/guzzle/guzzle-services/issues/118)
- Compatibility problem with PHP7.0 ? [\#116](https://github.com/guzzle/guzzle-services/issues/116)
- What is the correct type of Parameter static option [\#113](https://github.com/guzzle/guzzle-services/issues/113)
- Improve the construction of baseUri in Description [\#112](https://github.com/guzzle/guzzle-services/issues/112)
- Please create version tag for current master branch [\#110](https://github.com/guzzle/guzzle-services/issues/110)
- Problems with postField params [\#98](https://github.com/guzzle/guzzle-services/issues/98)
**Merged pull requests:**
- Fix serialization of query params [\#129](https://github.com/guzzle/guzzle-services/pull/129) ([bakura10](https://github.com/bakura10))
## [1.0.0](https://github.com/guzzle/guzzle-services/tree/1.0.0) (2016-11-24)
[Full Changelog](https://github.com/guzzle/guzzle-services/compare/0.6.0...1.0.0)
**Closed issues:**
- AbstractClient' not found [\#117](https://github.com/guzzle/guzzle-services/issues/117)
**Merged pull requests:**
- Make Guzzle Services compatible with Guzzle6 [\#109](https://github.com/guzzle/guzzle-services/pull/109) ([Konafets](https://github.com/Konafets))
## [0.6.0](https://github.com/guzzle/guzzle-services/tree/0.6.0) (2016-10-21)
[Full Changelog](https://github.com/guzzle/guzzle-services/compare/0.5.0...0.6.0)
**Closed issues:**
- Broken composer install [\#111](https://github.com/guzzle/guzzle-services/issues/111)
- The visit\(\) method is expected to return a RequestInterface but it doesn't in JsonLocation [\#106](https://github.com/guzzle/guzzle-services/issues/106)
- Allow parameters in baseUrl [\#102](https://github.com/guzzle/guzzle-services/issues/102)
- Have default params at client construction, gone away? [\#100](https://github.com/guzzle/guzzle-services/issues/100)
- Runtime Exception Error is always empty [\#99](https://github.com/guzzle/guzzle-services/issues/99)
- PHP Fatal error: Unsupported operand types in guzzlehttp/guzzle-services/src/GuzzleClient.php on line 72 [\#95](https://github.com/guzzle/guzzle-services/issues/95)
- Date of next version [\#94](https://github.com/guzzle/guzzle-services/issues/94)
- Map null reponse values to defined reponse model properties [\#91](https://github.com/guzzle/guzzle-services/issues/91)
- Map a json-array into a Model [\#80](https://github.com/guzzle/guzzle-services/issues/80)
- If property specified in json model but empty, notice raised [\#75](https://github.com/guzzle/guzzle-services/issues/75)
- Allow primitive response types for operations [\#73](https://github.com/guzzle/guzzle-services/issues/73)
- Allow shortened definition of properties in models [\#71](https://github.com/guzzle/guzzle-services/issues/71)
- Where's the ServiceDescriptionLoader/AbstractConfigLoader? [\#68](https://github.com/guzzle/guzzle-services/issues/68)
- errorResposnes from operation is never used [\#66](https://github.com/guzzle/guzzle-services/issues/66)
- Updating the description [\#65](https://github.com/guzzle/guzzle-services/issues/65)
- Parameter type validation is too strict [\#7](https://github.com/guzzle/guzzle-services/issues/7)
**Merged pull requests:**
- fix code example [\#115](https://github.com/guzzle/guzzle-services/pull/115) ([snoek09](https://github.com/snoek09))
- Bug Fix for GuzzleClient constructor [\#96](https://github.com/guzzle/guzzle-services/pull/96) ([peterfox](https://github.com/peterfox))
- add plugin section to readme [\#93](https://github.com/guzzle/guzzle-services/pull/93) ([gimler](https://github.com/gimler))
- Allow mapping null response values to defined response model properties [\#92](https://github.com/guzzle/guzzle-services/pull/92) ([shaun785](https://github.com/shaun785))
- Updated exception message for better debugging [\#85](https://github.com/guzzle/guzzle-services/pull/85) ([stovak](https://github.com/stovak))
- Gracefully handle null return from $this-\>getConfig\('defaults'\) [\#84](https://github.com/guzzle/guzzle-services/pull/84) ([fuhry](https://github.com/fuhry))
- Fixing issue \#82 to address regression for handling elements with the sa... [\#83](https://github.com/guzzle/guzzle-services/pull/83) ([sprak3000](https://github.com/sprak3000))
- Fix for specified property but no value in json \(notice for undefined in... [\#76](https://github.com/guzzle/guzzle-services/pull/76) ([rfink](https://github.com/rfink))
- Add ErrorHandler subscriber [\#67](https://github.com/guzzle/guzzle-services/pull/67) ([bakura10](https://github.com/bakura10))
- Fix combine base url and command uri [\#108](https://github.com/guzzle/guzzle-services/pull/108) ([vlastv](https://github.com/vlastv))
- Fixing JsonLocation::visit\(\) not returning a request \#106 [\#107](https://github.com/guzzle/guzzle-services/pull/107) ([Pinolo](https://github.com/Pinolo))
- Fix call to undefined method "GuzzleHttp\Psr7\Uri::combine" [\#105](https://github.com/guzzle/guzzle-services/pull/105) ([horrorin](https://github.com/horrorin))
- fix description for get request example [\#87](https://github.com/guzzle/guzzle-services/pull/87) ([snoek09](https://github.com/snoek09))
- Allow raw values \(non array/object\) for root model definitions [\#74](https://github.com/guzzle/guzzle-services/pull/74) ([rfink](https://github.com/rfink))
- Allow shortened definition of properties by assigning them directly to a type [\#72](https://github.com/guzzle/guzzle-services/pull/72) ([rfink](https://github.com/rfink))
## [0.5.0](https://github.com/guzzle/guzzle-services/tree/0.5.0) (2014-12-23)
[Full Changelog](https://github.com/guzzle/guzzle-services/compare/0.4.0...0.5.0)
**Closed issues:**
- Does it supports custom class instantiate to define an operation using a service description [\#62](https://github.com/guzzle/guzzle-services/issues/62)
- Tag version 0.4.0 [\#61](https://github.com/guzzle/guzzle-services/issues/61)
- XmlLocation not adding attributes to non-leaf child nodes [\#52](https://github.com/guzzle/guzzle-services/issues/52)
- XmlLocation response not handling multiple tags of the same name correctly [\#51](https://github.com/guzzle/guzzle-services/issues/51)
- Validation Bug [\#47](https://github.com/guzzle/guzzle-services/issues/47)
- CommandException doesn't contain response data [\#44](https://github.com/guzzle/guzzle-services/issues/44)
- \[Fix included\] XmlLocation requires text value to have attributes [\#37](https://github.com/guzzle/guzzle-services/issues/37)
- Question: Mocking a Response does not throw exception [\#35](https://github.com/guzzle/guzzle-services/issues/35)
- allow default 'location' on Model [\#26](https://github.com/guzzle/guzzle-services/issues/26)
- create mock subscriber requests from descriptions [\#25](https://github.com/guzzle/guzzle-services/issues/25)
**Merged pull requests:**
- Documentation: Add 'boolean-string' as a supported "format" value [\#63](https://github.com/guzzle/guzzle-services/pull/63) ([jwcobb](https://github.com/jwcobb))
## [0.4.0](https://github.com/guzzle/guzzle-services/tree/0.4.0) (2014-11-03)
[Full Changelog](https://github.com/guzzle/guzzle-services/compare/0.3.0...0.4.0)
**Closed issues:**
- Exceptions Thrown From Subscribers Are Ignored? [\#58](https://github.com/guzzle/guzzle-services/issues/58)
- Totally Broken With Guzzle 5 [\#57](https://github.com/guzzle/guzzle-services/issues/57)
- GuzzleHTTP/Command Dependency fail [\#50](https://github.com/guzzle/guzzle-services/issues/50)
- Request parameter PathLocation [\#46](https://github.com/guzzle/guzzle-services/issues/46)
- Requesting a new version tag [\#45](https://github.com/guzzle/guzzle-services/issues/45)
- CommandException expects second parameter to be CommandTransaction instance [\#43](https://github.com/guzzle/guzzle-services/issues/43)
- Cannot add Autorization header to my requests [\#39](https://github.com/guzzle/guzzle-services/issues/39)
- Resouce Itterators [\#36](https://github.com/guzzle/guzzle-services/issues/36)
- Question [\#33](https://github.com/guzzle/guzzle-services/issues/33)
- query location array can be comma separated [\#31](https://github.com/guzzle/guzzle-services/issues/31)
- Automatically returns array from command? [\#30](https://github.com/guzzle/guzzle-services/issues/30)
- Arrays nested under objects in JSON response broken? [\#27](https://github.com/guzzle/guzzle-services/issues/27)
- Question? [\#23](https://github.com/guzzle/guzzle-services/issues/23)
**Merged pull requests:**
- Bump the version in the readme [\#60](https://github.com/guzzle/guzzle-services/pull/60) ([GrahamCampbell](https://github.com/GrahamCampbell))
- Bump the next version to 0.4 [\#56](https://github.com/guzzle/guzzle-services/pull/56) ([GrahamCampbell](https://github.com/GrahamCampbell))
- Fixed the guzzlehttp/command version constraint [\#55](https://github.com/guzzle/guzzle-services/pull/55) ([GrahamCampbell](https://github.com/GrahamCampbell))
- Work with latest Guzzle 5 and Command updates [\#54](https://github.com/guzzle/guzzle-services/pull/54) ([mtdowling](https://github.com/mtdowling))
- Addressing Issue \#51 & Issue \#52 [\#53](https://github.com/guzzle/guzzle-services/pull/53) ([sprak3000](https://github.com/sprak3000))
- added description interface to extend it [\#49](https://github.com/guzzle/guzzle-services/pull/49) ([danieledangeli](https://github.com/danieledangeli))
- Update readme to improve documentation \(\#46\) [\#48](https://github.com/guzzle/guzzle-services/pull/48) ([bonndan](https://github.com/bonndan))
- Fixed the readme version constraint [\#42](https://github.com/guzzle/guzzle-services/pull/42) ([GrahamCampbell](https://github.com/GrahamCampbell))
- Update .travis.yml [\#41](https://github.com/guzzle/guzzle-services/pull/41) ([GrahamCampbell](https://github.com/GrahamCampbell))
- Added a branch alias [\#40](https://github.com/guzzle/guzzle-services/pull/40) ([GrahamCampbell](https://github.com/GrahamCampbell))
- Fixes Response\XmlLocation requires text value [\#38](https://github.com/guzzle/guzzle-services/pull/38) ([magnetik](https://github.com/magnetik))
- Removing unnecessary \(\) from docblock [\#32](https://github.com/guzzle/guzzle-services/pull/32) ([jamiehannaford](https://github.com/jamiehannaford))
- Fix JSON response location so that both is supported: arrays nested unde... [\#28](https://github.com/guzzle/guzzle-services/pull/28) ([ukautz](https://github.com/ukautz))
- Throw Any Exceptions On Process [\#59](https://github.com/guzzle/guzzle-services/pull/59) ([GrahamCampbell](https://github.com/GrahamCampbell))
- Allow extension to work recursively over models [\#34](https://github.com/guzzle/guzzle-services/pull/34) ([jamiehannaford](https://github.com/jamiehannaford))
- A custom class can be configured for command instances. [\#29](https://github.com/guzzle/guzzle-services/pull/29) ([robinvdvleuten](https://github.com/robinvdvleuten))
- \[WIP\] doing some experimentation [\#24](https://github.com/guzzle/guzzle-services/pull/24) ([cordoval](https://github.com/cordoval))
## [0.3.0](https://github.com/guzzle/guzzle-services/tree/0.3.0) (2014-06-01)
[Full Changelog](https://github.com/guzzle/guzzle-services/compare/0.2.0...0.3.0)
**Closed issues:**
- Testing Guzzle Services doesn't work [\#19](https://github.com/guzzle/guzzle-services/issues/19)
- Description factory [\#18](https://github.com/guzzle/guzzle-services/issues/18)
- support to load service description from file [\#15](https://github.com/guzzle/guzzle-services/issues/15)
- Update dependency on guzzlehttp/command [\#11](https://github.com/guzzle/guzzle-services/issues/11)
**Merged pull requests:**
- Add license file [\#22](https://github.com/guzzle/guzzle-services/pull/22) ([siwinski](https://github.com/siwinski))
- Fix 'Invalid argument supplied for foreach\(\)' [\#21](https://github.com/guzzle/guzzle-services/pull/21) ([Olden](https://github.com/Olden))
- Fixed string zero \('0'\) values not being filtered in XML. [\#20](https://github.com/guzzle/guzzle-services/pull/20) ([dragonwize](https://github.com/dragonwize))
- baseUrl can be a string or an uri template [\#16](https://github.com/guzzle/guzzle-services/pull/16) ([robinvdvleuten](https://github.com/robinvdvleuten))
## [0.2.0](https://github.com/guzzle/guzzle-services/tree/0.2.0) (2014-03-30)
[Full Changelog](https://github.com/guzzle/guzzle-services/compare/0.1.0...0.2.0)
**Closed issues:**
- please remove wiki [\#13](https://github.com/guzzle/guzzle-services/issues/13)
- Parameter validation fails for union types [\#12](https://github.com/guzzle/guzzle-services/issues/12)
- question on integration with Guzzle4 [\#8](https://github.com/guzzle/guzzle-services/issues/8)
- typehints for operations property [\#6](https://github.com/guzzle/guzzle-services/issues/6)
- improve exception message [\#5](https://github.com/guzzle/guzzle-services/issues/5)
**Merged pull requests:**
- Update composer.json [\#14](https://github.com/guzzle/guzzle-services/pull/14) ([GrahamCampbell](https://github.com/GrahamCampbell))
- Update composer.json [\#9](https://github.com/guzzle/guzzle-services/pull/9) ([GrahamCampbell](https://github.com/GrahamCampbell))
- some fixes [\#4](https://github.com/guzzle/guzzle-services/pull/4) ([cordoval](https://github.com/cordoval))
- Fix the CommandException path used in ValidateInput [\#2](https://github.com/guzzle/guzzle-services/pull/2) ([mookle](https://github.com/mookle))
- Minor improvements [\#1](https://github.com/guzzle/guzzle-services/pull/1) ([GrahamCampbell](https://github.com/GrahamCampbell))
- Use latest guzzlehttp/command to fix dependencies [\#10](https://github.com/guzzle/guzzle-services/pull/10) ([sbward](https://github.com/sbward))
- some collaboration using Gush :\) [\#3](https://github.com/guzzle/guzzle-services/pull/3) ([cordoval](https://github.com/cordoval))
## [0.1.0](https://github.com/guzzle/guzzle-services/tree/0.1.0) (2014-03-15)
\* *This Change Log was automatically generated by [github_changelog_generator](https://github.com/skywinder/Github-Changelog-Generator)*

View File

@ -0,0 +1,19 @@
Copyright (c) 2014 Michael Dowling, https://github.com/mtdowling <mtdowling@gmail.com>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.

View File

@ -0,0 +1,15 @@
all: clean test
test:
vendor/bin/phpunit
coverage:
vendor/bin/phpunit --coverage-html=artifacts/coverage
view-coverage:
open artifacts/coverage/index.html
clean:
rm -rf artifacts/*
.PHONY: coverage

View File

@ -0,0 +1,129 @@
# Guzzle Services
[![License](https://poser.pugx.org/guzzlehttp/guzzle-services/license)](https://packagist.org/packages/guzzlehttp/guzzle-services)
[![Build Status](https://travis-ci.org/guzzle/guzzle-services.svg?branch=master)](https://travis-ci.org/guzzle/guzzle-services)
[![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/guzzle/guzzle-services/badges/quality-score.png?b=master)](https://scrutinizer-ci.com/g/guzzle/guzzle-services/?branch=master)
[![Code Coverage](https://scrutinizer-ci.com/g/guzzle/guzzle-services/badges/coverage.png?b=master)](https://scrutinizer-ci.com/g/guzzle/guzzle-services/?branch=master)
[![SensioLabsInsight](https://insight.sensiolabs.com/projects/b08be676-b209-40b7-a6df-b6d13e8dff62/mini.png)](https://insight.sensiolabs.com/projects/b08be676-b209-40b7-a6df-b6d13e8dff62)
[![Latest Stable Version](https://poser.pugx.org/guzzlehttp/guzzle-services/v/stable)](https://packagist.org/packages/guzzlehttp/guzzle-services)
[![Latest Unstable Version](https://poser.pugx.org/guzzlehttp/guzzle-services/v/unstable)](https://packagist.org/packages/guzzlehttp/guzzle-services)
[![Total Downloads](https://poser.pugx.org/guzzlehttp/guzzle-services/downloads)](https://packagist.org/packages/guzzlehttp/guzzle-services)
Provides an implementation of the Guzzle Command library that uses Guzzle service descriptions to describe web services, serialize requests, and parse responses into easy to use model structures.
```php
use GuzzleHttp\Client;
use GuzzleHttp\Command\Guzzle\GuzzleClient;
use GuzzleHttp\Command\Guzzle\Description;
$client = new Client();
$description = new Description([
'baseUri' => 'http://httpbin.org/',
'operations' => [
'testing' => [
'httpMethod' => 'GET',
'uri' => '/get{?foo}',
'responseModel' => 'getResponse',
'parameters' => [
'foo' => [
'type' => 'string',
'location' => 'uri'
],
'bar' => [
'type' => 'string',
'location' => 'query'
]
]
]
],
'models' => [
'getResponse' => [
'type' => 'object',
'additionalProperties' => [
'location' => 'json'
]
]
]
]);
$guzzleClient = new GuzzleClient($client, $description);
$result = $guzzleClient->testing(['foo' => 'bar']);
echo $result['args']['foo'];
// bar
```
## Installing
This project can be installed using Composer:
``composer require guzzlehttp/guzzle-services``
For **Guzzle 5**, use ``composer require guzzlehttp/guzzle-services:0.6``.
**Note:** If Composer is not installed [globally](https://getcomposer.org/doc/00-intro.md#globally) then you may need to run the preceding Composer commands using ``php composer.phar`` (where ``composer.phar`` is the path to your copy of Composer), instead of just ``composer``.
## Plugins
* Load Service description from file [https://github.com/gimler/guzzle-description-loader]
## Transition guide from Guzzle 5.0 to 6.0
### Change regarding PostField and PostFile
The request locations `postField` and `postFile` were removed in favor of `formParam` and `multipart`. If your description looks like
```php
[
'baseUri' => 'http://httpbin.org/',
'operations' => [
'testing' => [
'httpMethod' => 'GET',
'uri' => '/get{?foo}',
'responseModel' => 'getResponse',
'parameters' => [
'foo' => [
'type' => 'string',
'location' => 'postField'
],
'bar' => [
'type' => 'string',
'location' => 'postFile'
]
]
]
],
]
```
you need to change `postField` to `formParam` and `postFile` to `multipart`.
More documentation coming soon.
## Cookbook
### Changing the way query params are serialized
By default, query params are serialized using strict RFC3986 rules, using `http_build_query` method. With this, array params are serialized this way:
```php
$client->myMethod(['foo' => ['bar', 'baz']]);
// Query params will be foo[0]=bar&foo[1]=baz
```
However, a lot of APIs in the wild require the numeric indices to be removed, so that the query params end up being `foo[]=bar&foo[]=baz`. You
can easily change the behaviour by creating your own serializer and overriding the "query" request location:
```php
use GuzzleHttp\Command\Guzzle\GuzzleClient;
use GuzzleHttp\Command\Guzzle\RequestLocation\QueryLocation;
use GuzzleHttp\Command\Guzzle\QuerySerializer\Rfc3986Serializer;
use GuzzleHttp\Command\Guzzle\Serializer;
$queryLocation = new QueryLocation('query', new Rfc3986Serializer(true));
$serializer = new Serializer($description, ['query' => $queryLocation]);
$guzzleClient = new GuzzleClient($client, $description, $serializer);
```
You can also create your own serializer if you have specific needs.

View File

@ -0,0 +1,49 @@
{
"name": "guzzlehttp/guzzle-services",
"description": "Provides an implementation of the Guzzle Command library that uses Guzzle service descriptions to describe web services, serialize requests, and parse responses into easy to use model structures.",
"type": "library",
"license": "MIT",
"authors": [
{
"name": "Michael Dowling",
"email": "mtdowling@gmail.com",
"homepage": "https://github.com/mtdowling"
},
{
"name": "Jeremy Lindblom",
"email": "jeremeamia@gmail.com",
"homepage": "https://github.com/jeremeamia"
},
{
"name": "Stefano Kowalke",
"email": "blueduck@mail.org",
"homepage": "https://github.com/konafets"
}
],
"require": {
"php": ">=5.5",
"guzzlehttp/guzzle": "^6.2",
"guzzlehttp/command": "~1.0"
},
"require-dev": {
"phpunit/phpunit": "~4.0"
},
"autoload": {
"psr-4": {
"GuzzleHttp\\Command\\Guzzle\\": "src/"
}
},
"autoload-dev": {
"psr-4": {
"GuzzleHttp\\Tests\\Command\\Guzzle\\": "tests/"
}
},
"suggest": {
"gimler/guzzle-description-loader": "^0.0.4"
},
"extra": {
"branch-alias": {
"dev-master": "1.0.x-dev"
}
}
}

View File

@ -0,0 +1,14 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit bootstrap="./vendor/autoload.php"
colors="true">
<testsuites>
<testsuite>
<directory>tests</directory>
</testsuite>
</testsuites>
<filter>
<whitelist>
<directory suffix=".php">src</directory>
</whitelist>
</filter>
</phpunit>

View File

@ -0,0 +1,265 @@
<?php
namespace GuzzleHttp\Command\Guzzle;
use GuzzleHttp\Psr7\Uri;
/**
* Represents a Guzzle service description
*/
class Description implements DescriptionInterface
{
/** @var array Array of {@see OperationInterface} objects */
private $operations = [];
/** @var array Array of API models */
private $models = [];
/** @var string Name of the API */
private $name;
/** @var string API version */
private $apiVersion;
/** @var string Summary of the API */
private $description;
/** @var array Any extra API data */
private $extraData = [];
/** @var Uri baseUri/basePath */
private $baseUri;
/** @var SchemaFormatter */
private $formatter;
/**
* @param array $config Service description data
* @param array $options Custom options to apply to the description
* - formatter: Can provide a custom SchemaFormatter class
*
* @throws \InvalidArgumentException
*/
public function __construct(array $config, array $options = [])
{
// Keep a list of default keys used in service descriptions that is
// later used to determine extra data keys.
static $defaultKeys = ['name', 'models', 'apiVersion', 'description'];
// Pull in the default configuration values
foreach ($defaultKeys as $key) {
if (isset($config[$key])) {
$this->{$key} = $config[$key];
}
}
// Set the baseUri
// Account for the old style of using baseUrl
if (isset($config['baseUrl'])) {
$config['baseUri'] = $config['baseUrl'];
}
$this->baseUri = isset($config['baseUri']) ? new Uri($config['baseUri']) : new Uri();
// Ensure that the models and operations properties are always arrays
$this->models = (array) $this->models;
$this->operations = (array) $this->operations;
// We want to add operations differently than adding the other properties
$defaultKeys[] = 'operations';
// Create operations for each operation
if (isset($config['operations'])) {
foreach ($config['operations'] as $name => $operation) {
if (!is_array($operation)) {
throw new \InvalidArgumentException('Operations must be arrays');
}
$this->operations[$name] = $operation;
}
}
// Get all of the additional properties of the service description and
// store them in a data array
foreach (array_diff(array_keys($config), $defaultKeys) as $key) {
$this->extraData[$key] = $config[$key];
}
// Configure the schema formatter
if (isset($options['formatter'])) {
$this->formatter = $options['formatter'];
} else {
static $defaultFormatter;
if (!$defaultFormatter) {
$defaultFormatter = new SchemaFormatter();
}
$this->formatter = $defaultFormatter;
}
}
/**
* Get the basePath/baseUri of the description
*
* @return Uri
*/
public function getBaseUri()
{
return $this->baseUri;
}
/**
* Get the API operations of the service
*
* @return Operation[] Returns an array of {@see Operation} objects
*/
public function getOperations()
{
return $this->operations;
}
/**
* Check if the service has an operation by name
*
* @param string $name Name of the operation to check
*
* @return bool
*/
public function hasOperation($name)
{
return isset($this->operations[$name]);
}
/**
* Get an API operation by name
*
* @param string $name Name of the command
*
* @return Operation
* @throws \InvalidArgumentException if the operation is not found
*/
public function getOperation($name)
{
if (!$this->hasOperation($name)) {
throw new \InvalidArgumentException("No operation found named $name");
}
// Lazily create operations as they are retrieved
if (!($this->operations[$name] instanceof Operation)) {
$this->operations[$name]['name'] = $name;
$this->operations[$name] = new Operation($this->operations[$name], $this);
}
return $this->operations[$name];
}
/**
* Get a shared definition structure.
*
* @param string $id ID/name of the model to retrieve
*
* @return Parameter
* @throws \InvalidArgumentException if the model is not found
*/
public function getModel($id)
{
if (!$this->hasModel($id)) {
throw new \InvalidArgumentException("No model found named $id");
}
// Lazily create models as they are retrieved
if (!($this->models[$id] instanceof Parameter)) {
$this->models[$id] = new Parameter(
$this->models[$id],
['description' => $this]
);
}
return $this->models[$id];
}
/**
* Get all models of the service description.
*
* @return array
*/
public function getModels()
{
$models = [];
foreach ($this->models as $name => $model) {
$models[$name] = $this->getModel($name);
}
return $models;
}
/**
* Check if the service description has a model by name.
*
* @param string $id Name/ID of the model to check
*
* @return bool
*/
public function hasModel($id)
{
return isset($this->models[$id]);
}
/**
* Get the API version of the service
*
* @return string
*/
public function getApiVersion()
{
return $this->apiVersion;
}
/**
* Get the name of the API
*
* @return string
*/
public function getName()
{
return $this->name;
}
/**
* Get a summary of the purpose of the API
*
* @return string
*/
public function getDescription()
{
return $this->description;
}
/**
* Format a parameter using named formats.
*
* @param string $format Format to convert it to
* @param mixed $input Input string
*
* @return mixed
*/
public function format($format, $input)
{
return $this->formatter->format($format, $input);
}
/**
* Get arbitrary data from the service description that is not part of the
* Guzzle service description specification.
*
* @param string $key Data key to retrieve or null to retrieve all extra
*
* @return null|mixed
*/
public function getData($key = null)
{
if ($key === null) {
return $this->extraData;
} elseif (isset($this->extraData[$key])) {
return $this->extraData[$key];
} else {
return null;
}
}
}

View File

@ -0,0 +1,107 @@
<?php
namespace GuzzleHttp\Command\Guzzle;
use GuzzleHttp\Psr7\Uri;
interface DescriptionInterface
{
/**
* Get the basePath/baseUri of the description
*
* @return Uri
*/
public function getBaseUri();
/**
* Get the API operations of the service
*
* @return Operation[] Returns an array of {@see Operation} objects
*/
public function getOperations();
/**
* Check if the service has an operation by name
*
* @param string $name Name of the operation to check
*
* @return bool
*/
public function hasOperation($name);
/**
* Get an API operation by name
*
* @param string $name Name of the command
*
* @return Operation
* @throws \InvalidArgumentException if the operation is not found
*/
public function getOperation($name);
/**
* Get a shared definition structure.
*
* @param string $id ID/name of the model to retrieve
*
* @return Parameter
* @throws \InvalidArgumentException if the model is not found
*/
public function getModel($id);
/**
* Get all models of the service description.
*
* @return array
*/
public function getModels();
/**
* Check if the service description has a model by name.
*
* @param string $id Name/ID of the model to check
*
* @return bool
*/
public function hasModel($id);
/**
* Get the API version of the service
*
* @return string
*/
public function getApiVersion();
/**
* Get the name of the API
*
* @return string
*/
public function getName();
/**
* Get a summary of the purpose of the API
*
* @return string
*/
public function getDescription();
/**
* Format a parameter using named formats.
*
* @param string $format Format to convert it to
* @param mixed $input Input string
*
* @return mixed
*/
public function format($format, $input);
/**
* Get arbitrary data from the service description that is not part of the
* Guzzle service description specification.
*
* @param string $key Data key to retrieve or null to retrieve all extra
*
* @return null|mixed
*/
public function getData($key = null);
}

View File

@ -0,0 +1,294 @@
<?php
namespace GuzzleHttp\Command\Guzzle;
use GuzzleHttp\Command\CommandInterface;
use GuzzleHttp\Command\Guzzle\ResponseLocation\BodyLocation;
use GuzzleHttp\Command\Guzzle\ResponseLocation\HeaderLocation;
use GuzzleHttp\Command\Guzzle\ResponseLocation\JsonLocation;
use GuzzleHttp\Command\Guzzle\ResponseLocation\ReasonPhraseLocation;
use GuzzleHttp\Command\Guzzle\ResponseLocation\ResponseLocationInterface;
use GuzzleHttp\Command\Guzzle\ResponseLocation\StatusCodeLocation;
use GuzzleHttp\Command\Guzzle\ResponseLocation\XmlLocation;
use GuzzleHttp\Command\Result;
use GuzzleHttp\Command\ResultInterface;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;
/**
* Handler used to create response models based on an HTTP response and
* a service description.
*
* Response location visitors are registered with this Handler to handle
* locations (e.g., 'xml', 'json', 'header'). All of the locations of a response
* model that will be visited first have their ``before`` method triggered.
* After the before method is called on every visitor that will be walked, each
* visitor is triggered using the ``visit()`` method. After all of the visitors
* are visited, the ``after()`` method is called on each visitor. This is the
* place in which you should handle things like additionalProperties with
* custom locations (i.e., this is how it is handled in the JSON visitor).
*/
class Deserializer
{
/** @var ResponseLocationInterface[] $responseLocations */
private $responseLocations;
/** @var DescriptionInterface $description */
private $description;
/** @var boolean $process */
private $process;
/**
* @param DescriptionInterface $description
* @param bool $process
* @param ResponseLocationInterface[] $responseLocations Extra response locations
*/
public function __construct(
DescriptionInterface $description,
$process,
array $responseLocations = []
) {
static $defaultResponseLocations;
if (!$defaultResponseLocations) {
$defaultResponseLocations = [
'body' => new BodyLocation(),
'header' => new HeaderLocation(),
'reasonPhrase' => new ReasonPhraseLocation(),
'statusCode' => new StatusCodeLocation(),
'xml' => new XmlLocation(),
'json' => new JsonLocation(),
];
}
$this->responseLocations = $responseLocations + $defaultResponseLocations;
$this->description = $description;
$this->process = $process;
}
/**
* Deserialize the response into the specified result representation
*
* @param ResponseInterface $response
* @param RequestInterface|null $request
* @param CommandInterface $command
* @return Result|ResultInterface|void|ResponseInterface
*/
public function __invoke(ResponseInterface $response, RequestInterface $request, CommandInterface $command)
{
// If the user don't want to process the result, just return the plain response here
if ($this->process === false) {
return $response;
}
$name = $command->getName();
$operation = $this->description->getOperation($name);
$this->handleErrorResponses($response, $request, $command, $operation);
// Add a default Model as the result if no matching schema was found
if (!($modelName = $operation->getResponseModel())) {
// Not sure if this should be empty or contains the response.
// Decided to do it how it was in the old version for now.
return new Result();
}
$model = $operation->getServiceDescription()->getModel($modelName);
if (!$model) {
throw new \RuntimeException("Unknown model: {$modelName}");
}
return $this->visit($model, $response);
}
/**
* Handles visit() and after() methods of the Response locations
*
* @param Parameter $model
* @param ResponseInterface $response
* @return Result|ResultInterface|void
*/
protected function visit(Parameter $model, ResponseInterface $response)
{
$result = new Result();
$context = ['visitors' => []];
if ($model->getType() === 'object') {
$result = $this->visitOuterObject($model, $result, $response, $context);
} elseif ($model->getType() === 'array') {
$result = $this->visitOuterArray($model, $result, $response, $context);
} else {
throw new \InvalidArgumentException('Invalid response model: ' . $model->getType());
}
// Call the after() method of each found visitor
/** @var ResponseLocationInterface $visitor */
foreach ($context['visitors'] as $visitor) {
$result = $visitor->after($result, $response, $model);
}
return $result;
}
/**
* Handles the before() method of Response locations
*
* @param string $location
* @param Parameter $model
* @param ResultInterface $result
* @param ResponseInterface $response
* @param array $context
* @return ResultInterface
*/
private function triggerBeforeVisitor(
$location,
Parameter $model,
ResultInterface $result,
ResponseInterface $response,
array &$context
) {
if (!isset($this->responseLocations[$location])) {
throw new \RuntimeException("Unknown location: $location");
}
$context['visitors'][$location] = $this->responseLocations[$location];
$result = $this->responseLocations[$location]->before(
$result,
$response,
$model
);
return $result;
}
/**
* Visits the outer object
*
* @param Parameter $model
* @param ResultInterface $result
* @param ResponseInterface $response
* @param array $context
* @return ResultInterface
*/
private function visitOuterObject(
Parameter $model,
ResultInterface $result,
ResponseInterface $response,
array &$context
) {
$parentLocation = $model->getLocation();
// If top-level additionalProperties is a schema, then visit it
$additional = $model->getAdditionalProperties();
if ($additional instanceof Parameter) {
// Use the model location if none set on additionalProperties.
$location = $additional->getLocation() ?: $parentLocation;
$result = $this->triggerBeforeVisitor($location, $model, $result, $response, $context);
}
// Use 'location' from all individual defined properties, but fall back
// to the model location if no per-property location is set. Collect
// the properties that need to be visited into an array.
$visitProperties = [];
foreach ($model->getProperties() as $schema) {
$location = $schema->getLocation() ?: $parentLocation;
if ($location) {
$visitProperties[] = [$location, $schema];
// Trigger the before method on each unique visitor location
if (!isset($context['visitors'][$location])) {
$result = $this->triggerBeforeVisitor($location, $model, $result, $response, $context);
}
}
}
// Actually visit each response element
foreach ($visitProperties as $property) {
$result = $this->responseLocations[$property[0]]->visit($result, $response, $property[1]);
}
return $result;
}
/**
* Visits the outer array
*
* @param Parameter $model
* @param ResultInterface $result
* @param ResponseInterface $response
* @param array $context
* @return ResultInterface|void
*/
private function visitOuterArray(
Parameter $model,
ResultInterface $result,
ResponseInterface $response,
array &$context
) {
// Use 'location' defined on the top of the model
if (!($location = $model->getLocation())) {
return;
}
// Trigger the before method on each unique visitor location
if (!isset($context['visitors'][$location])) {
$result = $this->triggerBeforeVisitor($location, $model, $result, $response, $context);
}
// Visit each item in the response
$result = $this->responseLocations[$location]->visit($result, $response, $model);
return $result;
}
/**
* Reads the "errorResponses" from commands, and trigger appropriate exceptions
*
* In order for the exception to be properly triggered, all your exceptions must be instance
* of "GuzzleHttp\Command\Exception\CommandException". If that's not the case, your exceptions will be wrapped
* around a CommandException
*
* @param ResponseInterface $response
* @param RequestInterface $request
* @param CommandInterface $command
* @param Operation $operation
*/
protected function handleErrorResponses(
ResponseInterface $response,
RequestInterface $request,
CommandInterface $command,
Operation $operation
) {
$errors = $operation->getErrorResponses();
// We iterate through each errors in service description. If the descriptor contains both a phrase and
// status code, there must be an exact match of both. Otherwise, a match of status code is enough
$bestException = null;
foreach ($errors as $error) {
$code = (int) $error['code'];
if ($response->getStatusCode() !== $code) {
continue;
}
if (isset($error['phrase']) && ! ($error['phrase'] === $response->getReasonPhrase())) {
continue;
}
$bestException = $error['class'];
// If there is an exact match of phrase + code, then we cannot find a more specialized exception in
// the array, so we can break early instead of iterating the remaining ones
if (isset($error['phrase'])) {
break;
}
}
if (null !== $bestException) {
throw new $bestException($response->getReasonPhrase(), $command, null, $request, $response);
}
// If we reach here, no exception could be match from descriptor, and Guzzle exception will propagate if
// option "http_errors" is set to true, which is the default setting.
}
}

View File

@ -0,0 +1,169 @@
<?php
namespace GuzzleHttp\Command\Guzzle;
use GuzzleHttp\ClientInterface;
use GuzzleHttp\Command\CommandInterface;
use GuzzleHttp\Command\Guzzle\Handler\ValidatedDescriptionHandler;
use GuzzleHttp\Command\ServiceClient;
use GuzzleHttp\HandlerStack;
/**
* Default Guzzle web service client implementation.
*/
class GuzzleClient extends ServiceClient
{
/** @var array $config */
private $config;
/** @var DescriptionInterface Guzzle service description */
private $description;
/**
* The client constructor accepts an associative array of configuration
* options:
*
* - defaults: Associative array of default command parameters to add to
* each command created by the client.
* - validate: Specify if command input is validated (defaults to true).
* Changing this setting after the client has been created will have no
* effect.
* - process: Specify if HTTP responses are parsed (defaults to true).
* Changing this setting after the client has been created will have no
* effect.
* - response_locations: Associative array of location types mapping to
* ResponseLocationInterface objects.
*
* @param ClientInterface $client HTTP client to use.
* @param DescriptionInterface $description Guzzle service description
* @param callable $commandToRequestTransformer
* @param callable $responseToResultTransformer
* @param HandlerStack $commandHandlerStack
* @param array $config Configuration options
*/
public function __construct(
ClientInterface $client,
DescriptionInterface $description,
callable $commandToRequestTransformer = null,
callable $responseToResultTransformer = null,
HandlerStack $commandHandlerStack = null,
array $config = []
) {
$this->config = $config;
$this->description = $description;
$serializer = $this->getSerializer($commandToRequestTransformer);
$deserializer = $this->getDeserializer($responseToResultTransformer);
parent::__construct($client, $serializer, $deserializer, $commandHandlerStack);
$this->processConfig($config);
}
/**
* Returns the command if valid; otherwise an Exception
* @param string $name
* @param array $args
* @return CommandInterface
* @throws \InvalidArgumentException
*/
public function getCommand($name, array $args = [])
{
if (!$this->description->hasOperation($name)) {
$name = ucfirst($name);
if (!$this->description->hasOperation($name)) {
throw new \InvalidArgumentException(
"No operation found named {$name}"
);
}
}
// Merge in default command options
$args += $this->getConfig('defaults');
return parent::getCommand($name, $args);
}
/**
* Return the description
*
* @return DescriptionInterface
*/
public function getDescription()
{
return $this->description;
}
/**
* Returns the passed Serializer when set, a new instance otherwise
*
* @param callable|null $commandToRequestTransformer
* @return \GuzzleHttp\Command\Guzzle\Serializer
*/
private function getSerializer($commandToRequestTransformer)
{
return $commandToRequestTransformer ==! null
? $commandToRequestTransformer
: new Serializer($this->description);
}
/**
* Returns the passed Deserializer when set, a new instance otherwise
*
* @param callable|null $responseToResultTransformer
* @return \GuzzleHttp\Command\Guzzle\Deserializer
*/
private function getDeserializer($responseToResultTransformer)
{
$process = (! isset($this->config['process']) || $this->config['process'] === true);
return $responseToResultTransformer ==! null
? $responseToResultTransformer
: new Deserializer($this->description, $process);
}
/**
* Get the config of the client
*
* @param array|string $option
* @return mixed
*/
public function getConfig($option = null)
{
return $option === null
? $this->config
: (isset($this->config[$option]) ? $this->config[$option] : []);
}
/**
* @param $option
* @param $value
*/
public function setConfig($option, $value)
{
$this->config[$option] = $value;
}
/**
* Prepares the client based on the configuration settings of the client.
*
* @param array $config Constructor config as an array
*/
protected function processConfig(array $config)
{
// set defaults as an array if not provided
if (!isset($config['defaults'])) {
$config['defaults'] = [];
}
// Add the handlers based on the configuration option
$stack = $this->getHandlerStack();
if (!isset($config['validate']) || $config['validate'] === true) {
$stack->push(new ValidatedDescriptionHandler($this->description), 'validate_description');
}
if (!isset($config['process']) || $config['process'] === true) {
// TODO: This belongs to the Deserializer and should be handled there.
// Question: What is the result when the Deserializer is bypassed?
// Possible answer: The raw response.
}
}
}

View File

@ -0,0 +1,82 @@
<?php namespace GuzzleHttp\Command\Guzzle\Handler;
use GuzzleHttp\Command\CommandInterface;
use GuzzleHttp\Command\Exception\CommandException;
use GuzzleHttp\Command\Guzzle\DescriptionInterface;
use GuzzleHttp\Command\Guzzle\SchemaValidator;
/**
* Handler used to validate command input against a service description.
*
* @author Stefano Kowalke <info@arroba-it.de>
*/
class ValidatedDescriptionHandler
{
/** @var SchemaValidator $validator */
private $validator;
/** @var DescriptionInterface $description */
private $description;
/**
* ValidatedDescriptionHandler constructor.
*
* @param DescriptionInterface $description
* @param SchemaValidator|null $schemaValidator
*/
public function __construct(DescriptionInterface $description, SchemaValidator $schemaValidator = null)
{
$this->description = $description;
$this->validator = $schemaValidator ?: new SchemaValidator();
}
/**
* @param callable $handler
* @return \Closure
*/
public function __invoke(callable $handler)
{
return function (CommandInterface $command) use ($handler) {
$errors = [];
$operation = $this->description->getOperation($command->getName());
foreach ($operation->getParams() as $name => $schema) {
$value = $command[$name];
if ($value) {
$value = $schema->filter($value);
}
if (! $this->validator->validate($schema, $value)) {
$errors = array_merge($errors, $this->validator->getErrors());
} elseif ($value !== $command[$name]) {
// Update the config value if it changed and no validation errors were encountered.
// This happen when the user extending an operation
// See https://github.com/guzzle/guzzle-services/issues/145
$command[$name] = $value;
}
}
if ($params = $operation->getAdditionalParameters()) {
foreach ($command->toArray() as $name => $value) {
// It's only additional if it isn't defined in the schema
if (! $operation->hasParam($name)) {
// Always set the name so that error messages are useful
$params->setName($name);
if (! $this->validator->validate($params, $value)) {
$errors = array_merge($errors, $this->validator->getErrors());
} elseif ($value !== $command[$name]) {
$command[$name] = $value;
}
}
}
}
if ($errors) {
throw new CommandException('Validation errors: ' . implode("\n", $errors), $command);
}
return $handler($command);
};
}
}

View File

@ -0,0 +1,312 @@
<?php
namespace GuzzleHttp\Command\Guzzle;
use GuzzleHttp\Command\ToArrayInterface;
/**
* Guzzle operation
*/
class Operation implements ToArrayInterface
{
/** @var array Parameters */
private $parameters = [];
/** @var Parameter Additional parameters schema */
private $additionalParameters;
/** @var DescriptionInterface */
private $description;
/** @var array Config data */
private $config;
/**
* Builds an Operation object using an array of configuration data.
*
* - name: (string) Name of the command
* - httpMethod: (string) HTTP method of the operation
* - uri: (string) URI template that can create a relative or absolute URL
* - parameters: (array) Associative array of parameters for the command.
* Each value must be an array that is used to create {@see Parameter}
* objects.
* - summary: (string) This is a short summary of what the operation does
* - notes: (string) A longer description of the operation.
* - documentationUrl: (string) Reference URL providing more information
* about the operation.
* - responseModel: (string) The model name used for processing response.
* - deprecated: (bool) Set to true if this is a deprecated command
* - errorResponses: (array) Errors that could occur when executing the
* command. Array of hashes, each with a 'code' (the HTTP response code),
* 'phrase' (response reason phrase or description of the error), and
* 'class' (a custom exception class that would be thrown if the error is
* encountered).
* - data: (array) Any extra data that might be used to help build or
* serialize the operation
* - additionalParameters: (null|array) Parameter schema to use when an
* option is passed to the operation that is not in the schema
*
* @param array $config Array of configuration data
* @param DescriptionInterface $description Service description used to resolve models if $ref tags are found
* @throws \InvalidArgumentException
*/
public function __construct(array $config = [], DescriptionInterface $description = null)
{
static $defaults = [
'name' => '',
'httpMethod' => '',
'uri' => '',
'responseModel' => null,
'notes' => '',
'summary' => '',
'documentationUrl' => null,
'deprecated' => false,
'data' => [],
'parameters' => [],
'additionalParameters' => null,
'errorResponses' => []
];
$this->description = $description === null ? new Description([]) : $description;
if (isset($config['extends'])) {
$config = $this->resolveExtends($config['extends'], $config);
}
$this->config = $config + $defaults;
// Account for the old style of using responseClass
if (isset($config['responseClass'])) {
$this->config['responseModel'] = $config['responseClass'];
}
$this->resolveParameters();
}
/**
* @return array
*/
public function toArray()
{
return $this->config;
}
/**
* Get the service description that the operation belongs to
*
* @return Description
*/
public function getServiceDescription()
{
return $this->description;
}
/**
* Get the params of the operation
*
* @return Parameter[]
*/
public function getParams()
{
return $this->parameters;
}
/**
* Get additionalParameters of the operation
*
* @return Parameter|null
*/
public function getAdditionalParameters()
{
return $this->additionalParameters;
}
/**
* Check if the operation has a specific parameter by name
*
* @param string $name Name of the param
*
* @return bool
*/
public function hasParam($name)
{
return isset($this->parameters[$name]);
}
/**
* Get a single parameter of the operation
*
* @param string $name Parameter to retrieve by name
*
* @return Parameter|null
*/
public function getParam($name)
{
return isset($this->parameters[$name])
? $this->parameters[$name]
: null;
}
/**
* Get the HTTP method of the operation
*
* @return string|null
*/
public function getHttpMethod()
{
return $this->config['httpMethod'];
}
/**
* Get the name of the operation
*
* @return string|null
*/
public function getName()
{
return $this->config['name'];
}
/**
* Get a short summary of what the operation does
*
* @return string|null
*/
public function getSummary()
{
return $this->config['summary'];
}
/**
* Get a longer text field to explain the behavior of the operation
*
* @return string|null
*/
public function getNotes()
{
return $this->config['notes'];
}
/**
* Get the documentation URL of the operation
*
* @return string|null
*/
public function getDocumentationUrl()
{
return $this->config['documentationUrl'];
}
/**
* Get the name of the model used for processing the response.
*
* @return string
*/
public function getResponseModel()
{
return $this->config['responseModel'];
}
/**
* Get whether or not the operation is deprecated
*
* @return bool
*/
public function getDeprecated()
{
return $this->config['deprecated'];
}
/**
* Get the URI that will be merged into the generated request
*
* @return string
*/
public function getUri()
{
return $this->config['uri'];
}
/**
* Get the errors that could be encountered when executing the operation
*
* @return array
*/
public function getErrorResponses()
{
return $this->config['errorResponses'];
}
/**
* Get extra data from the operation
*
* @param string $name Name of the data point to retrieve or null to
* retrieve all of the extra data.
*
* @return mixed|null
*/
public function getData($name = null)
{
if ($name === null) {
return $this->config['data'];
} elseif (isset($this->config['data'][$name])) {
return $this->config['data'][$name];
} else {
return null;
}
}
/**
* @param $name
* @param array $config
* @return array
*/
private function resolveExtends($name, array $config)
{
if (!$this->description->hasOperation($name)) {
throw new \InvalidArgumentException('No operation named ' . $name);
}
// Merge parameters together one level deep
$base = $this->description->getOperation($name)->toArray();
$result = $config + $base;
if (isset($base['parameters']) && isset($config['parameters'])) {
$result['parameters'] = $config['parameters'] + $base['parameters'];
}
return $result;
}
/**
* Process the description and extract the parameter config
*
* @return void
*/
private function resolveParameters()
{
// Parameters need special handling when adding
foreach ($this->config['parameters'] as $name => $param) {
if (!is_array($param)) {
throw new \InvalidArgumentException(
"Parameters must be arrays, {$this->config['name']}.$name is ".gettype($param)
);
}
$param['name'] = $name;
$this->parameters[$name] = new Parameter(
$param,
['description' => $this->description]
);
}
if ($this->config['additionalParameters']) {
if (is_array($this->config['additionalParameters'])) {
$this->additionalParameters = new Parameter(
$this->config['additionalParameters'],
['description' => $this->description]
);
} else {
$this->additionalParameters = $this->config['additionalParameters'];
}
}
}
}

View File

@ -0,0 +1,655 @@
<?php
namespace GuzzleHttp\Command\Guzzle;
use GuzzleHttp\Command\ToArrayInterface;
/**
* API parameter object used with service descriptions
*/
class Parameter implements ToArrayInterface
{
private $originalData;
/** @var string $name */
private $name;
/** @var string $description */
private $description;
/** @var string|array $type */
private $type;
/** @var bool $required*/
private $required;
/** @var array|null $enum */
private $enum;
/** @var string $pattern */
private $pattern;
/** @var int $minimum*/
private $minimum;
/** @var int $maximum */
private $maximum;
/** @var int $minLength */
private $minLength;
/** @var int $maxLength */
private $maxLength;
/** @var int $minItems */
private $minItems;
/** @var int $maxItems */
private $maxItems;
/** @var mixed $default */
private $default;
/** @var bool $static */
private $static;
/** @var array $filters */
private $filters;
/** @var string $location */
private $location;
/** @var string $sentAs */
private $sentAs;
/** @var array $data */
private $data;
/** @var array $properties */
private $properties = [];
/** @var array|bool|Parameter $additionalProperties */
private $additionalProperties;
/** @var array|Parameter $items */
private $items;
/** @var string $format */
private $format;
private $propertiesCache = null;
/** @var Description */
private $serviceDescription;
/**
* Create a new Parameter using an associative array of data.
*
* The array can contain the following information:
*
* - name: (string) Unique name of the parameter
*
* - type: (string|array) Type of variable (string, number, integer,
* boolean, object, array, numeric, null, any). Types are used for
* validation and determining the structure of a parameter. You can use a
* union type by providing an array of simple types. If one of the union
* types matches the provided value, then the value is valid.
*
* - required: (bool) Whether or not the parameter is required
*
* - default: (mixed) Default value to use if no value is supplied
*
* - static: (bool) Set to true to specify that the parameter value cannot
* be changed from the default.
*
* - description: (string) Documentation of the parameter
*
* - location: (string) The location of a request used to apply a parameter.
* Custom locations can be registered with a command, but the defaults
* are uri, query, header, body, json, xml, formParam, multipart.
*
* - sentAs: (string) Specifies how the data being modeled is sent over the
* wire. For example, you may wish to include certain headers in a
* response model that have a normalized casing of FooBar, but the actual
* header is x-foo-bar. In this case, sentAs would be set to x-foo-bar.
*
* - filters: (array) Array of static method names to run a parameter
* value through. Each value in the array must be a string containing the
* full class path to a static method or an array of complex filter
* information. You can specify static methods of classes using the full
* namespace class name followed by '::' (e.g. Foo\Bar::baz). Some
* filters require arguments in order to properly filter a value. For
* complex filters, use a hash containing a 'method' key pointing to a
* static method, and an 'args' key containing an array of positional
* arguments to pass to the method. Arguments can contain keywords that
* are replaced when filtering a value: '@value' is replaced with the
* value being validated, '@api' is replaced with the Parameter object.
*
* - properties: When the type is an object, you can specify nested parameters
*
* - additionalProperties: (array) This attribute defines a schema for all
* properties that are not explicitly defined in an object type
* definition. If specified, the value MUST be a schema or a boolean. If
* false is provided, no additional properties are allowed beyond the
* properties defined in the schema. The default value is an empty schema
* which allows any value for additional properties.
*
* - items: This attribute defines the allowed items in an instance array,
* and MUST be a schema or an array of schemas. The default value is an
* empty schema which allows any value for items in the instance array.
* When this attribute value is a schema and the instance value is an
* array, then all the items in the array MUST be valid according to the
* schema.
*
* - pattern: When the type is a string, you can specify the regex pattern
* that a value must match
*
* - enum: When the type is a string, you can specify a list of acceptable
* values.
*
* - minItems: (int) Minimum number of items allowed in an array
*
* - maxItems: (int) Maximum number of items allowed in an array
*
* - minLength: (int) Minimum length of a string
*
* - maxLength: (int) Maximum length of a string
*
* - minimum: (int) Minimum value of an integer
*
* - maximum: (int) Maximum value of an integer
*
* - data: (array) Any additional custom data to use when serializing,
* validating, etc
*
* - format: (string) Format used to coax a value into the correct format
* when serializing or unserializing. You may specify either an array of
* filters OR a format, but not both. Supported values: date-time, date,
* time, timestamp, date-time-http, and boolean-string.
*
* - $ref: (string) String referencing a service description model. The
* parameter is replaced by the schema contained in the model.
*
* @param array $data Array of data as seen in service descriptions
* @param array $options Options used when creating the parameter. You can
* specify a Guzzle service description in the 'description' key.
*
* @throws \InvalidArgumentException
*/
public function __construct(array $data = [], array $options = [])
{
$this->originalData = $data;
if (isset($options['description'])) {
$this->serviceDescription = $options['description'];
if (!($this->serviceDescription instanceof DescriptionInterface)) {
throw new \InvalidArgumentException('description must be a Description');
}
if (isset($data['$ref'])) {
if ($model = $this->serviceDescription->getModel($data['$ref'])) {
$name = isset($data['name']) ? $data['name'] : null;
$data = $model->toArray() + $data;
if ($name) {
$data['name'] = $name;
}
}
} elseif (isset($data['extends'])) {
// If this parameter extends from another parameter then start
// with the actual data union in the parent's data (e.g. actual
// supersedes parent)
if ($extends = $this->serviceDescription->getModel($data['extends'])) {
$data += $extends->toArray();
}
}
}
// Pull configuration data into the parameter
foreach ($data as $key => $value) {
$this->{$key} = $value;
}
$this->required = (bool) $this->required;
$this->data = (array) $this->data;
if ($this->filters) {
$this->setFilters((array) $this->filters);
}
if ($this->type == 'object' && $this->additionalProperties === null) {
$this->additionalProperties = true;
}
}
/**
* Convert the object to an array
*
* @return array
*/
public function toArray()
{
return $this->originalData;
}
/**
* Get the default or static value of the command based on a value
*
* @param string $value Value that is currently set
*
* @return mixed Returns the value, a static value if one is present, or a default value
*/
public function getValue($value)
{
if ($this->static || ($this->default !== null && $value === null)) {
return $this->default;
}
return $value;
}
/**
* Run a value through the filters OR format attribute associated with the
* parameter.
*
* @param mixed $value Value to filter
*
* @return mixed Returns the filtered value
* @throws \RuntimeException when trying to format when no service
* description is available.
*/
public function filter($value)
{
// Formats are applied exclusively and supersed filters
if ($this->format) {
if (!$this->serviceDescription) {
throw new \RuntimeException('No service description was set so '
. 'the value cannot be formatted.');
}
return $this->serviceDescription->format($this->format, $value);
}
// Convert Boolean values
if ($this->type == 'boolean' && !is_bool($value)) {
$value = filter_var($value, FILTER_VALIDATE_BOOLEAN);
}
// Apply filters to the value
if ($this->filters) {
foreach ($this->filters as $filter) {
if (is_array($filter)) {
// Convert complex filters that hold value place holders
foreach ($filter['args'] as &$data) {
if ($data == '@value') {
$data = $value;
} elseif ($data == '@api') {
$data = $this;
}
}
$value = call_user_func_array(
$filter['method'],
$filter['args']
);
} else {
$value = call_user_func($filter, $value);
}
}
}
return $value;
}
/**
* Get the name of the parameter
*
* @return string
*/
public function getName()
{
return $this->name;
}
/**
* Set the name of the parameter
*
* @param string $name Name to set
*/
public function setName($name)
{
$this->name = $name;
}
/**
* Get the key of the parameter, where sentAs will supersede name if it is
* set.
*
* @return string
*/
public function getWireName()
{
return $this->sentAs ?: $this->name;
}
/**
* Get the type(s) of the parameter
*
* @return string|array
*/
public function getType()
{
return $this->type;
}
/**
* Get if the parameter is required
*
* @return bool
*/
public function isRequired()
{
return $this->required;
}
/**
* Get the default value of the parameter
*
* @return string|null
*/
public function getDefault()
{
return $this->default;
}
/**
* Get the description of the parameter
*
* @return string|null
*/
public function getDescription()
{
return $this->description;
}
/**
* Get the minimum acceptable value for an integer
*
* @return int|null
*/
public function getMinimum()
{
return $this->minimum;
}
/**
* Get the maximum acceptable value for an integer
*
* @return int|null
*/
public function getMaximum()
{
return $this->maximum;
}
/**
* Get the minimum allowed length of a string value
*
* @return int
*/
public function getMinLength()
{
return $this->minLength;
}
/**
* Get the maximum allowed length of a string value
*
* @return int|null
*/
public function getMaxLength()
{
return $this->maxLength;
}
/**
* Get the maximum allowed number of items in an array value
*
* @return int|null
*/
public function getMaxItems()
{
return $this->maxItems;
}
/**
* Get the minimum allowed number of items in an array value
*
* @return int
*/
public function getMinItems()
{
return $this->minItems;
}
/**
* Get the location of the parameter
*
* @return string|null
*/
public function getLocation()
{
return $this->location;
}
/**
* Get the sentAs attribute of the parameter that used with locations to
* sentAs an attribute when it is being applied to a location.
*
* @return string|null
*/
public function getSentAs()
{
return $this->sentAs;
}
/**
* Retrieve a known property from the parameter by name or a data property
* by name. When no specific name value is passed, all data properties
* will be returned.
*
* @param string|null $name Specify a particular property name to retrieve
*
* @return array|mixed|null
*/
public function getData($name = null)
{
if (!$name) {
return $this->data;
} elseif (isset($this->data[$name])) {
return $this->data[$name];
} elseif (isset($this->{$name})) {
return $this->{$name};
}
return null;
}
/**
* Get whether or not the default value can be changed
*
* @return bool
*/
public function isStatic()
{
return $this->static;
}
/**
* Get an array of filters used by the parameter
*
* @return array
*/
public function getFilters()
{
return $this->filters ?: [];
}
/**
* Get the properties of the parameter
*
* @return Parameter[]
*/
public function getProperties()
{
if (!$this->propertiesCache) {
$this->propertiesCache = [];
foreach (array_keys($this->properties) as $name) {
$this->propertiesCache[$name] = $this->getProperty($name);
}
}
return $this->propertiesCache;
}
/**
* Get a specific property from the parameter
*
* @param string $name Name of the property to retrieve
*
* @return null|Parameter
*/
public function getProperty($name)
{
if (!isset($this->properties[$name])) {
return null;
}
if (!($this->properties[$name] instanceof self)) {
$this->properties[$name]['name'] = $name;
$this->properties[$name] = new static(
$this->properties[$name],
['description' => $this->serviceDescription]
);
}
return $this->properties[$name];
}
/**
* Get the additionalProperties value of the parameter
*
* @return bool|Parameter|null
*/
public function getAdditionalProperties()
{
if (is_array($this->additionalProperties)) {
$this->additionalProperties = new static(
$this->additionalProperties,
['description' => $this->serviceDescription]
);
}
return $this->additionalProperties;
}
/**
* Get the item data of the parameter
*
* @return Parameter
*/
public function getItems()
{
if (is_array($this->items)) {
$this->items = new static(
$this->items,
['description' => $this->serviceDescription]
);
}
return $this->items;
}
/**
* Get the enum of strings that are valid for the parameter
*
* @return array|null
*/
public function getEnum()
{
return $this->enum;
}
/**
* Get the regex pattern that must match a value when the value is a string
*
* @return string
*/
public function getPattern()
{
return $this->pattern;
}
/**
* Get the format attribute of the schema
*
* @return string
*/
public function getFormat()
{
return $this->format;
}
/**
* Set the array of filters used by the parameter
*
* @param array $filters Array of functions to use as filters
*
* @return self
*/
private function setFilters(array $filters)
{
$this->filters = [];
foreach ($filters as $filter) {
$this->addFilter($filter);
}
return $this;
}
/**
* Add a filter to the parameter
*
* @param string|array $filter Method to filter the value through
*
* @return self
* @throws \InvalidArgumentException
*/
private function addFilter($filter)
{
if (is_array($filter)) {
if (!isset($filter['method'])) {
throw new \InvalidArgumentException(
'A [method] value must be specified for each complex filter'
);
}
}
if (!$this->filters) {
$this->filters = [$filter];
} else {
$this->filters[] = $filter;
}
return $this;
}
/**
* Check if a parameter has a specific variable and if it set.
*
* @param string $var
* @return bool
*/
public function has($var)
{
if (!is_string($var)) {
throw new \InvalidArgumentException('Expected a string. Got: ' . (is_object($var) ? get_class($var) : gettype($var)));
}
return isset($this->{$var}) && !empty($this->{$var});
}
}

View File

@ -0,0 +1,13 @@
<?php
namespace GuzzleHttp\Command\Guzzle\QuerySerializer;
interface QuerySerializerInterface
{
/**
* Aggregate query params and transform them into a string
*
* @param array $queryParams
* @return string
*/
public function aggregate(array $queryParams);
}

View File

@ -0,0 +1,33 @@
<?php
namespace GuzzleHttp\Command\Guzzle\QuerySerializer;
class Rfc3986Serializer implements QuerySerializerInterface
{
/**
* @var bool
*/
private $removeNumericIndices;
/**
* @param bool $removeNumericIndices
*/
public function __construct($removeNumericIndices = false)
{
$this->removeNumericIndices = $removeNumericIndices;
}
/**
* {@inheritDoc}
*/
public function aggregate(array $queryParams)
{
$queryString = http_build_query($queryParams, null, '&', PHP_QUERY_RFC3986);
if ($this->removeNumericIndices) {
$queryString = preg_replace('/%5B[0-9]+%5D/simU', '%5B%5D', $queryString);
}
return $queryString;
}
}

View File

@ -0,0 +1,101 @@
<?php
namespace GuzzleHttp\Command\Guzzle\RequestLocation;
use GuzzleHttp\Command\CommandInterface;
use GuzzleHttp\Command\Guzzle\Operation;
use GuzzleHttp\Command\Guzzle\Parameter;
use Psr\Http\Message\RequestInterface;
abstract class AbstractLocation implements RequestLocationInterface
{
/** @var string */
protected $locationName;
/**
* Set the name of the location
*
* @param $locationName
*/
public function __construct($locationName)
{
$this->locationName = $locationName;
}
/**
* @param CommandInterface $command
* @param RequestInterface $request
* @param Parameter $param
* @return RequestInterface
*/
public function visit(
CommandInterface $command,
RequestInterface $request,
Parameter $param
) {
return $request;
}
/**
* @param CommandInterface $command
* @param RequestInterface $request
* @param Operation $operation
* @return RequestInterface
*/
public function after(
CommandInterface $command,
RequestInterface $request,
Operation $operation
) {
return $request;
}
/**
* Prepare (filter and set desired name for request item) the value for
* request.
*
* @param mixed $value
* @param Parameter $param
*
* @return array|mixed
*/
protected function prepareValue($value, Parameter $param)
{
return is_array($value)
? $this->resolveRecursively($value, $param)
: $param->filter($value);
}
/**
* Recursively prepare and filter nested values.
*
* @param array $value Value to map
* @param Parameter $param Parameter related to the current key.
*
* @return array Returns the mapped array
*/
protected function resolveRecursively(array $value, Parameter $param)
{
foreach ($value as $name => &$v) {
switch ($param->getType()) {
case 'object':
if ($subParam = $param->getProperty($name)) {
$key = $subParam->getWireName();
$value[$key] = $this->prepareValue($v, $subParam);
if ($name != $key) {
unset($value[$name]);
}
} elseif ($param->getAdditionalProperties() instanceof Parameter) {
$v = $this->prepareValue($v, $param->getAdditionalProperties());
}
break;
case 'array':
if ($items = $param->getItems()) {
$v = $this->prepareValue($v, $items);
}
break;
}
}
return $param->filter($value);
}
}

View File

@ -0,0 +1,49 @@
<?php
namespace GuzzleHttp\Command\Guzzle\RequestLocation;
use GuzzleHttp\Command\CommandInterface;
use GuzzleHttp\Command\Guzzle\Parameter;
use GuzzleHttp\Psr7;
use Psr\Http\Message\MessageInterface;
use Psr\Http\Message\RequestInterface;
/**
* Adds a body to a request
*/
class BodyLocation extends AbstractLocation
{
/**
* Set the name of the location
*
* @param string $locationName
*/
public function __construct($locationName = 'body')
{
parent::__construct($locationName);
}
/**
* @param CommandInterface $command
* @param RequestInterface $request
* @param Parameter $param
*
* @return MessageInterface
*/
public function visit(
CommandInterface $command,
RequestInterface $request,
Parameter $param
) {
$oldValue = $request->getBody()->getContents();
$value = $command[$param->getName()];
$value = $param->getName() . '=' . $param->filter($value);
if ($oldValue !== '') {
$value = $oldValue . '&' . $value;
}
return $request->withBody(Psr7\stream_for($value));
}
}

View File

@ -0,0 +1,84 @@
<?php
namespace GuzzleHttp\Command\Guzzle\RequestLocation;
use GuzzleHttp\Command\CommandInterface;
use GuzzleHttp\Command\Guzzle\Operation;
use GuzzleHttp\Command\Guzzle\Parameter;
use GuzzleHttp\Psr7;
use Psr\Http\Message\RequestInterface;
/**
* Add form_params to a request
*/
class FormParamLocation extends AbstractLocation
{
/** @var string $contentType */
protected $contentType = 'application/x-www-form-urlencoded; charset=utf-8';
/** @var array $formParamsData */
protected $formParamsData = [];
/**
* Set the name of the location
*
* @param string $locationName
*/
public function __construct($locationName = 'formParam')
{
parent::__construct($locationName);
}
/**
* @param CommandInterface $command
* @param RequestInterface $request
* @param Parameter $param
*
* @return RequestInterface
*/
public function visit(
CommandInterface $command,
RequestInterface $request,
Parameter $param
) {
$this->formParamsData['form_params'][$param->getWireName()] = $this->prepareValue(
$command[$param->getName()],
$param
);
return $request;
}
/**
* @param CommandInterface $command
* @param RequestInterface $request
* @param Operation $operation
*
* @return RequestInterface
*/
public function after(
CommandInterface $command,
RequestInterface $request,
Operation $operation
) {
$data = $this->formParamsData;
$this->formParamsData = [];
$modify = [];
// Add additional parameters to the form_params array
$additional = $operation->getAdditionalParameters();
if ($additional && $additional->getLocation() == $this->locationName) {
foreach ($command->toArray() as $key => $value) {
if (!$operation->hasParam($key)) {
$data['form_params'][$key] = $this->prepareValue($value, $additional);
}
}
}
$body = http_build_query($data['form_params'], '', '&');
$modify['body'] = Psr7\stream_for($body);
$modify['set_headers']['Content-Type'] = $this->contentType;
$request = Psr7\modify_request($request, $modify);
return $request;
}
}

View File

@ -0,0 +1,67 @@
<?php
namespace GuzzleHttp\Command\Guzzle\RequestLocation;
use GuzzleHttp\Command\CommandInterface;
use GuzzleHttp\Command\Guzzle\Operation;
use GuzzleHttp\Command\Guzzle\Parameter;
use Psr\Http\Message\MessageInterface;
use Psr\Http\Message\RequestInterface;
/**
* Request header location
*/
class HeaderLocation extends AbstractLocation
{
/**
* Set the name of the location
*
* @param string $locationName
*/
public function __construct($locationName = 'header')
{
parent::__construct($locationName);
}
/**
* @param CommandInterface $command
* @param RequestInterface $request
* @param Parameter $param
*
* @return MessageInterface
*/
public function visit(
CommandInterface $command,
RequestInterface $request,
Parameter $param
) {
$value = $command[$param->getName()];
return $request->withHeader($param->getWireName(), $param->filter($value));
}
/**
* @param CommandInterface $command
* @param RequestInterface $request
* @param Operation $operation
*
* @return RequestInterface
*/
public function after(
CommandInterface $command,
RequestInterface $request,
Operation $operation
) {
/** @var Parameter $additional */
$additional = $operation->getAdditionalParameters();
if ($additional && ($additional->getLocation() === $this->locationName)) {
foreach ($command->toArray() as $key => $value) {
if (!$operation->hasParam($key)) {
$request = $request->withHeader($key, $additional->filter($value));
}
}
}
return $request;
}
}

View File

@ -0,0 +1,85 @@
<?php
namespace GuzzleHttp\Command\Guzzle\RequestLocation;
use GuzzleHttp\Command\CommandInterface;
use GuzzleHttp\Command\Guzzle\Operation;
use GuzzleHttp\Command\Guzzle\Parameter;
use GuzzleHttp\Psr7;
use Psr\Http\Message\MessageInterface;
use Psr\Http\Message\RequestInterface;
/**
* Creates a JSON document
*/
class JsonLocation extends AbstractLocation
{
/** @var string Whether or not to add a Content-Type header when JSON is found */
private $jsonContentType;
/** @var array */
private $jsonData;
/**
* @param string $locationName Name of the location
* @param string $contentType Content-Type header to add to the request if
* JSON is added to the body. Pass an empty string to omit.
*/
public function __construct($locationName = 'json', $contentType = 'application/json')
{
parent::__construct($locationName);
$this->jsonContentType = $contentType;
}
/**
* @param CommandInterface $command
* @param RequestInterface $request
* @param Parameter $param
*
* @return RequestInterface
*/
public function visit(
CommandInterface $command,
RequestInterface $request,
Parameter $param
) {
$this->jsonData[$param->getWireName()] = $this->prepareValue(
$command[$param->getName()],
$param
);
return $request->withBody(Psr7\stream_for(\GuzzleHttp\json_encode($this->jsonData)));
}
/**
* @param CommandInterface $command
* @param RequestInterface $request
* @param Operation $operation
*
* @return MessageInterface
*/
public function after(
CommandInterface $command,
RequestInterface $request,
Operation $operation
) {
$data = $this->jsonData;
$this->jsonData = [];
// Add additional parameters to the JSON document
$additional = $operation->getAdditionalParameters();
if ($additional && ($additional->getLocation() === $this->locationName)) {
foreach ($command->toArray() as $key => $value) {
if (!$operation->hasParam($key)) {
$data[$key] = $this->prepareValue($value, $additional);
}
}
}
// Don't overwrite the Content-Type if one is set
if ($this->jsonContentType && !$request->hasHeader('Content-Type')) {
$request = $request->withHeader('Content-Type', $this->jsonContentType);
}
return $request->withBody(Psr7\stream_for(\GuzzleHttp\json_encode($data)));
}
}

View File

@ -0,0 +1,76 @@
<?php
namespace GuzzleHttp\Command\Guzzle\RequestLocation;
use GuzzleHttp\Command\CommandInterface;
use GuzzleHttp\Command\Guzzle\Operation;
use GuzzleHttp\Command\Guzzle\Parameter;
use GuzzleHttp\Psr7;
use Psr\Http\Message\RequestInterface;
/**
* Adds POST files to a request
*/
class MultiPartLocation extends AbstractLocation
{
/** @var string $contentType */
protected $contentType = 'multipart/form-data; boundary=';
/** @var array $formParamsData */
protected $multipartData = [];
/**
* Set the name of the location
*
* @param string $locationName
*/
public function __construct($locationName = 'multipart')
{
parent::__construct($locationName);
}
/**
* @param CommandInterface $command
* @param RequestInterface $request
* @param Parameter $param
* @return RequestInterface
*/
public function visit(
CommandInterface $command,
RequestInterface $request,
Parameter $param
) {
$this->multipartData[] = [
'name' => $param->getWireName(),
'contents' => $this->prepareValue($command[$param->getName()], $param)
];
return $request;
}
/**
* @param CommandInterface $command
* @param RequestInterface $request
* @param Operation $operation
* @return RequestInterface
*/
public function after(
CommandInterface $command,
RequestInterface $request,
Operation $operation
) {
$data = $this->multipartData;
$this->multipartData = [];
$modify = [];
$body = new Psr7\MultipartStream($data);
$modify['body'] = Psr7\stream_for($body);
$request = Psr7\modify_request($request, $modify);
if ($request->getBody() instanceof Psr7\MultipartStream) {
// Use a multipart/form-data POST if a Content-Type is not set.
$request->withHeader('Content-Type', $this->contentType . $request->getBody()->getBoundary());
}
return $request;
}
}

View File

@ -0,0 +1,92 @@
<?php
namespace GuzzleHttp\Command\Guzzle\RequestLocation;
use GuzzleHttp\Command\CommandInterface;
use GuzzleHttp\Command\Guzzle\Operation;
use GuzzleHttp\Command\Guzzle\Parameter;
use GuzzleHttp\Command\Guzzle\QuerySerializer\QuerySerializerInterface;
use GuzzleHttp\Command\Guzzle\QuerySerializer\Rfc3986Serializer;
use GuzzleHttp\Psr7;
use Psr\Http\Message\RequestInterface;
/**
* Adds query string values to requests
*/
class QueryLocation extends AbstractLocation
{
/**
* @var QuerySerializerInterface
*/
private $querySerializer;
/**
* Set the name of the location
*
* @param string $locationName
* @param QuerySerializerInterface|null $querySerializer
*/
public function __construct($locationName = 'query', QuerySerializerInterface $querySerializer = null)
{
parent::__construct($locationName);
$this->querySerializer = $querySerializer ?: new Rfc3986Serializer();
}
/**
* @param CommandInterface $command
* @param RequestInterface $request
* @param Parameter $param
*
* @return RequestInterface
*/
public function visit(
CommandInterface $command,
RequestInterface $request,
Parameter $param
) {
$uri = $request->getUri();
$query = Psr7\parse_query($uri->getQuery());
$query[$param->getWireName()] = $this->prepareValue(
$command[$param->getName()],
$param
);
$uri = $uri->withQuery($this->querySerializer->aggregate($query));
return $request->withUri($uri);
}
/**
* @param CommandInterface $command
* @param RequestInterface $request
* @param Operation $operation
*
* @return RequestInterface
*/
public function after(
CommandInterface $command,
RequestInterface $request,
Operation $operation
) {
$additional = $operation->getAdditionalParameters();
if ($additional && $additional->getLocation() == $this->locationName) {
foreach ($command->toArray() as $key => $value) {
if (!$operation->hasParam($key)) {
$uri = $request->getUri();
$query = Psr7\parse_query($uri->getQuery());
$query[$key] = $this->prepareValue(
$value,
$additional
);
$uri = $uri->withQuery($this->querySerializer->aggregate($query));
$request = $request->withUri($uri);
}
}
}
return $request;
}
}

View File

@ -0,0 +1,44 @@
<?php
namespace GuzzleHttp\Command\Guzzle\RequestLocation;
use GuzzleHttp\Command\CommandInterface;
use GuzzleHttp\Command\Guzzle\Operation;
use GuzzleHttp\Command\Guzzle\Parameter;
use Psr\Http\Message\RequestInterface;
/**
* Handles locations specified in a service description
*/
interface RequestLocationInterface
{
/**
* Visits a location for each top-level parameter
*
* @param CommandInterface $command Command being prepared
* @param RequestInterface $request Request being modified
* @param Parameter $param Parameter being visited
*
* @return RequestInterface Modified request
*/
public function visit(
CommandInterface $command,
RequestInterface $request,
Parameter $param
);
/**
* Called when all of the parameters of a command have been visited.
*
* @param CommandInterface $command Command being prepared
* @param RequestInterface $request Request being modified
* @param Operation $operation Operation being serialized
*
* @return RequestInterface Modified request
*/
public function after(
CommandInterface $command,
RequestInterface $request,
Operation $operation
);
}

View File

@ -0,0 +1,328 @@
<?php
namespace GuzzleHttp\Command\Guzzle\RequestLocation;
use GuzzleHttp\Command\CommandInterface;
use GuzzleHttp\Command\Guzzle\Operation;
use GuzzleHttp\Command\Guzzle\Parameter;
use GuzzleHttp\Psr7;
use Psr\Http\Message\RequestInterface;
/**
* Creates an XML document
*/
class XmlLocation extends AbstractLocation
{
/** @var \XMLWriter XML writer resource */
private $writer;
/** @var string Content-Type header added when XML is found */
private $contentType;
/** @var Parameter[] Buffered elements to write */
private $buffered = [];
/**
* @param string $locationName Name of the location
* @param string $contentType Set to a non-empty string to add a
* Content-Type header to a request if any XML content is added to the
* body. Pass an empty string to disable the addition of the header.
*/
public function __construct($locationName = 'xml', $contentType = 'application/xml')
{
parent::__construct($locationName);
$this->contentType = $contentType;
}
/**
* @param CommandInterface $command
* @param RequestInterface $request
* @param Parameter $param
*
* @return RequestInterface
*/
public function visit(
CommandInterface $command,
RequestInterface $request,
Parameter $param
) {
// Buffer and order the parameters to visit based on if they are
// top-level attributes or child nodes.
// @link https://github.com/guzzle/guzzle/pull/494
if ($param->getData('xmlAttribute')) {
array_unshift($this->buffered, $param);
} else {
$this->buffered[] = $param;
}
return $request;
}
/**
* @param CommandInterface $command
* @param RequestInterface $request
* @param Operation $operation
*
* @return RequestInterface
*/
public function after(
CommandInterface $command,
RequestInterface $request,
Operation $operation
) {
foreach ($this->buffered as $param) {
$this->visitWithValue(
$command[$param->getName()],
$param,
$operation
);
}
$this->buffered = [];
$additional = $operation->getAdditionalParameters();
if ($additional && $additional->getLocation() == $this->locationName) {
foreach ($command->toArray() as $key => $value) {
if (!$operation->hasParam($key)) {
$additional->setName($key);
$this->visitWithValue($value, $additional, $operation);
}
}
$additional->setName(null);
}
// If data was found that needs to be serialized, then do so
$xml = '';
if ($this->writer) {
$xml = $this->finishDocument($this->writer);
} elseif ($operation->getData('xmlAllowEmpty')) {
// Check if XML should always be sent for the command
$writer = $this->createRootElement($operation);
$xml = $this->finishDocument($writer);
}
if ($xml !== '') {
$request = $request->withBody(Psr7\stream_for($xml));
// Don't overwrite the Content-Type if one is set
if ($this->contentType && !$request->hasHeader('Content-Type')) {
$request = $request->withHeader('Content-Type', $this->contentType);
}
}
$this->writer = null;
return $request;
}
/**
* Create the root XML element to use with a request
*
* @param Operation $operation Operation object
*
* @return \XMLWriter
*/
protected function createRootElement(Operation $operation)
{
static $defaultRoot = ['name' => 'Request'];
// If no root element was specified, then just wrap the XML in 'Request'
$root = $operation->getData('xmlRoot') ?: $defaultRoot;
// Allow the XML declaration to be customized with xmlEncoding
$encoding = $operation->getData('xmlEncoding');
$writer = $this->startDocument($encoding);
$writer->startElement($root['name']);
// Create the wrapping element with no namespaces if no namespaces were present
if (!empty($root['namespaces'])) {
// Create the wrapping element with an array of one or more namespaces
foreach ((array) $root['namespaces'] as $prefix => $uri) {
$nsLabel = 'xmlns';
if (!is_numeric($prefix)) {
$nsLabel .= ':'.$prefix;
}
$writer->writeAttribute($nsLabel, $uri);
}
}
return $writer;
}
/**
* Recursively build the XML body
*
* @param \XMLWriter $writer XML to modify
* @param Parameter $param API Parameter
* @param mixed $value Value to add
*/
protected function addXml(\XMLWriter $writer, Parameter $param, $value)
{
$value = $param->filter($value);
$type = $param->getType();
$name = $param->getWireName();
$prefix = null;
$namespace = $param->getData('xmlNamespace');
if (false !== strpos($name, ':')) {
list($prefix, $name) = explode(':', $name, 2);
}
if ($type == 'object' || $type == 'array') {
if (!$param->getData('xmlFlattened')) {
if ($namespace) {
$writer->startElementNS(null, $name, $namespace);
} else {
$writer->startElement($name);
}
}
if ($param->getType() == 'array') {
$this->addXmlArray($writer, $param, $value);
} elseif ($param->getType() == 'object') {
$this->addXmlObject($writer, $param, $value);
}
if (!$param->getData('xmlFlattened')) {
$writer->endElement();
}
return;
}
if ($param->getData('xmlAttribute')) {
$this->writeAttribute($writer, $prefix, $name, $namespace, $value);
} else {
$this->writeElement($writer, $prefix, $name, $namespace, $value);
}
}
/**
* Write an attribute with namespace if used
*
* @param \XMLWriter $writer XMLWriter instance
* @param string $prefix Namespace prefix if any
* @param string $name Attribute name
* @param string $namespace The uri of the namespace
* @param string $value The attribute content
*/
protected function writeAttribute($writer, $prefix, $name, $namespace, $value)
{
if ($namespace) {
$writer->writeAttributeNS($prefix, $name, $namespace, $value);
} else {
$writer->writeAttribute($name, $value);
}
}
/**
* Write an element with namespace if used
*
* @param \XMLWriter $writer XML writer resource
* @param string $prefix Namespace prefix if any
* @param string $name Element name
* @param string $namespace The uri of the namespace
* @param string $value The element content
*/
protected function writeElement(\XMLWriter $writer, $prefix, $name, $namespace, $value)
{
if ($namespace) {
$writer->startElementNS($prefix, $name, $namespace);
} else {
$writer->startElement($name);
}
if (strpbrk($value, '<>&')) {
$writer->writeCData($value);
} else {
$writer->writeRaw($value);
}
$writer->endElement();
}
/**
* Create a new xml writer and start a document
*
* @param string $encoding document encoding
*
* @return \XMLWriter the writer resource
* @throws \RuntimeException if the document cannot be started
*/
protected function startDocument($encoding)
{
$this->writer = new \XMLWriter();
if (!$this->writer->openMemory()) {
throw new \RuntimeException('Unable to open XML document in memory');
}
if (!$this->writer->startDocument('1.0', $encoding)) {
throw new \RuntimeException('Unable to start XML document');
}
return $this->writer;
}
/**
* End the document and return the output
*
* @param \XMLWriter $writer
*
* @return string the writer resource
*/
protected function finishDocument($writer)
{
$writer->endDocument();
return $writer->outputMemory();
}
/**
* Add an array to the XML
*
* @param \XMLWriter $writer
* @param Parameter $param
* @param $value
*/
protected function addXmlArray(\XMLWriter $writer, Parameter $param, &$value)
{
if ($items = $param->getItems()) {
foreach ($value as $v) {
$this->addXml($writer, $items, $v);
}
}
}
/**
* Add an object to the XML
*
* @param \XMLWriter $writer
* @param Parameter $param
* @param $value
*/
protected function addXmlObject(\XMLWriter $writer, Parameter $param, &$value)
{
$noAttributes = [];
// add values which have attributes
foreach ($value as $name => $v) {
if ($property = $param->getProperty($name)) {
if ($property->getData('xmlAttribute')) {
$this->addXml($writer, $property, $v);
} else {
$noAttributes[] = ['value' => $v, 'property' => $property];
}
}
}
// now add values with no attributes
foreach ($noAttributes as $element) {
$this->addXml($writer, $element['property'], $element['value']);
}
}
/**
* @param $value
* @param Parameter $param
* @param Operation $operation
*/
private function visitWithValue(
$value,
Parameter $param,
Operation $operation
) {
if (!$this->writer) {
$this->createRootElement($operation);
}
$this->addXml($this->writer, $param, $value);
}
}

View File

@ -0,0 +1,69 @@
<?php
namespace GuzzleHttp\Command\Guzzle\ResponseLocation;
use GuzzleHttp\Command\Guzzle\Parameter;
use GuzzleHttp\Command\ResultInterface;
use Psr\Http\Message\ResponseInterface;
/**
* Class AbstractLocation
*
* @package GuzzleHttp\Command\Guzzle\ResponseLocation
*/
abstract class AbstractLocation implements ResponseLocationInterface
{
/** @var string $locationName */
protected $locationName;
/**
* Set the name of the location
*
* @param $locationName
*/
public function __construct($locationName)
{
$this->locationName = $locationName;
}
/**
* @param ResultInterface $result
* @param ResponseInterface $response
* @param Parameter $model
* @return ResultInterface
*/
public function before(
ResultInterface $result,
ResponseInterface $response,
Parameter $model
) {
return $result;
}
/**
* @param ResultInterface $result
* @param ResponseInterface $response
* @param Parameter $model
* @return ResultInterface
*/
public function after(
ResultInterface $result,
ResponseInterface $response,
Parameter $model
) {
return $result;
}
/**
* @param ResultInterface $result
* @param ResponseInterface $response
* @param Parameter $param
* @return ResultInterface
*/
public function visit(
ResultInterface $result,
ResponseInterface $response,
Parameter $param
) {
return $result;
}
}

View File

@ -0,0 +1,39 @@
<?php
namespace GuzzleHttp\Command\Guzzle\ResponseLocation;
use GuzzleHttp\Command\Guzzle\Parameter;
use GuzzleHttp\Command\ResultInterface;
use Psr\Http\Message\ResponseInterface;
/**
* Extracts the body of a response into a result field
*/
class BodyLocation extends AbstractLocation
{
/**
* Set the name of the location
*
* @param string $locationName
*/
public function __construct($locationName = 'body')
{
parent::__construct($locationName);
}
/**
* @param ResultInterface $result
* @param ResponseInterface $response
* @param Parameter $param
* @return ResultInterface
*/
public function visit(
ResultInterface $result,
ResponseInterface $response,
Parameter $param
) {
$result[$param->getName()] = $param->filter($response->getBody());
return $result;
}
}

View File

@ -0,0 +1,47 @@
<?php
namespace GuzzleHttp\Command\Guzzle\ResponseLocation;
use GuzzleHttp\Command\Guzzle\Parameter;
use GuzzleHttp\Command\ResultInterface;
use Psr\Http\Message\ResponseInterface;
/**
* Extracts headers from the response into a result fields
*/
class HeaderLocation extends AbstractLocation
{
/**
* Set the name of the location
*
* @param string $locationName
*/
public function __construct($locationName = 'header')
{
parent::__construct($locationName);
}
/**
* @param ResultInterface $result
* @param ResponseInterface $response
* @param Parameter $param
*
* @return ResultInterface
*/
public function visit(
ResultInterface $result,
ResponseInterface $response,
Parameter $param
) {
// Retrieving a single header by name
$name = $param->getName();
if ($header = $response->getHeader($param->getWireName())) {
if (is_array($header)) {
$header = array_shift($header);
}
$result[$name] = $param->filter($header);
}
return $result;
}
}

View File

@ -0,0 +1,176 @@
<?php
namespace GuzzleHttp\Command\Guzzle\ResponseLocation;
use GuzzleHttp\Command\Guzzle\Parameter;
use GuzzleHttp\Command\Result;
use GuzzleHttp\Command\ResultInterface;
use Psr\Http\Message\ResponseInterface;
/**
* Extracts elements from a JSON document.
*/
class JsonLocation extends AbstractLocation
{
/** @var array The JSON document being visited */
private $json = [];
/**
* Set the name of the location
*
* @param string $locationName
*/
public function __construct($locationName = 'json')
{
parent::__construct($locationName);
}
/**
* @param \GuzzleHttp\Command\ResultInterface $result
* @param \Psr\Http\Message\ResponseInterface $response
* @param \GuzzleHttp\Command\Guzzle\Parameter $model
*
* @return \GuzzleHttp\Command\ResultInterface
*/
public function before(
ResultInterface $result,
ResponseInterface $response,
Parameter $model
) {
$body = (string) $response->getBody();
$body = $body ?: "{}";
$this->json = \GuzzleHttp\json_decode($body, true);
// relocate named arrays, so that they have the same structure as
// arrays nested in objects and visit can work on them in the same way
if ($model->getType() === 'array' && ($name = $model->getName())) {
$this->json = [$name => $this->json];
}
return $result;
}
/**
* @param ResultInterface $result
* @param ResponseInterface $response
* @param Parameter $model
* @return ResultInterface
*/
public function after(
ResultInterface $result,
ResponseInterface $response,
Parameter $model
) {
// Handle additional, undefined properties
$additional = $model->getAdditionalProperties();
if (!($additional instanceof Parameter)) {
return $result;
}
// Use the model location as the default if one is not set on additional
$addLocation = $additional->getLocation() ?: $model->getLocation();
if ($addLocation == $this->locationName) {
foreach ($this->json as $prop => $val) {
if (!isset($result[$prop])) {
// Only recurse if there is a type specified
$result[$prop] = $additional->getType()
? $this->recurse($additional, $val)
: $val;
}
}
}
$this->json = [];
return $result;
}
/**
* @param ResultInterface $result
* @param ResponseInterface $response
* @param Parameter $param
* @return Result|ResultInterface
*/
public function visit(
ResultInterface $result,
ResponseInterface $response,
Parameter $param
) {
$name = $param->getName();
$key = $param->getWireName();
// Check if the result should be treated as a list
if ($param->getType() == 'array') {
// Treat as javascript array
if ($name) {
// name provided, store it under a key in the array
$subArray = isset($this->json[$key]) ? $this->json[$key] : null;
$result[$name] = $this->recurse($param, $subArray);
} else {
// top-level `array` or an empty name
$result = new Result(array_merge(
$result->toArray(),
$this->recurse($param, $this->json)
));
}
} elseif (isset($this->json[$key])) {
$result[$name] = $this->recurse($param, $this->json[$key]);
}
return $result;
}
/**
* Recursively process a parameter while applying filters
*
* @param Parameter $param API parameter being validated
* @param mixed $value Value to process.
* @return mixed|null
*/
private function recurse(Parameter $param, $value)
{
if (!is_array($value)) {
return $param->filter($value);
}
$result = [];
$type = $param->getType();
if ($type == 'array') {
$items = $param->getItems();
foreach ($value as $val) {
$result[] = $this->recurse($items, $val);
}
} elseif ($type == 'object' && !isset($value[0])) {
// On the above line, we ensure that the array is associative and
// not numerically indexed
if ($properties = $param->getProperties()) {
foreach ($properties as $property) {
$key = $property->getWireName();
if (array_key_exists($key, $value)) {
$result[$property->getName()] = $this->recurse(
$property,
$value[$key]
);
// Remove from the value so that AP can later be handled
unset($value[$key]);
}
}
}
// Only check additional properties if everything wasn't already
// handled
if ($value) {
$additional = $param->getAdditionalProperties();
if ($additional === null || $additional === true) {
// Merge the JSON under the resulting array
$result += $value;
} elseif ($additional instanceof Parameter) {
// Process all child elements according to the given schema
foreach ($value as $prop => $val) {
$result[$prop] = $this->recurse($additional, $val);
}
}
}
}
return $param->filter($result);
}
}

View File

@ -0,0 +1,41 @@
<?php
namespace GuzzleHttp\Command\Guzzle\ResponseLocation;
use GuzzleHttp\Command\Guzzle\Parameter;
use GuzzleHttp\Command\ResultInterface;
use Psr\Http\Message\ResponseInterface;
/**
* Extracts the reason phrase of a response into a result field
*/
class ReasonPhraseLocation extends AbstractLocation
{
/**
* Set the name of the location
*
* @param string $locationName
*/
public function __construct($locationName = 'reasonPhrase')
{
parent::__construct($locationName);
}
/**
* @param ResultInterface $result
* @param ResponseInterface $response
* @param Parameter $param
* @return ResultInterface
*/
public function visit(
ResultInterface $result,
ResponseInterface $response,
Parameter $param
) {
$result[$param->getName()] = $param->filter(
$response->getReasonPhrase()
);
return $result;
}
}

View File

@ -0,0 +1,61 @@
<?php
namespace GuzzleHttp\Command\Guzzle\ResponseLocation;
use GuzzleHttp\Command\Guzzle\Parameter;
use GuzzleHttp\Command\ResultInterface;
use Psr\Http\Message\ResponseInterface;
/**
* Location visitor used to parse values out of a response into an associative
* array
*/
interface ResponseLocationInterface
{
/**
* Called before visiting all parameters. This can be used for seeding the
* result of a command with default data (e.g. populating with JSON data in
* the response then adding to the parsed data).
*
* @param ResultInterface $result Result being created
* @param ResponseInterface $response Response being visited
* @param Parameter $model Response model
*
* @return ResultInterface Modified result
*/
public function before(
ResultInterface $result,
ResponseInterface $response,
Parameter $model
);
/**
* Called after visiting all parameters
*
* @param ResultInterface $result Result being created
* @param ResponseInterface $response Response being visited
* @param Parameter $model Response model
*
* @return ResultInterface Modified result
*/
public function after(
ResultInterface $result,
ResponseInterface $response,
Parameter $model
);
/**
* Called once for each parameter being visited that matches the location
* type.
*
* @param ResultInterface $result Result being created
* @param ResponseInterface $response Response being visited
* @param Parameter $param Parameter being visited
*
* @return ResultInterface Modified result
*/
public function visit(
ResultInterface $result,
ResponseInterface $response,
Parameter $param
);
}

View File

@ -0,0 +1,39 @@
<?php
namespace GuzzleHttp\Command\Guzzle\ResponseLocation;
use GuzzleHttp\Command\Guzzle\Parameter;
use GuzzleHttp\Command\ResultInterface;
use Psr\Http\Message\ResponseInterface;
/**
* Extracts the status code of a response into a result field
*/
class StatusCodeLocation extends AbstractLocation
{
/**
* Set the name of the location
*
* @param string $locationName
*/
public function __construct($locationName = 'statusCode')
{
parent::__construct($locationName);
}
/**
* @param ResultInterface $result
* @param ResponseInterface $response
* @param Parameter $param
* @return ResultInterface
*/
public function visit(
ResultInterface $result,
ResponseInterface $response,
Parameter $param
) {
$result[$param->getName()] = $param->filter($response->getStatusCode());
return $result;
}
}

View File

@ -0,0 +1,311 @@
<?php
namespace GuzzleHttp\Command\Guzzle\ResponseLocation;
use GuzzleHttp\Command\Guzzle\Parameter;
use GuzzleHttp\Command\Result;
use GuzzleHttp\Command\ResultInterface;
use Psr\Http\Message\ResponseInterface;
/**
* Extracts elements from an XML document
*/
class XmlLocation extends AbstractLocation
{
/** @var \SimpleXMLElement XML document being visited */
private $xml;
/**
* Set the name of the location
*
* @param string $locationName
*/
public function __construct($locationName = 'xml')
{
parent::__construct($locationName);
}
/**
* @param ResultInterface $result
* @param ResponseInterface $response
* @param Parameter $model
* @return ResultInterface
*/
public function before(
ResultInterface $result,
ResponseInterface $response,
Parameter $model
) {
$this->xml = simplexml_load_string((string) $response->getBody());
return $result;
}
/**
* @param ResultInterface $result
* @param ResponseInterface $response
* @param Parameter $model
* @return Result|ResultInterface
*/
public function after(
ResultInterface $result,
ResponseInterface $response,
Parameter $model
) {
// Handle additional, undefined properties
$additional = $model->getAdditionalProperties();
if ($additional instanceof Parameter &&
$additional->getLocation() == $this->locationName
) {
$result = new Result(array_merge(
$result->toArray(),
self::xmlToArray($this->xml)
));
}
$this->xml = null;
return $result;
}
/**
* @param ResultInterface $result
* @param ResponseInterface $response
* @param Parameter $param
* @return ResultInterface
*/
public function visit(
ResultInterface $result,
ResponseInterface $response,
Parameter $param
) {
$sentAs = $param->getWireName();
$ns = null;
if (strstr($sentAs, ':')) {
list($ns, $sentAs) = explode(':', $sentAs);
}
// Process the primary property
if (count($this->xml->children($ns, true)->{$sentAs})) {
$result[$param->getName()] = $this->recursiveProcess(
$param,
$this->xml->children($ns, true)->{$sentAs}
);
}
return $result;
}
/**
* Recursively process a parameter while applying filters
*
* @param Parameter $param API parameter being processed
* @param \SimpleXMLElement $node Node being processed
* @return array
*/
private function recursiveProcess(
Parameter $param,
\SimpleXMLElement $node
) {
$result = [];
$type = $param->getType();
if ($type == 'object') {
$result = $this->processObject($param, $node);
} elseif ($type == 'array') {
$result = $this->processArray($param, $node);
} else {
// We are probably handling a flat data node (i.e. string or
// integer), so let's check if it's childless, which indicates a
// node containing plain text.
if ($node->children()->count() == 0) {
// Retrieve text from node
$result = (string) $node;
}
}
// Filter out the value
if (isset($result)) {
$result = $param->filter($result);
}
return $result;
}
/**
* @param Parameter $param
* @param \SimpleXMLElement $node
* @return array
*/
private function processArray(Parameter $param, \SimpleXMLElement $node)
{
// Cast to an array if the value was a string, but should be an array
$items = $param->getItems();
$sentAs = $items->getWireName();
$result = [];
$ns = null;
if (strstr($sentAs, ':')) {
// Get namespace from the wire name
list($ns, $sentAs) = explode(':', $sentAs);
} else {
// Get namespace from data
$ns = $items->getData('xmlNs');
}
if ($sentAs === null) {
// A general collection of nodes
foreach ($node as $child) {
$result[] = $this->recursiveProcess($items, $child);
}
} else {
// A collection of named, repeating nodes
// (i.e. <collection><foo></foo><foo></foo></collection>)
$children = $node->children($ns, true)->{$sentAs};
foreach ($children as $child) {
$result[] = $this->recursiveProcess($items, $child);
}
}
return $result;
}
/**
* Process an object
*
* @param Parameter $param API parameter being parsed
* @param \SimpleXMLElement $node Value to process
* @return array
*/
private function processObject(Parameter $param, \SimpleXMLElement $node)
{
$result = $knownProps = $knownAttributes = [];
// Handle known properties
if ($properties = $param->getProperties()) {
foreach ($properties as $property) {
$name = $property->getName();
$sentAs = $property->getWireName();
$knownProps[$sentAs] = 1;
if (strpos($sentAs, ':')) {
list($ns, $sentAs) = explode(':', $sentAs);
} else {
$ns = $property->getData('xmlNs');
}
if ($property->getData('xmlAttribute')) {
// Handle XML attributes
$result[$name] = (string) $node->attributes($ns, true)->{$sentAs};
$knownAttributes[$sentAs] = 1;
} elseif (count($node->children($ns, true)->{$sentAs})) {
// Found a child node matching wire name
$childNode = $node->children($ns, true)->{$sentAs};
$result[$name] = $this->recursiveProcess(
$property,
$childNode
);
}
}
}
// Handle additional, undefined properties
$additional = $param->getAdditionalProperties();
if ($additional instanceof Parameter) {
// Process all child elements according to the given schema
foreach ($node->children($additional->getData('xmlNs'), true) as $childNode) {
$sentAs = $childNode->getName();
if (!isset($knownProps[$sentAs])) {
$result[$sentAs] = $this->recursiveProcess(
$additional,
$childNode
);
}
}
} elseif ($additional === null || $additional === true) {
// Blindly transform the XML into an array preserving as much data
// as possible. Remove processed, aliased properties.
$array = array_diff_key(self::xmlToArray($node), $knownProps);
// Remove @attributes that were explicitly plucked from the
// attributes list.
if (isset($array['@attributes']) && $knownAttributes) {
$array['@attributes'] = array_diff_key($array['@attributes'], $knownProps);
if (!$array['@attributes']) {
unset($array['@attributes']);
}
}
// Merge it together with the original result
$result = array_merge($array, $result);
}
return $result;
}
/**
* Convert an XML document to an array.
*
* @param \SimpleXMLElement $xml
* @param int $nesting
* @param null $ns
*
* @return array
*/
private static function xmlToArray(
\SimpleXMLElement $xml,
$ns = null,
$nesting = 0
) {
$result = [];
$children = $xml->children($ns, true);
foreach ($children as $name => $child) {
$attributes = (array) $child->attributes($ns, true);
if (!isset($result[$name])) {
$childArray = self::xmlToArray($child, $ns, $nesting + 1);
$result[$name] = $attributes
? array_merge($attributes, $childArray)
: $childArray;
continue;
}
// A child element with this name exists so we're assuming
// that the node contains a list of elements
if (!is_array($result[$name])) {
$result[$name] = [$result[$name]];
} elseif (!isset($result[$name][0])) {
// Convert the first child into the first element of a numerically indexed array
$firstResult = $result[$name];
$result[$name] = [];
$result[$name][] = $firstResult;
}
$childArray = self::xmlToArray($child, $ns, $nesting + 1);
if ($attributes) {
$result[$name][] = array_merge($attributes, $childArray);
} else {
$result[$name][] = $childArray;
}
}
// Extract text from node
$text = trim((string) $xml);
if ($text === '') {
$text = null;
}
// Process attributes
$attributes = (array) $xml->attributes($ns, true);
if ($attributes) {
if ($text !== null) {
$result['value'] = $text;
}
$result = array_merge($attributes, $result);
} elseif ($text !== null) {
$result = $text;
}
// Make sure we're always returning an array
if ($nesting == 0 && !is_array($result)) {
$result = [$result];
}
return $result;
}
}

View File

@ -0,0 +1,141 @@
<?php
namespace GuzzleHttp\Command\Guzzle;
/**
* JSON Schema formatter class
*/
class SchemaFormatter
{
/**
* Format a value by a registered format name
*
* @param string $format Registered format used to format the value
* @param mixed $value Value being formatted
*
* @return mixed
*/
public function format($format, $value)
{
switch ($format) {
case 'date-time':
return $this->formatDateTime($value);
case 'date-time-http':
return $this->formatDateTimeHttp($value);
case 'date':
return $this->formatDate($value);
case 'time':
return $this->formatTime($value);
case 'timestamp':
return $this->formatTimestamp($value);
case 'boolean-string':
return $this->formatBooleanAsString($value);
default:
return $value;
}
}
/**
* Perform the actual DateTime formatting
*
* @param int|string|\DateTime $dateTime Date time value
* @param string $format Format of the result
*
* @return string
* @throws \InvalidArgumentException
*/
protected function dateFormatter($dateTime, $format)
{
if (is_numeric($dateTime)) {
return gmdate($format, (int) $dateTime);
}
if (is_string($dateTime)) {
$dateTime = new \DateTime($dateTime);
}
if ($dateTime instanceof \DateTimeInterface) {
static $utc;
if (!$utc) {
$utc = new \DateTimeZone('UTC');
}
return $dateTime->setTimezone($utc)->format($format);
}
throw new \InvalidArgumentException('Date/Time values must be either '
. 'be a string, integer, or DateTime object');
}
/**
* Create a ISO 8601 (YYYY-MM-DDThh:mm:ssZ) formatted date time value in
* UTC time.
*
* @param string|integer|\DateTime $value Date time value
*
* @return string
*/
private function formatDateTime($value)
{
return $this->dateFormatter($value, 'Y-m-d\TH:i:s\Z');
}
/**
* Create an HTTP date (RFC 1123 / RFC 822) formatted UTC date-time string
*
* @param string|integer|\DateTime $value Date time value
*
* @return string
*/
private function formatDateTimeHttp($value)
{
return $this->dateFormatter($value, 'D, d M Y H:i:s \G\M\T');
}
/**
* Create a YYYY-MM-DD formatted string
*
* @param string|integer|\DateTime $value Date time value
*
* @return string
*/
private function formatDate($value)
{
return $this->dateFormatter($value, 'Y-m-d');
}
/**
* Create a hh:mm:ss formatted string
*
* @param string|integer|\DateTime $value Date time value
*
* @return string
*/
private function formatTime($value)
{
return $this->dateFormatter($value, 'H:i:s');
}
/**
* Formats a boolean value as a string
*
* @param string|integer|bool $value Value to convert to a boolean
* 'true' / 'false' value
*
* @return string
*/
private function formatBooleanAsString($value)
{
return filter_var($value, FILTER_VALIDATE_BOOLEAN) ? 'true' : 'false';
}
/**
* Return a UNIX timestamp in the UTC timezone
*
* @param string|integer|\DateTime $value Time value
*
* @return int
*/
private function formatTimestamp($value)
{
return (int) $this->dateFormatter($value, 'U');
}
}

View File

@ -0,0 +1,297 @@
<?php
namespace GuzzleHttp\Command\Guzzle;
use GuzzleHttp\Command\ToArrayInterface;
/**
* Default parameter validator
*/
class SchemaValidator
{
/**
* Whether or not integers are converted to strings when an integer is
* received for a string input
*
* @var bool
*/
protected $castIntegerToStringType;
/** @var array Errors encountered while validating */
protected $errors;
/**
* @param bool $castIntegerToStringType Set to true to convert integers
* into strings when a required type is a string and the input value is
* an integer. Defaults to true.
*/
public function __construct($castIntegerToStringType = true)
{
$this->castIntegerToStringType = $castIntegerToStringType;
}
/**
* @param Parameter $param
* @param $value
* @return bool
*/
public function validate(Parameter $param, &$value)
{
$this->errors = [];
$this->recursiveProcess($param, $value);
if (empty($this->errors)) {
return true;
} else {
sort($this->errors);
return false;
}
}
/**
* Get the errors encountered while validating
*
* @return array
*/
public function getErrors()
{
return $this->errors ?: [];
}
/**
* From the allowable types, determine the type that the variable matches
*
* @param string|array $type Parameter type
* @param mixed $value Value to determine the type
*
* @return string|false Returns the matching type on
*/
protected function determineType($type, $value)
{
foreach ((array) $type as $t) {
if ($t == 'string'
&& (is_string($value) || (is_object($value) && method_exists($value, '__toString')))
) {
return 'string';
} elseif ($t == 'object' && (is_array($value) || is_object($value))) {
return 'object';
} elseif ($t == 'array' && is_array($value)) {
return 'array';
} elseif ($t == 'integer' && is_integer($value)) {
return 'integer';
} elseif ($t == 'boolean' && is_bool($value)) {
return 'boolean';
} elseif ($t == 'number' && is_numeric($value)) {
return 'number';
} elseif ($t == 'numeric' && is_numeric($value)) {
return 'numeric';
} elseif ($t == 'null' && !$value) {
return 'null';
} elseif ($t == 'any') {
return 'any';
}
}
return false;
}
/**
* Recursively validate a parameter
*
* @param Parameter $param API parameter being validated
* @param mixed $value Value to validate and validate. The value may
* change during this validate.
* @param string $path Current validation path (used for error reporting)
* @param int $depth Current depth in the validation validate
*
* @return bool Returns true if valid, or false if invalid
*/
protected function recursiveProcess(
Parameter $param,
&$value,
$path = '',
$depth = 0
) {
// Update the value by adding default or static values
$value = $param->getValue($value);
$required = $param->isRequired();
// if the value is null and the parameter is not required or is static,
// then skip any further recursion
if ((null === $value && !$required) || $param->isStatic()) {
return true;
}
$type = $param->getType();
// Attempt to limit the number of times is_array is called by tracking
// if the value is an array
$valueIsArray = is_array($value);
// If a name is set then update the path so that validation messages
// are more helpful
if ($name = $param->getName()) {
$path .= "[{$name}]";
}
if ($type == 'object') {
// Determine whether or not this "value" has properties and should
// be traversed
$traverse = $temporaryValue = false;
// Convert the value to an array
if (!$valueIsArray && $value instanceof ToArrayInterface) {
$value = $value->toArray();
}
if ($valueIsArray) {
// Ensure that the array is associative and not numerically
// indexed
if (isset($value[0])) {
$this->errors[] = "{$path} must be an array of properties. Got a numerically indexed array.";
return false;
}
$traverse = true;
} elseif ($value === null) {
// Attempt to let the contents be built up by default values if
// possible
$value = [];
$temporaryValue = $valueIsArray = $traverse = true;
}
if ($traverse) {
if ($properties = $param->getProperties()) {
// if properties were found, validate each property
foreach ($properties as $property) {
$name = $property->getName();
if (isset($value[$name])) {
$this->recursiveProcess($property, $value[$name], $path, $depth + 1);
} else {
$current = null;
$this->recursiveProcess($property, $current, $path, $depth + 1);
// Only set the value if it was populated
if (null !== $current) {
$value[$name] = $current;
}
}
}
}
$additional = $param->getAdditionalProperties();
if ($additional !== true) {
// If additional properties were found, then validate each
// against the additionalProperties attr.
$keys = array_keys($value);
// Determine the keys that were specified that were not
// listed in the properties of the schema
$diff = array_diff($keys, array_keys($properties));
if (!empty($diff)) {
// Determine which keys are not in the properties
if ($additional instanceof Parameter) {
foreach ($diff as $key) {
$this->recursiveProcess($additional, $value[$key], "{$path}[{$key}]", $depth);
}
} else {
// if additionalProperties is set to false and there
// are additionalProperties in the values, then fail
foreach ($diff as $prop) {
$this->errors[] = sprintf('%s[%s] is not an allowed property', $path, $prop);
}
}
}
}
// A temporary value will be used to traverse elements that
// have no corresponding input value. This allows nested
// required parameters with default values to bubble up into the
// input. Here we check if we used a temp value and nothing
// bubbled up, then we need to remote the value.
if ($temporaryValue && empty($value)) {
$value = null;
$valueIsArray = false;
}
}
} elseif ($type == 'array' && $valueIsArray && $param->getItems()) {
foreach ($value as $i => &$item) {
// Validate each item in an array against the items attribute of the schema
$this->recursiveProcess($param->getItems(), $item, $path . "[{$i}]", $depth + 1);
}
}
// If the value is required and the type is not null, then there is an
// error if the value is not set
if ($required && $value === null && $type != 'null') {
$message = "{$path} is " . ($param->getType()
? ('a required ' . implode(' or ', (array) $param->getType()))
: 'required');
if ($param->has('description')) {
$message .= ': ' . $param->getDescription();
}
$this->errors[] = $message;
return false;
}
// Validate that the type is correct. If the type is string but an
// integer was passed, the class can be instructed to cast the integer
// to a string to pass validation. This is the default behavior.
if ($type && (!$type = $this->determineType($type, $value))) {
if ($this->castIntegerToStringType
&& $param->getType() == 'string'
&& is_integer($value)
) {
$value = (string) $value;
} else {
$this->errors[] = "{$path} must be of type " . implode(' or ', (array) $param->getType());
}
}
// Perform type specific validation for strings, arrays, and integers
if ($type == 'string') {
// Strings can have enums which are a list of predefined values
if (($enum = $param->getEnum()) && !in_array($value, $enum)) {
$this->errors[] = "{$path} must be one of " . implode(' or ', array_map(function ($s) {
return '"' . addslashes($s) . '"';
}, $enum));
}
// Strings can have a regex pattern that the value must match
if (($pattern = $param->getPattern()) && !preg_match($pattern, $value)) {
$this->errors[] = "{$path} must match the following regular expression: {$pattern}";
}
$strLen = null;
if ($min = $param->getMinLength()) {
$strLen = strlen($value);
if ($strLen < $min) {
$this->errors[] = "{$path} length must be greater than or equal to {$min}";
}
}
if ($max = $param->getMaxLength()) {
if (($strLen ?: strlen($value)) > $max) {
$this->errors[] = "{$path} length must be less than or equal to {$max}";
}
}
} elseif ($type == 'array') {
$size = null;
if ($min = $param->getMinItems()) {
$size = count($value);
if ($size < $min) {
$this->errors[] = "{$path} must contain {$min} or more elements";
}
}
if ($max = $param->getMaxItems()) {
if (($size ?: count($value)) > $max) {
$this->errors[] = "{$path} must contain {$max} or fewer elements";
}
}
} elseif ($type == 'integer' || $type == 'number' || $type == 'numeric') {
if (($min = $param->getMinimum()) && $value < $min) {
$this->errors[] = "{$path} must be greater than or equal to {$min}";
}
if (($max = $param->getMaximum()) && $value > $max) {
$this->errors[] = "{$path} must be less than or equal to {$max}";
}
}
return empty($this->errors);
}
}

View File

@ -0,0 +1,164 @@
<?php
namespace GuzzleHttp\Command\Guzzle;
use GuzzleHttp\Command\CommandInterface;
use GuzzleHttp\Command\Guzzle\RequestLocation\BodyLocation;
use GuzzleHttp\Command\Guzzle\RequestLocation\FormParamLocation;
use GuzzleHttp\Command\Guzzle\RequestLocation\HeaderLocation;
use GuzzleHttp\Command\Guzzle\RequestLocation\JsonLocation;
use GuzzleHttp\Command\Guzzle\RequestLocation\MultiPartLocation;
use GuzzleHttp\Command\Guzzle\RequestLocation\QueryLocation;
use GuzzleHttp\Command\Guzzle\RequestLocation\RequestLocationInterface;
use GuzzleHttp\Command\Guzzle\RequestLocation\XmlLocation;
use GuzzleHttp\Psr7\Request;
use GuzzleHttp\Psr7\Uri;
use Psr\Http\Message\RequestInterface;
/**
* Serializes requests for a given command.
*/
class Serializer
{
/** @var RequestLocationInterface[] */
private $locations;
/** @var DescriptionInterface */
private $description;
/**
* @param DescriptionInterface $description
* @param RequestLocationInterface[] $requestLocations Extra request locations
*/
public function __construct(
DescriptionInterface $description,
array $requestLocations = []
) {
static $defaultRequestLocations;
if (!$defaultRequestLocations) {
$defaultRequestLocations = [
'body' => new BodyLocation(),
'query' => new QueryLocation(),
'header' => new HeaderLocation(),
'json' => new JsonLocation(),
'xml' => new XmlLocation(),
'formParam' => new FormParamLocation(),
'multipart' => new MultiPartLocation(),
];
}
$this->locations = $requestLocations + $defaultRequestLocations;
$this->description = $description;
}
/**
* @param CommandInterface $command
* @return RequestInterface
*/
public function __invoke(CommandInterface $command)
{
$request = $this->createRequest($command);
return $this->prepareRequest($command, $request);
}
/**
* Prepares a request for sending using location visitors
*
* @param CommandInterface $command
* @param RequestInterface $request Request being created
* @return RequestInterface
* @throws \RuntimeException If a location cannot be handled
*/
protected function prepareRequest(
CommandInterface $command,
RequestInterface $request
) {
$visitedLocations = [];
$operation = $this->description->getOperation($command->getName());
// Visit each actual parameter
foreach ($operation->getParams() as $name => $param) {
/* @var Parameter $param */
$location = $param->getLocation();
// Skip parameters that have not been set or are URI location
if ($location == 'uri' || !$command->hasParam($name)) {
continue;
}
if (!isset($this->locations[$location])) {
throw new \RuntimeException("No location registered for $name");
}
$visitedLocations[$location] = true;
$request = $this->locations[$location]->visit($command, $request, $param);
}
// Ensure that the after() method is invoked for additionalParameters
/** @var Parameter $additional */
if ($additional = $operation->getAdditionalParameters()) {
$visitedLocations[$additional->getLocation()] = true;
}
// Call the after() method for each visited location
foreach (array_keys($visitedLocations) as $location) {
$request = $this->locations[$location]->after($command, $request, $operation);
}
return $request;
}
/**
* Create a request for the command and operation
*
* @param CommandInterface $command
*
* @return RequestInterface
* @throws \RuntimeException
*/
protected function createRequest(CommandInterface $command)
{
$operation = $this->description->getOperation($command->getName());
// If command does not specify a template, assume the client's base URL.
if (null === $operation->getUri()) {
return new Request(
$operation->getHttpMethod(),
$this->description->getBaseUri()
);
}
return $this->createCommandWithUri($operation, $command);
}
/**
* Create a request for an operation with a uri merged onto a base URI
*
* @param \GuzzleHttp\Command\Guzzle\Operation $operation
* @param \GuzzleHttp\Command\CommandInterface $command
*
* @return \GuzzleHttp\Psr7\Request
*/
private function createCommandWithUri(
Operation $operation,
CommandInterface $command
) {
// Get the path values and use the client config settings
$variables = [];
foreach ($operation->getParams() as $name => $arg) {
/* @var Parameter $arg */
if ($arg->getLocation() == 'uri') {
if (isset($command[$name])) {
$variables[$name] = $arg->filter($command[$name]);
if (!is_array($variables[$name])) {
$variables[$name] = (string) $variables[$name];
}
}
}
}
// Expand the URI template.
$uri = \GuzzleHttp\uri_template($operation->getUri(), $variables);
return new Request(
$operation->getHttpMethod(),
Uri::resolve($this->description->getBaseUri(), $uri)
);
}
}

Some files were not shown because too many files have changed in this diff Show More