由于composer在国内下载速度非常慢,大家都习惯使用中国镜像,如果正在使用的镜像没有更新或者出现故障你可以使用Composer Registry Manager帮助你轻松地切换到另外一个镜像。
默认带了一些镜像,当然你也可以添加新的镜像。
使用composer安装,执行下面命令
$ composer global require slince/composer-registry-manager
$ composer repo:ls
--- ---------- ------------------------------------------------
composer https://packagist.org
* aliyun https://mirrors.aliyun.com/composer
tencent https://mirrors.cloud.tencent.com/composer
huawei https://mirrors.huaweicloud.com/repository/php
cnpkg https://php.cnpkg.org
sjtug https://packagist.mirrors.sjtug.sjtu.edu.cn
--- ---------- ------------------------------------------------
标“*”表示当前正在使用的源;
$ composer repo:use
Please select your favorite repository (defaults to composer) [composer]:
[0] composer
[1] aliyun
[2] tencent
[3] huawei
[4] cnpkg
[5] sjtug
>
你也可以直接追加镜像名称来跳过选择
$ composer repo:use aliyun
添加选项 --current/-c
为当前项目切换源,默认是修改全局的源。
执行下面命令查看
$ composer repo
_____ _____ ___ ___
/ ___| | _ \ / |/ |
| | | |_| | / /| /| |
| | | _ / / / |__/ | |
| |___ | | \ \ / / | |
\_____| |_| \_\ /_/ |_|
Composer Repository Manager version 2.0.0
Usage:
command [options] [arguments]
Options:
-h, --help Display this help message
-q, --quiet Do not output any message
-V, --version Display this application version
--ansi Force ANSI output
--no-ansi Disable ANSI output
-n, --no-interaction Do not ask any interactive question
-v|vv|vvv, --verbose Increase the verbosity of messages: 1 for normal output, 2 for more verbose output and 3 for debug
Available commands for the "repo" namespace:
repo:add Creates a repository
repo:ls List all available repositories
repo:remove Remove a repository
repo:use Change current repository
使用阿里巴巴提供的 Composer
全量镜像 https://mirrors.aliyun.com/composer/
a). 配置只在当前项目生效composer config repo.packagist composer https://mirrors.aliyun.com/composer/
composer config --unset repos.packagist
b). 配置全局生效composer config -g repo.packagist composer https://mirrors.aliyun.com/composer/
composer config -g --unset repos.packagist
阿里云
https://mirrors.aliyun.com/composer/
华为云
https://mirrors.huaweicloud.com/repository/php/
腾讯云
https://mirrors.cloud.tencent.com/composer/
cnpkg
https://php.cnpkg.org
phpcomposer
https://packagist.phpcomposer.com
交通大学
https://packagist.mirrors.sjtug.sjtu.edu.cn
]]>riverslei/payment
是一款集成了阿里支付、微信支付的组件。它对php的版本要求很低(大于等于5.6),不过其文档不是特别完善,但是作为开发者去看下examples 下的 demo 够用了。当前sdk仅接入了支付宝支付、微信支付、招商支付(支付、退款)。中国现在电子支付的公司巨多,无法一一接入,欢迎各位发扬自己动手、丰衣足食的光荣传统,提交PR给我,代码贡献指南 看这里
通过composer,这是推荐的方式,可以使用composer.json 声明依赖,或者直接运行下面的命令。
composer require "riverslei/payment:*"
放入composer.json文件中
"require": {
"riverslei/payment": "*"
}
然后运行
composer update
或者项目中直接引入
当前项目依赖有 guzzle
第三方包,如果手动引入,请同时确保项目中存在 guzzle
这种方式主要是为了目前还没有使用 composer 进行包管理的项目,如果你是一个有追求的人,我建议尽快采用 composer。
下载地址: https://github.com/helei112g/payment/archive/master.zip
下载后,解压可以得到源码,放入自己的项目合适目录中。在需要用到的地方。通过 require 引入项目项目。这部分可参考项目中的 example 中的例子。require_once __DIR__ . './payment/autoload.php';
// 这里请提供 payment
目录下 autoload.php
的路径
使用起来还是很方便的 ,只需要关心如何正确组装支付数据,以及如何正确处理返回后的数据即可。
微信 APP 支付
$orderNo = time() . rand(1000, 9999);
// 订单信息
$payData = [
'body' => 'test body',
'subject' => 'test subject',
'order_no' => $orderNo,
'timeout_express' => time() + 600,// 表示必须 600s 内付款
'amount' => '3.01',// 微信沙箱模式,需要金额固定为3.01
'return_param' => '123',
'client_ip' => isset($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : '127.0.0.1',// 客户地址
];
try {
$ret = Charge::run(Config::WX_CHANNEL_APP, $wxConfig, $payData);
} catch (PayException $e) {
echo $e->errorMessage();
exit;
}
更多信息就看 官方文档或者官方 DEMO 吧
[gitwidget type='github' url='helei112g/payment']
[gitwidget type='gitee' url='helei112g/payment']
]]>这里我们使用Composer来生成php版的elasticsearch框架:
比方说,你的项目中需要一个php版的elasticsearch
框架。为了将它添加到你的项目中(下载),你所需要做的就是创建一个 composer.json
文件,其中描述了项目的依赖关系。注意文件要放在你执行composer
命令的目录中
{
"require":{
"elasticsearch/elasticsearch":"~2.0"
}
}
如果你项目中已经使用过 composer 那么您可以使用composer require elasticsearch/elasticsearch
来安装具体安装就不在多了具体看文档 https://packagist.org/packages/elasticsearch/elasticsearch
<?php
/**
* Elasticsearch检索引擎模型
*/
namespace app\index\model;
use Elasticsearch\ClientBuilder;
class Elasticsearch
{
//配置
private $config = [
'hosts' => ['http://127.0.0.1:9200']
];
private $api;
public function __construct()
{
$this->api = ClientBuilder::create()->setHosts($this->config['hosts'])->build();
}
/*************************************************************
/**
* 索引一个文档
* 说明:索引没有被创建时会自动创建索引
*/
public function addOne()
{
$params = [];
$params['index'] = 'ascwh';
$params['type'] = 'cat';
$params['id'] = '20180407001'; # 不指定就是es自动分配
$params['body'] = array('name' => 'ascwh编程');
return $this->api->index($params);
}
/**
* 索引多个文档
* 说明:索引没有被创建时会自动创建索引
*/
public function addAll()
{
$params = [];
for($i = 1; $i < 21; $i++) {
$params['body'][] = [
'index' => [
'_index' => 'test_index'.$i,
'_type' => 'cat_test',
'_id' => $i,
]
];
$params['body'][] = [
'name' => 'ascwh编程'.$i,
'content' => '内容'.$i
];
}
return $this->api->bulk($params);
}
/**
* 获取一个文档
*/
public function getOne()
{
$params = [];
$params['index'] = '
';
$params['type'] = 'cat';
$params['id'] = '20180407001';
return $this->api->get($params);
}
/**
* 搜索文档
*/
public function search()
{
$params = [];
$params['index'] = 'ascwh';
$params['type'] = 'cat';
$params['body']['query']['match']['name'] = 'ascwh编程';
return $this->api->search($params);
}
/**
* 删除文档
* 说明:文档删除后,不会删除对应索引。
*/
public function delete()
{
$params = [];
$params['index'] = 'ascwh';
$params['type'] = 'cat';
$params['id'] = '20180407001';
return $this->api->delete($params);
}
/*************************************************************
/**
* 创建索引
*/
public function createIndex()
{
$params = [];
$params['index'] = 'ascwh';
return $this->api->indices()->create($params);
}
/**
* 删除索引:匹配单个 | 匹配多个
* 说明: 索引删除后,索引下的所有文档也会被删除
*/
public function deleteIndex()
{
$params = [];
$params['index'] = 'test_index'; # 删除test_index单个索引
#$params['index'] = 'test_index*'; # 删除以test_index开始的所有索引
return $this->api->indices()->delete($params);
}
/*************************************************************
/**
* 设置索引配置
*/
public function setIndexConfig()
{
$params = [];
$params['index'] = 'ascwh';
$params['body']['index']['number_of_replicas'] = 0;
$params['body']['index']['refresh_interval'] = -1;
return $this->api->indices()->putSettings($params);
}
/**
* 获取索引配置
*/
public function getIndexConfig()
{
# 单个获取条件写法
$params['index'] = 'ascwh';
# 多个获取条件写法
//$params['index'] = ['ascwh', 'test_index'];
return $this->api->indices()->getSettings($params);
}
/**
* 设置索引映射
*/
public function setIndexMapping()
{
# 设置索引和类型
$params['index'] = 'ascwh';
$params['type'] = 'cat';
# 向现有索引添加新类型
$myTypeMapping = array(
'_source' => array(
'enabled' => true
),
'properties' => array(
'first_name' => array(
'type' => 'string',
'analyzer' => 'standard'
),
'age' => array(
'type' => 'integer'
)
)
);
$params['body']['cat'] = $myTypeMapping;
# 更新索引映射
$this->api->indices()->putMapping($params);
}
/**
* 获取索引映射
*/
public function getIndexMapping()
{
# 获取所有索引和类型的映射
$ret = $this->api->indices()->getMapping();
/*
# 获取索引为:ascwh的映射
$params['index'] = 'ascwh';
$ret = $this->api->indices()->getMapping($params);
# 获取类型为:cat的映射
$params['type'] = 'cat';
$ret = $this->api->indices()->getMapping($params);
# 获取(索引为:ascwh和 类型为:cat)的映射
$params['index'] = 'ascwh';
$params['type'] = 'cat'
$ret = $this->api->indices()->getMapping($params);
# 获取索引为:ascwh和test_index的映射
$params['index'] = ['ascwh', 'test_index'];
$ret = $this->api->indices()->getMapping($params);
*/
return $ret;
}
}
]]>Could not fetch https://api.github.com/repos/jquery/jquery-dist, please create a GitHub OAuth token to go over the API rate limit
Head to https://github.com/settings/tokens/new?scopes=repo&description=Composer+on+huadeMacBook-Pro.local+2017-03-08+1653
to retrieve a token. It will be stored in "/Users/hua/.composer/auth.json" for future use by Composer.
Token (hidden):
解决办法就是: 进入 https://github.com/settings/tokens
点击 「Generate new token」
新建一个 Token,选择默认新建就行,
然后就会得到一个 Token,然后输入这个值直接回车就 OK 了。
]]>只想更新某个特定的库,不想更新它的所有依赖,很简单:composer update foo/bar
此外,这个技巧还可以用来解决“警告信息问题”。你一定见过这样的警告信息:
Warning: The lock file is not up to date with the latest changes in composer.json, you may be getting outdated dependencies, run update to update them.
擦,哪里出问题了?别惊慌!如果你编辑了composer.json
,你应该会看到这样的信息。比如,如果你增加或更新了细节信息,比如库的描述、作者、更多参数,甚至仅仅增加了一个空格,都会改变文件的md5sum
。然后Composer
就会警告你哈希值和composer.lock中记载的不同。
那么我们该怎么办呢?update命令可以更新lock文件,但是如果仅仅增加了一些描述,应该是不打算更新任何库。这种情况下,只需
update nothing:
$ composer update nothing
Loading composer repositories with package information
Updating dependencies
Nothing to install or update
Writing lock file
Generating autoload files
这样一来,Composer不会更新库,但是会更新composer.lock。注意nothing并不是update命令的关键字。只是没有nothing 这个包导致的结果。如果你输入foobar,结果也一样。
如果你用的Composer版本足够新,那么你可以直接使用--lock选项:composer update --lock
你可能会觉得每安装一个库都需要修改composer.json太麻烦,那么你可以直接使用require命令。composer require "foo/bar:1.0.0"
这个方法也可以用来快速地新开一个项目。init命令有--require选项,可以自动编写composer.json:(注意我们使用-n,这样就不用回答问题)
$ composer init --require=foo/bar:1.0.0 -n
$ cat composer.json
{
"require": {
"foo/bar": "1.0.0"
}
}
初始化的时候,你试过create-project
命令么?composer create-project doctrine/orm path 2.2.0
这会自动克隆仓库,并检出指定的版本。克隆库的时候用这个命令很方便,不需要搜寻原始的URI了。
最近一年以来的Composer会自动存档你下载的dist包。默认设置下,dist包用于加了tag的版本,例如"symfony/symfony": "v2.1.4"
,或者是通配符或版本区间,"2.1.*"或">=2.2,<2.3-dev"
(如果你使用stable作为你的minimum-stability)。
dist
包也可以用于诸如dev-master
之类的分支,Github允许你下载某个git引用的压缩包。为了强制使用压缩包,而不是克隆源代码,你可以使用install和update的--prefer-dist
选项。
下面是一个例子(我使用了--profile选项来显示执行时间):
$ composer init --require="twig/twig:1.*" -n --profile
Memory usage: 3.94MB (peak: 4.08MB), time: 0s
$ composer install --profile
Loading composer repositories with package information
Installing dependencies
- Installing twig/twig (v1.12.2)
Downloading: 100%
Writing lock file
Generating autoload files
Memory usage: 10.13MB (peak: 12.65MB), time: 4.71s
$ rm -rf vendor
$ composer install --profile
Loading composer repositories with package information
Installing dependencies from lock file
- Installing twig/twig (v1.12.2)
Loading from cache
Generating autoload files
Memory usage: 4.96MB (peak: 5.57MB), time: 0.45s
这里,twig/twig:1.12.2的压缩包被保存在~/.composer/cache/files/twig/twig/1.12.2.0-v1.12.2.zip
。重新安装包时直接使用。
当你需要修改库的时候,克隆源代码就比下载包方便了。你可以使用--prefer-source来强制选择克隆源代码。composer update symfony/yaml --prefer-source
接下来你可以修改文件:
composer status -v
You have changes in the following dependencies:
/path/to/app/vendor/symfony/yaml/Symfony/Component/Yaml:
M Dumper.php
当你试图更新一个修改过的库的时候,Composer会提醒你,询问是否放弃修改:
$ composer update
Loading composer repositories with package information
Updating dependencies
- Updating symfony/symfony v2.2.0 (v2.2.0- => v2.2.0)
The package has modified files:
M Dumper.php
Discard changes [y,n,v,s,?]?
最后提醒一下,在部署代码到生产环境的时候,别忘了优化一下自动加载:
composer dump-autoload --optimize
安装包的时候可以同样使用--optimize-autoloader。
不加这一选项,你可能会发现20%到25%
的性能损失。