Skip to content

Commit 86e1b52

Browse files
authored
Merge pull request #390 from asgrim/audit-stdout-stderr
Replace Symfony Console output with ConsoleIO
2 parents f73cc5b + f24fa3a commit 86e1b52

File tree

83 files changed

+775
-679
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

83 files changed

+775
-679
lines changed

phpstan-baseline.neon

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ parameters:
2121
-
2222
message: '#^Cannot cast mixed to string\.$#'
2323
identifier: cast.string
24-
count: 2
24+
count: 1
2525
path: src/Command/InstallExtensionsForProjectCommand.php
2626

2727
-

src/Building/Build.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@
44

55
namespace Php\Pie\Building;
66

7+
use Composer\IO\IOInterface;
78
use Php\Pie\Downloading\DownloadedPackage;
89
use Php\Pie\File\BinaryFile;
910
use Php\Pie\Platform\TargetPhp\PhpizePath;
1011
use Php\Pie\Platform\TargetPlatform;
11-
use Symfony\Component\Console\Output\OutputInterface;
1212

1313
/** @internal This is not public API for PIE, so should not be depended upon unless you accept the risk of BC breaks */
1414
interface Build
@@ -18,7 +18,7 @@ public function __invoke(
1818
DownloadedPackage $downloadedPackage,
1919
TargetPlatform $targetPlatform,
2020
array $configureOptions,
21-
OutputInterface $output,
21+
IOInterface $io,
2222
PhpizePath|null $phpizePath,
2323
): BinaryFile;
2424
}

src/Building/UnixBuild.php

Lines changed: 42 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,14 @@
44

55
namespace Php\Pie\Building;
66

7+
use Composer\IO\IOInterface;
78
use Php\Pie\ComposerIntegration\BundledPhpExtensionsRepository;
89
use Php\Pie\Downloading\DownloadedPackage;
910
use Php\Pie\File\BinaryFile;
1011
use Php\Pie\Platform\TargetPhp\PhpizePath;
1112
use Php\Pie\Platform\TargetPlatform;
1213
use Php\Pie\Util\Process;
1314
use Php\Pie\Util\ProcessFailedWithLimitedOutput;
14-
use Symfony\Component\Console\Output\OutputInterface;
1515
use Symfony\Component\Process\Exception\ProcessFailedException;
1616
use Symfony\Component\Process\Process as SymfonyProcess;
1717

@@ -35,13 +35,13 @@ public function __invoke(
3535
DownloadedPackage $downloadedPackage,
3636
TargetPlatform $targetPlatform,
3737
array $configureOptions,
38-
OutputInterface $output,
38+
IOInterface $io,
3939
PhpizePath|null $phpizePath,
4040
): BinaryFile {
4141
$outputCallback = null;
42-
if ($output->isVerbose()) {
43-
$outputCallback = static function (string $type, string $outputMessage) use ($output): void {
44-
$output->write(sprintf(
42+
if ($io->isVerbose()) {
43+
$outputCallback = static function (string $type, string $outputMessage) use ($io): void {
44+
$io->write(sprintf(
4545
'%s%s%s',
4646
$type === SymfonyProcess::ERR ? '<comment>' : '',
4747
$outputMessage,
@@ -58,29 +58,29 @@ public function __invoke(
5858
* already clean anyway; however, sometimes we want to rebuild the
5959
* current ext, so this will perform a clean first
6060
*/
61-
$this->cleanup($phpizePath, $downloadedPackage, $output, $outputCallback);
61+
$this->cleanup($phpizePath, $downloadedPackage, $io, $outputCallback);
6262

6363
$this->phpize(
6464
$phpizePath,
6565
$downloadedPackage,
66-
$output,
66+
$io,
6767
$outputCallback,
6868
);
6969

70-
$output->writeln('<info>phpize complete</info>.');
70+
$io->write('<info>phpize complete</info>.');
7171

7272
$phpConfigPath = $targetPlatform->phpBinaryPath->phpConfigPath();
7373
if ($phpConfigPath !== null) {
7474
$configureOptions[] = '--with-php-config=' . $phpConfigPath;
7575
}
7676

77-
$this->configure($downloadedPackage, $configureOptions, $output, $outputCallback);
77+
$this->configure($downloadedPackage, $configureOptions, $io, $outputCallback);
7878

7979
$optionsOutput = count($configureOptions) ? ' with options: ' . implode(' ', $configureOptions) : '.';
80-
$output->writeln('<info>Configure complete</info>' . $optionsOutput);
80+
$io->write('<info>Configure complete</info>' . $optionsOutput);
8181

8282
try {
83-
$this->make($targetPlatform, $downloadedPackage, $output, $outputCallback);
83+
$this->make($targetPlatform, $downloadedPackage, $io, $outputCallback);
8484
} catch (ProcessFailedException $p) {
8585
throw ProcessFailedWithLimitedOutput::fromProcessFailedException($p);
8686
}
@@ -91,26 +91,26 @@ public function __invoke(
9191
throw ExtensionBinaryNotFound::fromExpectedBinary($expectedSoFile);
9292
}
9393

94-
$output->writeln(sprintf(
94+
$io->write(sprintf(
9595
'<info>Build complete:</info> %s',
9696
$expectedSoFile,
9797
));
9898

9999
return BinaryFile::fromFileWithSha256Checksum($expectedSoFile);
100100
}
101101

102-
private function renamesToConfigM4(DownloadedPackage $downloadedPackage, OutputInterface $output): void
102+
private function renamesToConfigM4(DownloadedPackage $downloadedPackage, IOInterface $io): void
103103
{
104104
$configM4 = $downloadedPackage->extractedSourcePath . DIRECTORY_SEPARATOR . 'config.m4';
105105
if (file_exists($configM4)) {
106106
return;
107107
}
108108

109-
$output->writeln('config.m4 does not exist; checking alternatives', OutputInterface::VERBOSITY_VERY_VERBOSE);
109+
$io->write('config.m4 does not exist; checking alternatives', verbosity: IOInterface::VERY_VERBOSE);
110110
foreach (['config0.m4', 'config9.m4'] as $alternateConfigM4) {
111111
$fullPathToAlternate = $downloadedPackage->extractedSourcePath . DIRECTORY_SEPARATOR . $alternateConfigM4;
112112
if (file_exists($fullPathToAlternate)) {
113-
$output->writeln(sprintf('Renaming %s to config.m4', $alternateConfigM4), OutputInterface::VERBOSITY_VERY_VERBOSE);
113+
$io->write(sprintf('Renaming %s to config.m4', $alternateConfigM4), verbosity: IOInterface::VERY_VERBOSE);
114114
rename($fullPathToAlternate, $configM4);
115115

116116
return;
@@ -122,16 +122,17 @@ private function renamesToConfigM4(DownloadedPackage $downloadedPackage, OutputI
122122
private function phpize(
123123
PhpizePath $phpize,
124124
DownloadedPackage $downloadedPackage,
125-
OutputInterface $output,
125+
IOInterface $io,
126126
callable|null $outputCallback,
127127
): void {
128128
$phpizeCommand = [$phpize->phpizeBinaryPath];
129129

130-
if ($output->isVerbose()) {
131-
$output->writeln('<comment>Running phpize step using: ' . implode(' ', $phpizeCommand) . '</comment>');
132-
}
130+
$io->write(
131+
'<comment>Running phpize step using: ' . implode(' ', $phpizeCommand) . '</comment>',
132+
verbosity: IOInterface::VERBOSE,
133+
);
133134

134-
$this->renamesToConfigM4($downloadedPackage, $output);
135+
$this->renamesToConfigM4($downloadedPackage, $io);
135136

136137
Process::run(
137138
$phpizeCommand,
@@ -148,14 +149,15 @@ private function phpize(
148149
private function configure(
149150
DownloadedPackage $downloadedPackage,
150151
array $configureOptions,
151-
OutputInterface $output,
152+
IOInterface $io,
152153
callable|null $outputCallback,
153154
): void {
154155
$configureCommand = ['./configure', ...$configureOptions];
155156

156-
if ($output->isVerbose()) {
157-
$output->writeln('<comment>Running configure step with: ' . implode(' ', $configureCommand) . '</comment>');
158-
}
157+
$io->write(
158+
'<comment>Running configure step with: ' . implode(' ', $configureCommand) . '</comment>',
159+
verbosity: IOInterface::VERBOSE,
160+
);
159161

160162
Process::run(
161163
$configureCommand,
@@ -169,13 +171,13 @@ private function configure(
169171
private function make(
170172
TargetPlatform $targetPlatform,
171173
DownloadedPackage $downloadedPackage,
172-
OutputInterface $output,
174+
IOInterface $io,
173175
callable|null $outputCallback,
174176
): void {
175177
$makeCommand = ['make'];
176178

177179
if ($targetPlatform->makeParallelJobs === 1) {
178-
$output->writeln('Running make without parallelization - try providing -jN to PIE where N is the number of cores you have.');
180+
$io->write('Running make without parallelization - try providing -jN to PIE where N is the number of cores you have.');
179181
} else {
180182
$makeCommand[] = sprintf('-j%d', $targetPlatform->makeParallelJobs);
181183
}
@@ -185,9 +187,10 @@ private function make(
185187
$downloadedPackage,
186188
);
187189

188-
if ($output->isVerbose()) {
189-
$output->writeln('<comment>Running make step with: ' . implode(' ', $makeCommand) . '</comment>');
190-
}
190+
$io->write(
191+
'<comment>Running make step with: ' . implode(' ', $makeCommand) . '</comment>',
192+
verbosity: IOInterface::VERBOSE,
193+
);
191194

192195
Process::run(
193196
$makeCommand,
@@ -201,7 +204,7 @@ private function make(
201204
private function cleanup(
202205
PhpizePath $phpize,
203206
DownloadedPackage $downloadedPackage,
204-
OutputInterface $output,
207+
IOInterface $io,
205208
callable|null $outputCallback,
206209
): void {
207210
/**
@@ -210,18 +213,20 @@ private function cleanup(
210213
* configure script manually...
211214
*/
212215
if (! file_exists($downloadedPackage->extractedSourcePath . '/configure')) {
213-
if ($output->isVerbose()) {
214-
$output->writeln('<comment>Skipping phpize --clean, configure does not exist</comment>');
215-
}
216+
$io->write(
217+
'<comment>Skipping phpize --clean, configure does not exist</comment>',
218+
verbosity: IOInterface::VERBOSE,
219+
);
216220

217221
return;
218222
}
219223

220224
$phpizeCleanCommand = [$phpize->phpizeBinaryPath, '--clean'];
221225

222-
if ($output->isVerbose()) {
223-
$output->writeln('<comment>Running phpize --clean step using: ' . implode(' ', $phpizeCleanCommand) . '</comment>');
224-
}
226+
$io->write(
227+
'<comment>Running phpize --clean step using: ' . implode(' ', $phpizeCleanCommand) . '</comment>',
228+
verbosity: IOInterface::VERBOSE,
229+
);
225230

226231
Process::run(
227232
$phpizeCleanCommand,
@@ -230,6 +235,6 @@ private function cleanup(
230235
$outputCallback,
231236
);
232237

233-
$output->writeln('<info>Build files cleaned up.</info>');
238+
$io->write('<info>Build files cleaned up.</info>');
234239
}
235240
}

src/Building/WindowsBuild.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@
44

55
namespace Php\Pie\Building;
66

7+
use Composer\IO\IOInterface;
78
use Php\Pie\Downloading\DownloadedPackage;
89
use Php\Pie\File\BinaryFile;
910
use Php\Pie\Platform\TargetPhp\PhpizePath;
1011
use Php\Pie\Platform\TargetPlatform;
1112
use Php\Pie\Platform\WindowsExtensionAssetName;
12-
use Symfony\Component\Console\Output\OutputInterface;
1313

1414
use function sprintf;
1515

@@ -21,12 +21,12 @@ public function __invoke(
2121
DownloadedPackage $downloadedPackage,
2222
TargetPlatform $targetPlatform,
2323
array $configureOptions,
24-
OutputInterface $output,
24+
IOInterface $io,
2525
PhpizePath|null $phpizePath,
2626
): BinaryFile {
2727
$prebuiltDll = WindowsExtensionAssetName::determineDllName($targetPlatform, $downloadedPackage);
2828

29-
$output->writeln(sprintf(
29+
$io->write(sprintf(
3030
'<info>Nothing to do on Windows, prebuilt DLL found:</info> %s',
3131
$prebuiltDll,
3232
));

src/Command/BuildCommand.php

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace Php\Pie\Command;
66

7+
use Composer\IO\IOInterface;
78
use Php\Pie\ComposerIntegration\ComposerIntegrationHandler;
89
use Php\Pie\ComposerIntegration\ComposerRunFailed;
910
use Php\Pie\ComposerIntegration\PieComposerFactory;
@@ -33,6 +34,7 @@ public function __construct(
3334
private readonly DependencyResolver $dependencyResolver,
3435
private readonly ComposerIntegrationHandler $composerIntegrationHandler,
3536
private readonly FindMatchingPackages $findMatchingPackages,
37+
private readonly IOInterface $io,
3638
) {
3739
parent::__construct();
3840
}
@@ -46,14 +48,14 @@ public function configure(): void
4648

4749
public function execute(InputInterface $input, OutputInterface $output): int
4850
{
49-
$targetPlatform = CommandHelper::determineTargetPlatformFromInputs($input, $output);
51+
$targetPlatform = CommandHelper::determineTargetPlatformFromInputs($input, $this->io);
5052
try {
5153
$requestedNameAndVersion = CommandHelper::requestedNameAndVersionPair($input);
5254
} catch (InvalidPackageName $invalidPackageName) {
5355
return CommandHelper::handlePackageNotFound(
5456
$invalidPackageName,
5557
$this->findMatchingPackages,
56-
$output,
58+
$this->io,
5759
$targetPlatform,
5860
$this->container,
5961
);
@@ -64,7 +66,7 @@ public function execute(InputInterface $input, OutputInterface $output): int
6466
$composer = PieComposerFactory::createPieComposer(
6567
$this->container,
6668
new PieComposerRequest(
67-
$output,
69+
$this->io,
6870
$targetPlatform,
6971
$requestedNameAndVersion,
7072
PieOperation::Resolve,
@@ -85,18 +87,18 @@ public function execute(InputInterface $input, OutputInterface $output): int
8587
return CommandHelper::handlePackageNotFound(
8688
$unableToResolveRequirement,
8789
$this->findMatchingPackages,
88-
$output,
90+
$this->io,
8991
$targetPlatform,
9092
$this->container,
9193
);
9294
} catch (BundledPhpExtensionRefusal $bundledPhpExtensionRefusal) {
93-
$output->writeln('');
94-
$output->writeln('<comment>' . $bundledPhpExtensionRefusal->getMessage() . '</comment>');
95+
$this->io->writeError('');
96+
$this->io->writeError('<comment>' . $bundledPhpExtensionRefusal->getMessage() . '</comment>');
9597

9698
return self::INVALID;
9799
}
98100

99-
$output->writeln(sprintf('<info>Found package:</info> %s which provides <info>%s</info>', $package->prettyNameAndVersion(), $package->extensionName()->nameWithExtPrefix()));
101+
$this->io->write(sprintf('<info>Found package:</info> %s which provides <info>%s</info>', $package->prettyNameAndVersion(), $package->extensionName()->nameWithExtPrefix()));
100102

101103
// Now we know what package we have, we can validate the configure options for the command and re-create the
102104
// Composer instance with the populated configure options
@@ -106,7 +108,7 @@ public function execute(InputInterface $input, OutputInterface $output): int
106108
$composer = PieComposerFactory::createPieComposer(
107109
$this->container,
108110
new PieComposerRequest(
109-
$output,
111+
$this->io,
110112
$targetPlatform,
111113
$requestedNameAndVersion,
112114
PieOperation::Build,
@@ -126,7 +128,7 @@ public function execute(InputInterface $input, OutputInterface $output): int
126128
false,
127129
);
128130
} catch (ComposerRunFailed $composerRunFailed) {
129-
$output->writeln('<error>' . $composerRunFailed->getMessage() . '</error>');
131+
$this->io->writeError('<error>' . $composerRunFailed->getMessage() . '</error>');
130132

131133
return $composerRunFailed->getCode();
132134
}

0 commit comments

Comments
 (0)