protected function execute(InputInterface $input, OutputInterface $output) { $dialog = $this->getHelperSet()->get('dialog'); $whitelist = array('name', 'description', 'author', 'require'); $options = array_filter(array_intersect_key($input->getOptions(), array_flip($whitelist))); if (isset($options['author'])) { $options['authors'] = $this->formatAuthors($options['author']); unset($options['author']); } $options['require'] = isset($options['require']) ? $this->formatRequirements($options['require']) : new \stdClass(); $file = new JsonFile('composer.json'); $json = $file->encode($options); if ($input->isInteractive()) { $output->writeln(array('', $json, '')); if (!$dialog->askConfirmation($output, $dialog->getQuestion('Do you confirm generation', 'yes', '?'), true)) { $output->writeln('<error>Command aborted</error>'); return 1; } } $file->write($options); if ($input->isInteractive()) { $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 ($dialog->askConfirmation($output, $question, true)) { $this->addVendorIgnore($ignoreFile); } } } }
protected static function unsetJsonNodes($json, $nodes) { $data = $json->read(); foreach ($nodes as $node) { unset($data[$node]); } return Json\JsonFile::encode($data); }
/** * {@inheritdoc} */ protected function execute(InputInterface $input, OutputInterface $output) { $whitelist = array('name', 'description', 'author', 'type', 'homepage', 'require', 'require-dev', 'stability', 'license'); $options = array_filter(array_intersect_key($input->getOptions(), array_flip($whitelist))); if (isset($options['author'])) { $options['authors'] = $this->formatAuthors($options['author']); unset($options['author']); } if (isset($options['stability'])) { $options['minimum-stability'] = $options['stability']; unset($options['stability']); } $options['require'] = isset($options['require']) ? $this->formatRequirements($options['require']) : new \stdClass(); if (array() === $options['require']) { $options['require'] = new \stdClass(); } if (isset($options['require-dev'])) { $options['require-dev'] = $this->formatRequirements($options['require-dev']); if (array() === $options['require-dev']) { $options['require-dev'] = new \stdClass(); } } $file = new JsonFile('composer.json'); $json = $file->encode($options); $io = $this->getIO(); if ($input->isInteractive()) { $io->writeError(array('', $json, '')); if (!$io->askConfirmation('Do you confirm generation [<comment>yes</comment>]? ', true)) { $io->writeError('<error>Command aborted</error>'); return 1; } } $file->write($options); 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, true)) { $this->addVendorIgnore($ignoreFile); } } } }
protected function execute(InputInterface $input, OutputInterface $output) { $composer = $this->getComposer(); $commandEvent = new CommandEvent(PluginEvents::COMMAND, 'licenses', $input, $output); $composer->getEventDispatcher()->dispatch($commandEvent->getName(), $commandEvent); $root = $composer->getPackage(); $repo = $composer->getRepositoryManager()->getLocalRepository(); if ($input->getOption('no-dev')) { $packages = $this->filterRequiredPackages($repo, $root); } else { $packages = $this->appendPackages($repo->getPackages(), array()); } ksort($packages); $io = $this->getIO(); switch ($format = $input->getOption('format')) { case 'text': $io->write('Name: <comment>' . $root->getPrettyName() . '</comment>'); $io->write('Version: <comment>' . $root->getFullPrettyVersion() . '</comment>'); $io->write('Licenses: <comment>' . (implode(', ', $root->getLicense()) ?: 'none') . '</comment>'); $io->write('Dependencies:'); $io->write(''); $table = new Table($output); $table->setStyle('compact'); $table->getStyle()->setVerticalBorderChar(''); $table->getStyle()->setCellRowContentFormat('%s '); $table->setHeaders(array('Name', 'Version', 'License')); foreach ($packages as $package) { $table->addRow(array($package->getPrettyName(), $package->getFullPrettyVersion(), implode(', ', $package->getLicense()) ?: 'none')); } $table->render(); break; case 'json': $dependencies = array(); foreach ($packages as $package) { $dependencies[$package->getPrettyName()] = array('version' => $package->getFullPrettyVersion(), 'license' => $package->getLicense()); } $io->write(JsonFile::encode(array('name' => $root->getPrettyName(), 'version' => $root->getFullPrettyVersion(), 'license' => $root->getLicense(), 'dependencies' => $dependencies))); break; default: throw new \RuntimeException(sprintf('Unsupported format "%s". See help for supported formats.', $format)); } }
/** * Merge the current config with the updated data * @param Event $event * @param array $info */ public static function mergeConfig(Event $event, array $info) { // start $io = $event->getIO(); $options = array("name" => $info['package'], "description" => $info['description'], "license" => $info['license'], "keywords" => array(), "type" => "wordpress-plugin", "authors" => array(array("name" => $info['author'], "email" => $info['author_email']) + (!empty($info['author_url']) ? array("homepage" => $info['author_url']) : array())), "scripts" => array("publish" => "Jumpstart\\Deploy::publish"), "minimum-stability" => "dev", "prefer-stable" => true, "require" => array("composer/installers" => "~1.0", "mrgrain/jumpstart-battery" => "~1.0"), "require-dev" => array("mrgrain/jumpstart-deploy" => "@stable"), "autoload" => array("psr-4" => array($info['namespace'] . "\\" => "src/"))); $file = new JsonFile('composer.json'); $json = $file->encode($options); $writeFile = true; $io->write('Jumpstart has generated a composer.json file for you.'); if ($io->askConfirmation('Do you want to check it before writing to disk? [yes]' . "\t")) { $writeFile = false; $io->write($json); if ($io->askConfirmation('Write the generated composer.json to disk? [yes]' . "\t")) { $writeFile = true; } } if ($writeFile) { $file->write($options); } }
protected function execute(InputInterface $input, OutputInterface $output) { $composer = $this->getComposer(); $commandEvent = new CommandEvent(PluginEvents::COMMAND, 'licenses', $input, $output); $composer->getEventDispatcher()->dispatch($commandEvent->getName(), $commandEvent); $root = $composer->getPackage(); $repo = $composer->getRepositoryManager()->getLocalRepository(); $versionParser = new VersionParser(); $packages = array(); foreach ($repo->getPackages() as $package) { $packages[$package->getName()] = $package; } ksort($packages); switch ($format = $input->getOption('format')) { case 'text': $output->writeln('Name: <comment>' . $root->getPrettyName() . '</comment>'); $output->writeln('Version: <comment>' . $versionParser->formatVersion($root) . '</comment>'); $output->writeln('Licenses: <comment>' . (implode(', ', $root->getLicense()) ?: 'none') . '</comment>'); $output->writeln('Dependencies:'); $table = $this->getHelperSet()->get('table'); $table->setLayout(TableHelper::LAYOUT_BORDERLESS); $table->setHorizontalBorderChar(''); foreach ($packages as $package) { $table->addRow(array($package->getPrettyName(), $versionParser->formatVersion($package), implode(', ', $package->getLicense()) ?: 'none')); } $table->render($output); break; case 'json': foreach ($packages as $package) { $dependencies[$package->getPrettyName()] = array('version' => $versionParser->formatVersion($package), 'license' => $package->getLicense()); } $output->writeln(JsonFile::encode(array('name' => $root->getPrettyName(), 'version' => $versionParser->formatVersion($root), 'license' => $root->getLicense(), 'dependencies' => $dependencies))); break; default: throw new \RuntimeException(sprintf('Unsupported format "%s". See help for supported formats.', $format)); } }
private function assertJsonFormat($json, $data, $options = null) { $file = new JsonFile('composer.json'); if (null === $options) { $this->assertEquals($json, $file->encode($data)); } else { $this->assertEquals($json, $file->encode($data, $options)); } }
/** * Writes includes JSON Files. * * @param array $packages List of packages to dump * @param string $includesUrl The includes url (optionally containing %hash%) * @param string $hashAlgorithm Hash algorithm {@see hash()} * * @return array The object for includes key in packages.json */ private function dumpPackageIncludeJson(array $packages, $includesUrl, $hashAlgorithm = 'sha1') { $filename = str_replace('%hash%', 'prep', $includesUrl); $path = $tmpPath = $this->outputDir . '/' . ltrim($filename, '/'); $repoJson = new JsonFile($path); $contents = $repoJson->encode(['packages' => $packages]) . "\n"; $hash = hash($hashAlgorithm, $contents); if (strpos($includesUrl, '%hash%') !== false) { $this->writtenIncludeJsons[] = [$hash, $includesUrl]; $filename = str_replace('%hash%', $hash, $includesUrl); if (file_exists($path = $this->outputDir . '/' . ltrim($filename, '/'))) { // When the file exists, we don't need to override it as we assume, // the contents satisfy the hash $path = null; } } if ($path) { $this->writeToFile($path, $contents); $this->output->writeln("<info>wrote packages to {$path}</info>"); } return [$filename => [$hashAlgorithm => $hash]]; }
public function format($data, $depth = 0) { if (is_array($data)) { reset($data); if (is_numeric(key($data))) { foreach ($data as $key => $val) { $data[$key] = $this->format($val, $depth + 1); } return '[' . implode(', ', $data) . ']'; } $out = '{' . $this->newline; $elems = array(); foreach ($data as $key => $val) { $elems[] = str_repeat($this->indent, $depth + 2) . JsonFile::encode($key) . ': ' . $this->format($val, $depth + 1); } return $out . implode(',' . $this->newline, $elems) . $this->newline . str_repeat($this->indent, $depth + 1) . '}'; } return JsonFile::encode($data); }
/** * Constructs the require.js file from the provided require.js JSON array. * * @param $json * The require.js JSON configuration. * * @return string * The RequireJS JavaScript configuration. */ public function requireJs(array $json = array()) { // Encode the array to a JSON array. $js = JsonFile::encode($json); // Construct the JavaScript output. $output = <<<EOT var components = {$js}; if (typeof require !== "undefined" && require.config) { require.config(components); } else { var require = components; } if (typeof exports !== "undefined" && typeof module !== "undefined") { module.exports = components; } EOT; return $output; }
/** * Configuration as a Json string. * * @api * * @return string Configuration */ public function json() { return JsonFile::encode($this->doConfig()); }
/** * Writes json data to file * * @param array $data data * @param string $file output file name template with '%hash%' to replace * * @return string filename */ private function writeJson($data, $file) { $options = JsonFile::JSON_UNESCAPED_SLASHES | JsonFile::JSON_UNESCAPED_UNICODE; $content = JsonFile::encode($data, $options); $hash = hash('sha256', $content); $file = strtr($file, array('%hash%' => $hash)); if (file_exists($file)) { return array($file, $hash, false); } else { (new JsonFile($file))->write($data, $options); return array($file, $hash, true); } }