|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Php\Pie\Installing\Ini; |
| 6 | + |
| 7 | +use Php\Pie\DependencyResolver\Package; |
| 8 | +use Php\Pie\ExtensionType; |
| 9 | +use Php\Pie\Platform\TargetPhp\PhpBinaryPath; |
| 10 | +use Symfony\Component\Console\Output\OutputInterface; |
| 11 | +use Throwable; |
| 12 | + |
| 13 | +use function file_get_contents; |
| 14 | +use function file_put_contents; |
| 15 | +use function is_string; |
| 16 | +use function is_writable; |
| 17 | +use function sprintf; |
| 18 | + |
| 19 | +use const PHP_EOL; |
| 20 | + |
| 21 | +/** @internal This is not public API for PIE, so should not be depended upon unless you accept the risk of BC breaks */ |
| 22 | +class AddExtensionToTheIniFile |
| 23 | +{ |
| 24 | + /** @param callable():bool|null $additionalEnableStep */ |
| 25 | + public function __invoke( |
| 26 | + string $ini, |
| 27 | + Package $package, |
| 28 | + PhpBinaryPath $phpBinaryPath, |
| 29 | + OutputInterface $output, |
| 30 | + callable|null $additionalEnableStep, |
| 31 | + ): bool { |
| 32 | + if (! is_writable($ini)) { |
| 33 | + $output->writeln( |
| 34 | + sprintf( |
| 35 | + 'PHP is configured to use %s, but it is not writable by PIE.', |
| 36 | + $ini, |
| 37 | + ), |
| 38 | + OutputInterface::VERBOSITY_VERBOSE, |
| 39 | + ); |
| 40 | + |
| 41 | + return false; |
| 42 | + } |
| 43 | + |
| 44 | + $originalIniContent = file_get_contents($ini); |
| 45 | + |
| 46 | + if (! is_string($originalIniContent)) { |
| 47 | + $output->writeln( |
| 48 | + sprintf( |
| 49 | + 'Tried making a backup of %s but could not read it, aborting enablement of extension', |
| 50 | + $ini, |
| 51 | + ), |
| 52 | + OutputInterface::VERBOSITY_VERBOSE, |
| 53 | + ); |
| 54 | + |
| 55 | + return false; |
| 56 | + } |
| 57 | + |
| 58 | + try { |
| 59 | + file_put_contents( |
| 60 | + $ini, |
| 61 | + $originalIniContent . $this->iniFileContent($package), |
| 62 | + ); |
| 63 | + $output->writeln( |
| 64 | + sprintf( |
| 65 | + 'Enabled extension %s in the INI file %s', |
| 66 | + $package->extensionName->name(), |
| 67 | + $ini, |
| 68 | + ), |
| 69 | + OutputInterface::VERBOSITY_VERBOSE, |
| 70 | + ); |
| 71 | + |
| 72 | + if ($additionalEnableStep !== null && ! $additionalEnableStep()) { |
| 73 | + return false; |
| 74 | + } |
| 75 | + |
| 76 | + $phpBinaryPath->assertExtensionIsLoadedInRuntime($package->extensionName, $output); |
| 77 | + |
| 78 | + return true; |
| 79 | + } catch (Throwable $anything) { |
| 80 | + file_put_contents($ini, $originalIniContent); |
| 81 | + |
| 82 | + $output->writeln(sprintf( |
| 83 | + '<error>Something went wrong enabling the %s extension: %s</error>', |
| 84 | + $package->extensionName->name(), |
| 85 | + $anything->getMessage(), |
| 86 | + )); |
| 87 | + |
| 88 | + return false; |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + /** @return non-empty-string */ |
| 93 | + private function iniFileContent(Package $package): string |
| 94 | + { |
| 95 | + return PHP_EOL |
| 96 | + . '; PIE automatically added this to enable the ' . $package->name . ' extension' . PHP_EOL |
| 97 | + . '; priority=' . $package->priority . PHP_EOL |
| 98 | + . ($package->extensionType === ExtensionType::PhpModule ? 'extension' : 'zend_extension') |
| 99 | + . '=' |
| 100 | + . $package->extensionName->name() . PHP_EOL; |
| 101 | + } |
| 102 | +} |
0 commit comments