-
Notifications
You must be signed in to change notification settings - Fork 22
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 #21 from asgrim/install-command-implementation
Install command implementation
- Loading branch information
Showing
39 changed files
with
950 additions
and
37 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Php\Pie\Command; | ||
|
||
use Php\Pie\Building\Build; | ||
use Php\Pie\DependencyResolver\DependencyResolver; | ||
use Php\Pie\Downloading\DownloadAndExtract; | ||
use Php\Pie\Installing\Install; | ||
use Php\Pie\Platform\TargetPlatform; | ||
use Symfony\Component\Console\Attribute\AsCommand; | ||
use Symfony\Component\Console\Command\Command; | ||
use Symfony\Component\Console\Input\InputInterface; | ||
use Symfony\Component\Console\Output\OutputInterface; | ||
|
||
#[AsCommand( | ||
name: 'install', | ||
description: 'Download, build, and install a PIE-compatible PHP extension.', | ||
)] | ||
final class InstallCommand extends Command | ||
{ | ||
public function __construct( | ||
private readonly DependencyResolver $dependencyResolver, | ||
private readonly DownloadAndExtract $downloadAndExtract, | ||
private readonly Build $build, | ||
private readonly Install $install, | ||
) { | ||
parent::__construct(); | ||
} | ||
|
||
public function configure(): void | ||
{ | ||
parent::configure(); | ||
|
||
CommandHelper::configureOptions($this); | ||
} | ||
|
||
public function execute(InputInterface $input, OutputInterface $output): int | ||
{ | ||
if (! TargetPlatform::isRunningAsRoot()) { | ||
$output->writeln('This command needs elevated privileges, and may prompt you for your password.'); | ||
} | ||
|
||
$targetPlatform = CommandHelper::determineTargetPlatformFromInputs($input, $output); | ||
|
||
$requestedNameAndVersionPair = CommandHelper::requestedNameAndVersionPair($input); | ||
|
||
$downloadedPackage = CommandHelper::downloadPackage( | ||
$this->dependencyResolver, | ||
$targetPlatform, | ||
$requestedNameAndVersionPair, | ||
$this->downloadAndExtract, | ||
$output, | ||
); | ||
|
||
CommandHelper::bindConfigureOptionsFromPackage($this, $downloadedPackage->package, $input); | ||
|
||
$configureOptionsValues = CommandHelper::processConfigureOptionsFromInput($downloadedPackage->package, $input); | ||
|
||
($this->build)($downloadedPackage, $targetPlatform, $configureOptionsValues, $output); | ||
|
||
($this->install)($downloadedPackage, $targetPlatform, $output); | ||
|
||
return Command::SUCCESS; | ||
} | ||
} |
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,17 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Php\Pie; | ||
|
||
/** @internal This is not public API for PIE, so should not be depended upon unless you accept the risk of BC breaks */ | ||
enum ExtensionType: string | ||
{ | ||
case PhpModule = 'php-ext'; | ||
case ZendExtension = 'php-ext-zend'; | ||
|
||
public static function isValid(string $toBeChecked): bool | ||
{ | ||
return self::tryFrom($toBeChecked) !== null; | ||
} | ||
} |
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,25 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Php\Pie\Installing; | ||
|
||
use Php\Pie\Downloading\DownloadedPackage; | ||
use Php\Pie\Platform\TargetPlatform; | ||
use Symfony\Component\Console\Output\OutputInterface; | ||
|
||
/** @internal This is not public API for PIE, so should not be depended upon unless you accept the risk of BC breaks */ | ||
interface Install | ||
{ | ||
/** | ||
* Install the extension in the given target platform's PHP, and return the location of the installed shared object | ||
* or DLL, depending on the platform implementation. | ||
* | ||
* @return non-empty-string | ||
*/ | ||
public function __invoke( | ||
DownloadedPackage $downloadedPackage, | ||
TargetPlatform $targetPlatform, | ||
OutputInterface $output, | ||
): string; | ||
} |
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,52 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Php\Pie\Installing; | ||
|
||
use Php\Pie\Downloading\DownloadedPackage; | ||
use Php\Pie\ExtensionType; | ||
use Php\Pie\Platform\TargetPlatform; | ||
use RuntimeException; | ||
use Symfony\Component\Console\Output\OutputInterface; | ||
use Symfony\Component\Process\Process; | ||
|
||
use function file_exists; | ||
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 */ | ||
final class UnixInstall implements Install | ||
{ | ||
public function __invoke(DownloadedPackage $downloadedPackage, TargetPlatform $targetPlatform, OutputInterface $output): string | ||
{ | ||
(new Process(['sudo', 'make', 'install'], $downloadedPackage->extractedSourcePath)) | ||
->mustRun() | ||
->getOutput(); | ||
|
||
$sharedObjectName = $downloadedPackage->package->extensionName->name() . '.so'; | ||
$expectedSharedObjectLocation = sprintf( | ||
'%s/%s', | ||
$targetPlatform->phpBinaryPath->extensionPath(), | ||
$sharedObjectName, | ||
); | ||
|
||
if (! file_exists($expectedSharedObjectLocation)) { | ||
throw new RuntimeException('Install failed, ' . $expectedSharedObjectLocation . ' was not installed.'); | ||
} | ||
|
||
$output->writeln('<info>Install complete:</info> ' . $expectedSharedObjectLocation); | ||
|
||
/** | ||
* @link https://github.com/php/pie/issues/20 | ||
* | ||
* @todo this should be improved in future to try to automatically set up the ext | ||
*/ | ||
$output->writeln(sprintf( | ||
'<comment>You must now add "%s=%s" to your php.ini</comment>', | ||
$downloadedPackage->package->extensionType === ExtensionType::PhpModule ? 'extension' : 'zend_extension', | ||
$downloadedPackage->package->extensionName->name(), | ||
)); | ||
|
||
return $expectedSharedObjectLocation; | ||
} | ||
} |
Oops, something went wrong.