Skip to content

Commit 860d280

Browse files
authored
Merge pull request #6 from php-etl/hotfix/jq-failures-on-composer-autoloads
Fixed issues related to jq clearing the composer.json
2 parents c8cb914 + beab9fe commit 860d280

File tree

1 file changed

+26
-6
lines changed

1 file changed

+26
-6
lines changed

src/PHP/ComposerAutoload.php

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -47,19 +47,39 @@ private static function pipe(string ...$commands): string
4747
return implode(' | ', $commands);
4848
}
4949

50+
private static function indirection(string $command, string $destination): string
51+
{
52+
return $command.' > '.self::escapeArgument($destination);
53+
}
54+
55+
private static function and(string ...$commands): string
56+
{
57+
return implode(' \\'.\PHP_EOL.' && ', $commands);
58+
}
59+
5060
public function __toString(): string
5161
{
5262
if (\count($this->autoloads) <= 0) {
5363
return '';
5464
}
5565

56-
$commands = implode(' \\'.\PHP_EOL.' && ', array_map(fn ($type, $autoload) => match ($type) {
57-
'psr4' => self::pipe(
58-
self::command('cat', 'composer.json'),
59-
self::command('jq', '--indent', '4', sprintf('.autoload."psr-4" |= . + %s', json_encode($autoload, \JSON_THROW_ON_ERROR))),
60-
self::command('tee', 'composer.json')
66+
$commands = self::and(
67+
...array_map(
68+
fn ($type, $autoload) => match ($type) {
69+
'psr4' => self::and(
70+
self::pipe(
71+
self::command('cat', 'composer.json'),
72+
self::command('jq', '--indent', '4', sprintf('.autoload."psr-4" |= . + %s', json_encode($autoload, \JSON_THROW_ON_ERROR))),
73+
self::command('tee', 'composer.temp')
74+
),
75+
self::indirection(self::command('cat', 'composer.temp'), 'composer.json'),
76+
self::command('rm', 'composer.temp'),
77+
)
78+
},
79+
array_keys($this->autoloads),
80+
array_values($this->autoloads)
6181
)
62-
}, array_keys($this->autoloads), array_values($this->autoloads)));
82+
);
6383

6484
return (string) new Dockerfile\Run(
6585
<<<RUN

0 commit comments

Comments
 (0)