Composer是PHP的包管理器,我们可以很方便地使用别人共享的包,当然我们也可以创建自己的包以分享给其他人使用。本文就是教你如何创建自己的第一个composer包。
首先创建如下目录结构及文件,talking_robot
为项目的根目录:
0 talking_robot $ tree
.
└── src
└── TalkingRobot
└── Talk.php
2 directories, 1 file
Talk.php
文件<?php
namespace TalkingRobot;
class Talk
{
public static function sayHello()
{
return 'Hello Composer';
}
}
现在要在项目的根目录里创建一个composer.json
的文件,我们可以手动创建,也可以在根目录里通过composer init
命令来根据提示创建:
0 talking_robot $ composer init
Welcome to the Composer config generator
This command will guide you through creating your composer.json config.
Package name (<vendor>/<name>) [cl/talking_robot]: greatcl/talking_robot
Description []: I am a talking robot and an example of composer package.
Author [caolei <caolei@qiyi.com>, n to skip]:
Minimum Stability []: dev
Package Type (e.g. library, project, metapackage, composer-plugin) []:
License []: MIT
Define your dependencies.
Would you like to define your dependencies (require) interactively [yes]?
Search for a package:
Would you like to define your dev dependencies (require-dev) interactively [yes]?
Search for a package:
{
"name": "greatcl/talking_robot",
"description": "I am a talking robot and an example of composer package.",
"license": "MIT",
"authors": [
{
"name": "caolei",
"email": "caolei@qiyi.com"
}
],
"minimum-stability": "dev",
"require": {}
}
Do you confirm generation [yes]?
创建完成后,根目录下就生成了composer.json
文件,目录结构如下:
0 talking_robot $ tree
.
├── composer.json
└── src
└── TalkingRobot
└── Talk.php
2 directories, 2 files
然后我们可以手动编辑生成的composer.json
文件,添加php的版本要求和自动加载(使用PSR-0),使用TalkingRobot
命名空间,加载src
目录下的所有文件,如下:
{
"name": "greatcl/talking_robot",
"description": "I am a talking robot and an example of composer package.",
"license": "MIT",
"authors": [
{
"name": "caolei",
"email": "caolei@qiyi.com"
}
],
"minimum-stability": "dev",
"require": {
"php": ">=5.3.0"
},
"autoload": {
"psr-0": {
"TalkingRobot": "src/"
}
}
}
在根目录中使用composer install
安装composer文件。
0 talking_robot $ composer install
Loading composer repositories with package information
Updating dependencies (including require-dev)
Nothing to install or update
Writing lock file
Generating autoload files
0 talking_robot $ tree -L 2
.
├── composer.json
├── composer.lock
├── src
│ └── TalkingRobot
│ └── Talk.php
└── vendor
├── autoload.php
└── composer
4 directories, 12 files
在根目录中创建tests
文件夹,在tests
文件夹里创建test.php
内容如下:
<?php
// Autoload files using Composer autoload
require_once __DIR__ . '/../vendor/autoload.php';
use TalkingRobot\Talk;
echo Talk::sayHello();
当前的目录结构为:
0 talking_robot $ tree -L 2
.
├── composer.json
├── composer.lock
├── src
│ └── TalkingRobot
├── tests
│ └── test.php
└── vendor
├── autoload.php
└── composer
5 directories, 4 files
在根目录中运行测试文件:
0 talking_robot $ php tests/test.php
Hello Composer
返回字符串Hello Composer
,恭喜你,测试通过!
最简单的方法是使用Github。
在Github上创建一个仓库talking_robot
,然后将代码推到Github仓库里。
我们先在根目录里创建.gitignore文件,把vendor目录和composer.lock文件排除git在外。
0 talking_robot (master) $ cat .gitignore
vendor/*
composer.lock
推送代码
git init
git add .
git commit -m 'First commit'
git remote add origin git@github.com:username/talking_robot.git
git push origin master
我们就可以在其他项目引用talking_robot这个包了,方法如下:
在需要引用的项目的composer.json
文件的require段加入:
{
"require": {
"greatcl/talking_robot":"dev-master"
}
}
如果项目初次使用composer执行composer install
进行安装,否则使用composer update
进行更新。
0 use-test-packagist $ composer install
Loading composer repositories with package information
Updating dependencies (including require-dev)
- Installing greatcl/talking_robot (dev-master 103b7cc)
Cloning 103b7cc721d9509d3b041487fe1d0e1c46bc8cce from cache
Writing lock file
Generating autoload files
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。