Composer帮你轻松管理PHP包
在PHP包管理上面,PHP发展的很缓慢,导致的结果就是很少发现程序员会使用像PEAR这样的工具。相反,大多数开发人员会选择他们自己喜欢的框架来处理代码,比如DB交互、ORIMS、Oauth、Amazon S3整合等。 缺点就是在转换框架的时候(或者根本不需要返回使用框架
在PHP包管理上面,PHP发展的很缓慢,导致的结果就是很少发现程序员会使用像PEAR这样的工具。相反,大多数开发人员会选择他们自己喜欢的框架来处理代码,比如DB交互、ORIM’S、Oauth、Amazon S3整合等。
缺点就是在转换框架的时候(或者根本不需要返回使用框架)就感觉像在做噩梦,因为涉及到使用新工具,你必须重新学习里面的一切东西,而这并不简单。OK,Composer来帮助你解决这些问题。
介绍
Composer通过把自己定位成“所有项目的粘合计”来着手解决问题。这也就意味着包可以被写,开发和以某种格式进行共享,其他开发人员可以轻松插入到应用程序中。
这篇文章将向大家讲解如何安装和使用Composer包。在文章最后,你就可以把代码块插入到任何一个框架中进行体验,你可以使用CodeIgniter,FuelPHP,Laravel,Symfony2, Lithium,Yii,Zend等。
安装
Composer包含两大逻辑部分:一个是用来存储包,另一个是命令行应用程序,帮助你发现、下载、更新和分享代码。
<ol> <li><span><span>$ cd/path/to/my/project </span></span></li> <li> <span>$ curl -s http:</span><span>//getcomposer.org/installer| php</span><span> </span> </li> </ol>
在项目列表中,会有一个composer.phar文件,里面包含了所有逻辑代码行工具。你可以通过运行下面代码来确定是否安装成功。
<ol><li><span><span>$ php composer.phar </span></span></li></ol>
这个命令执行后会显示所有可用的命令。
我个人比较建议大家使用这个命令:
<ol><li><span><span>$ sudo mv composer.phar /usr/bin/composer </span></span></li></ol>
把这个文件移到bin目录下,它允许你简化命令。
<ol><li><span><span>$ composer about </span></span></li></ol>
如果你是在Windows上运行,你可以下载这个文件,然后通过PHP解析器安装,无论在哪里都可以。
解析composer.json文件
如果你是一名Ruby程序员,你会觉得这个文件跟Gemfile文件很相似,或者你是一个Node程序员,那么会觉得和package.json文件很像。同样,Composer会根据你的应用需求用composer.json文件来指定设置和封装。
在大多数基本的form里面,composer文件看起来是这样的:
<ol> <li><span><span>{ </span></span></li> <li><span> "require": { </span></li> <li><span> "kriswallsmith/assetic": "*" </span></li> <li><span> } </span></li> <li><span>} </span></li> </ol>
意思是需要一个“assetic”包,通过“kriswallsmith”创建和指定任意一个版本。你也可以指定一个特殊的版本,你可以使用下面命令代替:
<ol><li><span><span>"kriswallsmith/assetic": "1.0.3" </span></span></li></ol>
你甚至还可以使用这种方法:
<ol><li><span><span>"kriswallsmith/assetic": "1.0.*" </span></span></li></ol>
这个有一些微小的变化,但是他不会升级到1.1.0,程序员需要注意界面上细微的变化。
安装要求
现在,在你的composer.json文件下面会有一个或多个包,这个时候可以运行:
<ol><li><span><span>$ php composer.phar install </span></span></li></ol>
或者,如果你听了我的建议,你只需要在Unix机器上面运行:
<ol><li><span><span>$ composer install </span></span></li></ol>
你会发现,正在下载文件并且会放在应用程序根目录下面的vendors文件夹里面。这个逻辑也可以使用下面的配置:
<ol> <li><span><span>{ </span></span></li> <li><span> "require": { </span></li> <li><span> "kriswallsmith/assetic": "1.0.*" </span></li> <li><span> }, </span></li> <li><span> "config" : { </span></li> <li><span> "vendor-dir" : "packages" </span></li> <li><span> } </span></li> <li><span>} </span></li> </ol>
自动加载
自动加载在PHP里面有一点乱糟糟的,作为开发人员,他们有属于自己操作方式。比如Smarty包,使用自己的自动载入。有一些开发人员会把多个类放到一个文件里面,或者文件名小写,这些做法都太随意啦!
PHP官方社区创建了PSR-0标准,从而来规范这些随意的做法。Composer默认支持这个标准。Composer里面自带PSR-0自动加载机制,在项目里面加入下面一行代码:
<ol><li><span><span>include_once './vendor/autoload.php'; </span></span></li></ol>
显然,如果autoload.php文件目录有变化,你也需要在代码里面做出相应改动。
下面,你可以在应用程序中使用如下代码:
<ol> <li><span><span></span><span>php</span><span> </span></span></li> <li><span>use Assetic\Asset\AssetCollection; </span></li> <li><span>use Assetic\Asset\FileAsset; </span></li> <li><span>use Assetic\Asset\GlobAsset; </span></li> <li> <span>$</span><span>js</span><span> = </span><span>new</span><span> AssetCollection(array( </span> </li> <li><span> new GlobAsset('/path/to/js/*'), </span></li> <li><span> new FileAsset('/path/to/another.js'), </span></li> <li><span>)); </span></li> <li><span> </span></li> <li><span>// the code is merged when the asset is dumped </span></li> <li> <span>echo $js-</span><span>></span><span>dump(); </span> </li> </ol>
这是一个使用Assetic的例子,当然,这里也有许多命名空间代码,但是这样做是为了避免包之间互相冲突。
PSR-0的命名惯例本质是:
<ol><li><span><span>\</span><span><span>Vendor</span><span> Name</span><span>></span><span>\(</span><span><span>Namespace</span><span>></span><span>\)*</span><span><span>Class</span><span> Name</span><span>></span><span> </span></span></span></span></span></li></ol>
下面这个例子是Buzz HTTP包:
<ol> <li><span><span>$</span><span>browser</span><span> = </span><span>new</span><span> Buzz\Browser; </span></span></li> <li> <span>$</span><span>response</span><span> = $browser-</span><span>></span><span>get('http://www.google.com'); </span> </li> <li> <span>echo $browser-</span><span>></span><span>getLastRequest()."\n"; </span> </li> <li><span>echo $response; </span></li> </ol>
看起来像是被美化的file_get_contents(),但是它处理所有类型的智能逻辑,并且在后台处理HTTP Response/Request,你也会发现命名空间语法也不是那么的强烈。
真实的世界
目前,大多数PHP存储依靠主代码库。如果你使用Facebook SDK,例如,你仅仅从GitHub或者zip文件中通过复制粘贴的方式把版本推到你的代码中,然后把它放到你的版本控制系统里面,将会变更。
版本和你的代码只是作为静态文件放在里面,在某种意义上,你可能会忘记升级,如果你关注到Facebook已经发布了一个新版本。最新版本文件会显示在最上面。
使用Composer就无需关注版本变化情况,你只需运行一下更新,那么所有需要更新的都会自动更新。但是为什么还会有大量的代码在你的仓库里呢?你不需要它们在那里吗?
最干脆的做法是添加vendors到你的“Ignore”列表里面(例如gitignore)并且让你的代码完全离开那里。当你在部署代码的时候,你只需运行composer install或者composer update。
如果你想使用更熟练,你可以手动运行整个过程,如果你有云端托管你可以设置hooks,一旦代码发布,就运行。
总结
将来,你将会看到更多的Composer,各种丰富多彩的框架已经开始提供了多种层次的集成。FuelPHP将构建Composer包,CodeIgniter提供自动加载并且已经在Symfony2上广泛使用。
使用Composer添加相关包到你的项目里面是一个很好的方式,无需安装PECLI扩展或者复制粘贴一个系列文件。那种方式已经很过时了,并且还浪费你大量的时间。(编译:张红月)
market@csdn.net。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

If you are an experienced PHP developer, you might have the feeling that you’ve been there and done that already.You have developed a significant number of applications, debugged millions of lines of code, and tweaked a bunch of scripts to achieve op

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.
