ホームページ  >  記事  >  バックエンド開発  >  PHP 依存関係管理ツール Composer のインストールと使用について

PHP 依存関係管理ツール Composer のインストールと使用について

不言
不言オリジナル
2018-06-13 16:02:035577ブラウズ

Composer は、PHP で依存関係を管理するために使用されるツールです。プロジェクト内で依存する外部ライブラリ (ライブラリ) を宣言すると、Composer がこれらの依存ライブラリ ファイルをインストールします。

PHP Composer のインストール

システム要件:

Composer を実行するには、PHP5.3.2 以降が必要です。いくつかの機密性の高い PHP 設定とコンパイル フラグも必要ですが、非互換性が存在する場合、インストーラーは警告を発行します。

たとえば、PHP 拡張機能の要件は、-disable-phar を指定せずに php をインストールまたは再コンパイルすることです。

単純な圧縮ファイル パッケージではなくソース アドレスからパッケージをインストールするには、次のようにします。 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 のインストール中のパラメータは、 | E:\USB\xampp_177\php\php.exe などの実際の PHP 環境パスに変更する必要があります。
インストールした環境では、PHP パス コマンドとして Composer を使用します。 , 使用方法は、E:\USB\xampp_177\php/php.execomposer.pharコマンドです。
私がインストールした環境が Windows であり、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 実行コマンドに。

2 番目のパラメータは、composer.phar モードでダウンロードされます。

3 番目のパラメータは、create-project などのアクション コマンドです。その目的は、ソフトウェア パッケージから指定されたディレクトリに新しいプロジェクトを作成することです。

4 番目のパラメータは、ローカル インストールのディレクトリです。
5 番目のパラメータは、インストールされているソフトウェア パッケージのバージョンです。

上記に従って、Composer を PHP5.4 環境にインストールしました。インストール コマンドは次のとおりです:

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 ファイルを自動的に検索します。 combos.json は、プロジェクト全体の依存関係パッケージを定義するために使用されます。例:

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

依存関係パッケージ 簡単に説明すると、このプロジェクトには 1.2 以降の任意のバージョンのいくつかの monolog/monolog パッケージが必要です。

Composer は、PHP の依存関係管理ツールです。 Composer を使用すると、ソフトウェアを公開するときに多くのファイルを保存し、依存パッケージのバージョンを制御できます。

上記がこの記事の全内容です。その他の関連コンテンツについては、PHP 中国語 Web サイトをご覧ください。

関連する推奨事項:

PHP フレームワーク Laravel プラグインについて カスタム ページングを実装するためのページネーション メソッド


以上がPHP 依存関係管理ツール Composer のインストールと使用についての詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

声明:
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。