function InitCommand::execute
Throws
\Seld\JsonLint\ParsingException
Overrides Command::execute
File
-
vendor/
composer/ composer/ src/ Composer/ Command/ InitCommand.php, line 87
Class
- InitCommand
- @author Justin Rainbow <justin.rainbow@gmail.com> @author Jordi Boggiano <j.boggiano@seld.be>
Namespace
Composer\CommandCode
protected function execute(InputInterface $input, OutputInterface $output) : int {
$io = $this->getIO();
$allowlist = [
'name',
'description',
'author',
'type',
'homepage',
'require',
'require-dev',
'stability',
'license',
'autoload',
];
$options = array_filter(array_intersect_key($input->getOptions(), array_flip($allowlist)), function ($val) {
return $val !== null && $val !== [];
});
if (isset($options['name']) && !Preg::isMatch('{^[a-z0-9_.-]+/[a-z0-9_.-]+$}D', $options['name'])) {
throw new \InvalidArgumentException('The package name ' . $options['name'] . ' is invalid, it should be lowercase and have a vendor name, a forward slash, and a package name, matching: [a-z0-9_.-]+/[a-z0-9_.-]+');
}
if (isset($options['author'])) {
$options['authors'] = $this->formatAuthors($options['author']);
unset($options['author']);
}
$repositories = $input->getOption('repository');
if (count($repositories) > 0) {
$config = Factory::createConfig($io);
foreach ($repositories as $repo) {
$options['repositories'][] = RepositoryFactory::configFromString($io, $config, $repo, true);
}
}
if (isset($options['stability'])) {
$options['minimum-stability'] = $options['stability'];
unset($options['stability']);
}
$options['require'] = isset($options['require']) ? $this->formatRequirements($options['require']) : new \stdClass();
if ([] === $options['require']) {
$options['require'] = new \stdClass();
}
if (isset($options['require-dev'])) {
$options['require-dev'] = $this->formatRequirements($options['require-dev']);
if ([] === $options['require-dev']) {
$options['require-dev'] = new \stdClass();
}
}
// --autoload - create autoload object
$autoloadPath = null;
if (isset($options['autoload'])) {
$autoloadPath = $options['autoload'];
$namespace = $this->namespaceFromPackageName((string) $input->getOption('name'));
$options['autoload'] = (object) [
'psr-4' => [
$namespace . '\\' => $autoloadPath,
],
];
}
$file = new JsonFile(Factory::getComposerFile());
$json = JsonFile::encode($options);
if ($input->isInteractive()) {
$io->writeError([
'',
$json,
'',
]);
if (!$io->askConfirmation('Do you confirm generation [<comment>yes</comment>]? ')) {
$io->writeError('<error>Command aborted</error>');
return 1;
}
}
else {
if (json_encode($options) === '{"require":{}}') {
throw new \RuntimeException('You have to run this command in interactive mode, or specify at least some data using --name, --require, etc.');
}
$io->writeError('Writing ' . $file->getPath());
}
$file->write($options);
try {
$file->validateSchema(JsonFile::LAX_SCHEMA);
} catch (JsonValidationException $e) {
$io->writeError('<error>Schema validation error, aborting</error>');
$errors = ' - ' . implode(PHP_EOL . ' - ', $e->getErrors());
$io->writeError($e->getMessage() . ':' . PHP_EOL . $errors);
Silencer::call('unlink', $file->getPath());
return 1;
}
// --autoload - Create src folder
if ($autoloadPath) {
$filesystem = new Filesystem();
$filesystem->ensureDirectoryExists($autoloadPath);
// dump-autoload only for projects without added dependencies.
if (!$this->hasDependencies($options)) {
$this->runDumpAutoloadCommand($output);
}
}
if ($input->isInteractive() && is_dir('.git')) {
$ignoreFile = realpath('.gitignore');
if (false === $ignoreFile) {
$ignoreFile = realpath('.') . '/.gitignore';
}
if (!$this->hasVendorIgnore($ignoreFile)) {
$question = 'Would you like the <info>vendor</info> directory added to your <info>.gitignore</info> [<comment>yes</comment>]? ';
if ($io->askConfirmation($question)) {
$this->addVendorIgnore($ignoreFile);
}
}
}
$question = 'Would you like to install dependencies now [<comment>yes</comment>]? ';
if ($input->isInteractive() && $this->hasDependencies($options) && $io->askConfirmation($question)) {
$this->updateDependencies($output);
}
// --autoload - Show post-install configuration info
if ($autoloadPath) {
$namespace = $this->namespaceFromPackageName((string) $input->getOption('name'));
$io->writeError('PSR-4 autoloading configured. Use "<comment>namespace ' . $namespace . ';</comment>" in ' . $autoloadPath);
$io->writeError('Include the Composer autoloader with: <comment>require \'vendor/autoload.php\';</comment>');
}
return 0;
}