1、Github新建一个项目
代码结构如下
composer.json配置如下
{
"name": "fationyyk/self-test-package",
"type" : "composer-plugin", // 必须指定类型为 composer-plugin
"description": "testcomposer",
"authors": [
{
"name": "fationyyk",
"email": "fation@126.com"
}
],
"require": {
"composer-plugin-api": "*" // 依赖包
},
"extra": {
"class": "fationyyk\\selfpackage\\Plugin" // 指定加载插件的类
},
"autoload": {
"psr-4": {
"fationyyk\\selfpackage\\": "src/"
}
}
}
src 代码说明
Plugin.php
<?php
namespace fationyyk\selfpackage;
use Composer\Composer;
use Composer\IO\IOInterface;
use Composer\Plugin\PluginInterface;
class Plugin implements PluginInterface
{
public function activate(Composer $composer, IOInterface $io)
{
$installer = new PluginInstaller($io, $composer);
$composer->getInstallationManager()->addInstaller($installer);
}
}
PluginInstaller.php
<?php
namespace fationyyk\selfpackage;
use Composer\Package\PackageInterface;
use Composer\Installer\LibraryInstaller;
class PluginInstaller extends LibraryInstaller
{
/**
* {@inheritDoc}
*/
public function getInstallPath(PackageInterface $package)
{
// $package->getPrettyName() 包的名字
$prefix = substr($package->getPrettyName(), 0, 23);
if ('fationyyk/push' !== $prefix) {
throw new \InvalidArgumentException(
'Unable to install template, phpdocumentor templates '
.'should always start their package name with '
.'"phpdocumentor/template-"'
);
}
// 返回指定路径
return './data/templates/'.substr($package->getPrettyName(), 23);
}
/**
* {@inheritDoc}
*/
public function supports($packageType)
{
return 'phpdocumentor-template' === $packageType;
}
}
2、git上新建另一个项目
composer.json
{
"name": "fationyyk/push",
"type":"phpdocumentor-template", // 这里的type必须和PluginInstaller.php里的supports方法支持的类型保持一致
"description": "Third party message push",
"authors": [
{
"name": "Yanlong Ma"
}
],
"license": "MIT",
"require": {
"php": ">=5.4"
},
"autoload": {
"psr-4": {
"YanlongMa\\Push\\": "src/"
}
}
}
src代码随意
<?php
namespace YanlongMa\Push;
class Jpush {
}
3、发布包,分别发布到https://packagist.org (怎么发包自行百度)
4、使用
新建composer.json文件
{
"name": "fationyyk/test",
"type": "fationyyk",
"description": "test",
"require": {
"fationyyk/self-test-package": "dev-master",
"fationyyk/push": "dev-master"
}
}
执行composer install
安装后效果如下
fationyyk/push包安装到自定义的data目录下