创建第一个Composer/Packagist包

作为一名PHPer都希望为PHP社区做出自己的贡献,Composer 的出现终于统一了PHP包管理的混乱现状,开启了PHP依赖管理的新时代。现在我们一起学习通过Composer和Packagist向PHP社区贡献代码包。如果你对composer还不了解,首先看一下 https://pkg.phpcomposer.com/

开始写package

首先我们创建一个项目,叫helloComposer, 目录是hello 。Composer 通常以 “vendor/package “存放在你的项目框架中的,package 一般以提供者喜欢命名,这里我们命名为 “penghai/hello”,目录结构如下:

$cd hello
$ tree
.
└── src
    └── helloComposer.php

1 directory, 1 file

编辑 helloComposer.php

<?php
namespace yuanmaketang\Hello;

class helloComposer
{
    public function sayHello()
    {
        return 'hello composer';
    }
}

1,在项目下 创建 composer.json 文件

在项目目录下执行:

$cd hello
$ composer init


  Welcome to the Composer config generator  



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

Package name (<vendor>/<name>) [wangpenghai/hello]: ll
 The package name ll is invalid, it should be lowercase and have a vendor name, a forward slash, and a package name, matching: [a-z0-9_.-]+/[a-z0-9_.-]+ 
Package name (<vendor>/<name>) [wangpenghai/hello]: vendor/penghai
Description []: first composer         
Author [, n to skip]: penghai
 Invalid author string.  Must be in the format: John Smith <john@example.com> 
Author [, n to skip]: penghai <wph45@163.com>
Minimum Stability []: 
Package Type (e.g. library, project, metapackage, composer-plugin) []: project
License []: MIT

Define your dependencies.

Would you like to define your dependencies (require) interactively [yes]? yes
Search for a package: 
Would you like to define your dev dependencies (require-dev) interactively [yes]? yes
Search for a package: 

{
    "name": "vendor/penghai",
    "description": "first composer",
    "type": "project",
    "license": "MIT",
    "authors": [
        {
            "name": "penghai",
            "email": "wph45@163.com"
        }
    ],
    "require": {}
}

Do you confirm generation [yes]? yes

跟着引导,一步步就创建好了, 在hello 目录下会创建一个composer.json 文件

hello $ tree
.
├── composer.json
└── src
    └── helloComposer.php

1 directory, 2 files

composer.json内容如下:

{
    "name": "vendor/penghai", #注意这块要手动改
    "description": "first composer",
    "type": "project",
    "license": "MIT",
    "authors": [
        {
            "name": "penghai",
            "email": "wph45@163.com"
        }
    ],
    "require": {}
}

2,添加自动加载

{
    "name": "yuanmaketang/firstcomposer",#修改过的
    "description": "first composer",
    "type": "project",
    "license": "MIT",
    "authors": [
        {
            "name": "penghai",
            "email": "wph45@163.com"
        }
    ],
    "require": {},
    "autoload": {
        "psr-4": {
            "yuanmaketang\\Hello\\": "src/"
        }
    }
}

3,测试

$cd hello #项目根目录
$ composer install
Loading composer repositories with package information
Updating dependencies (including require-dev)
Nothing to install or update
Generating autoload files

目录结构

$ tree
.
├── composer.json
├── src
│   └── helloComposer.php
└── vendor
    ├── autoload.php
    └── composer
        ├── ClassLoader.php
        ├── LICENSE
        ├── autoload_classmap.php
        ├── autoload_namespaces.php
        ├── autoload_psr4.php
        ├── autoload_real.php
        ├── autoload_static.php
        └── installed.json

3 directories, 11 files

创建一个测试文件,test.php

<?php
require_once __DIR__ . '/vendor/autoload.php';

use yuanmaketang\Hello\helloComposer;

$helloComposer = new helloComposer();
echo $helloComposer->sayHello();

执行test.php ($ php test.php )输出 hello composer 。 到这里就成功了。

接下发布我们的项目

上面我们在本地完成了编写和测试。那么想让更多人使用我们的包就需要把我们新建的包发送到Packagist.org。首先我们可以先将项目发布到Github。我们先去Github 创建一个公有仓库命名“hello”, 我们在项目根目录使用Git命令来完成发布。

我们先在根目录里创建.gitignore文件,把vendor目录和composer.lock文件排除git在外。

$ cat .gitignore
vendor/*
composer.lock

git 提交

echo "# hello" >> README.md #项目描述
git init
git add README.md
git commit -m "first commit"
git remote add origin https://github.com/pf070811/hello.git
git push -u origin master

记录一下:

git config --global user.name "你的github的用户名"
git config --global user.email "登录邮箱"

github 的使用参考:

https://guides.github.com/activities/hello-world/

提交到Packagist

1. 打开 https://packagist.org/ 登录, 没有账户注册账户

2. 找到导航上的 Summit 按钮

3. 在 Repository URL (Git/Svn/Hg) 输入:https://github.com/pf070811/hello

check的时候报了一个错:

 The vendor is already taken by someone else. You may ask them to add your package and give you maintainership access. If they add you as a maintainer on any package in that vendor namespace, you will then be able to add new packages in that namespace. The packages already in that vendor namespace can be found at vendor

大概意思是composer.json 里的 name 已存在了, 需要换一个。

4,设置Packagist 自动拉取

GitHub Hook
Enabling the Packagist service hook ensures that your package will always be updated instantly when you push to GitHub.

To do so you can:

Make sure you login via GitHub (if you already have an account not connected to GitHub, you can connect it on your profile). If you are logged in already, log out first then login via GitHub again to make sure you grant us the required permissions.
Make sure the Packagist application has access to all the GitHub organizations you need to publish packages from.
Check your package list to see if any has a warning about not being automatically synced.
If you still need to setup sync on some packages, try triggering a manual account sync to have Packagist try to set up hooks on your account again. Note that archived repositories can not be setup as they are readonly in GitHub’s API.

大概意思是不用到github设置啦,只要用github账户联合登录 packagist.org 就行啦。

参考:
https://packagist.org/about#how-to-update-packages

使用包

$composer require yuanmaketang/firstcomposer

注意:新发布的包, 中国镜像不会那么快同步,要改成官方镜像,在更新

composer config --list

看看是不是中国镜像,如果是用下面命令修改:

composer config -g repo.packagist composer https://packagist.org

改回中国镜像

composer config -g repo.packagist composer https://packagist.phpcomposer.com

参考:https://pkg.phpcomposer.com/

参考:https://docs.phpcomposer.com/02-libraries.html

@by ph.w


ph.w
ph.w 1年前

已被 0 人挖起

回应
登录 后发表评论
  • 消灭零回应