Skip to content
Merged
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
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -366,7 +366,9 @@ return (new PhpCsFixer\Config())
// A codec for handling placeholers in template strings; depends on the content being formatted.
interpolationCodec: new PlainStringCodec(),
// A normalizer for handling end-of-line characters.
lineEndingNormalizer: null
lineEndingNormalizer: null,
// Factory for creating processes. Defaults to Symfony process factory.
processFactory: null,
)
]),
]);
Expand Down Expand Up @@ -403,6 +405,8 @@ return (new PhpCsFixer\Config())
interpolationCodec: new PlainStringCodec(),
// A normalizer for handling end-of-line characters.
lineEndingNormalizer: null,
// Factory for creating processes. Defaults to Symfony process factory.
processFactory: null,
)
]),
]);
Expand Down
50 changes: 19 additions & 31 deletions src/Formatter/CliPipeFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@

namespace uuf6429\PhpCsFixerBlockstring\Formatter;

use Symfony\Component\Process\Process;
use uuf6429\PhpCsFixerBlockstring\InterpolationCodec\CodecInterface;
use uuf6429\PhpCsFixerBlockstring\LineEndingNormalizer\DefaultNormalizer;
use uuf6429\PhpCsFixerBlockstring\LineEndingNormalizer\NormalizerInterface;
use uuf6429\PhpCsFixerBlockstring\Process\ProcessFactoryInterface;
use uuf6429\PhpCsFixerBlockstring\Process\SymfonyProcessFactory;

/**
* It's no secret that the best formatting tools are not directly available in PHP. This formatter off-loads formatting
Expand All @@ -26,7 +27,9 @@
* // A codec for handling placeholers in template strings; depends on the content being formatted.
* interpolationCodec: new PlainStringCodec(),
* // A normalizer for handling end-of-line characters.
* lineEndingNormalizer: null
* lineEndingNormalizer: null,
* // Factory for creating processes. Defaults to Symfony process factory.
* processFactory: null,
* )
* ]),
* ]);
Expand All @@ -49,32 +52,25 @@ class CliPipeFormatter extends AbstractStringFormatter
*/
private array $formatter;

/**
* @readonly
*/
private ProcessFactoryInterface $processFactory;

/**
* @param TVersion|TCommand $versionValueOrCommand Either the version (as a string) or a command to retrieve the
* version (as an array).
* @param TCommand $formatCommand A command, as an array, to perform the formatting.
* @param null|bool|NormalizerInterface $lineEndingNormalizer
*/
public function __construct(
$versionValueOrCommand,
array $formatCommand,
?CodecInterface $interpolationCodec = null,
$lineEndingNormalizer = false
?NormalizerInterface $lineEndingNormalizer = null,
?ProcessFactoryInterface $processFactory = null
) {
$this->formatter = $formatCommand;

if (is_bool($lineEndingNormalizer)) {
trigger_deprecation(
'uuf6429/php-cs-fixer-blockstring',
'1.0.4',
'Passing a bool for argument $lineEndingNormalizer to %s is deprecated',
__METHOD__
);
$lineEndingNormalizer = new DefaultNormalizer(
DefaultNormalizer::LF,
$lineEndingNormalizer ? DefaultNormalizer::STRIP : DefaultNormalizer::NO_CHANGE
);
}
$this->processFactory = $processFactory ?? new SymfonyProcessFactory();

parent::__construct(
sprintf(
Expand All @@ -88,7 +84,7 @@ public function __construct(
: $this->exec($versionValueOrCommand, null)
),
$interpolationCodec,
$lineEndingNormalizer
$lineEndingNormalizer ?? new DefaultNormalizer(DefaultNormalizer::LF, DefaultNormalizer::NO_CHANGE)
);
}

Expand All @@ -97,23 +93,15 @@ public function __construct(
*/
protected function exec(array $spec, ?string $input): string
{
$process = is_array($spec['cmd'])
? new Process(
return $this->processFactory
->create(
$spec['cmd'],
$spec['cwd'] ?? null,
$spec['env'] ?? null,
$input,
null
$input
)
: Process::fromShellCommandline(
$spec['cmd'],
$spec['cwd'] ?? null,
$spec['env'] ?? null,
$input,
null
);

return $process->mustRun()->getOutput();
->mustRun()
->getOutput();
}

protected function formatContent(string $original): string
Expand Down
96 changes: 41 additions & 55 deletions src/Formatter/DockerPipeFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@

use InvalidArgumentException;
use RuntimeException;
use Symfony\Component\Process\Exception\ProcessFailedException;
use Symfony\Component\Process\Process;
use uuf6429\PhpCsFixerBlockstring\InterpolationCodec\CodecInterface;
use uuf6429\PhpCsFixerBlockstring\LineEndingNormalizer\DefaultNormalizer;
use uuf6429\PhpCsFixerBlockstring\LineEndingNormalizer\NormalizerInterface;
use uuf6429\PhpCsFixerBlockstring\Process\ProcessFactoryInterface;
use uuf6429\PhpCsFixerBlockstring\Process\ProcessFailedException;
use uuf6429\PhpCsFixerBlockstring\Process\SymfonyProcessFactory;

/**
* The minimal setup, stable repeatability, and a rich ecosystem make Docker images an ideal source of formatting
Expand All @@ -34,6 +35,8 @@
* interpolationCodec: new PlainStringCodec(),
* // A normalizer for handling end-of-line characters.
* lineEndingNormalizer: null,
* // Factory for creating processes. Defaults to Symfony process factory.
* processFactory: null,
* )
* ]),
* ]);
Expand Down Expand Up @@ -72,39 +75,32 @@ class DockerPipeFormatter extends AbstractStringFormatter
*/
private array $imageDetails;

/**
* @readonly
*/
private ProcessFactoryInterface $processFactory;

/**
* @param list<string> $options
* @param list<string> $command
* @param 'never'|'missing'|'always' $pullMode
* @param null|bool|NormalizerInterface $lineEndingNormalizer
*/
public function __construct(
string $image,
array $options = [],
array $command = [],
string $pullMode = 'never',
?CodecInterface $interpolationCodec = null,
$lineEndingNormalizer = true
string $image,
array $options = [],
array $command = [],
string $pullMode = 'never',
?CodecInterface $interpolationCodec = null,
?NormalizerInterface $lineEndingNormalizer = null,
?ProcessFactoryInterface $processFactory = null
) {
$this->image = $image;
$this->options = $options;
$this->command = $command;
$this->pullMode = $pullMode;
$this->processFactory = $processFactory ?? new SymfonyProcessFactory();
$this->imageDetails = $this->resolveImageDetails();

if (is_bool($lineEndingNormalizer)) {
trigger_deprecation(
'uuf6429/php-cs-fixer-blockstring',
'1.0.4',
'Passing a bool for argument $lineEndingNormalizer to %s is deprecated',
__METHOD__
);
$lineEndingNormalizer = new DefaultNormalizer(
DefaultNormalizer::LF,
$lineEndingNormalizer ? DefaultNormalizer::STRIP : DefaultNormalizer::NO_CHANGE
);
}

parent::__construct(
sprintf(
'%s: %s',
Expand All @@ -119,7 +115,7 @@ public function __construct(
)
),
$interpolationCodec,
$lineEndingNormalizer
$lineEndingNormalizer ?? new DefaultNormalizer(DefaultNormalizer::LF, DefaultNormalizer::STRIP)
);
}

Expand All @@ -139,7 +135,7 @@ private function resolveImageDetails(): array
}
$this->pullImage();
return $this->inspectImage(true);
// @codeCoverageIgnoreEnd
// @codeCoverageIgnoreEnd

case 'always':
$this->pullImage();
Expand All @@ -155,17 +151,12 @@ private function resolveImageDetails(): array
*/
private function inspectImage(bool $throwOnFailure): ?array
{
$process = new Process(
['docker', 'image', 'inspect', $this->image, '--format={{.Os}}/{{.Architecture}} {{.Id}}'],
null,
null,
null,
null
$process = $this->processFactory->create(
['docker', 'image', 'inspect', $this->image, '--format={{.Os}}/{{.Architecture}} {{.Id}}']
);
try {
$result = $process->mustRun()->getOutput();
$result = explode(' ', trim($result), 2);

return ['platform' => $result[0], 'digest' => $result[1]];
} catch (ProcessFailedException $ex) {
if (!$throwOnFailure) {
Expand All @@ -179,33 +170,28 @@ private function inspectImage(bool $throwOnFailure): ?array

private function pullImage(): void
{
(new Process(
['docker', 'image', 'pull', $this->image],
null,
null,
null,
null
))->mustRun();
$this->processFactory
->create(['docker', 'image', 'pull', $this->image])
->mustRun();
}

protected function formatContent(string $original): string
{
$process = new Process(
[
'docker',
'run',
'--rm',
'--interactive',
...$this->options,
$this->imageDetails['digest'],
...$this->command,
],
null,
null,
$original,
null
);

return $process->mustRun()->getOutput();
return $this->processFactory
->create(
[
'docker',
'run',
'--rm',
'--interactive',
...$this->options,
$this->imageDetails['digest'],
...$this->command,
],
null,
null,
$original
)->mustRun()
->getOutput();
}
}
26 changes: 10 additions & 16 deletions src/Formatter/WslPipeFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use uuf6429\PhpCsFixerBlockstring\InterpolationCodec\CodecInterface;
use uuf6429\PhpCsFixerBlockstring\LineEndingNormalizer\DefaultNormalizer;
use uuf6429\PhpCsFixerBlockstring\LineEndingNormalizer\NormalizerInterface;
use uuf6429\PhpCsFixerBlockstring\Process\ProcessFactoryInterface;

/**
* A formatter making use of Windows Subsystem for Linux (WSL). Of course, you will need to be running on Windows,
Expand All @@ -20,31 +21,24 @@ class WslPipeFormatter extends CliPipeFormatter

/**
* @param 'standard'|'login'|'none' $shellType
* @param null|bool|NormalizerInterface $lineEndingNormalizer
*/
public function __construct(
$versionValueOrCommand,
array $formatCommand,
?CodecInterface $interpolationCodec = null,
string $shellType = 'login',
$lineEndingNormalizer = true
?NormalizerInterface $lineEndingNormalizer = null,
?ProcessFactoryInterface $processFactory = null
) {
$this->shellType = $shellType;

if (is_bool($lineEndingNormalizer)) {
trigger_deprecation(
'uuf6429/php-cs-fixer-blockstring',
'1.0.4',
'Passing a bool for argument $lineEndingNormalizer to %s is deprecated',
__METHOD__
);
$lineEndingNormalizer = new DefaultNormalizer(
DefaultNormalizer::LF,
$lineEndingNormalizer ? DefaultNormalizer::STRIP : DefaultNormalizer::NO_CHANGE
);
}

parent::__construct($versionValueOrCommand, $formatCommand, $interpolationCodec, $lineEndingNormalizer);
parent::__construct(
$versionValueOrCommand,
$formatCommand,
$interpolationCodec,
$lineEndingNormalizer ?? new DefaultNormalizer(DefaultNormalizer::LF, DefaultNormalizer::STRIP),
$processFactory
);
}

protected function exec(array $spec, ?string $input): string
Expand Down
17 changes: 17 additions & 0 deletions src/Process/ProcessFactoryInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php declare(strict_types=1);

namespace uuf6429\PhpCsFixerBlockstring\Process;

interface ProcessFactoryInterface
{
/**
* @param string|list<string> $command
* @param null|array<string, string> $env
*/
public function create(
$command,
?string $cwd = null,
?array $env = null,
?string $input = null
): ProcessInterface;
}
10 changes: 10 additions & 0 deletions src/Process/ProcessFailedException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace uuf6429\PhpCsFixerBlockstring\Process;

use RuntimeException;

class ProcessFailedException extends RuntimeException
{

}
15 changes: 15 additions & 0 deletions src/Process/ProcessInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php declare(strict_types=1);

namespace uuf6429\PhpCsFixerBlockstring\Process;

interface ProcessInterface
{
/**
* @throws ProcessFailedException
*/
public function mustRun(): self;

public function getOutput(): string;

public function getErrorOutput(): string;
}
Loading
Loading