Maison  >  Article  >  développement back-end  >  À propos de l'installation et de l'utilisation de Composer, un outil de gestion des dépendances PHP

À propos de l'installation et de l'utilisation de Composer, un outil de gestion des dépendances PHP

不言
不言original
2018-06-13 16:02:035576parcourir

Composer est un outil utilisé en PHP pour gérer les relations de dépendance. Vous pouvez déclarer les bibliothèques externes (bibliothèques) dont vous dépendez dans votre projet, et Composer installera ces fichiers de bibliothèque dépendants pour vous.

Installation de PHP Composer

Exigences du système :

Composer nécessite PHP5.3.2+ ou supérieur pour s'exécuter. Plusieurs paramètres PHP sensibles et indicateurs de compilation sont également requis, mais le programme d'installation émettra un avertissement en cas d'incompatibilité.

Par exemple, l'exigence pour les extensions PHP est d'installer ou de recompiler php sans --disable-phar

Afin d'installer le package à partir de l'adresse source, plutôt que simplement le package de fichiers compressés , vous devrez installer des outils de contrôle de version pour les packages logiciels, tels que git, svn ou hg, etc.

Composer est compatible multiplateforme et fonctionne sous Windows, Linux et OSX.

L'installation a échoué avec un message d'erreur :

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.

Mon environnement de développement local par défaut est PHP 5.2.9 et lorsque j'essaie de l'installer en PHP Lors de l'installation sur 5.2.9, il indique que la version est trop basse et doit être mise à niveau vers PHP 5.3.2 ou supérieur.

J'ai donc téléchargé la version USB Lite de XAMPP 1.7.7.

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

L'environnement PHP sous XAMPP 1.7.7 est 5.3.8. Après avoir confirmé que mon environnement PHP est supérieur ou égal à PHP5.3.2.

PHP 5.3 Installer 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

Remarque :

J'ai désactivé XAMPP 1.7 .7 Module Apache SSL, Composer m'a donc envoyé un avertissement et m'a suggéré d'activer le module SSL pour des raisons de sécurité. Cela n'affecte pas l'utilisation s'il n'est pas allumé.
Les paramètres d'installation | PHP doivent être modifiés par le chemin réel de l'environnement PHP, tel que | E:USBxampp_177phpphp.exe
Utilisez Composer comme chemin PHP + commande Dans l'environnement que nous avons installé, la méthode d'utilisation est : E. :USBxampp_177php/php.exe commande composer.phar.
L'environnement que j'ai installé est sous Windows. S'il est sous Linux et que l'environnement PHP 5.3 n'est pas sous la commande PHP par défaut, vous devez utiliser | /usr/php/53/usr/bin/php, par exemple :

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

Une fois l'installation terminée, vérifiez la version installée.

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

Vérifiez la commande 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

Par ici nous sommes Composer dans l'environnement PHP5.3 a été installé.

De même, nous pouvons également installer l'environnement PHP 5.4.

PHP 5.4 Install Composer

Téléchargez la version USB Lite de XAMPP 1.8.1, vous obtiendrez l'environnement 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

Après avoir confirmé que la version est supérieure à PHP 5.3.2, lancez l'installation :

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

Une fois l'installation terminée, vérifiez la version installée.

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

De cette façon, Composer dans l'environnement PHP 5.4 est également installé avec succès.

PHP Composer utilisant

Ici, je vais simplement démontrer l'utilisation de PHP Composer pour installer le framework Symfony.

Tout d'abord, expliquez brièvement le format standard de la commande :

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

Le premier paramètre PHP est l'environnement de développement PHP local , pointant vers l'ordre d'exécution PHP.
Le deuxième paramètre est téléchargé en mode composer.phar.
Le troisième paramètre est une commande d'action, telle que create-project, qui est destinée à créer un nouveau projet à partir d'un progiciel vers le répertoire spécifié.
Le quatrième paramètre est le répertoire d'installation locale.
Le cinquième paramètre est la version du progiciel installé.

Selon ce qui précède, nous avons installé Composer dans l'environnement PHP5.4, notre commande d'installation est :

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

Résultat de l'installation :

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

À partir des résultats de la ligne de commande, nous voyons le processus d'installation et les résultats de l'ensemble du framework Symfony.

PHP Composer définit les dépendances

Composer recherchera automatiquement le fichier composer.json lorsqu'il est utilisé. composer.json sera utilisé pour définir les packages de dépendances de l’ensemble du projet. Par exemple :

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

Une brève description des packages de dépendances, notre projet nécessite des packages monolog/monolog de n'importe quelle version à partir de 1.2.

Composer est un outil de gestion des dépendances en PHP. Grâce à Composer, nous pouvons enregistrer de nombreux fichiers et contrôler les versions des packages dépendants lors de la publication de logiciels.

Ce qui précède représente l'intégralité du contenu de cet article. J'espère qu'il sera utile à l'étude de chacun. Pour plus de contenu connexe, veuillez faire attention au site Web PHP chinois !

Recommandations associées :

À propos de la méthode de pagination du plug-in Laravel du framework PHP pour implémenter une pagination personnalisée

Ce qui précède est le contenu détaillé de. pour plus d'informations, suivez d'autres articles connexes sur le site Web de PHP en chinois!

Déclaration:
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn