From a3490577bdd9bdcc82727de8e5ece6f9ce684e77 Mon Sep 17 00:00:00 2001 From: Granville Raper Date: Thu, 25 Aug 2022 08:11:48 -0700 Subject: [PATCH] Add context options for requests in composer extra configuration. --- README.md | 7 ++++++- src/NodeJsInstaller.php | 6 +++--- src/NodeJsPlugin.php | 28 +++++++++++++++++++--------- src/NodeJsVersionsLister.php | 17 ++++++++++++++++- 4 files changed, 44 insertions(+), 14 deletions(-) diff --git a/README.md b/README.md index 4eb65f7..c78592c 100644 --- a/README.md +++ b/README.md @@ -57,6 +57,11 @@ A number of options are available to customize NodeJS installation: "version": "~0.12", "targetDir": "vendor/nodejs/nodejs", "forceLocal": false + }, + "request_options": { + "http": { + "proxy": "http:http://example.webproxy.org" + } } } } @@ -82,7 +87,7 @@ Available options: is not impacted by this option. This option is only available in the root package. *Default value: false* - +- **request_options** (array): Build context request options array. @ref https://www.php.net/manual/en/context.php Custom script ------------- diff --git a/src/NodeJsInstaller.php b/src/NodeJsInstaller.php index f40e2e2..7e77649 100644 --- a/src/NodeJsInstaller.php +++ b/src/NodeJsInstaller.php @@ -193,7 +193,7 @@ public function getNodeJSUrl($version) * @param string $targetDirectory * @throws NodeJsInstallerException */ - public function install($version, $targetDirectory) + public function install($version, $targetDirectory, $progress = true, $requestOptions = array()) { $this->io->write("Installing NodeJS v".$version.""); $url = $this->getNodeJSUrl($version); @@ -203,7 +203,7 @@ public function install($version, $targetDirectory) $fileName = 'vendor/'.pathinfo(parse_url($url, PHP_URL_PATH), PATHINFO_BASENAME); - $this->rfs->copy(parse_url($url, PHP_URL_HOST), $url, $fileName); + $this->rfs->copy(parse_url($url, PHP_URL_HOST), $url, $fileName, $progress, $requestOptions); if (!file_exists($fileName)) { throw new \UnexpectedValueException($url.' could not be saved to '.$fileName.', make sure the' @@ -231,7 +231,7 @@ public function install($version, $targetDirectory) // We have to download the latest available version in a bin for Windows, then upgrade it: $url = "https://nodejs.org/dist/npm/npm-1.4.12.zip"; $npmFileName = "vendor/npm-1.4.12.zip"; - $this->rfs->copy(parse_url($url, PHP_URL_HOST), $url, $npmFileName); + $this->rfs->copy(parse_url($url, PHP_URL_HOST), $url, $npmFileName, $progress, $requestOptions); $this->unzip($npmFileName, $targetDirectory); diff --git a/src/NodeJsPlugin.php b/src/NodeJsPlugin.php index f54da3d..4409a20 100644 --- a/src/NodeJsPlugin.php +++ b/src/NodeJsPlugin.php @@ -84,6 +84,12 @@ public function onPostUpdateInstall(Event $event) $extra = $event->getComposer()->getPackage()->getExtra(); + $requestOptions = array(); + + if (isset($extra['mouf']['request_options'])) { + $requestOptions = $extra['mouf']['request_options']; + } + if (isset($extra['mouf']['nodejs'])) { $rootSettings = $extra['mouf']['nodejs']; $settings = array_merge($settings, $rootSettings); @@ -112,7 +118,7 @@ public function onPostUpdateInstall(Event $event) if ($settings['forceLocal']) { $this->verboseLog(" - Forcing local NodeJS install."); - $this->installLocalVersion($binDir, $nodeJsInstaller, $versionConstraint, $settings['targetDir']); + $this->installLocalVersion($binDir, $nodeJsInstaller, $versionConstraint, $settings['targetDir'], TRUE, $requestOptions); $isLocal = true; } else { $globalVersion = $nodeJsInstaller->getNodeJsGlobalInstallVersion(); @@ -123,10 +129,10 @@ public function onPostUpdateInstall(Event $event) if (!$npmPath) { $this->verboseLog(" - No NPM install found"); - $this->installLocalVersion($binDir, $nodeJsInstaller, $versionConstraint, $settings['targetDir']); + $this->installLocalVersion($binDir, $nodeJsInstaller, $versionConstraint, $settings['targetDir'], TRUE, $requestOptions); $isLocal = true; } elseif (!$nodeJsVersionMatcher->isVersionMatching($globalVersion, $versionConstraint)) { - $this->installLocalVersion($binDir, $nodeJsInstaller, $versionConstraint, $settings['targetDir']); + $this->installLocalVersion($binDir, $nodeJsInstaller, $versionConstraint, $settings['targetDir'], TRUE, $requestOptions); $isLocal = true; } else { $this->verboseLog(" - Global NodeJS install matches constraint ".$versionConstraint); @@ -165,9 +171,11 @@ private function verboseLog($message) * @param NodeJsInstaller $nodeJsInstaller * @param string $versionConstraint * @param string $targetDir + * @param bool $progress + * @param array $requestOptions * @throws NodeJsInstallerException */ - private function installLocalVersion($binDir, NodeJsInstaller $nodeJsInstaller, $versionConstraint, $targetDir) + private function installLocalVersion($binDir, NodeJsInstaller $nodeJsInstaller, $versionConstraint, $targetDir, $progress = TRUE, $requestOptions = array()) { $nodeJsVersionMatcher = new NodeJsVersionMatcher(); @@ -176,14 +184,14 @@ private function installLocalVersion($binDir, NodeJsInstaller $nodeJsInstaller, $this->verboseLog(" - Local NodeJS install found: v".$localVersion); if (!$nodeJsVersionMatcher->isVersionMatching($localVersion, $versionConstraint)) { - $this->installBestPossibleLocalVersion($nodeJsInstaller, $versionConstraint, $targetDir); + $this->installBestPossibleLocalVersion($nodeJsInstaller, $versionConstraint, $targetDir, $progress, $requestOptions); } else { // Question: should we update to the latest version? Should we have a nodejs.lock file??? $this->verboseLog(" - Local NodeJS install matches constraint ".$versionConstraint); } } else { $this->verboseLog(" - No local NodeJS install found"); - $this->installBestPossibleLocalVersion($nodeJsInstaller, $versionConstraint, $targetDir); + $this->installBestPossibleLocalVersion($nodeJsInstaller, $versionConstraint, $targetDir, $progress, $requestOptions); } } @@ -193,9 +201,11 @@ private function installLocalVersion($binDir, NodeJsInstaller $nodeJsInstaller, * @param NodeJsInstaller $nodeJsInstaller * @param string $versionConstraint * @param string $targetDir + * @param bool $progress + * @param array $requestOptions * @throws NodeJsInstallerException */ - private function installBestPossibleLocalVersion(NodeJsInstaller $nodeJsInstaller, $versionConstraint, $targetDir) + private function installBestPossibleLocalVersion(NodeJsInstaller $nodeJsInstaller, $versionConstraint, $targetDir, $progress = TRUE, $requestOptions = array()) { $nodeJsVersionsLister = new NodeJsVersionsLister($this->io, $this->composer); $allNodeJsVersions = $nodeJsVersionsLister->getList(); @@ -207,7 +217,7 @@ private function installBestPossibleLocalVersion(NodeJsInstaller $nodeJsInstalle throw new NodeJsInstallerNodeVersionException("No NodeJS version could be found for constraint '".$versionConstraint."'"); } - $nodeJsInstaller->install($bestPossibleVersion, $targetDir); + $nodeJsInstaller->install($bestPossibleVersion, $targetDir, $progress, $requestOptions); } /** @@ -216,7 +226,7 @@ private function installBestPossibleLocalVersion(NodeJsInstaller $nodeJsInstalle private function getMergedVersionConstraint() { $packagesList = $this->composer->getRepositoryManager()->getLocalRepository() - ->getCanonicalPackages(); + ->getCanonicalPackages(); $packagesList[] = $this->composer->getPackage(); $versions = array(); diff --git a/src/NodeJsVersionsLister.php b/src/NodeJsVersionsLister.php index 809ce73..3c6e440 100644 --- a/src/NodeJsVersionsLister.php +++ b/src/NodeJsVersionsLister.php @@ -15,20 +15,35 @@ class NodeJsVersionsLister */ private $io; + /** + * @var RemoteFilesystem + */ protected $rfs; + /** + * @var array + */ + protected $extra; + const NODEJS_DIST_URL = "https://nodejs.org/dist/"; public function __construct(IOInterface $io, Composer $composer) { $this->io = $io; $this->rfs = new RemoteFilesystem($io, $composer->getConfig()); + $this->extra = $composer->getPackage()->getExtra(); } public function getList() { + $requestOptions = array(); + + if (isset($this->extra['mouf']['request_options'])) { + $requestOptions = $this->extra['mouf']['request_options']; + } + // Let's download the content of HTML page https://nodejs.org/dist/ - $html = $this->rfs->getContents(parse_url(self::NODEJS_DIST_URL, PHP_URL_HOST), self::NODEJS_DIST_URL, false); + $html = $this->rfs->getContents(parse_url(self::NODEJS_DIST_URL, PHP_URL_HOST), self::NODEJS_DIST_URL, false, $requestOptions); // Now, let's parse it! $matches = array();