本文整理汇总了PHP中Composer\Json\JsonFile::getPath方法的典型用法代码示例。如果您正苦于以下问题:PHP JsonFile::getPath方法的具体用法?PHP JsonFile::getPath怎么用?PHP JsonFile::getPath使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Composer\Json\JsonFile
的用法示例。
在下文中一共展示了JsonFile::getPath方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: execute
/**
* Remove packages from the root install.
*
* @param $packages array Indexed array of package names to remove
*
* @throws \Bolt\Exception\PackageManagerException
*
* @return int 0 on success or a positive error code on failure
*/
public function execute(array $packages)
{
if (empty($packages)) {
throw new PackageManagerException('No package specified for removal');
}
$io = $this->app['extend.manager']->getIO();
$options = $this->app['extend.manager']->getOptions();
$jsonFile = new JsonFile($options['composerjson']);
$composerDefinition = $jsonFile->read();
$composerBackup = file_get_contents($jsonFile->getPath());
$json = new JsonConfigSource($jsonFile);
$type = $options['dev'] ? 'require-dev' : 'require';
// Remove packages from JSON
foreach ($packages as $package) {
if (isset($composerDefinition[$type][$package])) {
$json->removeLink($type, $package);
}
}
// Reload Composer config
$composer = $this->app['extend.manager']->getFactory()->resetComposer();
$install = Installer::create($io, $composer);
try {
$install->setVerbose($options['verbose'])->setDevMode(!$options['updatenodev'])->setUpdate(true)->setUpdateWhitelist($packages)->setWhitelistDependencies($options['updatewithdependencies'])->setIgnorePlatformRequirements($options['ignoreplatformreqs']);
$status = $install->run();
if ($status !== 0) {
// Write out old JSON file
file_put_contents($jsonFile->getPath(), $composerBackup);
}
} catch (\Exception $e) {
$msg = __CLASS__ . '::' . __FUNCTION__ . ' recieved an error from Composer: ' . $e->getMessage() . ' in ' . $e->getFile() . '::' . $e->getLine();
$this->app['logger.system']->critical($msg, array('event' => 'exception', 'exception' => $e));
throw new PackageManagerException($e->getMessage(), $e->getCode(), $e);
}
return $status;
}
示例2: manipulateJson
protected function manipulateJson($method, $args, $fallback)
{
$args = func_get_args();
// remove method & fallback
array_shift($args);
$fallback = array_pop($args);
if ($this->file->exists()) {
$contents = file_get_contents($this->file->getPath());
} else {
$contents = "{\n \"config\": {\n }\n}\n";
}
$manipulator = new JsonManipulator($contents);
$newFile = !$this->file->exists();
// try to update cleanly
if (call_user_func_array(array($manipulator, $method), $args)) {
file_put_contents($this->file->getPath(), $manipulator->getContents());
} else {
// on failed clean update, call the fallback and rewrite the whole file
$config = $this->file->read();
$this->array_unshift_ref($args, $config);
call_user_func_array($fallback, $args);
$this->file->write($config);
}
if ($newFile) {
@chmod($this->file->getPath(), 0600);
}
}
示例3: execute
protected function execute(InputInterface $input, OutputInterface $output)
{
$file = Factory::getComposerFile();
if (!file_exists($file) && !file_put_contents($file, "{\n}\n")) {
$output->writeln('<error>' . $file . ' could not be created.</error>');
return 1;
}
if (!is_readable($file)) {
$output->writeln('<error>' . $file . ' is not readable.</error>');
return 1;
}
if (!is_writable($file)) {
$output->writeln('<error>' . $file . ' is not writable.</error>');
return 1;
}
$json = new JsonFile($file);
$composer = $json->read();
$composerBackup = file_get_contents($json->getPath());
$requirements = $this->determineRequirements($input, $output, $input->getArgument('packages'));
$requireKey = $input->getOption('dev') ? 'require-dev' : 'require';
$removeKey = $input->getOption('dev') ? 'require' : 'require-dev';
$baseRequirements = array_key_exists($requireKey, $composer) ? $composer[$requireKey] : array();
$requirements = $this->formatRequirements($requirements);
// validate requirements format
$versionParser = new VersionParser();
foreach ($requirements as $constraint) {
$versionParser->parseConstraints($constraint);
}
if (!$this->updateFileCleanly($json, $baseRequirements, $requirements, $requireKey, $removeKey)) {
foreach ($requirements as $package => $version) {
$baseRequirements[$package] = $version;
if (isset($composer[$removeKey][$package])) {
unset($composer[$removeKey][$package]);
}
}
$composer[$requireKey] = $baseRequirements;
$json->write($composer);
}
$output->writeln('<info>' . $file . ' has been updated</info>');
if ($input->getOption('no-update')) {
return 0;
}
$updateDevMode = !$input->getOption('update-no-dev');
// Update packages
$composer = $this->getComposer();
$composer->getDownloadManager()->setOutputProgress(!$input->getOption('no-progress'));
$io = $this->getIO();
$commandEvent = new CommandEvent(PluginEvents::COMMAND, 'require', $input, $output);
$composer->getEventDispatcher()->dispatch($commandEvent->getName(), $commandEvent);
$install = Installer::create($io, $composer);
$install->setVerbose($input->getOption('verbose'))->setPreferSource($input->getOption('prefer-source'))->setPreferDist($input->getOption('prefer-dist'))->setDevMode($updateDevMode)->setUpdate(true)->setUpdateWhitelist(array_keys($requirements))->setWhitelistDependencies($input->getOption('update-with-dependencies'));
$status = $install->run();
if ($status !== 0) {
$output->writeln("\n" . '<error>Installation failed, reverting ' . $file . ' to its original content.</error>');
file_put_contents($json->getPath(), $composerBackup);
}
return $status;
}
示例4: execute
protected function execute(InputInterface $input, OutputInterface $output)
{
$packages = $input->getArgument('packages');
$file = Factory::getComposerFile();
$jsonFile = new JsonFile($file);
$composer = $jsonFile->read();
$composerBackup = file_get_contents($jsonFile->getPath());
$json = new JsonConfigSource($jsonFile);
$type = $input->getOption('dev') ? 'require-dev' : 'require';
$altType = !$input->getOption('dev') ? 'require-dev' : 'require';
$io = $this->getIO();
foreach ($packages as $package) {
if (isset($composer[$type][$package])) {
$json->removeLink($type, $package);
} elseif (isset($composer[$altType][$package])) {
$io->writeError('<warning>' . $package . ' could not be found in ' . $type . ' but it is present in ' . $altType . '</warning>');
if ($io->isInteractive()) {
if ($io->askConfirmation('Do you want to remove it from ' . $altType . ' [<comment>yes</comment>]? ', true)) {
$json->removeLink($altType, $package);
}
}
} else {
$io->writeError('<warning>' . $package . ' is not required in your composer.json and has not been removed</warning>');
}
}
if ($input->getOption('no-update')) {
return 0;
}
// Update packages
$composer = $this->getComposer();
$composer->getDownloadManager()->setOutputProgress(!$input->getOption('no-progress'));
$commandEvent = new CommandEvent(PluginEvents::COMMAND, 'remove', $input, $output);
$composer->getEventDispatcher()->dispatch($commandEvent->getName(), $commandEvent);
$install = Installer::create($io, $composer);
$updateDevMode = !$input->getOption('update-no-dev');
$optimize = $input->getOption('optimize-autoloader') || $composer->getConfig()->get('optimize-autoloader');
$authoritative = $input->getOption('classmap-authoritative') || $composer->getConfig()->get('classmap-authoritative');
$install->setVerbose($input->getOption('verbose'))->setDevMode($updateDevMode)->setOptimizeAutoloader($optimize)->setClassMapAuthoritative($authoritative)->setUpdate(true)->setUpdateWhitelist($packages)->setWhitelistDependencies($input->getOption('update-with-dependencies'))->setIgnorePlatformRequirements($input->getOption('ignore-platform-reqs'));
$status = $install->run();
if ($status !== 0) {
$io->writeError("\n" . '<error>Removal failed, reverting ' . $file . ' to its original content.</error>');
file_put_contents($jsonFile->getPath(), $composerBackup);
}
return $status;
}
示例5: execute
protected function execute(InputInterface $input, OutputInterface $output)
{
$file = Factory::getComposerFile();
if (!file_exists($file) && !file_put_contents($file, "{\n}\n")) {
$output->writeln('<error>' . $file . ' could not be created.</error>');
return 1;
}
if (!is_readable($file)) {
$output->writeln('<error>' . $file . ' is not readable.</error>');
return 1;
}
if (!is_writable($file)) {
$output->writeln('<error>' . $file . ' is not writable.</error>');
return 1;
}
$dialog = $this->getHelperSet()->get('dialog');
$json = new JsonFile($file);
$composer = $json->read();
$composerBackup = file_get_contents($json->getPath());
$requirements = $this->determineRequirements($input, $output, $input->getArgument('packages'));
$requireKey = $input->getOption('dev') ? 'require-dev' : 'require';
$baseRequirements = array_key_exists($requireKey, $composer) ? $composer[$requireKey] : array();
$requirements = $this->formatRequirements($requirements);
if (!$this->updateFileCleanly($json, $baseRequirements, $requirements, $requireKey)) {
foreach ($requirements as $package => $version) {
$baseRequirements[$package] = $version;
}
$composer[$requireKey] = $baseRequirements;
$json->write($composer);
}
$output->writeln('<info>' . $file . ' has been updated</info>');
if ($input->getOption('no-update')) {
return 0;
}
// Update packages
$composer = $this->getComposer();
$composer->getDownloadManager()->setOutputProgress(!$input->getOption('no-progress'));
$io = $this->getIO();
$install = Installer::create($io, $composer);
$install->setVerbose($input->getOption('verbose'))->setPreferSource($input->getOption('prefer-source'))->setPreferDist($input->getOption('prefer-dist'))->setDevMode(true)->setUpdate(true)->setUpdateWhitelist(array_keys($requirements));
if (!$install->run()) {
$output->writeln("\n" . '<error>Installation failed, reverting ' . $file . ' to its original content.</error>');
file_put_contents($json->getPath(), $composerBackup);
return 1;
}
return 0;
}
示例6: manipulateJson
protected function manipulateJson($method, $args, $fallback)
{
$args = func_get_args();
// remove method & fallback
array_shift($args);
$fallback = array_pop($args);
if ($this->file->exists()) {
if (!is_writable($this->file->getPath())) {
throw new \RuntimeException(sprintf('The file "%s" is not writable.', $this->file->getPath()));
}
if (!is_readable($this->file->getPath())) {
throw new \RuntimeException(sprintf('The file "%s" is not readable.', $this->file->getPath()));
}
$contents = file_get_contents($this->file->getPath());
} elseif ($this->authConfig) {
$contents = "{\n}\n";
} else {
$contents = "{\n \"config\": {\n }\n}\n";
}
$manipulator = new JsonManipulator($contents);
$newFile = !$this->file->exists();
// override manipulator method for auth config files
if ($this->authConfig && $method === 'addConfigSetting') {
$method = 'addSubNode';
list($mainNode, $name) = explode('.', $args[0], 2);
$args = array($mainNode, $name, $args[1]);
} elseif ($this->authConfig && $method === 'removeConfigSetting') {
$method = 'removeSubNode';
list($mainNode, $name) = explode('.', $args[0], 2);
$args = array($mainNode, $name);
}
// try to update cleanly
if (call_user_func_array(array($manipulator, $method), $args)) {
file_put_contents($this->file->getPath(), $manipulator->getContents());
} else {
// on failed clean update, call the fallback and rewrite the whole file
$config = $this->file->read();
$this->arrayUnshiftRef($args, $config);
call_user_func_array($fallback, $args);
$this->file->write($config);
}
if ($newFile) {
Silencer::call('chmod', $this->file->getPath(), 0600);
}
}
示例7: createComposer
/**
* Creates a Composer instance
*
* @param IOInterface $io IO instance
* @param array|string|null $localConfig either a configuration array or a filename to read from, if null it will
* read from the default filename
* @param bool $disablePlugins Whether plugins should not be loaded
* @throws \InvalidArgumentException
* @throws \UnexpectedValueException
* @return Composer
*/
public function createComposer(IOInterface $io, $localConfig = null, $disablePlugins = false)
{
// load Composer configuration
if (null === $localConfig) {
$localConfig = static::getComposerFile();
}
if (is_string($localConfig)) {
$composerFile = $localConfig;
$file = new JsonFile($localConfig, new RemoteFilesystem($io));
if (!$file->exists()) {
if ($localConfig === './composer.json' || $localConfig === 'composer.json') {
$message = 'Composer could not find a composer.json file in ' . getcwd();
} else {
$message = 'Composer could not find the config file: ' . $localConfig;
}
$instructions = 'To initialize a project, please create a composer.json file as described in the http://getcomposer.org/ "Getting Started" section';
throw new \InvalidArgumentException($message . PHP_EOL . $instructions);
}
$file->validateSchema(JsonFile::LAX_SCHEMA);
$localConfig = $file->read();
}
// Load config and override with local config/auth config
$config = static::createConfig($io);
$config->merge($localConfig);
if (isset($composerFile)) {
if ($io && $io->isDebug()) {
$io->write('Loading config file ' . $composerFile);
}
$localAuthFile = new JsonFile(dirname(realpath($composerFile)) . '/auth.json');
if ($localAuthFile->exists()) {
if ($io && $io->isDebug()) {
$io->write('Loading config file ' . $localAuthFile->getPath());
}
$config->merge(array('config' => $localAuthFile->read()));
$config->setAuthConfigSource(new JsonConfigSource($localAuthFile, true));
}
}
// load auth configs into the IO instance
$io->loadConfiguration($config);
$vendorDir = $config->get('vendor-dir');
$binDir = $config->get('bin-dir');
// setup process timeout
ProcessExecutor::setTimeout((int) $config->get('process-timeout'));
// initialize composer
$composer = new Composer();
$composer->setConfig($config);
// initialize event dispatcher
$dispatcher = new EventDispatcher($composer, $io);
// initialize repository manager
$rm = $this->createRepositoryManager($io, $config, $dispatcher);
// load local repository
$this->addLocalRepository($rm, $vendorDir);
// load package
$parser = new VersionParser();
$loader = new Package\Loader\RootPackageLoader($rm, $config, $parser, new ProcessExecutor($io));
$package = $loader->load($localConfig);
// initialize installation manager
$im = $this->createInstallationManager();
// Composer composition
$composer->setPackage($package);
$composer->setRepositoryManager($rm);
$composer->setInstallationManager($im);
// initialize download manager
$dm = $this->createDownloadManager($io, $config, $dispatcher);
$composer->setDownloadManager($dm);
$composer->setEventDispatcher($dispatcher);
// initialize autoload generator
$generator = new AutoloadGenerator($dispatcher, $io);
$composer->setAutoloadGenerator($generator);
// add installers to the manager
$this->createDefaultInstallers($im, $composer, $io);
$globalRepository = $this->createGlobalRepository($config, $vendorDir);
$pm = $this->createPluginManager($composer, $io, $globalRepository);
$composer->setPluginManager($pm);
if (!$disablePlugins) {
$pm->loadInstalledPlugins();
}
// purge packages if they have been deleted on the filesystem
$this->purgePackages($rm, $im);
// init locker if possible
if (isset($composerFile)) {
$lockFile = "json" === pathinfo($composerFile, PATHINFO_EXTENSION) ? substr($composerFile, 0, -4) . 'lock' : $composerFile . '.lock';
$locker = new Package\Locker($io, new JsonFile($lockFile, new RemoteFilesystem($io, $config)), $rm, $im, md5_file($composerFile));
$composer->setLocker($locker);
}
return $composer;
}
示例8: getConfig
/**
* @return Config
*
* @throws \Exception
*/
protected function getConfig()
{
if (!isset($this->config)) {
$configFilePath = $this->configPathFromInput ?: ConfigFactory::getHomeDir() . '/config.json';
$this->config = ConfigFactory::createConfig($configFilePath);
$configFile = new JsonFile($configFilePath);
$this->configPath = $configFile->getPath();
$this->configSource = new JsonConfigSource($configFile);
// initialize the global file if it's not there
if (!$configFile->exists()) {
$path = $configFile->getPath();
$dir = dirname($path);
if (!is_dir($dir)) {
mkdir($dir, 0777, true);
}
touch($configFile->getPath());
$configFile->write(['config' => new \ArrayObject()]);
@chmod($configFile->getPath(), 0600);
}
if (!$configFile->exists()) {
throw new \RuntimeException('No config.json found in the current directory');
}
}
return $this->config;
}
示例9: updateFileCleanly
protected function updateFileCleanly(JsonFile $json, array $base, array $new, $requireKey, $removeKey, $sortPackages)
{
$extra = $this->extra;
$contents = file_get_contents($json->getPath());
$manipulator = new JsonManipulator($contents);
foreach ($new as $package => $constraint) {
if (!$manipulator->addLink($requireKey, $package, $constraint, $sortPackages)) {
return false;
}
if (!$manipulator->removeSubNode($removeKey, $package)) {
return false;
}
}
if (isset($extra['installer-modules']) && is_array($extra['installer-modules'])) {
$manipulator->addSubNode('extra', 'installer-modules', $extra['installer-modules']);
}
$paths = $this->getComposer()->getPackage()->getExtra();
$paths = $paths['installer-paths'];
if (isset($extra['installer-paths']) && is_array($extra['installer-paths'])) {
foreach ($extra['installer-paths'] as $path => $list) {
if (!is_array($list)) {
continue;
}
if (isset($paths[$path])) {
$result = array_merge($paths[$path], $list);
$result = array_keys(array_flip($result));
$paths[$path] = $result;
} else {
$paths[$path] = $list;
}
}
}
if (isset($extra['system-modules']) && is_array($extra['system-modules'])) {
$modules = $extra['system-modules'];
foreach ($modules as &$module) {
if (strpos($module, '/') === false) {
$module = 'opis-colibri/' . $module;
}
}
$path = 'system/modules/{$name}/';
if (isset($paths[$path])) {
$result = array_merge($paths[$path], $modules);
$result = array_keys(array_flip($result));
$paths[$path] = $result;
}
}
$manipulator->addSubNode('extra', 'installer-paths', $paths);
file_put_contents($json->getPath(), $manipulator->getContents());
return true;
}
示例10: createComposer
/**
* Creates a Composer instance
*
* @param IOInterface $io IO instance
* @param array|string|null $localConfig either a configuration array or a filename to read from, if null it will
* read from the default filename
* @param bool $disablePlugins Whether plugins should not be loaded
* @param bool $fullLoad Whether to initialize everything or only main project stuff (used when loading the global composer)
* @throws \InvalidArgumentException
* @throws \UnexpectedValueException
* @return Composer
*/
public function createComposer(IOInterface $io, $localConfig = null, $disablePlugins = false, $cwd = null, $fullLoad = true)
{
$cwd = $cwd ?: getcwd();
// load Composer configuration
if (null === $localConfig) {
$localConfig = static::getComposerFile();
}
if (is_string($localConfig)) {
$composerFile = $localConfig;
$file = new JsonFile($localConfig, null, $io);
if (!$file->exists()) {
if ($localConfig === './composer.json' || $localConfig === 'composer.json') {
$message = 'Composer could not find a composer.json file in ' . $cwd;
} else {
$message = 'Composer could not find the config file: ' . $localConfig;
}
$instructions = 'To initialize a project, please create a composer.json file as described in the https://getcomposer.org/ "Getting Started" section';
throw new \InvalidArgumentException($message . PHP_EOL . $instructions);
}
$file->validateSchema(JsonFile::LAX_SCHEMA);
$jsonParser = new JsonParser();
try {
$jsonParser->parse(file_get_contents($localConfig), JsonParser::DETECT_KEY_CONFLICTS);
} catch (\Seld\JsonLint\DuplicateKeyException $e) {
$details = $e->getDetails();
$io->writeError('<warning>Key ' . $details['key'] . ' is a duplicate in ' . $localConfig . ' at line ' . $details['line'] . '</warning>');
}
$localConfig = $file->read();
}
// Load config and override with local config/auth config
$config = static::createConfig($io, $cwd);
$config->merge($localConfig);
if (isset($composerFile)) {
$io->writeError('Loading config file ' . $composerFile, true, IOInterface::DEBUG);
$localAuthFile = new JsonFile(dirname(realpath($composerFile)) . '/auth.json');
if ($localAuthFile->exists()) {
$io->writeError('Loading config file ' . $localAuthFile->getPath(), true, IOInterface::DEBUG);
$config->merge(array('config' => $localAuthFile->read()));
$config->setAuthConfigSource(new JsonConfigSource($localAuthFile, true));
}
}
$vendorDir = $config->get('vendor-dir');
$binDir = $config->get('bin-dir');
// initialize composer
$composer = new Composer();
$composer->setConfig($config);
if ($fullLoad) {
// load auth configs into the IO instance
$io->loadConfiguration($config);
}
$rfs = self::createRemoteFilesystem($io, $config);
// initialize event dispatcher
$dispatcher = new EventDispatcher($composer, $io);
$composer->setEventDispatcher($dispatcher);
// initialize repository manager
$rm = $this->createRepositoryManager($io, $config, $dispatcher, $rfs);
$composer->setRepositoryManager($rm);
// load local repository
$this->addLocalRepository($io, $rm, $vendorDir);
// force-set the version of the global package if not defined as
// guessing it adds no value and only takes time
if (!$fullLoad && !isset($localConfig['version'])) {
$localConfig['version'] = '1.0.0';
}
// load package
$parser = new VersionParser();
$guesser = new VersionGuesser($config, new ProcessExecutor($io), $parser);
$loader = new Package\Loader\RootPackageLoader($rm, $config, $parser, $guesser);
$package = $loader->load($localConfig, 'Composer\\Package\\RootPackage', $cwd);
$composer->setPackage($package);
// initialize installation manager
$im = $this->createInstallationManager();
$composer->setInstallationManager($im);
if ($fullLoad) {
// initialize download manager
$dm = $this->createDownloadManager($io, $config, $dispatcher, $rfs);
$composer->setDownloadManager($dm);
// initialize autoload generator
$generator = new AutoloadGenerator($dispatcher, $io);
$composer->setAutoloadGenerator($generator);
}
// add installers to the manager (must happen after download manager is created since they read it out of $composer)
$this->createDefaultInstallers($im, $composer, $io);
if ($fullLoad) {
$globalComposer = $this->createGlobalComposer($io, $config, $disablePlugins);
$pm = $this->createPluginManager($io, $composer, $globalComposer, $disablePlugins);
$composer->setPluginManager($pm);
$pm->loadInstalledPlugins();
//.........这里部分代码省略.........
示例11: updateFileCleanly
/**
* Cleanly update a Composer JSON file.
*
* @param JsonFile $json
* @param array $new
* @param string $requireKey
* @param string $removeKey
* @param boolean $sortPackages
* @param boolean $postreset
*
* @return boolean
*/
private function updateFileCleanly(JsonFile $json, array $new, $requireKey, $removeKey, $sortPackages, $postreset)
{
$contents = file_get_contents($json->getPath());
$manipulator = new JsonManipulator($contents);
foreach ($new as $package => $constraint) {
if ($postreset) {
$constraint = $this->findBestVersionForPackage($package);
}
if (!$manipulator->addLink($requireKey, $package, $constraint, $sortPackages)) {
return false;
}
if (!$manipulator->removeSubNode($removeKey, $package)) {
return false;
}
}
file_put_contents($json->getPath(), $manipulator->getContents());
return true;
}
示例12: load
/**
* Loads previously dumped Packages in order to merge with updates.
*
* @return PackageInterface[]
*/
public function load()
{
$packages = [];
$repoJson = new JsonFile($this->filename);
$dirName = dirname($this->filename);
if ($repoJson->exists()) {
$loader = new ArrayLoader();
$packagesJson = $repoJson->read();
$jsonIncludes = isset($packagesJson['includes']) && is_array($packagesJson['includes']) ? $packagesJson['includes'] : [];
if (isset($packagesJson['providers']) && is_array($packagesJson['providers']) && isset($packagesJson['providers-url'])) {
$baseUrl = $this->homepage ? parse_url(rtrim($this->homepage, '/'), PHP_URL_PATH) . '/' : null;
$baseUrlLength = strlen($baseUrl);
foreach ($packagesJson['providers'] as $packageName => $provider) {
$file = str_replace(['%package%', '%hash%'], [$packageName, $provider['sha256']], $packagesJson['providers-url']);
if ($baseUrl && substr($file, 0, $baseUrlLength) === $baseUrl) {
$file = substr($file, $baseUrlLength);
}
$jsonIncludes[$file] = $provider;
}
}
foreach ($jsonIncludes as $includeFile => $includeConfig) {
$includeJson = new JsonFile($dirName . '/' . $includeFile);
if (!$includeJson->exists()) {
$this->output->writeln(sprintf('<error>File \'%s\' does not exist, defined in "includes" in \'%s\'</error>', $includeJson->getPath(), $repoJson->getPath()));
continue;
}
$jsonPackages = $includeJson->read();
$jsonPackages = isset($jsonPackages['packages']) && is_array($jsonPackages['packages']) ? $jsonPackages['packages'] : [];
foreach ($jsonPackages as $jsonPackage) {
if (is_array($jsonPackage)) {
foreach ($jsonPackage as $jsonVersion) {
if (is_array($jsonVersion)) {
if (isset($jsonVersion['name']) && in_array($jsonVersion['name'], $this->packagesFilter)) {
continue;
}
$package = $loader->load($jsonVersion);
$packages[$package->getUniqueName()] = $package;
}
}
}
}
}
}
return $packages;
}
示例13: updateFileCleanly
protected function updateFileCleanly(JsonFile $json, array $base, array $new, $requireKey, $removeKey, $sortPackages)
{
$contents = file_get_contents($json->getPath());
$manipulator = new JsonManipulator($contents);
foreach ($new as $package => $constraint) {
if (!$manipulator->addLink($requireKey, $package, $constraint, $sortPackages)) {
return false;
}
if (!$manipulator->removeSubNode($removeKey, $package)) {
return false;
}
}
file_put_contents($json->getPath(), $manipulator->getContents());
return true;
}
示例14: execute
protected function execute(InputInterface $input, OutputInterface $output)
{
$packages = $input->getArgument('packages');
$packages = array_map('strtolower', $packages);
$file = Factory::getComposerFile();
$jsonFile = new JsonFile($file);
$composer = $jsonFile->read();
$composerBackup = file_get_contents($jsonFile->getPath());
$json = new JsonConfigSource($jsonFile);
$type = $input->getOption('dev') ? 'require-dev' : 'require';
$altType = !$input->getOption('dev') ? 'require-dev' : 'require';
$io = $this->getIO();
if ($input->getOption('update-with-dependencies')) {
$io->writeError('<warning>You are using the deprecated option "update-with-dependencies". This is now default behaviour. The --no-update-with-dependencies option can be used to remove a package without its dependencies.</warning>');
}
// make sure name checks are done case insensitively
foreach (array('require', 'require-dev') as $linkType) {
if (isset($composer[$linkType])) {
foreach ($composer[$linkType] as $name => $version) {
$composer[$linkType][strtolower($name)] = $version;
}
}
}
foreach ($packages as $package) {
if (isset($composer[$type][$package])) {
$json->removeLink($type, $package);
} elseif (isset($composer[$altType][$package])) {
$io->writeError('<warning>' . $package . ' could not be found in ' . $type . ' but it is present in ' . $altType . '</warning>');
if ($io->isInteractive()) {
if ($io->askConfirmation('Do you want to remove it from ' . $altType . ' [<comment>yes</comment>]? ', true)) {
$json->removeLink($altType, $package);
}
}
} else {
$io->writeError('<warning>' . $package . ' is not required in your composer.json and has not been removed</warning>');
}
}
if ($input->getOption('no-update')) {
return 0;
}
// Update packages
$this->resetComposer();
$composer = $this->getComposer(true, $input->getOption('no-plugins'));
$composer->getDownloadManager()->setOutputProgress(!$input->getOption('no-progress'));
$commandEvent = new CommandEvent(PluginEvents::COMMAND, 'remove', $input, $output);
$composer->getEventDispatcher()->dispatch($commandEvent->getName(), $commandEvent);
$install = Installer::create($io, $composer);
$updateDevMode = !$input->getOption('update-no-dev');
$optimize = $input->getOption('optimize-autoloader') || $composer->getConfig()->get('optimize-autoloader');
$authoritative = $input->getOption('classmap-authoritative') || $composer->getConfig()->get('classmap-authoritative');
$install->setVerbose($input->getOption('verbose'))->setDevMode($updateDevMode)->setOptimizeAutoloader($optimize)->setClassMapAuthoritative($authoritative)->setUpdate(true)->setUpdateWhitelist($packages)->setWhitelistDependencies(!$input->getOption('no-update-with-dependencies'))->setIgnorePlatformRequirements($input->getOption('ignore-platform-reqs'))->setRunScripts(!$input->getOption('no-scripts'));
$exception = null;
try {
$status = $install->run();
} catch (\Exception $exception) {
$status = 1;
}
if ($status !== 0) {
$io->writeError("\n" . '<error>Removal failed, reverting ' . $file . ' to its original content.</error>');
file_put_contents($jsonFile->getPath(), $composerBackup);
}
if ($exception) {
throw $exception;
}
return $status;
}
示例15: load
/**
* Loads previously dumped Packages in order to merge with updates.
*
* @return PackageInterface[]
*/
public function load()
{
$packages = array();
$repoJson = new JsonFile($this->filename);
$dirName = dirname($this->filename);
if ($repoJson->exists()) {
$loader = new ArrayLoader();
$jsonIncludes = $repoJson->read();
$jsonIncludes = isset($jsonIncludes['includes']) && is_array($jsonIncludes['includes']) ? $jsonIncludes['includes'] : array();
foreach ($jsonIncludes as $includeFile => $includeConfig) {
$includeJson = new JsonFile($dirName . '/' . $includeFile);
if (!$includeJson->exists()) {
$this->output->writeln(sprintf('<error>File \'%s\' does not exist, defined in "includes" in \'%s\'</error>', $includeJson->getPath(), $repoJson->getPath()));
continue;
}
$jsonPackages = $includeJson->read();
$jsonPackages = isset($jsonPackages['packages']) && is_array($jsonPackages['packages']) ? $jsonPackages['packages'] : array();
foreach ($jsonPackages as $jsonPackage) {
if (is_array($jsonPackage)) {
foreach ($jsonPackage as $jsonVersion) {
if (is_array($jsonVersion)) {
if (isset($jsonVersion['name']) && in_array($jsonVersion['name'], $this->packagesFilter)) {
continue;
}
$package = $loader->load($jsonVersion);
$packages[$package->getUniqueName()] = $package;
}
}
}
}
}
}
return $packages;
}