mirror of
https://github.com/codeguy/php-the-right-way.git
synced 2025-08-20 04:11:44 +02:00
Updated wording on Composer / PEAR stuff.
This commit is contained in:
@@ -4,16 +4,14 @@ anchor: dependency_management
|
|||||||
|
|
||||||
# Dependency Management {#dependency_management_title}
|
# Dependency Management {#dependency_management_title}
|
||||||
|
|
||||||
There are a ton of PHP libraries, frameworks, and components to choose from. Your project will likely use several of
|
There are a ton of PHP libraries, frameworks, and components to choose from. Your project will likely use
|
||||||
them — these are project dependencies. Until recently, PHP did not have a good way to manage these project
|
several of them — these are project dependencies. Until recently, PHP did not have a good way to manage
|
||||||
dependencies. Even if you managed them manually, you still had to worry about autoloaders. No more.
|
these project dependencies. Even if you managed them manually, you still had to worry about autoloaders.
|
||||||
|
That is no longer an issue.
|
||||||
|
|
||||||
Currently there are two major package management systems for PHP - Composer and PEAR. Which one is right for you? The
|
Currently there are two major package management systems for PHP - [Composer] and [PEAR]. Composer is
|
||||||
answer is both.
|
the main package manager to use for PHP, however for a long time PEAR used to fill that role. Knowing what
|
||||||
|
PEAR is will be a good idea as you may still find references to it, even if you never use it.
|
||||||
|
|
||||||
* Use **Composer** when managing dependencies for a single project.
|
[Composer]: /#composer_and_packagist
|
||||||
* Use **PEAR** when managing dependencies for PHP as a whole on your system.
|
[PEAR]: /#pear
|
||||||
|
|
||||||
In general, Composer packages will be available only in the projects that you explicitly specify whereas a PEAR package
|
|
||||||
would be available to all of your PHP projects. While PEAR might sound like the easier approach at first glance, there
|
|
||||||
are advantages to using a project-by-project approach to your dependencies.
|
|
@@ -10,7 +10,7 @@ with a few simple commands, Composer will automatically download your project's
|
|||||||
you.
|
you.
|
||||||
|
|
||||||
There are already a lot of PHP libraries that are compatible with Composer, ready to be used in your project. These
|
There are already a lot of PHP libraries that are compatible with Composer, ready to be used in your project. These
|
||||||
"packages" are listed on [Packagist][1], the official repository for Composer-compatible PHP libraries.
|
"packages" are listed on [Packagist], the official repository for Composer-compatible PHP libraries.
|
||||||
|
|
||||||
### How to Install Composer
|
### How to Install Composer
|
||||||
|
|
||||||
@@ -27,14 +27,16 @@ dependencies.
|
|||||||
code online first to confirm it is safe.
|
code online first to confirm it is safe.
|
||||||
|
|
||||||
#### Installing on Windows
|
#### Installing on Windows
|
||||||
For Windows users the easiest way to get up and running is to use the [ComposerSetup][6] installer, which performs a
|
|
||||||
global install and sets up your `$PATH` so that you can just call `composer` from any directory in your command line.
|
For Windows users the easiest way to get up and running is to use the [ComposerSetup] installer, which
|
||||||
|
performs a global install and sets up your `$PATH` so that you can just call `composer` from any
|
||||||
|
directory in your command line.
|
||||||
|
|
||||||
### How to Install Composer (manually)
|
### How to Install Composer (manually)
|
||||||
|
|
||||||
Manually installing Composer is an advanced technique; however, there are various reasons why a developer might prefer
|
Manually installing Composer is an advanced technique; however, there are various reasons why a
|
||||||
this method vs. using the interactive installation routine. The interactive installation checks your PHP installation
|
developer might prefer this method vs. using the interactive installation routine. The interactive
|
||||||
to ensure that:
|
installation checks your PHP installation to ensure that:
|
||||||
|
|
||||||
- a sufficient version of PHP is being used
|
- a sufficient version of PHP is being used
|
||||||
- `.phar` files can be executed correctly
|
- `.phar` files can be executed correctly
|
||||||
@@ -42,19 +44,19 @@ to ensure that:
|
|||||||
- certain problematic extensions are not loaded
|
- certain problematic extensions are not loaded
|
||||||
- certain `php.ini` settings are set
|
- certain `php.ini` settings are set
|
||||||
|
|
||||||
Since a manual installation performs none of these checks, you have to decide whether the trade-off is worth it for
|
Since a manual installation performs none of these checks, you have to decide whether the trade-off is
|
||||||
you. As such, below is how to obtain Composer manually:
|
worth it for you. As such, below is how to obtain Composer manually:
|
||||||
|
|
||||||
{% highlight console %}
|
{% highlight console %}
|
||||||
curl -s https://getcomposer.org/composer.phar -o $HOME/local/bin/composer
|
curl -s https://getcomposer.org/composer.phar -o $HOME/local/bin/composer
|
||||||
chmod +x $HOME/local/bin/composer
|
chmod +x $HOME/local/bin/composer
|
||||||
{% endhighlight %}
|
{% endhighlight %}
|
||||||
|
|
||||||
The path `$HOME/local/bin` (or a directory of your choice) should be in your `$PATH` environment variable. This will
|
The path `$HOME/local/bin` (or a directory of your choice) should be in your `$PATH` environment
|
||||||
result in a `composer` command being available.
|
variable. This will result in a `composer` command being available.
|
||||||
|
|
||||||
When you come across documentation that states to run Composer as `php composer.phar install`, you can substitute that
|
When you come across documentation that states to run Composer as `php composer.phar install`, you can
|
||||||
with:
|
substitute that with:
|
||||||
|
|
||||||
{% highlight console %}
|
{% highlight console %}
|
||||||
composer install
|
composer install
|
||||||
@@ -64,25 +66,26 @@ This section will assume you have installed composer globally.
|
|||||||
|
|
||||||
### How to Define and Install Dependencies
|
### How to Define and Install Dependencies
|
||||||
|
|
||||||
Composer keeps track of your project's dependencies in a file called `composer.json`. You can manage it by hand if you
|
Composer keeps track of your project's dependencies in a file called `composer.json`. You can manage it
|
||||||
like, or use Composer itself. The `composer require` command adds a project dependency and if you don't have a
|
by hand if you like, or use Composer itself. The `composer require` command adds a project dependency
|
||||||
`composer.json` file, one will be created. Here's an example that adds [Twig][2] as a dependency of your project.
|
and if you don't have a `composer.json` file, one will be created. Here's an example that adds [Twig]
|
||||||
|
as a dependency of your project.
|
||||||
|
|
||||||
{% highlight console %}
|
{% highlight console %}
|
||||||
composer require twig/twig:~1.8
|
composer require twig/twig:~1.8
|
||||||
{% endhighlight %}
|
{% endhighlight %}
|
||||||
|
|
||||||
Alternatively the `composer init` command will guide you through creating a full `composer.json` file for your project.
|
Alternatively the `composer init` command will guide you through creating a full `composer.json` file
|
||||||
Either way, once you've created your `composer.json` file you can tell Composer to download and install your
|
for your project. Either way, once you've created your `composer.json` file you can tell Composer to
|
||||||
dependencies into the `vendor/` directory. This also applies to projects you've downloaded that already provide a
|
download and install your dependencies into the `vendor/` directory. This also applies to projects
|
||||||
`composer.json` file:
|
you've downloaded that already provide a `composer.json` file:
|
||||||
|
|
||||||
{% highlight console %}
|
{% highlight console %}
|
||||||
composer install
|
composer install
|
||||||
{% endhighlight %}
|
{% endhighlight %}
|
||||||
|
|
||||||
Next, add this line to your application's primary PHP file; this will tell PHP to use Composer's autoloader for your
|
Next, add this line to your application's primary PHP file; this will tell PHP to use Composer's
|
||||||
project dependencies.
|
autoloader for your project dependencies.
|
||||||
|
|
||||||
{% highlight php %}
|
{% highlight php %}
|
||||||
<?php
|
<?php
|
||||||
@@ -93,32 +96,47 @@ Now you can use your project dependencies, and they'll be autoloaded on demand.
|
|||||||
|
|
||||||
### Updating your dependencies
|
### Updating your dependencies
|
||||||
|
|
||||||
Composer creates a file called `composer.lock` which stores the exact version of each package it downloaded when you
|
Composer creates a file called `composer.lock` which stores the exact version of each package it
|
||||||
first ran `php composer.phar install`. If you share your project with other coders and the `composer.lock` file is
|
downloaded when you
|
||||||
part of your distribution, when they run `php composer.phar install` they'll get the same versions as you. To update
|
first ran `composer install`. If you share your project with other coders and the `composer.lock` file
|
||||||
your dependencies, run `php composer.phar update`.
|
is part of your distribution, when they run `composer install` they'll get the same versions as you.
|
||||||
|
To update your dependencies, run `composer update`.
|
||||||
|
|
||||||
This is most useful when you define your version requirements flexibly. For instance a version requirement of `~1.8`
|
This is most useful when you define your version requirements flexibly. For instance a version
|
||||||
means "anything newer than `1.8.0`, but less than `2.0.x-dev`". You can also use the `*` wildcard as in `1.8.*`. Now
|
requirement of `~1.8` means "anything newer than `1.8.0`, but less than `2.0.x-dev`". You can also use
|
||||||
Composer's `php composer.phar update` command will upgrade all your dependencies to the newest version that fits the
|
the `*` wildcard as in `1.8.*`. Now Composer's `composer update` command will upgrade all your
|
||||||
restrictions you define.
|
dependencies to the newest version that fits the restrictions you define.
|
||||||
|
|
||||||
### Update Notifications
|
### Update Notifications
|
||||||
|
|
||||||
To receive notifications about new version releases you can sign up for [VersionEye][3], a web service that can monitor
|
To receive notifications about new version releases you can sign up for [VersionEye], a web service
|
||||||
your GitHub and BitBucket accounts for `composer.json` files and send emails with new package releases.
|
that can monitor your GitHub and BitBucket accounts for `composer.json` files and send emails with new
|
||||||
|
package releases.
|
||||||
|
|
||||||
### Checking your dependencies for security issues
|
### Checking your dependencies for security issues
|
||||||
|
|
||||||
The [Security Advisories Checker][4] is a web service and a command-line tool, both will examine your `composer.lock`
|
The [Security Advisories Checker] is a web service and a command-line tool, both will examine your `composer.lock`
|
||||||
file and tell you if you need to update any of your dependencies.
|
file and tell you if you need to update any of your dependencies.
|
||||||
|
|
||||||
* [Learn about Composer][5]
|
### Handling global dependencies with Composer
|
||||||
|
|
||||||
|
Composer can also handle global dependencies and their binaries. Usage is straight-forward, all you need
|
||||||
|
to do is prefix your command with `global`. If per example you wanted to install PHPUnit and have it
|
||||||
|
available globally, you'd run the following command:
|
||||||
|
|
||||||
[1]: http://packagist.org/
|
{% highlight console %}
|
||||||
[2]: http://twig.sensiolabs.org
|
composer global require phpunit/phpunit
|
||||||
[3]: https://www.versioneye.com/
|
{% endhighlight %}
|
||||||
[4]: https://security.sensiolabs.org/
|
|
||||||
[5]: http://getcomposer.org/doc/00-intro.md
|
This will create a `~/.composer` folder where your global dependencies reside. To have the installed
|
||||||
[6]: https://getcomposer.org/Composer-Setup.exe
|
packages' binaries available everywhere, you'd then add the `~/.composer/vendor/bin` folder to your
|
||||||
|
`$PATH` variable.
|
||||||
|
|
||||||
|
* [Learn about Composer]
|
||||||
|
|
||||||
|
[Packagist]: http://packagist.org/
|
||||||
|
[Twig]: http://twig.sensiolabs.org
|
||||||
|
[VersionEye]: https://www.versioneye.com/
|
||||||
|
[Security Advisories Checker]: https://security.sensiolabs.org/
|
||||||
|
[Learn about Composer]: http://getcomposer.org/doc/00-intro.md
|
||||||
|
[ComposerSetup]: https://getcomposer.org/Composer-Setup.exe
|
||||||
|
@@ -5,7 +5,7 @@ anchor: pear
|
|||||||
|
|
||||||
## PEAR {#pear_title}
|
## PEAR {#pear_title}
|
||||||
|
|
||||||
Another veteran package manager that many PHP developers enjoy is [PEAR][1]. It behaves much the same way as Composer,
|
A veteran package manager that some PHP developers enjoy is [PEAR][1]. It behaves similarly to Composer,
|
||||||
but has some notable differences.
|
but has some notable differences.
|
||||||
|
|
||||||
PEAR requires each package to have a specific structure, which means that the author of the package must prepare it for
|
PEAR requires each package to have a specific structure, which means that the author of the package must prepare it for
|
||||||
@@ -17,11 +17,11 @@ version conflicts between two projects arise.
|
|||||||
|
|
||||||
### How to install PEAR
|
### How to install PEAR
|
||||||
|
|
||||||
You can install PEAR by downloading the phar installer and executing it. The PEAR documentation has detailed
|
You can install PEAR by downloading the `.phar` installer and executing it. The PEAR documentation has
|
||||||
[install instructions][2] for every operating system.
|
detailed [install instructions][2] for every operating system.
|
||||||
|
|
||||||
If you are using Linux, you can also have a look at your distribution package manager. Debian and Ubuntu, for example,
|
If you are using Linux, you can also have a look at your distribution package manager. Debian and Ubuntu,
|
||||||
have an apt ``php-pear`` package.
|
for example, have an apt `php-pear` package.
|
||||||
|
|
||||||
### How to install a package
|
### How to install a package
|
||||||
|
|
||||||
@@ -56,7 +56,7 @@ handle your PEAR dependencies. This example will install code from `pear2.php.ne
|
|||||||
}
|
}
|
||||||
{% endhighlight %}
|
{% endhighlight %}
|
||||||
|
|
||||||
The first section `"repositories"` will be used to let Composer know it should "initialise" (or "discover" in PEAR
|
The first section `"repositories"` will be used to let Composer know it should "initialize" (or "discover" in PEAR
|
||||||
terminology) the pear repo. Then the require section will prefix the package name like this:
|
terminology) the pear repo. Then the require section will prefix the package name like this:
|
||||||
|
|
||||||
> pear-channel/Package
|
> pear-channel/Package
|
||||||
|
Reference in New Issue
Block a user