Composer 不是一個包管理器。是的,它涉及 "packages" 和 "libraries",但它在每一個項目的基礎上進行管理,在你項目的某個目錄中(例如 vendor)進行安裝。默認狀況下它不會在全局安裝任何東西。所以,這僅僅是一個依賴管理。php
Composer 受到了 node's npm 和 ruby's bundler 的強烈啓發。而當時 PHP 下並無相似的工具。html
系統學習composer的使用請移步 composer中文學習node
要點:理解composer如何實現自動加載第三方組件; 理解
psr-0
和psr-4
的規範; 理解基於psr-0
,psr-4
,classmap
,files如何實現自動加載。 理解Composer和Packagistgit
composer
請參考composer中文學習 mkdir try-make-package cd try-make-package
這個try-make-package
文件夾就是你的包的根目錄了,你只須要記住composer.json在包的哪一個目錄下面,通常那就是包的根目錄了。
如今咱們尚未composer.json文件,下面咱們來初始化github
☁ composer init Welcome to the Composer config generator This command will guide you through creating your composer.json config. Package name (<vendor>/<name>) [lingan/try-make-package]: Description []: try make a package Author [saboran <saboran@163.com>, 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]? n Would you like to define your dev dependencies (require-dev) interactively [yes]? n { "name": "lingan/try-make-package", "description": "try make a package", "type": "library", "license": "MIT", "authors": [ { "name": "saboran", "email": "saboran@163.com" } ], "minimum-stability": "dev", "require": {} } Do you confirm generation [yes]? yes
通過一番上面的掙扎在當前目錄就生成了composer.json
文件,下面咱們對這個文件進行一些修改npm
composer.json
增長自動加載規範(命名空間和目錄映射關係)和環境要求{ "name": "lingan/try-make-package", "description": "try make a package", "type": "library", "license": "MIT", "authors": [ { "name": "saboran", "email": "saboran@163.com" } ], "minimum-stability": "dev", "require": { "php": ">=7.0" }, "autoload": { "psr-4": { "Flower\\Rose\\": "src/Flower/Rose", "Flower\\Lily\\": "src/Flower/Lily" } } }
根據上面的命名空間和目錄的映射關係,建立對應目錄和文件,以下圖json
對應文件內容api
<?php /** * Created by PhpStorm. * User: Lingan * Date: 2017/11/6 * Time: 21:39 */ namespace Flower\Rose; class Rose { public function desc() { echo "this is rose flower"; } }
<?php /** * Created by PhpStorm. * User: Lingan * Date: 2017/11/6 * Time: 21:38 */ namespace Flower\Lily; class Lily { public function desc() { echo "this is lily flower"; } }
以上代碼都梳理完畢後,須要
composer install
來測試咱們的包是否能夠正常工做此時會在vendor/composer/autoload_psr4.php
中生成命名空間和目錄的映射關係,被包在一個數組中:數組
<?php // autoload_psr4.php @generated by Composer $vendorDir = dirname(dirname(__FILE__)); $baseDir = dirname($vendorDir); return array( 'Flower\\Rose\\' => array($baseDir . '/src/Flower/Rose'), 'Flower\\Lily\\' => array($baseDir . '/src/Flower/Lily'), );
.gitignore
爲git忽略一些文件,增長readme.md爲項目增長描述.idea vendor/ composer.lock
如今本身的Github主頁上建立一個新的倉庫而後將項目提交到對應倉庫,參考以下ruby
git init git add -A git commit -am "init && dev package" git remote add origin git@github.com:linganmin/try-make-package.git git push -u origin master
將本身的項目地址粘貼
點擊check,而後點擊submit,至此,本身的composer包就提交成功了,
mkdir test-my-package cd test-my-package
composer require lingan/try-make-package dev-master ./composer.json has been updated Loading composer repositories with package information Updating dependencies (including require-dev) Package operations: 1 install, 0 updates, 0 removals - Installing lingan/try-make-package (dev-master d7b9f94): Cloning d7b9f941b0 from cache Writing lock file Generating autoload files
autoload_psr4.php
<?php // autoload_psr4.php @generated by Composer $vendorDir = dirname(dirname(__FILE__)); $baseDir = dirname($vendorDir); return array( 'Flower\\Rose\\' => array($vendorDir . '/lingan/try-make-package/src/Flower/Rose'), 'Flower\\Lily\\' => array($vendorDir . '/lingan/try-make-package/src/Flower/Lily'), );
index.php
,開始測試本身寫的包<?php /** * Created by PhpStorm. * User: Lingan * Date: 2017/11/7 * Time: 11:04 */ require_once './vendor/autoload.php'; // 加載自動加載文件 use Flower\Rose\Rose; use Flower\Lily\Lily; $rose = new Rose(); $lily = new Lily(); echo $rose->desc(); echo "\n"; echo $lily->desc();
php index.php this is rose flower this is lily flower%
* 至此說明咱們本身開發的擴展包能夠正常使用
在沒有設置自動更新時,Packagist上包詳情頁會有這樣一個提示
This package is not auto-updated. Please set up the GitHub Service Hook for Packagist so that it gets updated whenever you push!(這個軟件包不是自動更新的。請爲Packagist 設置GitHub服務鉤,以便在您推送時更新!)
簡化步驟以下:
設置
,點擊集成和服務
,點擊添加Packagist
服務並配置你的API令牌,以及你的Packagist用戶名和帳號對應域名active
,點擊add service
做爲一枚渣渣,發現本身的不足,嘗試接觸更多新技術和工具,以上文字如有錯誤歡迎指正
Happy Coding