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
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 $composer->exists()
$composer->install()
<?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()
. $composer->installPackages()
<?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 $composer->rawCommand($command)
<?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.