本文整理汇总了PHP中Composer\Json\JsonFile::exists方法的典型用法代码示例。如果您正苦于以下问题:PHP JsonFile::exists方法的具体用法?PHP JsonFile::exists怎么用?PHP JsonFile::exists使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Composer\Json\JsonFile
的用法示例。
在下文中一共展示了JsonFile::exists方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: execute
/**
* {@inheritdoc}
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$composer = $this->getComposer();
$config = $composer->getConfig();
$isInitDone = true;
// Init packages.json
$directory = $this->getRepositoryDirectory();
$file = new JsonFile($directory . '/packages.json');
if (!$file->exists()) {
$output->writeln('<info>Initializing composer repository in</info> <comment>' . $directory . '</comment>');
$file->write(array('packages' => (object) array()));
$isInitDone = false;
}
// Init ~/composer/config.json
$file = new JsonFile($this->getComposerHome() . '/config.json');
$config = $file->exists() ? $file->read() : array();
if (!isset($config['repositories'])) {
$config['repositories'] = array();
}
$isRepoActived = false;
foreach ($config['repositories'] as $repo) {
if ($repo['type'] === 'composer' && $repo['url'] === 'file://' . $directory) {
$isRepoActived = true;
}
}
if (!$isRepoActived) {
$output->writeln('<info>Writing stone repository in global configuration</info>');
$config['repositories'][] = array('type' => 'composer', 'url' => 'file://' . $directory);
$file->write($config);
$isInitDone = false;
}
if ($isInitDone) {
$output->writeln('<info>It seems stone is already configured</info>');
}
}
示例2: execute
/**
* @param InputInterface $input The input instance
* @param OutputInterface $output The output instance
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$verbose = $input->getOption('verbose');
$file = new JsonFile($input->getArgument('file'));
if (!$file->exists()) {
$output->writeln('<error>File not found: ' . $input->getArgument('file') . '</error>');
return 1;
}
$config = $file->read();
// disable packagist by default
unset(Config::$defaultRepositories['packagist']);
// fetch options
$requireAll = isset($config['require-all']) && true === $config['require-all'];
if (!$requireAll && !isset($config['require'])) {
$output->writeln('No explicit requires defined, enabling require-all');
$requireAll = true;
}
$composer = $this->getApplication()->getComposer(true, $config);
$packages = $this->selectPackages($composer, $output, $verbose, $requireAll);
if (!($outputDir = $input->getArgument('output-dir'))) {
$outputDir = isset($config['output-dir']) ? $config['output-dir'] : null;
}
if ($htmlView = !$input->getOption('no-html-output')) {
$htmlView = !isset($config['output-html']) || $config['output-html'];
}
$filename = $outputDir . '/packages.json';
$this->dumpJson($packages, $output, $filename);
if ($htmlView) {
$rootPackage = $composer->getPackage();
$this->dumpWeb($packages, $output, $rootPackage, $outputDir);
}
}
示例3: updateJson
/**
* Set up Composer JSON file.
*
* @return array|null
*/
public function updateJson()
{
if (!is_file($this->getOption('composerjson'))) {
$this->initJson($this->getOption('composerjson'));
}
$jsonFile = new JsonFile($this->getOption('composerjson'));
if ($jsonFile->exists()) {
$json = $jsonorig = $jsonFile->read();
// Workaround Bolt 2.0 installs with "require": []
if (isset($json['require']) && empty($json['require'])) {
unset($json['require']);
}
$json = $this->setJsonDefaults($json);
} else {
// Error
$this->messages[] = Trans::__("The Bolt extensions file '%composerjson%' isn't readable.", ['%composerjson%' => $this->getOption('composerjson')]);
$this->app['extend.writeable'] = false;
$this->app['extend.online'] = false;
return null;
}
// Write out the file, but only if it's actually changed, and if it's writable.
if ($json != $jsonorig) {
try {
umask(00);
$jsonFile->write($json);
} catch (\Exception $e) {
$this->messages[] = Trans::__('The Bolt extensions Repo at %repository% is currently unavailable. Check your connection and try again shortly.', ['%repository%' => $this->app['extend.site']]);
}
}
return $json;
}
示例4: has
/**
* Search for a given package version.
*
* Usage examples : Composition::has('php', '5.3.*') // PHP version
* Composition::has('ext-memcache') // PHP extension
* Composition::has('vendor/package', '>2.1') // Package version
*
* @param type $packageName The package name
* @param type $prettyString An optional version constraint
*
* @return boolean Wether or not the package has been found.
*/
public static function has($packageName, $prettyString = '*')
{
if (null === self::$pool) {
if (null === self::$rootDir) {
self::$rootDir = getcwd();
if (!file_exists(self::$rootDir . '/composer.json')) {
throw new \RuntimeException('Unable to guess the project root dir, please specify it manually using the Composition::setRootDir method.');
}
}
$minimumStability = 'dev';
$config = new Config();
$file = new JsonFile(self::$rootDir . '/composer.json');
if ($file->exists()) {
$projectConfig = $file->read();
$config->merge($projectConfig);
if (isset($projectConfig['minimum-stability'])) {
$minimumStability = $projectConfig['minimum-stability'];
}
}
$vendorDir = self::$rootDir . '/' . $config->get('vendor-dir');
$pool = new Pool($minimumStability);
$pool->addRepository(new PlatformRepository());
$pool->addRepository(new InstalledFilesystemRepository(new JsonFile($vendorDir . '/composer/installed.json')));
$pool->addRepository(new InstalledFilesystemRepository(new JsonFile($vendorDir . '/composer/installed_dev.json')));
self::$pool = $pool;
}
$parser = new VersionParser();
$constraint = $parser->parseConstraints($prettyString);
$packages = self::$pool->whatProvides($packageName, $constraint);
return empty($packages) ? false : true;
}
示例5: 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);
}
}
示例6: execute
/**
* @param InputInterface $input The input instance
* @param OutputInterface $output The output instance
*
* @return int
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
/** @var FormatterHelper $formatter */
$formatter = $this->getHelper('formatter');
$configFile = $input->getArgument('file');
$repositoryUrl = $input->getArgument('url');
if (preg_match('{^https?://}i', $configFile)) {
$output->writeln('<error>Unable to write to remote file ' . $configFile . '</error>');
return 2;
}
$file = new JsonFile($configFile);
if (!$file->exists()) {
$output->writeln('<error>File not found: ' . $configFile . '</error>');
return 1;
}
if (!$this->isRepositoryValid($repositoryUrl)) {
$output->writeln('<error>Invalid Repository URL: ' . $repositoryUrl . '</error>');
return 3;
}
$config = $file->read();
if (!isset($config['repositories']) || !is_array($config['repositories'])) {
$config['repositories'] = [];
}
foreach ($config['repositories'] as $repository) {
if (isset($repository['url']) && $repository['url'] == $repositoryUrl) {
$output->writeln('<error>Repository already added to the file</error>');
return 4;
}
}
$config['repositories'][] = ['type' => 'vcs', 'url' => $repositoryUrl];
$file->write($config);
$output->writeln(['', $formatter->formatBlock('Your configuration file successfully updated! It\'s time to rebuild your repository', 'bg=blue;fg=white', true), '']);
return 0;
}
示例7: initialize
/**
* {@inheritDoc}
*/
protected function initialize(InputInterface $input, OutputInterface $output)
{
if ($input->getOption('global') && 'composer.json' !== $input->getOption('file')) {
throw new \RuntimeException('--file and --global can not be combined');
}
$this->config = Factory::createConfig($this->getIO());
// Get the local composer.json, global config.json, or if the user
// passed in a file to use
$configFile = $input->getOption('global') ? $this->config->get('home') . '/config.json' : $input->getOption('file');
$this->configFile = new JsonFile($configFile);
$this->configSource = new JsonConfigSource($this->configFile);
$authConfigFile = $input->getOption('global') ? $this->config->get('home') . '/auth.json' : dirname(realpath($input->getOption('file'))) . '/auth.json';
$this->authConfigFile = new JsonFile($authConfigFile);
$this->authConfigSource = new JsonConfigSource($this->authConfigFile, true);
// initialize the global file if it's not there
if ($input->getOption('global') && !$this->configFile->exists()) {
touch($this->configFile->getPath());
$this->configFile->write(array('config' => new \ArrayObject()));
@chmod($this->configFile->getPath(), 0600);
}
if ($input->getOption('global') && !$this->authConfigFile->exists()) {
touch($this->authConfigFile->getPath());
$this->authConfigFile->write(array('http-basic' => new \ArrayObject(), 'github-oauth' => new \ArrayObject()));
@chmod($this->authConfigFile->getPath(), 0600);
}
if (!$this->configFile->exists()) {
throw new \RuntimeException('No composer.json found in the current directory');
}
}
示例8: createComposer
/**
* Creates a Composer instance
*
* @param IOInterface $io IO instance
* @param mixed $localConfig either a configuration array or a filename to read from, if null it will read from the default filename
* @return Composer
*/
public function createComposer(IOInterface $io, $localConfig = null)
{
// load Composer configuration
if (null === $localConfig) {
$localConfig = $this->getComposerFile();
}
if (is_string($localConfig)) {
$composerFile = $localConfig;
$file = new JsonFile($localConfig, new RemoteFilesystem($io));
if (!$file->exists()) {
if ($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();
}
// Configuration defaults
$config = static::createConfig();
$config->merge($localConfig);
$vendorDir = $config->get('vendor-dir');
$binDir = $config->get('bin-dir');
// setup process timeout
ProcessExecutor::setTimeout((int) $config->get('process-timeout'));
// initialize repository manager
$rm = $this->createRepositoryManager($io, $config);
// load default repository unless it's explicitly disabled
$localConfig = $this->addPackagistRepository($localConfig);
// load local repository
$this->addLocalRepository($rm, $vendorDir);
// load package
$loader = new Package\Loader\RootPackageLoader($rm);
$package = $loader->load($localConfig);
// initialize download manager
$dm = $this->createDownloadManager($io);
// initialize installation manager
$im = $this->createInstallationManager($rm, $dm, $vendorDir, $binDir, $io);
// purge packages if they have been deleted on the filesystem
$this->purgePackages($rm, $im);
// initialize composer
$composer = new Composer();
$composer->setConfig($config);
$composer->setPackage($package);
$composer->setRepositoryManager($rm);
$composer->setDownloadManager($dm);
$composer->setInstallationManager($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(new JsonFile($lockFile, new RemoteFilesystem($io)), $rm, md5_file($composerFile));
$composer->setLocker($locker);
}
return $composer;
}
示例9: getExtensionConfig
public function getExtensionConfig()
{
$json = new JsonFile($this->getBasepath() . '/composer.json');
if ($json->exists()) {
$composerjson = $json->read();
return array(strtolower($composerjson['name']) => array('name' => $this->getName(), 'json' => $composerjson));
} else {
return array($this->getName() => array('name' => $extension->getName(), 'json' => array()));
}
}
示例10: readAuthConfig
/**
* Reads auth config from given json file path and returns it.
*
* @param string $authFilePath
* @return array
* @throws \Seld\JsonLint\ParsingException
*/
private function readAuthConfig($authFilePath)
{
$file = new JsonFile($authFilePath);
if ($file->exists()) {
$auth = $file->read();
if (isset($auth['config']) && isset($auth['config']['basic-auth'])) {
return $auth['config']['basic-auth'];
}
}
return array();
}
示例11: createConfig
/**
* @param string $path
*
* @return Config
*/
public static function createConfig($path)
{
$config = new Config();
// load global config
$file = new JsonFile($path);
if ($file->exists()) {
$config->merge($file->read());
}
$config->setConfigSource(new JsonConfigSource($file));
return $config;
}
示例12: getComposerJSON
/**
* Get the contents of the extension's composer.json file, lazy-loading
* as needed.
*/
public function getComposerJSON()
{
if (!$this->composerJsonLoaded && !$this->composerJson) {
$this->composerJsonLoaded = true;
$this->composerJson = null;
$jsonFile = new JsonFile(__DIR__ . '/composer.json');
if ($jsonFile->exists()) {
$this->composerJson = $jsonFile->read();
}
}
return $this->composerJson;
}
示例13: execute
protected function execute(InputInterface $input, OutputInterface $output)
{
$configFile = $input->getArgument('file');
$file = new JsonFile($configFile);
if (!$file->exists()) {
$output->writeln('<error>File not found: ' . $configFile . '</error>');
return 1;
}
$config = $file->read();
/**
* Check whether archive is defined
*/
if (!isset($config['archive']) || !isset($config['archive']['directory'])) {
$output->writeln('<error>You must define "archive" parameter in your ' . $configFile . '</error>');
return 1;
}
if (!($outputDir = $input->getArgument('output-dir'))) {
throw new \InvalidArgumentException('The output dir must be specified as second argument');
}
$files = glob($outputDir . "/include/*.json");
if (empty($files)) {
$output->writeln('<info>No log file</info>');
return 1;
}
$files = array_combine($files, array_map("filemtime", $files));
arsort($files);
$file = file_get_contents(key($files));
$json = json_decode($file, true);
$needed = null;
foreach ($json['packages'] as $key => $value) {
$needed[] = str_replace("/", "-", $key);
}
/**
* Packages in output-dir
*/
$files = scandir($outputDir . "/" . $config['archive']['directory'], 1);
if (empty($files)) {
$output->writeln('<info>No archived files</info>');
return 1;
}
/**
* Get vendor-package of archived files
*/
$regex = "/^(.+)-(?:[^-]+)-(?:[^-]+)\\.(?:.+)\$/";
foreach ($files as $file) {
preg_match($regex, $file, $matches);
if (isset($matches[1]) && !in_array($matches[1], $needed)) {
$output->writeln("<info>" . $matches[1] . " :: deleted</info>");
unlink($outputDir . "/" . $config['archive']['directory'] . "/" . $file);
}
}
$output->writeln("<info>Purge :: finished</info>");
}
示例14: 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);
}
}
示例15: execute
/**
* Generate configuration file
*
* @param InputInterface $input The input instance
* @param OutputInterface $output The output instance
* @return int
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$formatter = $this->getHelperSet()->get('formatter');
$configFile = $input->getArgument('file');
if (preg_match('{^https?://}i', $configFile)) {
$output->writeln('<error>Unable to write to remote file ' . $configFile . '</error>');
return 2;
}
$file = new JsonFile($configFile);
if ($file->exists()) {
$output->writeln('<error>Configuration file already exists</error>');
return 1;
}
$config = array('name' => $input->getOption('name'), 'homepage' => $input->getOption('homepage'), 'repositories' => array(), 'require-all' => true);
$file->write($config);
$output->writeln(array('', $formatter->formatBlock('Your configuration file successfully created!', 'bg=blue;fg=white', true), ''));
$output->writeln(array('', 'You are ready to add your package repositories', 'Use <comment>satis add repository-url</comment> to add them.', ''));
return 0;
}