Introduction to Composer [Command-line interface]

Introduction

As a PHP developer, you surely have already realized that one of the most powerful tools in your toolset is the command line.

While I agree that spending time on the IDE of your choice and write code is the most beautiful task, I cannot deny the fact that several tools still use the terminal as the primary way to interact with them.

This old-style black and white window, in fact, hide treasures that let you work fast and in a more efficient way.

A clear example of this is Composer.

Composer, with its library of more than 30 commands, rely completely on the command-line interface (CLI for friends).

In this post, we are going to look at all of them and I’ll provide an explanation of what they do and how you can use them most efficiently.

How to read this article

This is not a typical blog post.

In here you will see all the commands included in the Composer library, some are very common and are being used multiple time a day other is used only in specific cases.

Also, there will be no job interview in which they will ask you what the command composer why -tnv does.

The way you need to look at this post is to read through it and discover what command you might use, then come here again every now and then to check if there are any commands you can implement in your working routine.

The series

This article is part of the series Introduction to Composer,

If you haven’t read the previous part already head to installation and components

The Commands

Working with Composer is not difficult at all,

You will see that on a daily basis you are going to use almost always the same 5 or 6 commands that, after a bit, will become second nature.

It is important though, to know that there are a dozen other commands outside of our comfort zone, and it is good to know that they exist and what they do.

The most basic command you can think of is the command composer, it just lists all the command available through the Composer tool.

You can achieve the same effect by using the command

composer list

Global Options

Composer provides some flags that are available for every command in this list.

These flags are available for a multitude of command, in this list, you can see a detailed explanation of the majority of them.

Then in the list of commands below, you will be able to understand if a flag is usable or not for that given command

The most popular flags are:

— verbose (-v)

Increase the verbosity of messages.

You can add up to 3 v-es, 1 for normal output, 2 for more verbose output and 3 for debug

— help (-h)

This command shows more information regarding the command you have just typed.

It is normally used as a suffix.

— quiet (-q)

It hides any message from the command line

— no-interaction (-n)

Sometimes Composer may ask you question such as confirming that you want to do an action or setting the permission of a folder.

By adding the -n flag at the end of the command this behaviour will be suppressed, what it does is to trigger Composer to pick the default option whenever an interactive action is necessary:

— no-plugins

In Composer you can create or remove plugins that are indicated in the composer.json file.

Using this flag you make sure that no plugins will be considered

— no-cache

Located on C:\Users\\AppData\Local\Composer on Windows and $XDG_CACHE_HOME/composer on Unix systems, it stores all the caches used by Composer.

When this command is used it disables the use of the cache directory.

You can achieve the same result by setting the COMPOSER_CACHE_DIR env var to /dev/null (or NUL on Windows).

— working-dir (-d)

Let’s say I am working on the patch /home/Nico and I want to install composer on a folder inside the previous directory.

What I normally do is to go inside the folder I want to run the command and go back.

This flag allows us to stay in our current position and installing Composer or doing all the work we want to do anyway.

Here is how we do so:

composer install -d=/home/Nico/myproject

— profile

This flag attaches the timing required to perform the command and the current memory usage information.

— ansi:

ANSI output is a standard that controls the cursor location, colour, and a few other options on video text terminals and terminal emulators.

By default, Composer uses ANSI for example by running composer you should see the list of command highlight from the explanation on the right and side

— no-ansi

if you add this flag to a Composer command it disables the ANSI standard output, colour and a few syntaxes will disappear

— version (-V)

this is straight forward, it displays the version of the composer you are running currently.

composer init [options]

The init command creates a basic composer.json file in the current directory.

It basically makes your project become an anonymous component.

With this command you can use several options, they are listed below

composer install [options] [ — ] []

After the composer.json has been created the install command reads the composer.lock file from the directory selected.

It downloads and installs all the libraries and dependencies outlined in the file.

If the file does not exist it will look for composer.json and do the same.

Daniel Pataki wrote about this command and how to use on his guide on phpnewspot

Here is the list of options available for this command:

composer update,

composer update vendor/package1 foo/mypackage […],

composer update vendor/package1 foo/* […]

The update command reads the composer.json file from the selected directory and updates, removes or installs all the dependencies.

There are a few versions of this command.

For instance, you can use the second version shown above to limit the update operation to a few packages, you can list the package(s) you want to update as such:

You can also use an asterisk (*) pattern to limit the update operation to package(s) from a specific vendor.

Eventually to select packages names interactively with auto-completion use -i.

Here are the flags:

Composer require [options] [ — ] []

If your application needs a new package the require command is what you are looking for.

composer requires adds required packages to your composer.json and installs them.

Generally, you want to use this command and specify what package you want to install.

If instead, you do not want to specify any package, Composer will prompt you to search for a package then it will provide a list of matches to require.

Regarding the package version, if it is not specified, what Composer will do is to choose a suitable one based on the available package versions.

And important flag available for this command is the — no-update flag.

You can use it in case you do not want to install the new dependencies immediately.

The other flags are:

composer remove [options] [ — ]

As you might have already understood the composer remove command removes the package you indicated from the current list of installed packages.

You can also remove more than a package at once by listing them one after another.

This and other commands are available on the cheat sheet developed by the guys over devhints.io

This command has several options as well and they are:

composer check-platform-reqs [options]

This command verifies that your version of PHP and its extensions versions match the platform requirements of the installed packages.

It will return a table containing the extensions, its version and a success or failure bool.

Unlike the command composer update and composer install, composer check-platform-reqs ignores the settings inside config.platform and check the real platform packages.

It does it so you can be certain you have the required platform dependencies.

Its flags are:

Little preface:

Composer has several environmental variables.

One of these is COMPOSER_HOME.

it allows you to change the Composer home directory.

This is a hidden, global (per-user on the machine) directory that is shared between all projects.

It points to C:\Users\\AppData\Roaming\Composer on Windows and /Users//.composer on macOS.

composer global is used as a wrapper to run other Composer commands within the global context of COMPOSER_HOME.

More information are available on the official documentation

You can use this command to install CLI utilities globally, all you need is to add the COMPOSER_HOME/vendor/bin dir to your PATH environmental variable.

Of course, pay attention to the fact that this path can change depending on your machine customizations to bin-dir in composer.json or the environmental variable COMPOSER_BIN_DIR.

composer search [options] [ — ]

This is quite a straightforward command

The search command simply searches for packages by its name.

It returns a list of packages with the format vendor/name and a little description of the relative package.

composer search’s flags are:

The composer show and the composer info commands display detailed information about one or more packages that have been indicated in the command;

You can specify both package and the version you need more details about.

The flag for these two commands are the same and are:

composer outdated [options] [ — ] []

composer outdated is simply a proxy for `composer show -l`, the two, in fact, perform exactly the same task

Which is to show information about one or a list of packages their current version and the latest version available.

It also adds a coding colour (only if you have ANSI colour enabled otherwise it will be a signature) that indicates how easy would be to implement each package to your application

Here is how it works:

  • green or (=): Dependency is in the latest version and is up to date.
  • Yellow or (~): Dependency has a new version available that includes backwards compatibility breaks according to semver, so upgrade when you can but it may involve work.
  • Red or (!): Dependency has a new version that is semver-compatible and you should upgrade it.

If you see any red or yellow you should run the command composer update.

The flag for this one are:

Become a Patron!

Conclusion

As I said previously Composer is one of the most important tools that you must learn if you want to take your career as a PHP developer seriously.

In this post, we have seen some of the first 10 commands now available on your arsenal.

as I said previously my advise is to read through them and try to understand what they do and what flag you may find more useful during your day to day job.

You don’t need to learn every command and every flag by heart,

it would be enough for you to know that these commands exist and when to actually use them.

In the following posts, we are going to discover more command and some Composer tricks, for this reason, if you haven’t done it already add your email to the subscription form and be notified as soon as the article comes out.

What you can do now?

after learning about tools it is good to go back and brush up you PHP knowledge, you can do that by reading the basics of PHP

Otherwise, you can jump on another series and learn about Domain-driven design.

Originally published at https://anastasionico.uk.

--

--

--

An Italian coder and design enthusiast. I publish content on my blog that helps web developers improve their skills and share knowledge. http://anastasionico.uk

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Supercharge Your MacBook Touch Bar

Integrating Edge computing in Computer vision applications

Building Reliability Through Culture with Veteran Google SRE, Steve McGhee

Best Flutter Fitness App Template 2021

How to use Terraform modules to create an AWS ECS cluster with an attached ALB.

Access Control, Interoperability and Cultural Heritage

Stiff? Naah, SayAgile or Adaptable.

Microservices framework-Spring Cloud vs Oracle Helidon

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Nico Anastasio

Nico Anastasio

An Italian coder and design enthusiast. I publish content on my blog that helps web developers improve their skills and share knowledge. http://anastasionico.uk

More from Medium

Best Free VS Code Themes 2022🤩

Laravel Project deployment in Cpanel via GitHub

Basic features that every web developer wants 👩‍💻