从官网下载composer

https://getcomposer.org/

composer是跨平台的, 这里只介绍windows使用composer

下载好了后, 进行安装.

wKioL1XQRv2gdvCZAAEJmNCozAk201.jpg

可以选择 "Install Shell Menus", 这将会在右键菜单里面增加composer的快捷入口.

wKiom1XQRRTjGVvVAADoZzoBy04890.jpg

composer会自动找到php.exe的位置. 也可以自定义php的位置.

wKioL1XQRzKzYy9nAAEP9l-uLlg453.jpg


wKiom1XQRVfA1XqvAACWDYvSzKQ842.jpg

下载composer.phar

wKioL1XQR3_TyyPnAAEe3zqIMJE481.jpg

该死的"墙", 连接上×××.

wKiom1XQRZDSAULeAAFKzIYR4-c585.jpg

我的php 5.3.3 确实够老的. 点击next.

wKioL1XQR7Ghmo_8AAE298pkOCY027.jpg

成功, composer设置了 path 环境变量. 所以可以在command line的任意位置使用composer命令了.

wKioL1XQR9DQgmvuAAFJSKfk_8U480.jpg

下面就来用一下composer.

新建一个文件夹, C:\clearvale\elgg\ztest\oauth_server_bshaffer

由于装了  shell menu,

所以直接在文件夹上右键选择 Use Composer here

wKiom1XQReWipylpAAB2mZmBYU8360.jpg


现在遵循最传统的方式使用composer, 新建oauth_server_bshaffer/composer.json 文件.

这里用 bshaffer的oauth2 server来演示. 在composer.php里面写入如下内容.

{

    "require": {

        "bshaffer/oauth2-server-php": "~0.9"

    }

}


<<<<<<<<<<<<<<<<<<<<

这里记录一下, 之前写入的是 

{

    "require": {

        "bshaffer/oauth2-server-php": "~1.7",

    }

}


首先多了个 , 号

导致composer报错.


C:\clearvale\elgg\ztest\oauth_server_bshaffer>composer install

  [Seld\JsonLint\ParsingException]

  "./composer.json" does not contain valid JSON

  Parse error on line 3:

}

  ---------------------^

  Expected: 'STRING' - It appears you have an extra trailing comma

install [--prefer-source] [--prefer-dist] [--dry-run] [--dev] [--no-dev] [--no-plugins] [--no-custom-installers] [--no-autoloader] [--no-scripts] [--n

o-progress] [-v|vv|vvv|--verbose] [-o|--optimize-autoloader] [--ignore-platform-reqs] [packages1] ... [packagesN]



去掉逗号后, 又报如下错误:

C:\clearvale\elgg\ztest\oauth_server_bshaffer>composer install

Loading composer repositories with package information

Installing dependencies (including require-dev)

Your requirements could not be resolved to an installable set of packages.

  Problem 1

    - Installation request for bshaffer/oauth2-server-php dev-develop -> satisfiable by bshaffer/oauth2-server-php[dev-develop].

    - bshaffer/oauth2-server-php dev-develop requires php >=5.3.9 -> your PHP version (5.3.3) or "config.platform.php" value does not satisfy that req

uirement.

C:\clearvale\elgg\ztest\oauth_server_bshaffer>composer install

Loading composer repositories with package information

Installing dependencies (including require-dev)

Your requirements could not be resolved to an installable set of packages.

  Problem 1

    - bshaffer/oauth2-server-php v1.7.1 requires php >=5.3.9 -> your PHP version (5.3.3) or "config.platform.php" value does not satisfy that requirem

ent.

    - bshaffer/oauth2-server-php v1.7.0 requires php >=5.3.9 -> your PHP version (5.3.3) or "config.platform.php" value does not satisfy that requirem

ent.

    - Installation request for bshaffer/oauth2-server-php ~1.7 -> satisfiable by bshaffer/oauth2-server-php[v1.7.0, v1.7.1].


大意是说: 1.7这个版本的最低php环境是 php >= 5.3.9

那么到底有多少个版本, 哪个版本支持php5.3.3 呢?

可以访问composer的packagelist官网.

https://packagist.org/packages/bshaffer/oauth2-server-php#v0.9

wKioL1XQSBKzAa3RAANywUZeNU8092.jpg


正好, bshaffer也推荐v0.9为stable release. 

>>>>>>>>>>>>>>>>>>>>>>>>>



所以就装 v0.9, 继续composer install

C:\clearvale\elgg\ztest\oauth_server_bshaffer>composer install

Loading composer repositories with package information

Installing dependencies (including require-dev)

  - Installing bshaffer/oauth2-server-php (v0.9)

    Downloading: 100%


Writing lock file

Generating autoload files


成功了. composer已经成功安装了oauth2-server-php (v0.9),

查看安装后的文件目录.

wKiom1XQRjHhcL-OAAD1XIDWFSc286.jpg

composer.lock用于锁定所有包的版本.  安装的时候会检查composer.lock文件的存在.

composer.lock和composer.json一起控制项目的版本.

比如现在已经装完了包, 再次运行composer install, 会显示 Nothing to install or update.

wKiom1XQRkmTJ3FzAACuUSphgEc312.jpg

oauth_server_php v0.9包已经安装完毕.

下面测试一下 auto_load

新建: C:\clearvale\elgg\ztest\oauth_server_bshaffer\test.php

写入如下内容:

<?php
require 'vendor/autoload.php';
//token controller
$dsn = "mysql:my_oauth2_db;host=localhost";
$username = "root";
$password = "root";
$storage = new OAuth2_Storage_Pdo(array('dsn' => $dsn, 'username' => $username, 'password' => $password));
print_r($storage);

最后打印出了 $storage对象.

spacer.gifwKiom1XQRmLw4JnJAAGNOSIamdk140.jpg

那么可见包里面的文件已经被自动加载. 只需要 require 'vendor/autoload.php';

包里面的所有文件都可以使用了.

end...


(有关composer更多的内容, 访问: http://www.phpcomposer.com/)