Skip to content

Commit

Permalink
Merge pull request #526 from jolicode/php-ini
Browse files Browse the repository at this point in the history
Add support for custom php.ini in compile command
  • Loading branch information
pyrech committed Sep 16, 2024
2 parents 545cde4 + 7ecebf5 commit df0603a
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 6 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## Not released yet

### Features

* Add support for custom php.ini in compile command

## 0.18.2 (2024-09-03)

* Do not build static-darwin-arm64 binary anymore (may be temporary)
Expand Down
23 changes: 17 additions & 6 deletions src/Console/Command/CompileCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ protected function configure(): void
->addOption('arch', null, InputOption::VALUE_REQUIRED, 'Target architecture for PHP compilation', 'x86_64', ['x86_64', 'aarch64'])
->addOption('php-version', null, InputOption::VALUE_REQUIRED, 'PHP version in major.minor format', '8.3')
->addOption('php-extensions', null, InputOption::VALUE_REQUIRED, 'PHP extensions required, in a comma-separated format. Defaults are the minimum required to run a basic "Hello World" task in Castor.', 'mbstring,phar,posix,tokenizer,curl,filter,openssl')
->addOption('php-ini-file', 'N', InputOption::VALUE_REQUIRED, 'ini file to inject into micro.sfx when combining')
->addOption('php-rebuild', null, InputOption::VALUE_NONE, 'Ignore cache and force PHP build compilation.')
->setHidden(true)
;
Expand Down Expand Up @@ -101,6 +102,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$input->getArgument('phar-path'),
$binaryPath,
$spcBinaryDir,
$input->getOption('php-ini-file'),
$io
);

Expand Down Expand Up @@ -214,18 +216,27 @@ private function buildPHP(string $spcBinaryPath, mixed $phpExtensions, mixed $os
$buildProcess->mustRun(fn ($type, $buffer) => print $buffer);
}

private function mergePHPandPHARIntoSingleExecutable(string $spcBinaryPath, string $pharFilePath, string $appBinaryFilePath, string $spcBinaryDir, SymfonyStyle $io): void
private function mergePHPandPHARIntoSingleExecutable(string $spcBinaryPath, string $pharFilePath, string $appBinaryFilePath, string $spcBinaryDir, ?string $iniFile, SymfonyStyle $io): void
{
if (!$this->fs->isAbsolutePath($pharFilePath)) {
$pharFilePath = PathHelper::getRoot() . '/' . $pharFilePath;
}

$command = [
$spcBinaryPath,
'micro:combine', $pharFilePath,
'--output=' . $appBinaryFilePath,
];
if ($iniFile) {
if (!file_exists($iniFile)) {
throw new \InvalidArgumentException(\sprintf('The ini file "%s" does not exist.', $iniFile));
}
$iniFile = Path::makeAbsolute($iniFile, getcwd() ?: PathHelper::getRoot());
$command[] = '--with-ini-file=' . $iniFile;
}

$mergePHPandPHARProcess = new Process(
[
$spcBinaryPath,
'micro:combine', $pharFilePath,
'--output=' . $appBinaryFilePath,
],
$command,
cwd: $spcBinaryDir,
timeout: null,
);
Expand Down
5 changes: 5 additions & 0 deletions tests/Slow/CompileCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ public function test()
'--os', 'linux',
'--binary-path', $binary,
'--php-extensions', 'mbstring,phar,posix,tokenizer',
'--php-ini-file', 'php.ini',
'-vvv',
],
cwd: $castorAppDirPath,
Expand All @@ -55,5 +56,9 @@ public function test()
// run
$p = (new Process([$binary, 'hello'], cwd: $castorAppDirPath))->mustRun();
$this->assertSame('hello', $p->getOutput());

// Test php.ini
$p = (new Process([$binary, 'timezone'], cwd: $castorAppDirPath))->mustRun();
$this->assertSame('Arctic/Longyearbyen', $p->getOutput());
}
}
13 changes: 13 additions & 0 deletions tests/Slow/RepackCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ public static function setupRepackedCastorApp(string $castorAppDirName): string
use Castor\Attribute\AsTask;
use function Castor\import;
use function Castor\io;
use function Castor\run;
import('composer://pyrech/castor-example');
Expand All @@ -88,6 +89,12 @@ function ls(): void
{
run(['ls', 'my-app.linux.phar']);
}
#[AsTask()]
function timezone(): void
{
io()->write(date_default_timezone_get());
}
PHP
);

Expand All @@ -103,6 +110,12 @@ function ls(): void
],
]));

// Only for the compile test
$fs->dumpFile($castorAppDirPath . '/php.ini', <<<'INI'
date.timezone=Arctic/Longyearbyen
INI
);

(new Process(['composer', 'install'],
cwd: $castorAppDirPath,
env: ['COMPOSER_MIRROR_PATH_REPOS' => '1'],
Expand Down

0 comments on commit df0603a

Please sign in to comment.