热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

PHP管理依赖(dependency)关系工具Composer安装与使用

Composer是PHP中用来管理依赖(dependency)关系的工具。你可以在自己的项目中声明所依赖的外部工具库(libraries),Composer会帮你安装这些依赖的库文件。
PHP Composer 安装

系统需求:

Composer 需要PHP5.3.2+ 以上的环境来运行。有几个敏感的PHP设置和编译标志也是必需的,但安装程序会发出警告当存在任何不兼容的情况。

比如PHP的扩展的要求是,安装或重新编译php without –disable-phar

为了从源地址安装软件包,而不是简单的压缩文件包,您将需要安装软件包的版本控制工具,比如git、svn或hg等。

Composer 是兼容多平台的,其运行适用于Windows,Linux和OSX。

安装失败的错误消息:

curl -sS https://getcomposer.org/installer | PHP 
#!/usr/bin/env php 
some settings on your machine make Composer unable to work properly. 
Make sure that you fix the issues listed below and run this script again:

The phar extension is missing. 
Install it or recompile php without –disable-phar

Your PHP (5.2.9) is too old, you must upgrade to PHP 5.3.2 or higher.

我的本地默认开发环境是PHP 5.2.9,当我尝试在PHP 5.2.9上安装时,提示版本过低,需要升级到PHP 5.3.2 或者更高的版本。

于是我下载XAMPP 1.7.7 usb lite 版本。

E:\USB\Dropbox\phpstorm\php53>E:\USB\xampp_177\php\php.exe -v 
PHP 5.3.8 (cli) (built: Aug 23 2011 11:50:20) 
Copyright (c) 1997-2011 The PHP Group 
Zend Engine v2.3.0, Copyright (c) 1998-2011 Zend Technologies

XAMPP 1.7.7 下的PHP环境是5.3.8。确认我的PHP环境大于等于PHP5.3.2后。

PHP 5.3 安装 Composer

E:\USB\Dropbox\phpstorm\php53>curl -sS https://getcomposer.org/installer | E:\USB\xampp_177\php\php.exe 
#!/usr/bin/env php 
Some settings on your machine may cause stability issues with Composer. 
If you encounter issues, try to change the following:

The openssl extension is missing, which will reduce the security and stability o 
f Composer. 
If possible you should enable it or recompile php with –with-openssl

Downloading…

Composer successfully installed to: E:\USB\Dropbox\phpstorm\php53\composer.phar 
Use it: php composer.phar

注意:

我关闭了XAMPP 1.7.7 Apache的SSL模块,所以Composer提示我警告,为了安全因素建议我开启SSL模块。不开启也不影响使用。
安装时的参数 | PHP,一定要更改为真实的PHP环境路径,比如 | E:\USB\xampp_177\php\php.exe
使用Composer为PHP路径 + 命令,在我们安装的环境中,使用方法为:E:\USB\xampp_177\php/php.exe composer.phar 命令。
我安装的环境是在Windows下,如果在Linux下,而PHP 5.3 环境也不在默认PHP命令下时,应该采用 | /usr/php/53/usr/bin/php,例如:

curl -sS https://getcomposer.org/installer | /usr/php/53/usr/bin/php

安装完成后,检查安装版本。

E:\USB\Dropbox\phpstorm\php53>E:\USB\xampp_177\php/php.exe composer.phar -V 
Composer version 0209bd31a0ac3aeb2a68fc81e2d03c71072bef33

检查PHP Composer命令:

E:\USB\Dropbox\phpstorm\php53>E:\USB\xampp_177\php/php.exe composer.phar 
  ______ 
 / ____/___ ____ ___ ____ ____ ________ _____ 
/ /  / __ \/ __ `__ \/ __ \/ __ \/ ___/ _ \/ ___/ 
/ /___/ /_/ / / / / / / /_/ / /_/ (__ ) __/ / 
\____/\____/_/ /_/ /_/ .___/\____/____/\___/_/ 
          /_/ 
Composer version 0209bd31a0ac3aeb2a68fc81e2d03c71072bef33

Usage: 
 [options] command [arguments]

Options: 
 –help      -h Display this help message. 
 –quiet     -q Do not output any message. 
 –verbose    -v Increase verbosity of messages. 
 –version    -V Display this application version. 
 –ansi       Force ANSI output. 
 –no-ansi      Disable ANSI output. 
 –no-interaction -n Do not ask any interactive question. 
 –profile      Display timing and memory usage information 
 –working-dir  -d If specified, use the given directory as working directory 
.

Available commands: 
 about      Short information about Composer 
 archive     Create an archive of this composer package 
 config      Set config options 
 create-project  Create new project from a package into given directory. 
 depends     Shows which packages depend on the given package 
 diagnose     Diagnoses the system to identify common errors. 
 dump-autoload  Dumps the autoloader 
 dumpautoload   Dumps the autoloader 
 help       Displays help for a command 
 init       Creates a basic composer.json file in current directory. 
 install     Installs the project dependencies from the composer.lock file 
if present, or falls back on the composer.json. 
 list       Lists commands 
 require     Adds required packages to your composer.json and installs the 
m 
 run-script    Run the scripts defined in composer.json. 
 search      Search for packages 
 self-update   Updates composer.phar to the latest version. 
 selfupdate    Updates composer.phar to the latest version. 
 show       Show information about packages 
 status      Show a list of locally modified packages 
 update      Updates your dependencies to the latest version according to 
composer.json, and updates the composer.lock file. 
 validate     Validates a composer.json

这样我们在PHP5.3环境下的Composer已经安装完成。

类似的方法我们也可以安装PHP 5.4的环境上。

PHP 5.4 安装 Composer

下载XAMPP 1.8.1 usb lite 版本,将得到PHP5.4.7环境。

E:\USB\Dropbox\phpstorm\php53>E:\USB\xampp_181\php\php.exe -v 
PHP 5.4.7 (cli) (built: Sep 12 2012 23:48:31) 
Copyright (c) 1997-2012 The PHP Group 
Zend Engine v2.4.0, Copyright (c) 1998-2012 Zend Technologies

确定版本高于PHP 5.3.2 后,开始安装:

E:\USB\Dropbox\phpstorm\php54>curl -sS https://getcomposer.org/installer | E:\USB\xampp_181\php\php.exe 
#!/usr/bin/env php 
Some settings on your machine may cause stability issues with Composer. 
If you encounter issues, try to change the following:

The openssl extension is missing, which will reduce the security and stability o 
f Composer. 
If possible you should enable it or recompile php with –with-openssl

Downloading…

Composer successfully installed to: E:\USB\Dropbox\phpstorm\php54\composer.phar 
Use it: php composer.phar

安装完成后,检查安装版本。

E:\USB\Dropbox\phpstorm\php54>E:\USB\xampp_181\php/php.exe composer.phar -V 
Composer version 0209bd31a0ac3aeb2a68fc81e2d03c71072bef33

这样,PHP 5.4 环境下的Composer也安装成功了。

PHP Composer 使用

这里我只简单演示使用PHP Composer安装Symfony框架。

首先大概讲解一下命令的标准格式:

php composer.phar create-project symfony/framework-standard-edition path/ 2.2.1

第一个参数PHP为本地PHP开发环境,指向PHP运行命令。
第二个参数,为composer.phar方式下载。
第三个参数,为动作命令,例如create-project,目的是为了创建一个新的项目从一个软件包到指定目录。
第四个参数,为本地安装的目录。
第五个参数,为安装软件包的版本。

根据上述我们在PHP5.4环境下安装的Composer,我们的安装命令为:

E:\USB\Dropbox\phpstorm\php54>E:\USB\xampp_181\php/php.exe composer.phar create-project symfony/framework-standard-edition E:\USB\xampp_181\htdocs\Symfony 2.2.1

安装结果:

Installing symfony/framework-standard-edition (v2.2.1) 
 – Installing symfony/framework-standard-edition (v2.2.1) 
  Downloading: 100%
Created project in E:\USB\xampp_181\htdocs\Symfony 
Loading composer repositories with package information 
Installing dependencies from lock file 
 – Installing doctrine/lexer (v1.0) 
  Downloading: 100%
 – Installing doctrine/annotations (v1.1) 
  Downloading: 100%
 – Installing doctrine/cache (v1.0) 
  Downloading: 100%
 – Installing doctrine/collections (v1.1) 
  Downloading: 100%
 – Installing twig/twig (v1.12.2) 
  Downloading: 100%
 – Installing psr/log (1.0.0) 
  Downloading: 100%
 – Installing doctrine/inflector (v1.0) 
  Downloading: 100%
 – Installing doctrine/common (2.4.0-RC1) 
  Downloading: 100%
 – Installing symfony/symfony (v2.2.1) 
  Downloading: 100%
 – Installing jdorn/sql-formatter (v1.2.0) 
  Downloading: 100%
 – Installing doctrine/dbal (2.3.3) 
  Downloading: 100%
 – Installing doctrine/doctrine-bundle (v1.2.0-beta1) 
  Downloading: 100%
 – Installing doctrine/orm (2.3.3) 
  Downloading: 100%
 – Installing jms/cg (1.0.0) 
  Downloading: 100%
 – Installing phpoption/phpoption (1.2.0) 
  Downloading: 100%
 – Installing jms/parser-lib (1.0.0) 
  Downloading: 100%
 – Installing jms/metadata (1.3.0) 
  Downloading: 100%
 – Installing jms/aop-bundle (1.0.0) 
  Downloading: 100%
 – Installing jms/di-extra-bundle (1.3.0) 
  Downloading: 100%
 – Installing jms/security-extra-bundle (1.4.0) 
  Downloading: 100%
 – Installing sensio/distribution-bundle (v2.2.1) 
  Downloading: 100%
 – Installing sensio/framework-extra-bundle (v2.2.1) 
  Downloading: 100%
 – Installing sensio/generator-bundle (v2.2.1) 
  Downloading: 100%
 – Installing kriswallsmith/assetic (v1.1.0-alpha4) 
  Downloading: 100%
 – Installing symfony/assetic-bundle (v2.1.2) 
  Downloading: 100%
 – Installing monolog/monolog (1.4.1) 
  Downloading: 100%
 – Installing symfony/monolog-bundle (v2.2.0) 
  Downloading: 100%
 – Installing swiftmailer/swiftmailer (v4.3.0) 
  Downloading: 100%
 – Installing symfony/swiftmailer-bundle (v2.2.0) 
  Downloading: 100%
 – Installing twig/extensions (v1.0.0) 
  Downloading: 100%

kriswallsmith/assetic suggests installing leafo/lessphp (Assetic provides the integration with the lessphp LESS compiler) 
kriswallsmith/assetic suggests installing leafo/scssphp (Assetic provides the integration with the scssphp SCSS compiler) 
kriswallsmith/assetic suggests installing leafo/scssphp-compass (Assetic provides the integration with the SCSS compass plugin) 
kriswallsmith/assetic suggests installing ptachoire/cssembed (Assetic provides the integration with phpcssembed to embed data uris) 
monolog/monolog suggests installing doctrine/couchdb (Allow sending log messages to a CouchDB server) 
monolog/monolog suggests installing ext-amqp (Allow sending log messages to an AMQP server (1.0+ required)) 
monolog/monolog suggests installing ext-mongo (Allow sending log messages to a MongoDB server) 
monolog/monolog suggests installing mlehner/gelf-php (Allow sending log messages to a GrayLog2 server) 
monolog/monolog suggests installing raven/raven (Allow sending log messages to a Sentry server) 
Generating autoload files 
Clearing the cache for the dev environment with debug true 
Installing assets using the hard copy option 
Installing assets for Symfony\Bundle\FrameworkBundle into web/bundles/framework 
Installing assets for Acme\DemoBundle into web/bundles/acmedemo 
Installing assets for Sensio\Bundle\DistributionBundle into web/bundles/sensiodistribution

从命令行结果,我们看到了整个Symfony框架的安装过程与结果。

PHP Composer 定义依赖关系

Composer在使用时会自动寻找composer.json这个文件。 composer.json将用来定义整个项目的依赖包。例如:

{ 
  "require": { 
    "monolog/monolog": "1.2.*" 
  } 
}

依赖包简单的说明,我们的项目需要一些从1.2开始的任何版本的monolog/monolog包。

Composer是PHP中的一个依赖关系管理工具。通过Composer我们可以在发布软件时节省大量的文件数目和控制依赖包的版本。

PHP Composer 项目

Composer 项目官方:http://getcomposer.org

Composer Github项目:https://github.com/composer/composer

PHP Composer软件包列表:https://packagist.org/

推荐阅读
  • 本文介绍了在开发Android新闻App时,搭建本地服务器的步骤。通过使用XAMPP软件,可以一键式搭建起开发环境,包括Apache、MySQL、PHP、PERL。在本地服务器上新建数据库和表,并设置相应的属性。最后,给出了创建new表的SQL语句。这个教程适合初学者参考。 ... [详细]
  • Java验证码——kaptcha的使用配置及样式
    本文介绍了如何使用kaptcha库来实现Java验证码的配置和样式设置,包括pom.xml的依赖配置和web.xml中servlet的配置。 ... [详细]
  • 如何实现织梦DedeCms全站伪静态
    本文介绍了如何通过修改织梦DedeCms源代码来实现全站伪静态,以提高管理和SEO效果。全站伪静态可以避免重复URL的问题,同时通过使用mod_rewrite伪静态模块和.htaccess正则表达式,可以更好地适应搜索引擎的需求。文章还提到了一些相关的技术和工具,如Ubuntu、qt编程、tomcat端口、爬虫、php request根目录等。 ... [详细]
  • 本文讨论了Alink回归预测的不完善问题,指出目前主要针对Python做案例,对其他语言支持不足。同时介绍了pom.xml文件的基本结构和使用方法,以及Maven的相关知识。最后,对Alink回归预测的未来发展提出了期待。 ... [详细]
  • 在说Hibernate映射前,我们先来了解下对象关系映射ORM。ORM的实现思想就是将关系数据库中表的数据映射成对象,以对象的形式展现。这样开发人员就可以把对数据库的操作转化为对 ... [详细]
  • 安卓select模态框样式改变_微软Office风格的多端(Web、安卓、iOS)组件库——Fabric UI...
    介绍FabricUI是微软开源的一套Office风格的多端组件库,共有三套针对性的组件,分别适用于web、android以及iOS,Fab ... [详细]
  • 使用在线工具jsonschema2pojo根据json生成java对象
    本文介绍了使用在线工具jsonschema2pojo根据json生成java对象的方法。通过该工具,用户只需将json字符串复制到输入框中,即可自动将其转换成java对象。该工具还能解析列表式的json数据,并将嵌套在内层的对象也解析出来。本文以请求github的api为例,展示了使用该工具的步骤和效果。 ... [详细]
  • 本文介绍了一些Java开发项目管理工具及其配置教程,包括团队协同工具worktil,版本管理工具GitLab,自动化构建工具Jenkins,项目管理工具Maven和Maven私服Nexus,以及Mybatis的安装和代码自动生成工具。提供了相关链接供读者参考。 ... [详细]
  • 在Android中解析Gson解析json数据是很方便快捷的,可以直接将json数据解析成java对象或者集合。使用Gson解析json成对象时,默认将json里对应字段的值解析到java对象里对应字段的属性里面。然而,当我们自己定义的java对象里的属性名与json里的字段名不一样时,我们可以使用@SerializedName注解来将对象里的属性跟json里字段对应值匹配起来。本文介绍了使用@SerializedName注解解析json数据的方法,并给出了具体的使用示例。 ... [详细]
  • 本文介绍了如何使用JSONObiect和Gson相关方法实现json数据与kotlin对象的相互转换。首先解释了JSON的概念和数据格式,然后详细介绍了相关API,包括JSONObject和Gson的使用方法。接着讲解了如何将json格式的字符串转换为kotlin对象或List,以及如何将kotlin对象转换为json字符串。最后提到了使用Map封装json对象的特殊情况。文章还对JSON和XML进行了比较,指出了JSON的优势和缺点。 ... [详细]
  • Android日历提醒软件开源项目分享及使用教程
    本文介绍了一款名为Android日历提醒软件的开源项目,作者分享了该项目的代码和使用教程,并提供了GitHub项目地址。文章详细介绍了该软件的主界面风格、日程信息的分类查看功能,以及添加日程提醒和查看详情的界面。同时,作者还提醒了读者在使用过程中可能遇到的Android6.0权限问题,并提供了解决方法。 ... [详细]
  • express工程中的json调用方法
    本文介绍了在express工程中如何调用json数据,包括建立app.js文件、创建数据接口以及获取全部数据和typeid为1的数据的方法。 ... [详细]
  • Java如何导入和导出Excel文件的方法和步骤详解
    本文详细介绍了在SpringBoot中使用Java导入和导出Excel文件的方法和步骤,包括添加操作Excel的依赖、自定义注解等。文章还提供了示例代码,并将代码上传至GitHub供访问。 ... [详细]
  • PHP组合工具以及开发所需的工具
    本文介绍了PHP开发中常用的组合工具和开发所需的工具。对于数据分析软件,包括Excel、hihidata、SPSS、SAS、MARLAB、Eview以及各种BI与报表工具等。同时还介绍了PHP开发所需的PHP MySQL Apache集成环境,包括推荐的AppServ等版本。 ... [详细]
  • 本文总结了初学者在使用dubbo设计架构过程中遇到的问题,并提供了相应的解决方法。问题包括传输字节流限制、分布式事务、序列化、多点部署、zk端口冲突、服务失败请求3次机制以及启动时检查。通过解决这些问题,初学者能够更好地理解和应用dubbo设计架构。 ... [详细]
author-avatar
可爱嘟嘟豬5
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有