Jinyun's Notes

没什么天赋,爱好也不多,但愿坚持做些喜欢的事情

0%

使用 Satis 搭建 Composer 私有源

201906221121.jpg

有时候我们不想公开业务开发中封装的一些工具或者开发包,想通过搭建自己专属的 Composer 源来安装这些包,那么就得使用 Satis 这种工具搭建 Composer 私有源,当然,Satis 并不是唯一的方式。

简介

Satis 是一个静态的 Composer 库生成器。它有点像 packagist 的超轻量级、基于静态文件的版本,可以用来托管公司的私有包或自己的包的元数据。它基本上是一个微型容器。可以从 GitHub 获取它,或者通过命令行界面安装。

èµ·æ­¥

准备安装包

本文中以托管到 码云 上的项目为例(相关配置本文暂略)。

创建项目

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
// 切换指定目录
$ cd ~/Codes/laravel

// 生成项目骨架
$ composer create-project composer/satis package.test --stability=dev --keep-vcs
Installing composer/satis (dev-master 735f6d4e0d933e3bd0263f96865a0e50b80f4257)
- Installing composer/satis (dev-master 735f6d4): Cloning 735f6d4e0d from cache
Created project in package.test
Loading composer repositories with package information
Installing dependencies (including require-dev) from lock file
Package operations: 55 installs, 0 updates, 0 removals
- Installing symfony/process (v4.2.7): Loading from cache
- Installing symfony/finder (v4.2.7): Loading from cache
- Installing symfony/polyfill-ctype (v1.11.0): Loading from cache
- Installing symfony/filesystem (v4.2.7): Loading from cache
- Installing symfony/polyfill-mbstring (v1.11.0): Loading from cache
- Installing symfony/contracts (v1.0.2): Loading from cache
- Installing symfony/console (v4.2.7): Loading from cache
- Installing seld/phar-utils (1.0.1): Loading from cache
- Installing seld/jsonlint (1.7.1): Loading from cache
- Installing psr/log (1.1.0): Loading from cache
- Installing justinrainbow/json-schema (5.2.8): Loading from cache
- Installing composer/xdebug-handler (1.3.2): Loading from cache
- Installing composer/spdx-licenses (1.5.1): Loading from cache
- Installing composer/semver (1.5.0): Loading from cache
- Installing composer/ca-bundle (1.1.4): Loading from cache
- Installing composer/composer (1.8.5): Loading from cache
- Installing twig/twig (v2.8.1): Loading from cache
- Installing doctrine/lexer (v1.0.1): Loading from cache
- Installing symfony/stopwatch (v4.2.7): Loading from cache
- Installing symfony/polyfill-php72 (v1.11.0): Loading from cache
- Installing paragonie/random_compat (v9.99.99): Loading from cache
- Installing symfony/polyfill-php70 (v1.11.0): Loading from cache
- Installing symfony/options-resolver (v4.2.7): Loading from cache
- Installing symfony/event-dispatcher (v4.2.7): Loading from cache
- Installing php-cs-fixer/diff (v1.3.0): Loading from cache
- Installing doctrine/annotations (v1.6.1): Loading from cache
- Installing friendsofphp/php-cs-fixer (v2.14.2): Loading from cache
- Installing mikey179/vfsstream (v1.6.6): Loading from cache
- Installing webmozart/assert (1.4.0): Loading from cache
- Installing phpdocumentor/reflection-common (1.0.1): Loading from cache
- Installing phpdocumentor/type-resolver (0.4.0): Loading from cache
- Installing phpdocumentor/reflection-docblock (4.3.0): Loading from cache
- Installing phpunit/php-token-stream (3.0.1): Loading from cache
- Installing sebastian/version (2.0.1): Loading from cache
- Installing sebastian/resource-operations (2.0.1): Loading from cache
- Installing sebastian/recursion-context (3.0.0): Loading from cache
- Installing sebastian/object-reflector (1.1.1): Loading from cache
- Installing sebastian/object-enumerator (3.0.3): Loading from cache
- Installing sebastian/global-state (3.0.0): Loading from cache
- Installing sebastian/exporter (3.1.0): Loading from cache
- Installing sebastian/environment (4.2.0): Loading from cache
- Installing sebastian/diff (3.0.2): Loading from cache
- Installing sebastian/comparator (3.0.2): Loading from cache
- Installing phpunit/php-timer (2.1.1): Loading from cache
- Installing phpunit/php-text-template (1.2.1): Loading from cache
- Installing phpunit/php-file-iterator (2.0.2): Loading from cache
- Installing theseer/tokenizer (1.1.2): Loading from cache
- Installing sebastian/code-unit-reverse-lookup (1.0.1): Loading from cache
- Installing phpunit/php-code-coverage (7.0.3): Loading from cache
- Installing doctrine/instantiator (1.2.0): Loading from cache
- Installing phpspec/prophecy (1.8.0): Loading from cache
- Installing phar-io/version (2.0.1): Loading from cache
- Installing phar-io/manifest (1.0.3): Loading from cache
- Installing myclabs/deep-copy (1.9.1): Loading from cache
- Installing phpunit/phpunit (8.1.3): Loading from cache
symfony/contracts suggests installing psr/cache (When using the Cache contracts)
symfony/contracts suggests installing psr/container (When using the Service contracts)
symfony/contracts suggests installing symfony/cache-contracts-implementation
symfony/contracts suggests installing symfony/service-contracts-implementation
symfony/contracts suggests installing symfony/translation-contracts-implementation
symfony/console suggests installing symfony/lock
paragonie/random_compat suggests installing ext-libsodium (Provides a modern crypto API that can be used to generate random bytes.)
symfony/event-dispatcher suggests installing symfony/dependency-injection
symfony/event-dispatcher suggests installing symfony/http-kernel
friendsofphp/php-cs-fixer suggests installing php-cs-fixer/phpunit-constraint-isidenticalstring (For IsIdenticalString constraint.)
friendsofphp/php-cs-fixer suggests installing php-cs-fixer/phpunit-constraint-xmlmatchesxsd (For XmlMatchesXsd constraint.)
sebastian/global-state suggests installing ext-uopz (*)
phpunit/phpunit suggests installing phpunit/php-invoker (^2.0)
Generating autoload files

私有源项目配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
// 切换到项目目录
$ cd ~/Codes/laravel/package.test

// 创建 satis.json 文件,并填入以下内容
$ touch satis.json
{
"name": "xiaohe/packages",
"description": "Xiaohe PHP Development Package",
"homepage": "http://package.test",
"repositories": [
{
"type": "vcs",
"url": "git@gitee.com:xiaoheinc/laravel-sms.git"
}
],
"repositories-dep": [],
"output-dir": "public",
"require-all": false,
"require": {
"xiaohe/laravel-sms": "dev-master"
},
"require-dependencies": false,
"require-dev-dependencies": false,
"output-html": true,
"config": {
"secure-http": false
}
}

私有源生成

1
2
3
4
5
6
7
8
9
10
$ php -c ./php-cli.ini bin/satis build -vv --skip-errors satis.json public/
Scanning packages
Reading composer.json of xiaohe/laravel-sms (master)
Importing branch master (dev-master)
Selected xiaohe/laravel-sms (dev-master)
wrote packages to public//include/all$979ee73568f8982cf167859ee4a0d1d7e8679915.json
Writing packages.json
Pruning include directories
Deleted public//include/all$ab346115d9b4c32b0da1504f27e7ed1ec29bbf73.json
Writing web view

让私有源项目可访问

配置私有源项目,并重启 Nginx 服务器:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
server {

listen 80;
listen [::]:80;

# For https
# listen 443 ssl;
# listen [::]:443 ssl ipv6only=on;
# ssl_certificate /etc/nginx/ssl/default.crt;
# ssl_certificate_key /etc/nginx/ssl/default.key;

server_name package.test;
root /var/www/package.test/public;
index index.php index.html index.htm;

location ~ \.php$ {
try_files $uri /index.php =404;
fastcgi_pass php-upstream;
fastcgi_index index.php;
fastcgi_buffers 16 16k;
fastcgi_buffer_size 32k;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
# fixes timeouts
fastcgi_read_timeout 600;
include fastcgi_params;
}

location ~ /\.ht {
deny all;
}

error_log /var/log/nginx/package-error.log;
access_log /var/log/nginx/package-access.log;
}

访问私有源项目:

composer-package.jpg

私有源使用

项目中配置

此处以 Laravel 项目为例。

将以下内容添加到项目的 composer.json 文件中:

1
2
3
4
5
6
7
8
9
10
11
{
.
.
.
"repositories": [
{
"type": "composer",
"url": "http://package.test/"
}
]
}

安装测试

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
$ composer require xiaohe/laravel-sms
Warning: Accessing package.test over http which is an insecure protocol.
1/3: https://packagist.laravel-china.org/p/provider-latest$dfdf6692476db7cc6ba617894e2d61d7b41970cc0e5e2aa0d79f14e5c0c0b526.json
2/3: https://packagist.laravel-china.org/p/provider-2018-10$4bfb9a1a71d8062fdf36b1dd4d35635da47e70cf04f2d8978668127e70c43dcf.json
3/3: https://packagist.laravel-china.org/p/provider-2019-04$e201694c08014f9b374db1cbcfc61a971eb80d8965069a87a7d557efb338aaf6.json
Finished: success: 3, skipped: 0, failure: 0, total: 3
Using version dev-master for xiaohe/laravel-sms
./composer.json has been updated
Warning: Accessing package.test over http which is an insecure protocol.
Loading composer repositories with package information
Updating dependencies (including require-dev)
Package operations: 1 install, 0 updates, 0 removals
- Installing xiaohe/laravel-sms (dev-master 9fdc80d): Cloning 9fdc80d63d from cache
Writing lock file
Generating optimized autoload files
> Illuminate\Foundation\ComposerScripts::postAutoloadDump
> @php artisan package:discover --ansi
Discovered Package: barryvdh/laravel-ide-helper
Discovered Package: beyondcode/laravel-dump-server
Discovered Package: dingo/api
Discovered Package: fideloper/proxy
Discovered Package: intervention/image
Discovered Package: jenssegers/mongodb
Discovered Package: laravel/horizon
Discovered Package: laravel/slack-notification-channel
Discovered Package: laravel/tinker
Discovered Package: liyu/dingo-serializer-switch
Discovered Package: mews/captcha
Discovered Package: nesbot/carbon
Discovered Package: nunomaduro/collision
Discovered Package: nunomaduro/larastan
Discovered Package: nunomaduro/phpinsights
Discovered Package: nwidart/laravel-modules
Discovered Package: tymon/jwt-auth
Discovered Package: xiaohe/laravel-sms
Package manifest generated successfully.
ocramius/package-versions: Generating version class...
ocramius/package-versions: ...done generating version class

参考

本笔记是笔者在学习和工作中的一些整理,如对您有用,请鼓励我继续写作