-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #155 from asgrim/20-detect-setup-php-ini
Automatically setup php.ini entries
- Loading branch information
Showing
59 changed files
with
3,142 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Php\Pie\Installing\Ini; | ||
|
||
use Php\Pie\DependencyResolver\Package; | ||
use Php\Pie\ExtensionType; | ||
use Php\Pie\Platform\TargetPhp\PhpBinaryPath; | ||
use Symfony\Component\Console\Output\OutputInterface; | ||
use Throwable; | ||
|
||
use function file_get_contents; | ||
use function file_put_contents; | ||
use function is_string; | ||
use function is_writable; | ||
use function sprintf; | ||
|
||
use const PHP_EOL; | ||
|
||
/** @internal This is not public API for PIE, so should not be depended upon unless you accept the risk of BC breaks */ | ||
class AddExtensionToTheIniFile | ||
{ | ||
/** @param callable():bool|null $additionalEnableStep */ | ||
public function __invoke( | ||
string $ini, | ||
Package $package, | ||
PhpBinaryPath $phpBinaryPath, | ||
OutputInterface $output, | ||
callable|null $additionalEnableStep, | ||
): bool { | ||
if (! is_writable($ini)) { | ||
$output->writeln( | ||
sprintf( | ||
'PHP is configured to use %s, but it is not writable by PIE.', | ||
$ini, | ||
), | ||
OutputInterface::VERBOSITY_VERBOSE, | ||
); | ||
|
||
return false; | ||
} | ||
|
||
$originalIniContent = file_get_contents($ini); | ||
|
||
if (! is_string($originalIniContent)) { | ||
$output->writeln( | ||
sprintf( | ||
'Tried making a backup of %s but could not read it, aborting enablement of extension', | ||
$ini, | ||
), | ||
OutputInterface::VERBOSITY_VERBOSE, | ||
); | ||
|
||
return false; | ||
} | ||
|
||
try { | ||
file_put_contents( | ||
$ini, | ||
$originalIniContent . $this->iniFileContent($package), | ||
); | ||
$output->writeln( | ||
sprintf( | ||
'Enabled extension %s in the INI file %s', | ||
$package->extensionName->name(), | ||
$ini, | ||
), | ||
OutputInterface::VERBOSITY_VERBOSE, | ||
); | ||
|
||
if ($additionalEnableStep !== null && ! $additionalEnableStep()) { | ||
return false; | ||
} | ||
|
||
$phpBinaryPath->assertExtensionIsLoadedInRuntime($package->extensionName, $output); | ||
|
||
return true; | ||
} catch (Throwable $anything) { | ||
file_put_contents($ini, $originalIniContent); | ||
|
||
$output->writeln(sprintf( | ||
'<error>Something went wrong enabling the %s extension: %s</error>', | ||
$package->extensionName->name(), | ||
$anything->getMessage(), | ||
)); | ||
|
||
return false; | ||
} | ||
} | ||
|
||
/** @return non-empty-string */ | ||
private function iniFileContent(Package $package): string | ||
{ | ||
return PHP_EOL | ||
. '; PIE automatically added this to enable the ' . $package->name . ' extension' . PHP_EOL | ||
. '; priority=' . $package->priority . PHP_EOL | ||
. ($package->extensionType === ExtensionType::PhpModule ? 'extension' : 'zend_extension') | ||
. '=' | ||
. $package->extensionName->name() . PHP_EOL; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Php\Pie\Installing\Ini; | ||
|
||
use Php\Pie\Downloading\DownloadedPackage; | ||
use Php\Pie\Platform\TargetPlatform; | ||
use Symfony\Component\Console\Output\OutputInterface; | ||
use Throwable; | ||
|
||
use function file_exists; | ||
use function is_readable; | ||
use function sprintf; | ||
|
||
/** @internal This is not public API for PIE, so should not be depended upon unless you accept the risk of BC breaks */ | ||
class CheckAndAddExtensionToIniIfNeeded | ||
{ | ||
public function __construct( | ||
private readonly IsExtensionAlreadyInTheIniFile $isExtensionAlreadyInTheIniFile, | ||
private readonly AddExtensionToTheIniFile $addExtensionToTheIniFile, | ||
) { | ||
} | ||
|
||
/** | ||
* @param non-empty-string $iniFile | ||
* @param callable():bool|null $additionalEnableStep | ||
*/ | ||
public function __invoke( | ||
string $iniFile, | ||
TargetPlatform $targetPlatform, | ||
DownloadedPackage $downloadedPackage, | ||
OutputInterface $output, | ||
callable|null $additionalEnableStep, | ||
): bool { | ||
if (! file_exists($iniFile) || ! is_readable($iniFile)) { | ||
$output->writeln( | ||
sprintf( | ||
'PHP is configured to use %s, but it did not exist, or is not readable by PIE.', | ||
$iniFile, | ||
), | ||
OutputInterface::VERBOSITY_VERBOSE, | ||
); | ||
|
||
return false; | ||
} | ||
|
||
if (($this->isExtensionAlreadyInTheIniFile)($iniFile, $downloadedPackage->package->extensionName)) { | ||
$output->writeln( | ||
sprintf( | ||
'Extension is already enabled in the INI file %s', | ||
$iniFile, | ||
), | ||
OutputInterface::VERBOSITY_VERBOSE, | ||
); | ||
|
||
if ($additionalEnableStep !== null && ! $additionalEnableStep()) { | ||
return false; | ||
} | ||
|
||
try { | ||
$targetPlatform->phpBinaryPath->assertExtensionIsLoadedInRuntime($downloadedPackage->package->extensionName, $output); | ||
|
||
return true; | ||
} catch (Throwable $anything) { | ||
$output->writeln(sprintf( | ||
'<error>Something went wrong verifying the %s extension is enabled: %s</error>', | ||
$downloadedPackage->package->extensionName->name(), | ||
$anything->getMessage(), | ||
)); | ||
|
||
return false; | ||
} | ||
} | ||
|
||
return ($this->addExtensionToTheIniFile)( | ||
$iniFile, | ||
$downloadedPackage->package, | ||
$targetPlatform->phpBinaryPath, | ||
$output, | ||
$additionalEnableStep, | ||
); | ||
} | ||
} |
Oops, something went wrong.