find(); } if (!is_executable($phpExecutable)) { throw new RuntimeException(sprintf('`%s` is not a valid PHP executable', $phpExecutable)); } $this->guzzle = $guzzle; $this->phpExecutable = $phpExecutable; } /** * Downloads composer installer and setups it to the given target. * * @param string $target * * @throws RuntimeException */ public function setup($target) { $installer = tempnam(sys_get_temp_dir(), 'install'); $handle = fopen($installer, 'w+'); $request = $this->guzzle->get('https://getcomposer.org/installer', null, $handle); try { $response = $request->send(); fclose($handle); } catch (GuzzleException $e) { fclose($handle); throw new RuntimeException('Unable to download composer install script.'); } if (200 !== $response->getStatusCode()) { @unlink($installer); throw new RuntimeException('Unable to download composer install script.'); } $dir = getcwd(); if (!@chdir(dirname($target))) { throw new RuntimeException('Unable to move to target directory for composer install.'); } $process = ProcessBuilder::create(array($this->phpExecutable, $installer))->getProcess(); try { $process->run(); @unlink($installer); } catch (ProcessException $e) { @unlink($installer); throw new RuntimeException('Unable run composer install script.'); } if (!@rename(getcwd() . '/composer.phar', $target)) { throw new RuntimeException('Composer install failed.'); } if (!@chdir($dir)) { throw new RuntimeException('Unable to move back to origin directory.'); } if (!$process->isSuccessful()) { throw new RuntimeException('Composer install failed.'); } if (!file_exists($target)) { throw new RuntimeException('Composer install failed.'); } } }