composer 开发自定义 vendor扩展包
一、在gitee 或者github 上新建一个项目仓库 zzs-email
二、把gitee上的zzs-email 拉到本地
三、因为是基于自动加载机制,接下来就是composer配置了,首先切换到zzs-email目录,然后命令行运行composer init 这个是在当前目录配置composer的意思
$ composer init Package name (<vendor>/<name>) [lenovo/zzs-email]:zzs/email Description []:zzs send email Author [zzs <3004788270@qq.com>, n to skip]: Minimum Stability []: dev Package Type (e.g. library, project, metapackage, composer-plugin) []: library License []: MIT Define your dependencies. Would you like to define your dependencies (require) interactively [yes]? Search for a package: Would you like to define your dev dependencies (require-dev) interactively [yes]? Search for a package: { "name": "test/auto", "description": "test auto", "type": "library", "license": "MIT", "authors": [ { "name": "zzs", "email": "3004788270@qq.com" } ], "minimum-stability": "dev", "require": {} } Do you confirm generation [yes]?
四、composer 安装
$ composer install Loading composer repositories with package information Warning from https://mirrors.aliyun.com/composer: You are using an outdated version of Composer. Composer 2 is now available and you should upgrade. See https://getcomposer.org/2 Updating dependencies (including require-dev) Nothing to install or update Generating autoload files
安装后的文件目录
composer init 的结果解释:
说明地址:https://docs.phpcomposer.com/04-schema.html
https://docs.phpcomposer.com/04-schema.html#authors
https://docs.phpcomposer.com/02-libraries.html
命令 解释 案例
name 项目名称 pack/test
description 项目描述(不要中文) this is pack test
type 安装类型 默认为 library (4种类型:library,project,metapackage,composer-plugin)
license 许可协议 MIT
authors 作者 { “name”: “json”,“email”: “json_yin@fionacos.com” }
minimum-stability 版本 dev、alpha、beta、RC、stable
五、添加src目录(存放业务文件目录),创建业务文件
Lenovo@DESKTOP-QQM50FI MINGW64 /c/web/zzs-email $ mkdir src Lenovo@DESKTOP-QQM50FI MINGW64 /c/web/zzs-email $ ls composer.json src/ vendor/
src目录下创建Service.php (创建业务执行代码)
<?php /** * Created by PhpStorm. * User: Lenovo * Date: 12/4/2021 * Time: 下午2:47 */ #命名空间 namespace Zzs\Email; class Service { public function __construct() { } public function hello() { return 'hello world!!'; } }
目录结构如下
六、修改composer.json 添加autoload
{ "name": "zzs/email", "description": "send email", "type": "library", "license": "MIT", "authors": [ { "name": "zzs", "email": "3004788270@qq.com" } ], "minimum-stability": "dev", "require": {}, "autoload":{ "psr-4":{ "Zzs\\Email\\":"src/" } } }
修改完composer.json,需要执行composer dump-autoload
$ composer dump-autoload Generating autoload files Generated autoload files
七、在本项目里先 创建test.php测试
<?php /** * Created by PhpStorm. * User: Lenovo * Date: 12/4/2021 * Time: 下午2:51 */ require_once __DIR__ . '/vendor/autoload.php'; use Zzs\Email\Service; $obj = new Service(); $msg = $obj->hello(); var_dump($msg);
php-cli 命令方式执行测试:
C:\web\zzs-email>php test.php string(13) "hello world!!"
八、如果没什么问题,把代码推送到git远程仓库里,gitignore 设置忽略 vendor
vendor文件可以不提交到远程仓库,获取代码没有vendor可以执行composer dump-autoload
九、其他项目如何使用composer 引用我们刚开发的zzs-email包,这边使用laravel框架的项目作为测试
编辑laravel根目录的composer.json文件,添加require zzs-email项目包
"require": {
"php": "^7.3|^8.0",
"fideloper/proxy": "^4.4",
"fruitcake/laravel-cors": "^2.0",
"guzzlehttp/guzzle": "^7.0.1",
"laravel/framework": "^8.12",
"laravel/tinker": "^2.5",
"zzs/email": "dev-develop"
},
指定自定义包的引入地址,添加中国镜像地址
"repositories": { "0": { "type": "vcs", "url": "https://gitee.com/zzs1986/zzs-email.git" }, "packagist": { "type": "composer", "url": "https://mirrors.aliyun.com/composer/" } }
composer 安装指定的zzs-email包
C:\web\example-app>composer require zzs/email
在新项目里新建command 测试引入的zzs-email 能否正常使用
C:\web\example-app>php artisan make:command TestZzs Console command created successfully.
在Command/TestZzs.php 文件里 引入测试
<?php namespace App\Console\Commands; use Illuminate\Console\Command; use Zzs\Email\Service; class TestZzs extends Command { /** * The name and signature of the console command. * * @var string */ protected $signature = 'TestZzs'; /** * The console command description. * * @var string */ protected $description = 'Command description'; /** * Create a new command instance. * * @return void */ public function __construct() { parent::__construct(); } /** * Execute the console command. * * @return int */ public function handle() { $obj = new Service(); echo $obj->hello(); } }
使用命令执行 command
C:\web\example-app>php artisan TestZzs hello world!!
改进策略:
如果希望任何人都可以使用,可以发布到 packagist
好了,现在你可以将包发布。但每次都指定版本仓库这太笨重了。你一定不想让所有的使用者都这样做。
你应该注意到我们没有为 monolog/monolog 指定版本仓库。这是为什么呢?答案就是 Packagist。
Packagist 是 Composer 的主要仓库,并且它默认就是启用的。所有发布到 Packagist 的内容都默认可以被 Composer 获取。由于 Monolog 在 Packagist,所以我们可以不指定仓库地址来依赖它。
如果我们想和全世界分享 hello-world ,我们也可以将它发布到 Packagist。要想实现这个很容易。
访问 Packagist 并且点击 "Submit" 按钮。如果你没有账户它会提示注册,然后提交你的版本仓库地址,Packagist 会开始爬取它。当一切完成的时候,所有人就都可以用你的包了!
https://packagist.org/packages/zzs/email
项目里可以像其他包一样,不用配置,直接运行命令
C:\web\zzs-app>composer require zzs/email Warning from https://mirrors.aliyun.com/composer: You are using an outdated version of Composer. Composer 2 is now available and you should upgrade. See https://getcomposer.org/2 Using version dev-develop for zzs/email ./composer.json has been updated Loading composer repositories with package information Warning from https://mirrors.aliyun.com/composer: You are using an outdated version of Composer. Composer 2 is now available and you should upgrade. See https://getcomposer.org/2 Updating dependencies (including require-dev) Package operations: 1 install, 0 updates, 0 removals - Installing zzs/email (dev-develop fb3f02d): Cloning fb3f02d6e3 from cache Writing lock file Generating optimized autoload files > Illuminate\Foundation\ComposerScripts::postAutoloadDump > @php artisan package:discover --ansi Discovered Package: facade/ignition Discovered Package: fideloper/proxy Discovered Package: fruitcake/laravel-cors Discovered Package: laravel/sail Discovered Package: laravel/tinker Discovered Package: nesbot/carbon Discovered Package: nunomaduro/collision Package manifest generated successfully. C:\web\zzs-app>
本次实验,参考 https://blog.csdn.net/yinjinshui/article/details/106827071