Skip to content

Commit 06dca19

Browse files
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 4efb0b2 commit 06dca19

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
{
@@ -61,8 +63,58 @@ protected function configure()
6163
*/
6264
public function handle($fixCode, $elaborateSummary)
6365
{
66+
if ($this->hasStdinInput()) {
67+
return $this->fixStdinInput($fixCode);
68+
}
69+
6470
[$totalFiles, $changes] = $fixCode->execute();
6571

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

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)