Skip to content

Commit e1db268

Browse files
committed
composer can tell us the bin paths
1 parent 21ab33c commit e1db268

File tree

3 files changed

+98
-5
lines changed

3 files changed

+98
-5
lines changed

src/Command/MakerCommand.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,8 @@ protected function interact(InputInterface $input, OutputInterface $output): voi
9595

9696
protected function execute(InputInterface $input, OutputInterface $output): int
9797
{
98+
$output->write($this->linter->getLinterUserMessage());
99+
98100
$this->maker->generate($input, $this->io, $this->generator);
99101

100102
// sanity check for custom makers

src/Util/TemplateLinter.php

Lines changed: 52 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@
2424
*/
2525
final class TemplateLinter
2626
{
27+
private bool $usingBundledPhpCsFixer = true;
28+
private bool $usingBundledPhpCsFixerConfig = true;
29+
2730
public function __construct(
2831
private ?string $phpCsFixerBinaryPath = null,
2932
private ?string $phpCsFixerConfigPath = null,
@@ -58,25 +61,69 @@ public function lintPhpTemplate(string|array $templateFilePath): void
5861
}
5962
}
6063

64+
public function getLinterUserMessage(): array
65+
{
66+
$message = [];
67+
$message[0] = 'Linting Generated Files With:'.\PHP_EOL;
68+
$message[1] = $this->usingBundledPhpCsFixer ?
69+
'Bundled PHP-CS-Fixer & ' :
70+
sprintf('System PHP-CS-Fixer (<info>%s</info>) & ', $this->phpCsFixerBinaryPath)
71+
72+
;
73+
74+
$message[1] .= $this->usingBundledPhpCsFixerConfig ?
75+
'Bundled PHP-CS-Fixer Configuration'.\PHP_EOL :
76+
sprintf('System PHP-CS-Fixer Configuration (<info>%s</info>)', $this->phpCsFixerConfigPath).\PHP_EOL
77+
;
78+
79+
$message[] = \PHP_EOL;
80+
81+
return $message;
82+
}
83+
6184
private function setBinary(): void
6285
{
6386
if (null !== $this->phpCsFixerBinaryPath) {
6487
$this->checkPathExists($this->phpCsFixerBinaryPath, false);
6588

89+
$this->usingBundledPhpCsFixer = false;
90+
6691
return;
6792
}
6893

69-
$this->phpCsFixerBinaryPath = (new ExecutableFinder())->find(
70-
'php-cs-fixer',
71-
\dirname(__DIR__).'/Resources/bin/php-cs-fixer-v3.13.0.phar'
94+
$finder = new ExecutableFinder();
95+
$finder->addSuffix('.phar');
96+
97+
($process = Process::fromShellCommandline('composer config bin-dir && composer global config bin-dir --absolute'))
98+
->run();
99+
100+
$composerBinPaths = explode(\PHP_EOL, $process->getOutput());
101+
102+
$this->phpCsFixerBinaryPath = $finder->find(
103+
name: 'php-cs-fixer',
104+
extraDirs: [
105+
...$composerBinPaths,
106+
'tools/php-cs-fixer/vendor/bin',
107+
'tools/php-cs-fixer/bin',
108+
]
72109
);
110+
111+
if (null !== $this->phpCsFixerBinaryPath) {
112+
$this->usingBundledPhpCsFixer = false;
113+
114+
return;
115+
}
116+
117+
$this->phpCsFixerBinaryPath = \dirname(__DIR__).'/Resources/bin/php-cs-fixer-v3.13.0.phar';
73118
}
74119

75120
private function setConfig(): void
76121
{
77122
if (null !== $this->phpCsFixerConfigPath) {
78123
$this->checkPathExists($this->phpCsFixerConfigPath, true);
79124

125+
$this->usingBundledPhpCsFixerConfig = false;
126+
80127
return;
81128
}
82129

@@ -85,6 +132,8 @@ private function setConfig(): void
85132
if (file_exists($defaultConfigPath)) {
86133
$this->phpCsFixerConfigPath = $defaultConfigPath;
87134

135+
$this->usingBundledPhpCsFixerConfig = false;
136+
88137
return;
89138
}
90139

tests/Maker/TemplateLinterTest.php

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,14 @@ public function getTestDetails(): \Generator
4747
);
4848

4949
// Voter class name
50-
$runner->runMaker(['FooBar']);
50+
$output = $runner->runMaker(['FooBar']);
5151

5252
$generatedTemplate = file_get_contents($runner->getPath('src/Security/Voter/FooBarVoter.php'));
5353

5454
self::assertStringContainsString('Linted by custom php-cs-config', $generatedTemplate);
55+
56+
$expectedOutput = 'System PHP-CS-Fixer (bin/php-cs-fixer) & System PHP-CS-Fixer Configuration (php-cs-fixer.test.php)';
57+
self::assertStringContainsString($expectedOutput, $output);
5558
}),
5659
];
5760

@@ -70,11 +73,50 @@ public function getTestDetails(): \Generator
7073
);
7174

7275
// Voter class name
73-
$runner->runMaker(['FooBar']);
76+
$output = $runner->runMaker(['FooBar']);
7477

7578
$generatedTemplate = file_get_contents($runner->getPath('src/Security/Voter/FooBarVoter.php'));
7679

7780
self::assertStringContainsString('Linted with stock php-cs-config', $generatedTemplate);
81+
82+
$expectedOutput = 'System PHP-CS-Fixer (bin/php-cs-fixer) & System PHP-CS-Fixer Configuration (.php-cs-fixer.dist.php)';
83+
self::assertStringContainsString($expectedOutput, $output);
84+
}),
85+
];
86+
87+
yield 'lints_templates_with_bundled_php_cs_fixer' => [$this->createMakerTest()
88+
->run(function (MakerTestRunner $runner) {
89+
// Voter class name
90+
$output = $runner->runMaker(['FooBar']);
91+
92+
$expectedOutput = 'Bundled PHP-CS-Fixer & Bundled PHP-CS-Fixer Configuration';
93+
self::assertStringContainsString($expectedOutput, $output);
94+
}),
95+
];
96+
97+
yield 'lints_templates_with_php_cs_fixer_in_tools_dir' => [$this->createMakerTest()
98+
->run(function (MakerTestRunner $runner) {
99+
$runner->writeFile('tools/php-cs-fixer/.gitignore', ''); // create the dir
100+
$runner->runProcess('composer require --working-dir=tools/php-cs-fixer friendsofphp/php-cs-fixer');
101+
102+
// Voter class name
103+
$output = $runner->runMaker(['FooBar']);
104+
105+
$expectedOutput = 'System PHP-CS-Fixer (tools/php-cs-fixer/bin/php-cs-fixer) & Bundled PHP-CS-Fixer Configuration';
106+
self::assertStringContainsString($expectedOutput, $output);
107+
}),
108+
];
109+
110+
yield 'lints_templates_with_composer_global_php_cs_fixer' => [$this->createMakerTest()
111+
->run(function (MakerTestRunner $runner) {
112+
$runner->runProcess('composer global require friendsofphp/php-cs-fixer');
113+
114+
// Voter class name
115+
$output = $runner->runMaker(['FooBar']);
116+
117+
// We don't know the prefix to the global "composer" dir. e.g. /your/system/composer/....
118+
$expectedOutput = 'composer/bin/php-cs-fixer) & Bundled PHP-CS-Fixer Configuration';
119+
self::assertStringContainsString($expectedOutput, $output);
78120
}),
79121
];
80122
}

0 commit comments

Comments
 (0)