I would like to copy some file located inside a package after I installed this package from Composer.
Actually, I would like that, after I install or update a package from Composer, copy some file which might be inside the downloaded package to another directory. I use scripts, with the post-package-install and post-package-update command, but I do not find how to get the install path.
This is my current script :
use Composer\Script\PackageEvent;
class MyScript {
public static function copyFiles(PackageEvent $event)
{
$package = $event->getOperation()->getPackage();
$originDir = $package->someFunctionToFind(); #Here, I should retrieve the install dir
if (file_exists($originDir) && is_dir($originDir)) {
//copy files from $originDir to a new location
}
}
}
Does anybody knows how to get the install dir of the installed/updated package from the PackageEvent class (which is provided in parameter) ?
NOTE :
I tryed $event->getOperation()->getPackage->targetDir()
but this does not provide the install path, but the targetDir of the package, defined in composer.json
You can check your installed composer version using a command composer -v at the current path. Such as: composer -v.
An Introduction to PHP Composer Composer manages packages and libraries on a per-project basis, so it is more accurately termed a dependency manager. All libraries are installed in a designated directory inside the project directory. Composer does not install any packages globally.
To configure Composer for your PHP app json file specifies required packages. Verify that a composer. json file is present in the root of your git repository. Run composer install (on your local machine) to install the required packages and generate a composer.
composer install is primarily used in the 'deploying phase' to install our application on a production server or on a testing environment, using the same dependencies stored in the composer. lock file created by composer update.
I could get the install path with the Composer\Installation\InstallationManager::getInstallPath method.
Theoric answer :
use Composer\Script\PackageEvent;
class MyScript {
public static function copyFiles(PackageEvent $event)
{
$package = $event->getOperation()->getPackage();
$installationManager = $event->getComposer()->getInstallationManager();
$originDir = $installationManager->getInstallPath($package);
if (file_exists($originDir) && is_dir($originDir)) {
//copy files from $originDir to a new location
}
}
}
But this answer is theoric because I could not find a soluce to debug my code without really installing a package (which was painful: I should have to remove a package, and reinstall it to check my code).
So I switch to post-install-cmd and post-update-cmd, and my come became :
use Composer\Script\CommandEvent; #the event is different !
class MyScript {
public static function copyFiles(CommandEvent $event)
{
// wet get ALL installed packages
$packages = $event->getComposer()->getRepositoryManager()
->getLocalRepository()->getPackages();
$installationManager = $event->getComposer()->getInstallationManager();
foreach ($packages as $package) {
$installPath = $installationManager->getInstallPath($package);
//do my process here
}
}
}
Do not forget to add the command to composer.json :
"scripts": {
"post-install-cmd": [
"MyScript::copyFiles"
],
"post-update-cmd": [
"MyScript::copyFiles"
]
}
To debug the code, I had to run composer.phar run-script post-install-cmd.
NOTE : this code should work with psr4. For psr0, it might be necessary to add a $package->targetDir() to get the correct install path. Feel free to comment or improve my answer.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With