ติดตั้ง composer ไว้บริหารจัดการ PHP Library

composer เป็นเครื่องมือใช้ในการบริหารจัดการ ช่วยให้การติดตั้ง PHP Library ที่ต้องการได้ง่ายขึ้น เพราะ composer จะช่วยค้นหาและติดตั้ง Library อื่นๆ รวมทั้งเวอร์ชั่นที่จำเป็นโดยอัตโนมัติ ก่อนที่จะติดตั้่ง Library ที่ต้องการ

เวอร์ชั่นของ PHP ที่จะใช้ composer ได้คือ 5.3.2+

ในที่นี้จะทดสอบบน CentOS 6 ที่มี PHP 5.3.3 ติดตั้งมาโดยดีฟอลต์

[alice@cent6-php ~]$ php v

PHP 5.3.3 (cli) (built: Oct 30 2014 20:12:53)

Copyright (c) 1997-2010 The PHP Group

Zend Engine v2.3.0, Copyright (c) 1998-2010 Zend Technologies

ติดตั้ง COMPOSER

วิธีการติดตั้งตามคำแนะนำของเว็บไซต์ getcomposer.org

[alice@cent6-php ~]$ curl sS https://getcomposer.org/installer | php

#!/usr/bin/env php

Some settings on your machine may cause stability issues with Composer.

If you encounter issues, try to change the following:

 

Your PHP (5.3.3) is quite old, upgrading to PHP 5.3.4 or higher is recommended.

Composer works with 5.3.2+ for most people, but there might be edge case issues.

 

Downloading…

 

Composer successfully installed to: /home/alice/composer.phar

Use it: php composer.phar

ไฟล์ที่ได้จากการติดตั้งจะมีแค่ไฟล์เดียวคือ composer.phar ซึ่งเป็นไฟล์ในรูปแบบ phar (PHP Archive)

[alice@cent6-php ~]$ ls l

total 1024

-rwxr-xr-x. 1 alice users 1047893 Jan  4 11:31 composer.phar

สามารถรัน composer ได้จากการใช้คำสั่ง php ตามด้วยชื่อไฟล์ composer.phar หรือถ้า permission อนุญาตให้รัน (execute) ได้ ก็สามารถรันได้โดยตรง ./composer.phar

[alice@cent6-php ~]$ php composer.phar

______

/ ____/___  ____ ___  ____  ____  ________  _____

/ /   / __ \/ __ `__ \/ __ \/ __ \/ ___/ _ \/ ___/

/ /___/ /_/ / / / / / / /_/ / /_/ (__  )  __/ /

\____/\____/_/ /_/ /_/ .___/\____/____/\___/_/

/_/

Composer version 1.0-dev (e172cd81a146666d844d2b7b37b5feccb89e2b61) 2015-01-02 07:55:29

 

Usage:

[options] command [arguments]

 

Options:

–help (-h)           Display this help message.

–quiet (-q)          Do not output any message.

–verbose (-v|vv|vvv) Increase the verbosity of messages: 1 for normal output, 2 for more verbose output and 3 for debug.

–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

browse           Opens the package’s repository URL or homepage in your browser.

clear-cache      Clears composer’s internal package cache.

clearcache       Clears composer’s internal package cache.

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

global           Allows running commands in the global composer dir ($COMPOSER_HOME).

help             Displays help for a command

home             Opens the package’s repository URL or homepage in your browser.

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.

licenses         Show information about licenses of dependencies

list             Lists commands

remove           Removes a package from the require or require-dev

require          Adds required packages to your composer.json and installs them

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

เพื่อความสะดวกและให้ผู้ใช้ (user) ทุกคนบนเซิร์ฟเวอร์สามารถรันได้ด้วย วิธีการที่แนะนำคือใช้ root เพื่อ copy ไฟล์ composer.phar ไปไว้ในไดเร็กทอรีหรือ path ที่สามารถรันและมองเห็นได้ทุกคน เช่น /usr/local/bin

ล็อกอินด้วย root แล้ว copy ไฟล์ composer.phar ไปเป็นไฟล์ /usr/local/bin/composer

[root@cent6-php alice]# cp composer.phar /usr/local/bin/composer

 

[root@cent6-php alice]# ls l /usr/local/bin/

total 1024

-rwxr-xr-x. 1 root root 1047893 Jan  4 11:40 composer

ตัวอย่างการรัน composer หลังจาก copy ไฟล์และเปลี่ยนชื่อไฟล์ไปไว้ใน /usr/local/bin/

[alice@cent6-php ~]$ composer V

Composer version 1.0-dev (e172cd81a146666d844d2b7b37b5feccb89e2b61) 2015-01-02 07:55:29

ทดลองใช้ COMPOSER ติดตั้ง MONOLOG

วิธีการใช้ composer มีหลายรูปแบบ วิธีหนึ่งคือสร้างไฟล์ composer.json แล้วใช้คำสั่ง composer install

สร้างไฟล์ composer.json

[alice@cent6-php ~]$ vi composer.json

{

“require”: {

“monolog/monolog”: “1.2.*”

}

}

รันคำสั่ง composer install

[alice@cent6-php ~]$ composer install

Loading composer repositories with package information

Installing dependencies (including require-dev)

– Installing monolog/monolog (1.2.1)

Downloading: 100%

 

monolog/monolog suggests installing mlehner/gelf-php (Allow sending log messages to a GrayLog2 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)

Writing lock file

Generating autoload files

ดูไฟล์ผลลัพธ์ที่ได้จากการติดตั้่ง

[alice@cent6-php ~]$ ls l

total 1036

-rw-r–r–. 1 alice users      62 Jan  4 12:14 composer.json

-rw-r–r–. 1 alice users    2509 Jan  4 12:15 composer.lock

-rwxr-xr-x. 1 alice users 1047893 Jan  4 11:31 composer.phar

drwxr-xr-x. 4 alice users    4096 Jan  4 12:15 vendor

 

[alice@cent6-php ~]$ ls l vendor/

total 12

-rw-r–r–. 1 alice users  183 Jan  4 12:15 autoload.php

drwxr-xr-x. 2 alice users 4096 Jan  4 12:15 composer

drwxr-xr-x. 3 alice users 4096 Jan  4 12:15 monolog

ข้อมูลอ้างอิง

 

ใส่ความเห็น

อีเมลของคุณจะไม่แสดงให้คนอื่นเห็น ช่องที่ต้องการถูกทำเครื่องหมาย *