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
20 changes: 17 additions & 3 deletions src/Helper/ProcessHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,10 @@ public function console(array $args): void

public function runAndTail(string $code): void
{
$start = $this->printPreStart([$code]);
$originalCode = $code;
$code = $this->replaceVariables($code);

$start = $this->printPreStart([$originalCode]);

$process = new Process(['sh', '-c', $code], $this->projectDir);
$process->setTimeout($this->timeout);
Expand All @@ -110,10 +113,10 @@ public function runAndTail(string $code): void
});

if (!$process->isSuccessful()) {
throw new \RuntimeException('Execution of ' . $code . ' failed');
throw new \RuntimeException('Execution of ' . $originalCode . ' failed');
}

$this->printPostStart([$code], $start);
$this->printPostStart([$originalCode], $start);
}

public function getPluginList(): string
Expand Down Expand Up @@ -194,4 +197,15 @@ private function validateTimeout(?float $timeout): ?float

return $timeout;
}

/**
* Replaces placeholders in hook/script commands with actual values.
*
* Supported placeholders:
* - %php.bin%: The path to the PHP binary that started the current process
*/
private function replaceVariables(string $code): string
{
return str_replace('%php.bin%', escapeshellarg(\PHP_BINARY), $code);
}
}
28 changes: 28 additions & 0 deletions tests/Helper/ProcessHelperTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

declare(strict_types=1);

namespace Shopware\Deployment\Tests\Helper;

use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\TestCase;
use Shopware\Deployment\Helper\ProcessHelper;

#[CoversClass(ProcessHelper::class)]
class ProcessHelperTest extends TestCase
{
public function testRunAndTailReplacesPhpBinPlaceholder(): void
{
$tempFile = tempnam(sys_get_temp_dir(), 'php_bin_test_');

try {
$helper = new ProcessHelper('/tmp');
$helper->runAndTail('echo %php.bin% > ' . $tempFile);

static::assertFileExists($tempFile);
static::assertStringContainsString(\PHP_BINARY, (string) file_get_contents($tempFile));
} finally {
@unlink($tempFile);
}
}
}