Skip to content

[11.x] Add format artisan command #52572

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 79 additions & 0 deletions src/Illuminate/Foundation/Console/FormatCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
<?php

namespace Illuminate\Foundation\Console;

use Exception;
use Illuminate\Console\Command;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Process\Process;

#[AsCommand(name: 'format')]
class FormatCommand extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'format
{path? : The path to the file or directory to format}
{--test : Run Pint in test mode}
{--dirty : Only modify uncommitted files}';

/**
* The console command name.
*
* @var string
*/
protected $name = 'format';

/**
* The console command description.
*
* @var string
*/
protected $description = 'Format the application code using Laravel Pint';

/**
* Execute the console command.
*
* @return int
*/
public function handle()
{
try {
$command = ['./vendor/bin/pint'];

if ($this->option('test')) {
$command[] = '--test';
}

if ($this->option('dirty')) {
$command[] = '--dirty';
}

$path = $this->argument('path');
if ($path) {
$command[] = $path;
}

$process = new Process($command);
$process->setTimeout(null);

if (Process::isTtySupported()) {
$process->setTty(true);
}

$process->run();

return 0;
} catch (Exception $e) {
$this->components->error(sprintf(
'Failed to format the application: %s.',
$e->getMessage(),
));

return 1;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
use Illuminate\Foundation\Console\EventListCommand;
use Illuminate\Foundation\Console\EventMakeCommand;
use Illuminate\Foundation\Console\ExceptionMakeCommand;
use Illuminate\Foundation\Console\FormatCommand;
use Illuminate\Foundation\Console\InterfaceMakeCommand;
use Illuminate\Foundation\Console\JobMakeCommand;
use Illuminate\Foundation\Console\KeyGenerateCommand;
Expand Down Expand Up @@ -222,6 +223,7 @@ class ArtisanServiceProvider extends ServiceProvider implements DeferrableProvid
'TraitMake' => TraitMakeCommand::class,
'VendorPublish' => VendorPublishCommand::class,
'ViewMake' => ViewMakeCommand::class,
'Format' => FormatCommand::class,
];

/**
Expand Down