发布时间:2018-09-21 17:06:03编辑:文明阅读(1023)
首先让我们踏着欢快的脚步去Github创建一个新库,这里取名 composer-car,又欢快的将它克隆到本地:
$ git clone git@github.com:victorruan/composer-car.git
$ cd composer-car
这个composer-car文件夹就是你的包的根目录了,你只需要记住composer.json在包的哪个目录下面,一般那就是包的根目录了。现在我们还没有composer.json文件,你可以根据composer文档生成并编辑它,当然composer贴心的为我们准备了命令行,look:
$ composer init
Welcome to the Composer config generator
This command will guide you through creating your composer.json config.
Package name (
<?php
namespace Ford\Fiesta;
class Fiesta2016
{
public function info()
{
echo "This is Ford Fiesta2016!
";
}
}
<?php
namespace Ford\Fiesta;
class Fiesta2016
{
public function info()
{
echo "This is Ford Fiesta2016!
";
}
}
<?php
namespace Ford\Focus;
class Focus2016
{
public function info()
{
echo "This is Ford Focus2016!
";
}
}
<?php
namespace Ford\Fusion;
class Fusion2016
{
public function info()
{
echo 'This is Ford Fusion2016 !
';
}
}
以上代码都梳理完毕后,需要安装composer来测试我们的包是否可以正常工作,安装它很简单,在包的根目录下install即可:闪过几行神秘的提示之后即安装完毕,此时会在vendor/composer/autoload_psr4.php中生成命名空间和目录的映射关系,被包在一个数组中:
<?php
// autoload_psr4.php @generated by Composer
$vendorDir = dirname(dirname(__FILE__));
$baseDir = dirname($vendorDir);
return array(
'Ford\\Fusion\\' => array($baseDir . '/src/Ford/Fusion'),
'Ford\\Focus\\' => array($baseDir . '/src/Ford/Focus'),
'Ford\\Fiesta\\' => array($baseDir . '/src/Ford/Fiesta'),
'Ford\\Escape\\' => array($baseDir . '/src/Ford/Escape'),
);
如果发布成packagist包然后进行安装的话,到时候这里就不是$baseDir了而是$vendorDir。 好,接下来让我们去发布我们的Packagist! 编辑.gitignore文件如下:
/vendor/
composer.lock
提交代码到github
$ git add .;git commit -am"in order to build my packagist";git push;
访问 packagist submit 当然前提是你已经注册并登陆了。
eck,之后submit. 到这里,你的composer包就提交成功了,如图
最后我们需要使用刚刚上传的composer包在目录中新建文件夹use-composer-car
$ cd ../ ; mkdir use-composer-car ; cd use-composer-car; composer init;
新建文件 composer.json
{
"minimum-stability": "dev",
"require": {},
"require-dev":{
"victorruan/composer-car":"dev-master"
}
}
终端执行 $ composer install 或者 $composer require victorruan/composer-car
$ composer install
Loading composer repositories with package information
Updating dependencies (including require-dev)
- Installing victorruan/composer-car (dev-master c303814)
Cloning c30381476eeb58fefb87db18f32c43e1e14810fc from cache
Writing lock file
Generating autoload files
查看 use-composer-car/vendor/composer/autoload_psr4.php ,发现$baseDir 已经被更改为 $vendorDir 了
<?php
// autoload_psr4.php @generated by Composer
$vendorDir = dirname(dirname(__FILE__));
$baseDir = dirname($vendorDir);
return array(
'Ford\\Fusion\\' => array($vendorDir . '/victorruan/composer-car/src/Ford/Fusion'),
'Ford\\Focus\\' => array($vendorDir . '/victorruan/composer-car/src/Ford/Focus'),
'Ford\\Fiesta\\' => array($vendorDir . '/victorruan/composer-car/src/Ford/Fiesta'),
'Ford\\Escape\\' => array($vendorDir . '/victorruan/composer-car/src/Ford/Escape'),
);
新建 use-composer-car/demo.php
<?php
require './vendor/autoload.php';
use Ford\Fusion\Fusion2016;
$car = new Fusion2016();
$car->info();
执行 php demo.php
$ php demo.php
This is Ford Fusion2016 !
%
到这里为止,所有工作都已经大功告成了!!
文章转自:https://blog.csdn.net/m0_38088941/article/details/73442426
7
4
4
2
2