Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add context options for requests in composer extra configuration. #45

Open
wants to merge 1 commit into
base: 1.0
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
}
}
Expand All @@ -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
-------------
Expand Down
6 changes: 3 additions & 3 deletions src/NodeJsInstaller.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 <info>NodeJS v".$version."</info>");
$url = $this->getNodeJSUrl($version);
Expand All @@ -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'
Expand Down Expand Up @@ -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);

Expand Down
28 changes: 19 additions & 9 deletions src/NodeJsPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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();
Expand All @@ -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);
Expand Down Expand Up @@ -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();

Expand All @@ -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);
}
}

Expand All @@ -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();
Expand All @@ -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);
}

/**
Expand All @@ -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();
Expand Down
17 changes: 16 additions & 1 deletion src/NodeJsVersionsLister.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down