Skip to content

Commit 6e42c83

Browse files
calebdwtaylorotwell
authored andcommitted
feat: add support for formatting code from stdin (#390)
* feat: add support for formatting code from stdin * Formatting --------- Co-authored-by: Taylor Otwell <[email protected]>
1 parent 5a97b1a commit 6e42c83

File tree

2 files changed

+119
-0
lines changed

2 files changed

+119
-0
lines changed

app/Commands/DefaultCommand.php

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@
22

33
namespace App\Commands;
44

5+
use App\Actions\FixCode;
56
use LaravelZero\Framework\Commands\Command;
67
use Symfony\Component\Console\Input\InputArgument;
78
use Symfony\Component\Console\Input\InputOption;
9+
use Throwable;
810

911
class DefaultCommand extends Command
1012
{
@@ -62,8 +64,58 @@ protected function configure()
6264
*/
6365
public function handle($fixCode, $elaborateSummary)
6466
{
67+
if ($this->hasStdinInput()) {
68+
return $this->fixStdinInput($fixCode);
69+
}
70+
6571
[$totalFiles, $changes] = $fixCode->execute();
6672

6773
return $elaborateSummary->execute($totalFiles, $changes);
6874
}
75+
76+
/**
77+
* Fix the code sent to Pint on stdin and output to stdout.
78+
*/
79+
protected function fixStdinInput(FixCode $fixCode): int
80+
{
81+
$paths = $this->argument('path');
82+
83+
$contextPath = ! empty($paths) ? $paths[0] : 'stdin.php';
84+
$tempFile = sys_get_temp_dir().DIRECTORY_SEPARATOR.'pint_stdin_'.uniqid().'.php';
85+
86+
$this->input->setArgument('path', [$tempFile]);
87+
$this->input->setOption('format', 'json');
88+
89+
try {
90+
file_put_contents($tempFile, stream_get_contents(STDIN));
91+
$fixCode->execute();
92+
fwrite(STDOUT, file_get_contents($tempFile));
93+
94+
return self::SUCCESS;
95+
} catch (Throwable $e) {
96+
fwrite(STDERR, "pint: error processing {$contextPath}: {$e->getMessage()}\n");
97+
98+
return self::FAILURE;
99+
} finally {
100+
if (file_exists($tempFile)) {
101+
@unlink($tempFile);
102+
}
103+
}
104+
}
105+
106+
/**
107+
* Determine if there is input available on stdin.
108+
*/
109+
protected function hasStdinInput(): bool
110+
{
111+
if ($this->option('test') || $this->option('bail') || $this->option('repair')) {
112+
return false;
113+
}
114+
115+
if (! is_resource(STDIN) || stream_isatty(STDIN)) {
116+
return false;
117+
}
118+
119+
return ! stream_get_meta_data(STDIN)['eof'];
120+
}
69121
}

tests/Feature/StdinTest.php

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
<?php
2+
3+
use Illuminate\Support\Facades\Process;
4+
5+
it('formats code from stdin', function (string $input, ?string $expected) {
6+
$result = Process::input($input)->run('php pint app/Test.php')->throw();
7+
8+
expect($result)
9+
->output()->toBe($expected ?? $input)
10+
->errorOutput()->toBe('');
11+
})->with([
12+
'basic array and conditional' => [
13+
<<<'PHP'
14+
<?php
15+
$array = array("a","b");
16+
if($condition==true){
17+
echo "test";
18+
}
19+
PHP,
20+
<<<'PHP'
21+
<?php
22+
23+
$array = ['a', 'b'];
24+
if ($condition == true) {
25+
echo 'test';
26+
}
27+
28+
PHP,
29+
],
30+
'class with method' => [
31+
<<<'PHP'
32+
<?php
33+
class Test{
34+
public function method(){
35+
return array("key"=>"value");
36+
}
37+
}
38+
PHP,
39+
<<<'PHP'
40+
<?php
41+
42+
class Test
43+
{
44+
public function method()
45+
{
46+
return ['key' => 'value'];
47+
}
48+
}
49+
50+
PHP,
51+
],
52+
'already formatted code' => [
53+
<<<'PHP'
54+
<?php
55+
56+
class AlreadyFormatted
57+
{
58+
public function method()
59+
{
60+
return ['key' => 'value'];
61+
}
62+
}
63+
64+
PHP,
65+
null,
66+
],
67+
]);

0 commit comments

Comments
 (0)