|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Boson\Component\Compiler\Action; |
| 6 | + |
| 7 | +use Boson\Component\Compiler\Configuration; |
| 8 | +use Boson\Component\Compiler\Target\TargetInterface; |
| 9 | + |
| 10 | +/** |
| 11 | + * @template-extends TargetAction<CompileStatus> |
| 12 | + */ |
| 13 | +final readonly class CompileAction extends TargetAction |
| 14 | +{ |
| 15 | + /** |
| 16 | + * @var non-empty-string |
| 17 | + */ |
| 18 | + private const string DEFAULT_INI_CONFIG = <<<'INI' |
| 19 | + ffi.enable=1 |
| 20 | + opcache.enable=1 |
| 21 | + opcache.enable_cli=1 |
| 22 | + INI; |
| 23 | + |
| 24 | + public function __construct( |
| 25 | + /** |
| 26 | + * @var non-empty-string |
| 27 | + */ |
| 28 | + private string $sfx, |
| 29 | + /** |
| 30 | + * @var non-empty-string |
| 31 | + */ |
| 32 | + private string $targetFilename, |
| 33 | + TargetInterface $target, |
| 34 | + ) { |
| 35 | + parent::__construct($target); |
| 36 | + } |
| 37 | + |
| 38 | + public function process(Configuration $config): iterable |
| 39 | + { |
| 40 | + yield $this->target => CompileStatus::ReadyToCompile; |
| 41 | + |
| 42 | + $targetPathname = $this->getBuildDirectory($config) |
| 43 | + . \DIRECTORY_SEPARATOR |
| 44 | + . $this->targetFilename; |
| 45 | + |
| 46 | + $targetStream = @\fopen($targetPathname, 'wb+'); |
| 47 | + |
| 48 | + if ($targetStream === false) { |
| 49 | + throw new \RuntimeException(\sprintf( |
| 50 | + 'Unable to create target binary "%s"', |
| 51 | + $targetPathname, |
| 52 | + )); |
| 53 | + } |
| 54 | + |
| 55 | + \flock($targetStream, \LOCK_EX); |
| 56 | + |
| 57 | + yield $this->target => CompileStatus::Progress; |
| 58 | + $this->appendSfxArchive($targetStream); |
| 59 | + |
| 60 | + yield $this->target => CompileStatus::Progress; |
| 61 | + $this->appendPhpConfig($targetStream, $config); |
| 62 | + |
| 63 | + yield $this->target => CompileStatus::Progress; |
| 64 | + $this->appendSource($targetStream, $config); |
| 65 | + |
| 66 | + \flock($targetStream, \LOCK_UN); |
| 67 | + \fclose($targetStream); |
| 68 | + |
| 69 | + yield $this->target => CompileStatus::Compiled; |
| 70 | + } |
| 71 | + |
| 72 | + /** |
| 73 | + * @return non-empty-string |
| 74 | + */ |
| 75 | + private function getPhpConfigString(Configuration $config): string |
| 76 | + { |
| 77 | + $ini = self::DEFAULT_INI_CONFIG; |
| 78 | + |
| 79 | + foreach ($config->ini as $key => $value) { |
| 80 | + $ini .= "\n$key=" . match ($value) { |
| 81 | + false => '0', |
| 82 | + true => '1', |
| 83 | + default => (string) $value, |
| 84 | + }; |
| 85 | + } |
| 86 | + |
| 87 | + return $ini . "\n"; |
| 88 | + } |
| 89 | + |
| 90 | + /** |
| 91 | + * @param resource $stream |
| 92 | + */ |
| 93 | + private function appendSource(mixed $stream, Configuration $config): void |
| 94 | + { |
| 95 | + $sourceStream = @\fopen($config->pharPathname, 'rb'); |
| 96 | + |
| 97 | + if ($sourceStream === false) { |
| 98 | + throw new \RuntimeException(\sprintf( |
| 99 | + 'Unable to open application phar file "%s"', |
| 100 | + $config->pharPathname, |
| 101 | + )); |
| 102 | + } |
| 103 | + |
| 104 | + \flock($sourceStream, \LOCK_SH); |
| 105 | + \stream_copy_to_stream($sourceStream, $stream); |
| 106 | + \fclose($sourceStream); |
| 107 | + } |
| 108 | + |
| 109 | + /** |
| 110 | + * @param resource $stream |
| 111 | + */ |
| 112 | + private function appendPhpConfig(mixed $stream, Configuration $config): void |
| 113 | + { |
| 114 | + $ini = $this->getPhpConfigString($config); |
| 115 | + |
| 116 | + \fwrite($stream, "\xfd\xf6\x69\xe6"); |
| 117 | + \fwrite($stream, \pack('N', \strlen($ini))); |
| 118 | + \fwrite($stream, $ini); |
| 119 | + } |
| 120 | + |
| 121 | + /** |
| 122 | + * @param resource $stream |
| 123 | + */ |
| 124 | + private function appendSfxArchive(mixed $stream): void |
| 125 | + { |
| 126 | + $archiveStream = @\fopen($this->sfx, 'rb'); |
| 127 | + |
| 128 | + if ($archiveStream === false) { |
| 129 | + throw new \RuntimeException(\sprintf( |
| 130 | + 'Unable to open application SFX file "%s"', |
| 131 | + $this->sfx, |
| 132 | + )); |
| 133 | + } |
| 134 | + |
| 135 | + \flock($archiveStream, \LOCK_SH); |
| 136 | + \stream_copy_to_stream($archiveStream, $stream); |
| 137 | + \fclose($archiveStream); |
| 138 | + } |
| 139 | +} |
0 commit comments