Introducing Project-Level Composer.

Composer is an application-level package manager for the PHP programming language that provides a standard format for managing dependencies of PHP software and required libraries. It was developed by Nils Adermann and Jordi Boggiano, who continue to manage the project.

Composer is heavily inspired by NPM for Node JS and Bundler for Ruby. Although, Composer is a must have package manager for the PHP community but some hosting providers do not have them preinstalled in the server. Therefore, leading the developers to the frustration of doing the package management locally in their own system and uploading the files later to the server.

To save all the PHP community developers from this hustle and to contribute to the Open-sourced world, I have developed a library (Project-Level Composer) that can integrate with any PHP Project and binds itself with the local installation of the composer in the project if it already exists. On top of it, this library does not require you to have access to the Terminal (SSH) of the server since it provides a Nice PHP-Class interface for the Composer commands so you execute the commands using your PHP files itself.

Installation

To install Project-Level Composer library into your project, simply download all the files and place it inside your PHP project.

Usage

To use this library just require the Composer.php file available in the Project-Level Composer library you just downloaded in the file you want to use the Composer commands and create an instance of the Composer by providing the path as an argument to the Root Directory of the application where Composer.json file is located and the Bin Directory where Composer should be installed inside the project. Although, providing the Bin Directory Path is optional and when not supplied this library automatically creates a Bin Directory inside the Root Directory of the project.

<?php

require_once 'Path\To\Composer.php';

// $composer = new \abhishek6262\Composer\Composer('pathToRootDirectory', 'pathToBinDirectory');
$composer = new \abhishek6262\Composer\Composer(__DIR__);

Finally, now all the Composer commands can be executed from the $composer variable we just set the Composer instance to in the code above using the class methods syntax.

Install Composer

You can verify whether the composer has already been installed in the project by using $composer->exists() method and if not already installed then You can always install it using $composer->install() method.

<?php

$composer = new \abhishek6262\Composer\Composer(__DIR__);

if (! $composer->exists()) {
    $composer->install();
}

Install Composer Packages

You will always need the packages in your application to ease your work and this library provides an easy-to-go implementation for it. It can verify whether the packages exist in the Composer.json file have been installed in the project already. You can simply install the packages using the command $composer->installPackages(). The $composer->installPackages() method returns the instance of terminal response with the output and status code so you can validate whether the commands ran successfully.

<?php

$composer = new \abhishek6262\Composer\Composer(__DIR__);

if ($composer->packagesExists() && ! $composer->packagesInstalled()) {
    $response = $composer->installPackages();

    if ($response->statusCode() == '0') {
        echo "Packages successfully installed.";
    } else {
        echo "Packages failed to install.";
    }

    print_r($response->output());
}

Additional Commands

This library provides you an extra method $composer->rawCommand($command) that takes the command as an argument to run the composer commands whose methods are not yet available in the library.

<?php

$composer = new \abhishek6262\Composer\Composer(__DIR__);

$response = $composer->rawCommand('-v');

print_r($response->output());

Conclusion

The Project-Level Composer library is a must have library for any PHP project that needs to deal with Composer. Not only it solves the problem of running Composer commands through Terminal but it solves the issue of manually having to install Composer for the projects and frameworks that the developers and users often have to.

That’s it for Project-Level Composer. Let me know about your experience with the library in the comments below. Moreover, if you’re looking for the same implementation for Node JS, NPM or NPX then look no further than NodePHP (A Project-Level Node JS) library.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.