欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

创建自己的composer包

程序员文章站 2024-01-07 14:15:58
...

创建一个composer/packagist包

 

在github上面创建一个新库,这里取名car, 初始化readme.md, 然后克隆到本地:

git clone https://github.com/maxwelldu/car.git
cd car

 

 

这里的car文件夹就是包的root目录了,只需要记住composer.json在包的哪个目录下面,一般那就是包的root目录了。

现在还没有composer.json文件,可以通过命令: composer init, 中括号里面的为默认值,如果直接回车将使用里面的值

 

➜  car git:(master) composer init


  Welcome to the Composer config generator



This command will guide you through creating your composer.json config.

Package name (<vendor>/<name>) [maxwelldu/car]:
Description []: composer study
Author [maxwelldu <dcj3sjt@163.com>]:
Minimum Stability []:
Package Type []:
License []:

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
<?php

<?php
{
<?php
    "name": "maxwelldu/car",
namespace Ford\Fusion;
    "description": "composer study",
    "authors": [
        {
            "name": "maxwelldu",
            "email": "dcj3sjt@163.com"
        }
    ],
    "require": {}
}

Do you confirm generation [yes]? yes

 

将composer.json修改成如下:

 

{
    "name": "maxwelldu/car",
    "description": "composer study",
    "license": "MIT",
    "authors": [
        {
            "name": "maxwelldu",
            "email": "dcj3sjt@163.com"
        }
    ],
    "minimum-stability": "dev",
    "require": {
        "php": ">=5.3.0"
    },
    "autoload": {
        "psr-4": {
            "Ford\\Escape\\": "src/Ford/Escape",
            "Ford\\Fusion\\": "src/Ford/Fusion",
            "Ford\\Focus\\": "src/Ford/Focus",
            "Ford\\Fiesta\\": "src/Ford/Fiesta"
        }
    }
}

 

 

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

 

|____.gitignore

|____composer.json

|____composer.lock

|____README.md

|____src

| |____Ford

| | |____Escape

| | | |____Escape2013.php

| | |____Fiesta

| | | |____Fiesta2013.php

| | |____Focus

| | | |____Focus2013.php

| | |____Fusion

| | | |____Fusion2013.php

 

Escape2013.php

 

<?php
  
namespace Ford\Escape;
  
class Escape2013
{
    public static function info()
    {
        echo "This is Ford Escape2013!<br />";
    }
}

 

Fiesta2013.php

 

<?php
  
namespace Ford\Fiesta;
  
class Fiesta2013
{
    public static function info()
    {
        echo "This is Ford Fiesta2013!<br />";
    }
}

 

 

Focus2013.php

 

<?php
  
namespace Ford\Focus;
  
class Focus2013
{
    public static function info()
    {
        echo "This is Ford Focus2013!<br />";
    }
}

 

Fusion2013.php

 

<?php
  
namespace Ford\Fusion;
  
class Fusion2013
{
    public static function info()
    {
        echo "This is Ford Fusion2013!<br />";
    }
}

 

以上都梳理完毕后,需要安装composer来测试我们的包是否可以正常工作,安装它很简单在包的root目录下install即可:

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'),
);

 

然后我们新建一个测试文件show.php,用以下内容填充:

<?php

require 'vendor/autoload.php';

use Ford\Escape as Escape;
use Ford\Fiesta as Fiesta;
use Ford\Focus as Focus;
use Ford\Fusion as Fusion;

echo Escape\Escape2013::info();
echo Fiesta\Fiesta2013::info();
echo Focus\Focus2013::info();
echo Fusion\Fusion2013::info();

 

然后在当前目录启动一个服务器

php -S localhost:8080

 

 

打开浏览器输入: http://localhost:8080/show.php

浏览器上面依次输出了:

 

This is Ford Escape2013!

This is Ford Fiesta2013!

This is Ford Focus2013!

This is Ford Fusion2013!

 

然后我们将show.php删除,然后将代码推送到github上面

 

打开 https://packagist.org/

用github帐号登录

 

然后提交一个包, 将github的地址 git@github.com:maxwelldu/car.git 填上

然后进行提交即可

 

相关标签: composer

上一篇:

下一篇: