Limited Time Offer!

For Less Than the Cost of a Starbucks Coffee, Access All DevOpsSchool Videos on YouTube Unlimitedly.
Master DevOps, SRE, DevSecOps Skills!

Enroll Now

What is Composer and Its user guide & Commands

Composer is a dependency management tool for PHP. It allows developers to declare the libraries or packages their project depends on and manages (downloads, installs, updates) them for the project. Composer is similar to tools such as npm for Node.js, pip for Python, or bundler for Ruby.

Composer works by reading a configuration file called composer.json which lists the required packages and their versions. It then downloads the packages from repositories such as Packagist and installs them in the project’s vendor directory. This way, the required dependencies are easily manageable and can be updated or removed with ease. Additionally, Composer allows developers to define their own packages and publish them to repositories for others to use.

List of All Composer commands with their explanation

Here is a list of some of the most commonly used Composer commands and their explanations:

  1. composer init: Initializes a new Composer package in the current directory and prompts for package details such as name, description, author, and dependencies.
  2. composer install: Installs the dependencies listed in the composer.lock file or in the composer.json file if composer.lock is not present.
  3. composer update: Updates the dependencies listed in the composer.json file to their latest compatible version and updates the composer.lock file.
  4. composer require: Adds a new dependency to the composer.json file and installs it.
  5. composer remove: Removes a dependency from the composer.json file and uninstalls it.
  6. composer show: Shows information about installed packages, including their version, dependencies, and installation location.
  7. composer dump-autoload: Regenerates the autoloader files based on the packages installed.
  8. composer validate: Validates the composer.json file to ensure it is a valid JSON file and has required fields.
  9. composer create-project: Creates a new project based on a specified package and installs its dependencies.
  10. composer config: Sets or gets configuration options for Composer.
  11. composer run-script: Runs a specified script from the composer.json file.
  12. composer self-update: Updates the Composer executable to the latest version.
  13. composer diagnose: Checks the system for common issues with Composer installation and configuration.
  14. composer archive: Creates a compressed archive of the current project.
  15. composer status: Shows the differences between the current codebase and the last installed packages.

How compose works?

Composer is a dependency management tool for PHP that simplifies the process of installing and updating the libraries and packages that your project depends on. It works by reading a configuration file named composer.json that lists the dependencies of your project and their required versions.

When you run composer install or composer update, Composer reads the composer.json file and downloads the necessary packages and their dependencies from the internet. It then installs them into the vendor directory of your project.

During this process, Composer also generates an autoload.php file that you can use to automatically load the classes and files of your dependencies. This makes it easy to include third-party libraries and components in your PHP projects.

In summary, Composer works by:

  1. Reading the composer.json file to determine the dependencies of your project.
  2. Resolving the version constraints of each dependency.
  3. Downloading the required packages and their dependencies from online repositories.
  4. Installing the packages into the vendor directory of your project.
  5. Generating an autoload.php file to enable automatic class loading.

Example of composer.json and explanation


{
    "name": "myproject/myproject",
    "description": "My project description",
    "type": "project",
    "require": {
        "php": "^7.4",
        "monolog/monolog": "^2.0"
    },
    "autoload": {
        "psr-4": {
            "MyProject\\": "src/"
        }
    },
    "scripts": {
        "test": "phpunit tests/",
        "build": [
            "phpunit --coverage-clover build/logs/clover.xml",
            "phpcs --standard=PSR2 src/",
            "phpmd src/ text cleancode,codesize,controversial,design,naming,unusedcode",
            "phpcpd src/"
        ]
    },
    "config": {
        "sort-packages": true
    }
}

  • "name": "myproject/myproject": This line specifies the name of the project and follows the format vendor/package. It should be unique, as it will be used to identify the package when it is installed or published.
  • "description": "My project description": This line is an optional description of the project that provides some context and information about the purpose of the project.
  • "type": "project": This line specifies the type of the project, which can be "library", "project", "metapackage", or "composer-plugin".
  • "require": { "php": "^7.4", "monolog/monolog": "^2.0" }: This line specifies the dependencies required by the project. In this example, it requires PHP version 7.4 or higher and the Monolog logging library version 2.0 or higher.
  • "autoload": { "psr-4": { "MyProject\\": "src/" } }: This line defines the autoloading rules for the project. In this example, it uses the PSR-4 autoloading standard and specifies that the MyProject namespace should be mapped to the src directory.
  • "scripts": { "test": "phpunit tests/", "build": [ "phpunit --coverage-clover build/logs/clover.xml", "phpcs --standard=PSR2 src/", "phpmd src/ text cleancode,codesize,controversial,design,naming,unusedcode", "phpcpd src/" ] }: This line defines custom scripts that can be run using the composer run-script command. In this example, there are two scripts defined: test, which runs the PHPUnit tests in the tests directory, and build, which runs several commands in sequence to build the project, including generating code coverage reports, checking code style with PHP_CodeSniffer, running PHP Mess Detector, and detecting duplicated code with PHP Copy/Paste Detector.
  • "config": { "sort-packages": true }: This line specifies the configuration options for Composer. In this example, it enables sorting of the packages in the composer.lock file for consistency.
Rajesh Kumar
Follow me
Latest posts by Rajesh Kumar (see all)
Subscribe
Notify of
guest
0 Comments
Newest
Oldest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x