public static function createConfig(IOInterface $io = null, $cwd = null) { $cwd = $cwd ?: getcwd(); $home = self::getHomeDir(); $cacheDir = self::getCacheDir($home); foreach (array($home, $cacheDir) as $dir) { if (!file_exists($dir . '/.htaccess')) { if (!is_dir($dir)) { @mkdir($dir, 0777, true); } @file_put_contents($dir . '/.htaccess', 'Deny from all'); } } $config = new Config(true, $cwd); $config->merge(array('config' => array('home' => $home, 'cache-dir' => $cacheDir))); $file = new JsonFile($config->get('home') . '/config.json'); if ($file->exists()) { if ($io && $io->isDebug()) { $io->writeError('Loading config file ' . $file->getPath()); } $config->merge($file->read()); } $config->setConfigSource(new JsonConfigSource($file)); $file = new JsonFile($config->get('home') . '/auth.json'); if ($file->exists()) { if ($io && $io->isDebug()) { $io->writeError('Loading config file ' . $file->getPath()); } $config->merge(array('config' => $file->read())); } $config->setAuthConfigSource(new JsonConfigSource($file, true)); return $config; }
/** * @dataProvider getAssetTypes */ public function testRedirectUrlWithNonexistentRepository($type, $filename) { $this->setExpectedException('RuntimeException'); $repoUrl = 'http://github.com/composer-test/repo-name'; $repoApiUrl = 'https://api.github.com/repos/composer-test/repo-name'; $identifier = 'v0.0.0'; $io = $this->getMock('Composer\\IO\\IOInterface'); $io->expects($this->any())->method('isInteractive')->will($this->returnValue(true)); $remoteFilesystem = $this->getMockBuilder('Composer\\Util\\RemoteFilesystem')->setConstructorArgs(array($io))->getMock(); $remoteFilesystem->expects($this->at(0))->method('getContents')->with($this->equalTo('github.com'), $this->equalTo($repoApiUrl), $this->equalTo(false))->will($this->throwException(new TransportException('HTTP/1.1 404 Not Found', 404))); $io->expects($this->once())->method('ask')->with($this->equalTo('Username: '******'someuser')); $io->expects($this->once())->method('askAndHideAnswer')->with($this->equalTo('Password: '******'somepassword')); $io->expects($this->any())->method('setAuthentication')->with($this->equalTo('github.com'), $this->matchesRegularExpression('{someuser|abcdef}'), $this->matchesRegularExpression('{somepassword|x-oauth-basic}')); $remoteFilesystem->expects($this->at(1))->method('getContents')->with($this->equalTo('github.com'), $this->equalTo('https://github.com/composer-test/repo-name'), $this->equalTo(false))->will($this->throwException(new TransportException('HTTP/1.1 404 Not Found', 404))); $remoteFilesystem->expects($this->at(2))->method('getContents')->with($this->equalTo('github.com'), $this->equalTo($repoApiUrl), $this->equalTo(false))->will($this->throwException(new TransportException('HTTP/1.1 404 Not Found', 404))); $remoteFilesystem->expects($this->at(3))->method('getContents')->with($this->equalTo('github.com'), $this->equalTo('https://api.github.com/authorizations'), $this->equalTo(false))->will($this->returnValue('[]')); $remoteFilesystem->expects($this->at(4))->method('getContents')->with($this->equalTo('github.com'), $this->equalTo('https://api.github.com/authorizations'), $this->equalTo(false))->will($this->returnValue('{"token": "abcdef"}')); $remoteFilesystem->expects($this->at(5))->method('getContents')->with($this->equalTo('github.com'), $this->equalTo($repoApiUrl), $this->equalTo(false))->will($this->throwException(new TransportException('HTTP/1.1 404 Not Found', 404))); $remoteFilesystem->expects($this->at(6))->method('getContents')->with($this->equalTo('github.com'), $this->equalTo($repoApiUrl . '/contents/' . $filename . '?ref=' . $identifier), $this->equalTo(false))->will($this->throwException(new TransportException('HTTP/1.1 404 Not Found', 404))); $configSource = $this->getMock('Composer\\Config\\ConfigSourceInterface'); $authConfigSource = $this->getMock('Composer\\Config\\ConfigSourceInterface'); /* @var ConfigSourceInterface $configSource */ /* @var ConfigSourceInterface $authConfigSource */ $this->config->setConfigSource($configSource); $this->config->setAuthConfigSource($authConfigSource); $repoConfig = array('url' => $repoUrl, 'asset-type' => $type, 'filename' => $filename); /* @var IOInterface $io */ /* @var RemoteFilesystem $remoteFilesystem */ $gitHubDriver = new GitHubDriver($repoConfig, $io, $this->config, null, $remoteFilesystem); $firstNonexistent = false; try { $gitHubDriver->initialize(); } catch (TransportException $e) { $firstNonexistent = true; } $this->assertTrue($firstNonexistent); $gitHubDriver->getComposerInformation($identifier); }
/** * Get the package index instance * * We need to construct the instance manually, because there's no way to select * a particular instance using $composer->getRepositoryManager() * * @return ComposerRepository */ private function package_index() { static $package_index; if (!$package_index) { $config = new Config(); $config->merge(array('config' => array('secure-http' => true, 'home' => dirname($this->get_composer_json_path())))); $config->setConfigSource(new JsonConfigSource($this->get_composer_json())); try { $package_index = new ComposerRepository(array('url' => self::PACKAGE_INDEX_URL), new NullIO(), $config); } catch (Exception $e) { WP_CLI::error($e->getMessage()); } } return $package_index; }
/** * @param IOInterface|null $io * @return Config */ public static function createConfig(IOInterface $io = null, $cwd = null) { $cwd = $cwd ?: getcwd(); $config = new Config(true, $cwd); // determine and add main dirs to the config $home = self::getHomeDir(); $config->merge(array('config' => array('home' => $home, 'cache-dir' => self::getCacheDir($home), 'data-dir' => self::getDataDir($home)))); // Protect directory against web access. Since HOME could be // the www-data's user home and be web-accessible it is a // potential security risk $dirs = array($config->get('home'), $config->get('cache-dir'), $config->get('data-dir')); foreach ($dirs as $dir) { if (!file_exists($dir . '/.htaccess')) { if (!is_dir($dir)) { Silencer::call('mkdir', $dir, 0777, true); } Silencer::call('file_put_contents', $dir . '/.htaccess', 'Deny from all'); } } // load global config $file = new JsonFile($config->get('home') . '/config.json'); if ($file->exists()) { if ($io && $io->isDebug()) { $io->writeError('Loading config file ' . $file->getPath()); } $config->merge($file->read()); } $config->setConfigSource(new JsonConfigSource($file)); // load global auth file $file = new JsonFile($config->get('home') . '/auth.json'); if ($file->exists()) { if ($io && $io->isDebug()) { $io->writeError('Loading config file ' . $file->getPath()); } $config->merge(array('config' => $file->read())); } $config->setAuthConfigSource(new JsonConfigSource($file, true)); // load COMPOSER_AUTH environment variable if set if ($composerAuthEnv = getenv('COMPOSER_AUTH')) { $authData = json_decode($composerAuthEnv, true); if (is_null($authData)) { throw new \UnexpectedValueException('COMPOSER_AUTH environment variable is malformed, should be a valid JSON object'); } if ($io && $io->isDebug()) { $io->writeError('Loading auth config from COMPOSER_AUTH'); } $config->merge(array('config' => $authData)); } return $config; }
/** * @param IOInterface|null $io * @return Config */ public static function createConfig(IOInterface $io = null, $cwd = null) { $cwd = $cwd ?: getcwd(); // determine home and cache dirs $home = self::getHomeDir(); $cacheDir = self::getCacheDir($home); $dataDir = self::getDataDir($home); // Protect directory against web access. Since HOME could be // the www-data's user home and be web-accessible it is a // potential security risk foreach (array($home, $cacheDir, $dataDir) as $dir) { if (!file_exists($dir . '/.htaccess')) { if (!is_dir($dir)) { @mkdir($dir, 0777, true); } @file_put_contents($dir . '/.htaccess', 'Deny from all'); } } $config = new Config(true, $cwd); // add dirs to the config $config->merge(array('config' => array('home' => $home, 'cache-dir' => $cacheDir, 'data-dir' => $dataDir))); // load global config $file = new JsonFile($config->get('home') . '/config.json'); if ($file->exists()) { if ($io && $io->isDebug()) { $io->writeError('Loading config file ' . $file->getPath()); } $config->merge($file->read()); } $config->setConfigSource(new JsonConfigSource($file)); // load global auth file $file = new JsonFile($config->get('home') . '/auth.json'); if ($file->exists()) { if ($io && $io->isDebug()) { $io->writeError('Loading config file ' . $file->getPath()); } $config->merge(array('config' => $file->read())); } $config->setAuthConfigSource(new JsonConfigSource($file, true)); return $config; }
/** * @return Config */ public static function createConfig() { // determine home and cache dirs $home = getenv('COMPOSER_HOME'); $cacheDir = getenv('COMPOSER_CACHE_DIR'); if (!$home) { if (defined('PHP_WINDOWS_VERSION_MAJOR')) { if (!getenv('APPDATA')) { throw new \RuntimeException('The APPDATA or COMPOSER_HOME environment variable must be set for composer to run correctly'); } $home = strtr(getenv('APPDATA'), '\\', '/') . '/Composer'; } else { if (!getenv('HOME')) { throw new \RuntimeException('The HOME or COMPOSER_HOME environment variable must be set for composer to run correctly'); } $home = rtrim(getenv('HOME'), '/') . '/.composer'; } } if (!$cacheDir) { if (defined('PHP_WINDOWS_VERSION_MAJOR')) { if ($cacheDir = getenv('LOCALAPPDATA')) { $cacheDir .= '/Composer'; } else { $cacheDir = getenv('APPDATA') . '/Composer/cache'; } $cacheDir = strtr($cacheDir, '\\', '/'); } else { $cacheDir = $home . '/cache'; } } // Protect directory against web access. Since HOME could be // the www-data's user home and be web-accessible it is a // potential security risk foreach (array($home, $cacheDir) as $dir) { if (!file_exists($dir . '/.htaccess')) { if (!is_dir($dir)) { @mkdir($dir, 0777, true); } @file_put_contents($dir . '/.htaccess', 'Deny from all'); } } $config = new Config(); // add dirs to the config $config->merge(array('config' => array('home' => $home, 'cache-dir' => $cacheDir))); $file = new JsonFile($home . '/config.json'); if ($file->exists()) { $config->merge($file->read()); } $config->setConfigSource(new JsonConfigSource($file)); // move old cache dirs to the new locations $legacyPaths = array('cache-repo-dir' => array('/cache' => '/http*', '/cache.svn' => '/*', '/cache.github' => '/*'), 'cache-vcs-dir' => array('/cache.git' => '/*', '/cache.hg' => '/*'), 'cache-files-dir' => array('/cache.files' => '/*')); foreach ($legacyPaths as $key => $oldPaths) { foreach ($oldPaths as $oldPath => $match) { $dir = $config->get($key); if ('/cache.github' === $oldPath) { $dir .= '/github.com'; } $oldPath = $config->get('home') . $oldPath; $oldPathMatch = $oldPath . $match; if (is_dir($oldPath) && $dir !== $oldPath) { if (!is_dir($dir)) { if (!@mkdir($dir, 0777, true)) { continue; } } if (is_array($children = glob($oldPathMatch))) { foreach ($children as $child) { @rename($child, $dir . '/' . basename($child)); } } @rmdir($oldPath); } } } return $config; }
/** * Get the package index instance * * We need to construct the instance manually, because there's no way to select * a particular instance using $composer->getRepositoryManager() * * @return ComposerRepository */ private function package_index() { static $package_index; if (!$package_index) { $config = new Config(); $config->merge(array('config' => array('home' => dirname($this->get_composer_json_path())))); $config->setConfigSource(new JsonConfigSource($this->get_composer_json())); $package_index = new ComposerRepository(array('url' => self::PACKAGE_INDEX_URL), new NullIO(), $config); } return $package_index; }