如何创建一个自己的【Composer/Packagist】包-演道网

本网站用的阿里云ECS,推荐大家用。自己搞个学习研究也不错

首先让我们踏着欢快的脚步去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 (/) [root/composer-car]: victorruan/composer-car
Description []: This a demo for build composer packagist .
Author [阮佳佳 , 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]? no
Would you like to define your dev dependencies (require-dev) interactively [yes]? no

{
    "name": "victorruan/composer-car",
    "description": "This a demo for build composer packagist .",
    "type": "library",
    "license": "MIT",
    "authors": [
        {
            "name": "阮佳佳",
            "email": "250069802@qq.com"
        }
    ],
    "minimum-stability": "dev",
    "require": {}
}

Do you confirm generation [yes]? yes
Would you like the vendor directory added to your .gitignore [yes]? yes

虽然经过以上的一番挣扎生成了composer.json文件,不过我们还得往里面加点东西。使用你熟悉的编辑器打开composer.json文件修改至如下:

{
    "name": "victorruan/composer-car",
    "description": "This a demo for build composer packagist .",
    "type": "library",
    "license": "MIT",
    "authors": [
        {
            "name": "阮佳佳",
            "email": "250069802@qq.com"
        }
    ],
    "minimum-stability": "dev",
    "require": {
        "php": ">=7.0.0"
    },
    "autoload": {
        "psr-4": {
            "Ford\\Escape\\": "src/Ford/Escape",
            "Ford\\Fusion\\": "src/Ford/Fusion",
            "Ford\\Focus\\": "src/Ford/Focus",
            "Ford\\Fiesta\\": "src/Ford/Fiesta"
        }
    }
}

细心的小伙伴可能已经认出了福特的商标(Ford),这说明我们都是同道中人,你一定也很喜欢汽车,对吧对吧? :-)
我们登陆一下福特的网站看看都有哪些热销车型,嗯嗯分别有ESCAPE、FUSION、FOCUS、FIESTA,
中文名称分别是翼虎、蒙迪欧、福克斯、嘉年华,嘉年华ST我的梦想啊~~~
好了好了,那位看官放下你手里的板砖,我承认一说到汽车就会滔滔不绝,下面我们把水分挤出去继续讲解。

根据上面的命名空间和目录的映射关系,包的结构现在应该是下面这个样子:

composer-car
- src
- - Ford
- - - Escape
- - - - Escape2016.php
- - - Fiesta
- - - - Fiesta2016.php
- - - Focus
- - - - Focus2016.php
- - - Fusion
- - - - Fusion2016.php
- .gitignore
- composer.json
- README.md
<?php

namespace Ford\Escape;

class Escape2016
{
    public static function info()
    {
        echo "This is Ford Escape2016!
"; } }
<?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中生成命名空间和目录的映射关系,被包在一个数组中:

 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 当然前提是你已经注册并登陆了。
样图
点击check,之后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 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 了

 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

info();

执行 php demo.php

$ php demo.php
This is Ford Fusion2016 !
%

到这里为止,所有工作都已经大功告成了!!

转载自演道,想查看更及时的互联网产品技术热点文章请点击http://go2live.cn

未经允许不得转载:演道网 » 如何创建一个自己的【Composer/Packagist】包-演道网

赞 (0)
分享到:更多 ()
已有 0 条评论 新浪微博