ehime/hello-world
Composer 安装命令:
composer require ehime/hello-world
包简介
Sample Composer project
关键字:
README 文档
README
Hi everybody! Today I'll write about how you can contribute with PHP community creating packages (or updating your's) using Composer and Packagist.
Using Composer
Composer is a package manager for PHP. You can use packages the community developed and you can contribute with your packages too. Here I'll show how to create a project/package, install Composer inside it and send to Packagist, where others developers can use it inside their projects.
Creating the Package
You can create a new project or update one to use Composer. I'll create a hello world class. It's a simple class but you can create complex projects and share them with the others developers. I'll use "hello-world" as project's name. Composer work in "vendor/package" name format. Here we can set as "vendor" name my name: "ehime" and as package name "hello-world", the name of the project.
Files Structure
You can put all files inside the main dir, but I strongly recommend to create another dir, as "src" to be easier to understand and maintain your code organized. The project structure will start with the follow: * hello-world (root dir) * src * HelloWorld * SayHello.php Our SayHello.php file will have:
<?php
namespace HelloWorld;
class SayHello
{
public static function world()
{
return 'Hello World, Composer!';
}
}
Starting Composer
As our project is ready we can "install" Composer inside it. This is only create a "composer.json" file inside root dir, but Composer can do that for you. Inside your project root: composer init You'll have the follow Composer response:
localhost:composer-helloworld $ composer init
Welcome to the Composer config generator
This command will guide you through creating your composer.json config.
Package name (<vendor>/<name>) [ehime/hello-world]:
You can accept the default or customize it like "yourname/hello" or what you want. Complete all Composer questions like:
Package name (<vendor>/<name>) [ehime/hello-world]: ehime/hello-world
Description []: My first Composer project
Author [ehime <dodomeki@gmail.com>]: Your Name <your@name.com>
Minimum Stability []: dev
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": "ehime/hello-world",
"description": "My first Composer project",
"authors": [
{
"name": "Your Name",
"email": "your@name.com"
}
],
"minimum-stability": "dev",
"require": {
}
}
Do you confirm generation [yes]? yes
Now you have "composer.json" file saved in your root dir. It's almost ready but we must do some changes:
{
"name": "ehime/hello-world",
"description": "My first Composer project",
"authors": [
{
"name": "Your Name",
"email": "your@name.com"
}
],
"require": {
"php": ">=5.3.0"
},
"autoload": {
"psr-0": {
"HelloWorld": "src/"
}
}
}
What we did here is add information about PHP 5.3 as minimum requirements (require section) and tell Composer to "autoload" (using PSR-0) all files with "HelloWorld" namespace that are inside "src" dir.
Testing Package
Sure we want to do a simple test to verify if our class is working well. You can create a new project and "paste" your classes inside it or test inside your own project, which is better and easier. We're creating a Composer project so we must have Composer files installed inside our projects. So, install it running "composer install" inside your root dir:
composer install
As you have only "php >=5.3.0" inside "composer.json", Composer will install only it's own files. With Composer installed create a directory "tests" inside your root dir. Create the "TestCase.php" file inside it with the follow content:
<?php
require_once __DIR__ . '/../vendor/autoload.php'; // Autoload files using Composer autoload
use HelloWorld\SayHello;
echo SayHello::world();
Go to the terminal (or create a PHP web server inside "tests" dir) and type:
php -f tests/HelloWorld/Tests/TestCase.php
You'll get "Hello World, Composer!". It's working now.
Sending to Packagist.org
Now your project is working and you want to send it to Packagist. The easy way is push your project to Github using Git. Go to Github and create a new public repo called "composer-helloworld", start the Git project inside your root dir and push it:
git init
git add .
git commit -m "First commit"
git remote add origin git@github.com:username/hello-world.git
git push origin master
Now you have your project inside a Github repo and you're ready to send it to Packagist. Go to Packagist web site, create your account, login and Submit a Package. Packagist'll ask you for Repository URL (Git/Svn/Hg). Paste there git@github.com:username/hello-world.git and click "Check!". Packagist will check your project and return the project name. If it's correct accept it.
Packagist Details
Every time you do a new commit to Github you must update the Packagist. Go to your account, your package and click "Force Update!". Packagist will go to Github and update the sources. You can turn on "auto update" going to your Github repo, clicking "Settings", after "Service Hooks" and click the "Packagist" service. There update with your information, like:
- User: your Packagist username, like ehime
- Token: your API token, that you can find inside your Packagist settings link
- Domain: packagist.org Ok! Auto update finished and your package is available to other developers.
From here on out you can now embed this project into another workflow by using the following command.
composer require ehime/hello-world
Our first Composer package is finished, but you can do much more using it. Thanks!
NOTE
If at any time you change your repository url you will sever the Packagist connection and will no longer be able to run autoupdate!
ehime/hello-world 适用场景与选型建议
ehime/hello-world 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 175.62k 次下载、GitHub Stars 达 2, 最近一次更新时间为 2015 年 07 月 31 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「test」 「sample」 「helloworld」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 ehime/hello-world 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 ehime/hello-world 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 ehime/hello-world 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Testing Suite For Lumen like Laravel does.
Specification-oriented BDD helpers for PHPUnit
KissTest is command-line free, fast, simple and beautiful unit test framework.
PWWEB Artomator
Hello world module for Quaver
raxon/test see https://raxon.org for detailed usage
统计信息
- 总下载量: 175.62k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 3
- 点击次数: 16
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2015-07-31