Created
November 10, 2020 01:40
-
-
Save sam-ngu/9664da10d5593b304238d85752999b70 to your computer and use it in GitHub Desktop.
Laravel helper class to run composer command programmatically.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App; | |
class Composer extends \Illuminate\Support\Composer | |
{ | |
public function run(array $command) | |
{ | |
// findComposer() resolves to composer's binary | |
// $command is an array that looks like ['composer', 'some-composer-command'] | |
$command = array_merge($this->findComposer(), $command); | |
// we then pass the command array to getProcess() | |
// getProcess() returns a Symfony Process() instance, which runs the command for us in the shell | |
// the run() method execute the composer command | |
$this->getProcess($command)->run(function ($type, $data) { | |
// $type can be 'err' or 'out' | |
// 'err' when there is an error | |
// 'out' is stdout from the command | |
// $data is the command output | |
// ie whatever composer spits out when the command runs. | |
echo $data; | |
}, [ | |
// we can pass in env var to the process instance here | |
// setting any additional environmental variable to the process | |
'COMPOSER_HOME' => '$HOME/.config/composer' | |
]); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment