Skip to content

Instantly share code, notes, and snippets.

@isimmons
Last active October 3, 2022 00:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save isimmons/19daa27f3d61ade41399f18806fd19c7 to your computer and use it in GitHub Desktop.
Save isimmons/19daa27f3d61ade41399f18806fd19c7 to your computer and use it in GitHub Desktop.
pointless artisan command to run vendor/bin/pint but was a good learning experience how to call php scripts from artisan. Borrowed code from artisan serve
<?php
namespace App\Console\Commands;
use Closure;
use Exception;
use Illuminate\Console\Command;
use Symfony\Component\Process\PhpExecutableFinder;
use Symfony\Component\Process\Process;
class Pint extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'pint';
/**
* The console command description.
*
* @var string
*/
protected $description = 'run pint as an artisan command';
/**
* Execute the console command.
*
* @return int
* @throws Exception
*/
public function handle(): int
{
$this->comment('Running pint');
$process = new Process($this->pintRunner(), base_path());
$process->start($this->handleProcessOutput());
while ($process->isRunning()) {
usleep(500 * 1000);
}
return $process->getExitCode();
}
/**
* @throws Exception
*/
public function pintRunner(): array
{
if (! file_exists(base_path('vendor/bin/pint'))) {
throw new Exception('You have to install laravel/pint you big dummy!');
}
$pint = base_path('vendor/bin/pint');
return [
(new PhpExecutableFinder)->find(false),
$pint,
];
}
public function handleProcessOutput(): Closure
{
return fn ($type, $buffer) => str($buffer)->explode("\n")->each(function ($line) {
if ($line) {
//dump($line);
if (str($line)->contains('PASS')) {
$this->output->write(
"<fg=white;bg=bright-green>{$line}</>"
);
$this->newLine();
} elseif (str($line)->contains('FIXED')) {
$this->output->write(
"<fg=white;bg=bright-yellow>{$line}</>"
);
$this->newLine();
} elseif (str($line)->contains('Laravel')) {
$this->newLine();
$this->output->writeln("<fg=gray>{$line}</>");
} elseif (str($line)->contains('.') || str($line)->contains('✓')) {
$this->output->write("<fg=bright-green>{$line}</>");
} else {
$this->newLine();
}
}
});
}
}
@isimmons
Copy link
Author

isimmons commented Oct 2, 2022

It's pointless since you can run 'pint' from the cli but I wanted to figure out how to make an artisan command that can execute a php script.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment