/** * @param Application $application * @param string|null $namespace * * @return \DOMDocument */ public function getApplicationDocument(Application $application, $namespace = null) { $dom = new \DOMDocument('1.0', 'UTF-8'); $dom->appendChild($rootXml = $dom->createElement('symfony')); if ($application->getName() !== 'UNKNOWN') { $rootXml->setAttribute('name', $application->getName()); if ($application->getVersion() !== 'UNKNOWN') { $rootXml->setAttribute('version', $application->getVersion()); } } $rootXml->appendChild($commandsXML = $dom->createElement('commands')); $description = new ApplicationDescription($application, $namespace); if ($namespace) { $commandsXML->setAttribute('namespace', $namespace); } foreach ($description->getCommands() as $command) { $this->appendDocument($commandsXML, $this->getCommandDocument($command)); } if (!$namespace) { $rootXml->appendChild($namespacesXML = $dom->createElement('namespaces')); foreach ($description->getNamespaces() as $namespaceDescription) { $namespacesXML->appendChild($namespaceArrayXML = $dom->createElement('namespace')); $namespaceArrayXML->setAttribute('id', $namespaceDescription['id']); foreach ($namespaceDescription['commands'] as $name) { $namespaceArrayXML->appendChild($commandXML = $dom->createElement('command')); $commandXML->appendChild($dom->createTextNode($name)); } } } return $dom; }
public static function routeCommand(ConsoleApplication $console) { // TODO - currently in PHP it is impossible to make a constructor of a child class // have a constructor that has more restricted visibility than then parent class. // In an ideal world, ConsoleApplication would be a child class that does not have a // default constructor, as that would prevent 'accidental' insertion of an unitialized // object. // However that is not currently possible, and would require an RFC to change. //Figure out what Command was requested. try { $parsedCommand = $console->parseCommandLine(); } catch (\Exception $e) { //@TODO change to just catch parseException when that's implemented $output = new BufferedOutput(); $console->renderException($e, $output); echo $output->fetch(); exit(-1); } $output = $parsedCommand->getOutput(); $formatter = $output->getFormatter(); $formatter->setStyle('question', new OutputFormatterStyle('blue')); $formatter->setStyle('info', new OutputFormatterStyle('blue')); $questionHelper = new QuestionHelper(); $questionHelper->setHelperSet($console->getHelperSet()); $injectionParams = InjectionParams::fromParams($parsedCommand->getParams()); $executable = new Executable($parsedCommand->getCallable(), $injectionParams); $executable->setAllowedToReturnNull(true); return $executable; }
/** * Sets the application instance for this command. * * @param Application $application An Application instance * * @api */ public function setApplication(Application $application = null) { $this->application = $application; if ($application) { $this->setHelperSet($application->getHelperSet()); } else { $this->helperSet = null; } }
public function testExecuteForApplicationCommandWithXmlOption() { $application = new Application(); $commandTester = new CommandTester($application->get('help')); $parsedCommand = $commandTester->execute(array('command_name' => 'list', '--format' => 'xml')); $provider = new Provider(); $provider->execute($parsedCommand->getCallable(), []); $this->assertRegExp('/list \\[--xml\\] \\[--raw\\] \\[--format="\\.\\.\\."\\] \\[namespace\\]/', $commandTester->getDisplay(), '->execute() returns a text help for the given command'); $this->assertRegExp('/<command/', $commandTester->getDisplay(), '->execute() returns an XML help text if --format=xml is passed'); }
/** @dataProvider getDescribeApplicationTestData */ public function testDescribeApplication(Application $application, $expectedDescription) { // Replaces the dynamic placeholders of the command help text with a static version. // The placeholder %command.full_name% includes the script path that is not predictable // and can not be tested against. foreach ($application->all() as $command) { $command->setHelp(str_replace('%command.full_name%', 'app/console %command.name%', $command->getHelp())); } $this->assertDescription($expectedDescription, $application); }
public function testCommandFromApplication() { $application = new Application(); $application->setAutoExit(false); $callable = function ($input, $output) { $output->writeln('foo'); }; $command = new Command('foo', $callable); $application->add($command); $tester = new CommandTester($application->find('foo')); $result = $tester->execute(array()); $this->assertInstanceOf('Danack\\Console\\Command\\ParsedCommand', $result); }
/** * Overridden so that the application doesn't expect the command * name to be the first argument. */ public function getDefinition() { $inputDefinition = parent::getDefinition(); // clear out the normal first argument, which is the command name $inputDefinition->setArguments(); return $inputDefinition; }
public function testExecuteListsCommandsWithNamespaceArgument() { require_once realpath(__DIR__ . '/../Fixtures/FooCommand.php'); $application = new Application(); $application->add(new \FooCommand()); $commandTester = new CommandTester($command = $application->get('list')); $parsedCommand = $commandTester->execute(array('command' => $command->getName(), 'namespace' => 'foo', '--raw' => true)); /** @var $parsedCommand ParsedCommand */ $injector = new \Auryn\Provider(); $injector->execute($parsedCommand->getCallable()); $output = <<<EOF foo:bar The foo:bar command EOF; $this->assertEquals($output, $commandTester->getDisplay(true)); }
/** * Creates a console application with all of the commands attached. * @return Application */ function createApplication() { // $rpmCommand = new Command('rpmdir', ['Bastion\RPMProcess', 'packageSingleDirectory']); // $rpmCommand->addArgument('directory', InputArgument::REQUIRED, "The directory containing the composer'd project to build into an RPM."); ////$uploadCommand->addOption('dir', null, InputArgument::OPTIONAL, 'Which directory to upload from', './'); // $rpmCommand->setDescription("Build an RPM from an directory that contains all the files of a project. Allows for faster testing than having to re-tag, and download zip files repeatedly."); $statsCommand = new Command('statsRunner', 'Stats\\SimpleStats::run'); $statsCommand->setDescription("Run the stats collector and send the results to Librato."); $taskCommand = new Command('imageRunner', 'ImagickDemo\\Queue\\ImagickTaskRunner::run'); $taskCommand->setDescription("Pull image request jobs off the queue and generated the images."); $clearCacheCommand = new Command('clearCache', 'ImagickDemo\\Config\\APCCacheEnvReader::clearCache'); $clearCacheCommand->setDescription("Clear the apc cache."); $envWriteCommand = new Command('genEnvSettings', 'ImagickDemo\\Config\\EnvConfWriter::writeEnvFile'); $envWriteCommand->setDescription("Write an env setting bash script."); $envWriteCommand->addArgument('env', InputArgument::REQUIRED, 'Which environment the settings should be generated for.'); $envWriteCommand->addArgument('filename', InputArgument::REQUIRED, 'The file name that the env settings should be written to.'); $clearRedisCommand = new Command('clearRedis', 'ImagickDemo\\Queue\\ImagickTaskQueue::clearStatusQueue'); $clearRedisCommand->setDescription("Clear the imagick task queue."); $console = new Application("ImagickDemos", "1.0.0"); $console->add($statsCommand); $console->add($taskCommand); $console->add($clearCacheCommand); $console->add($clearRedisCommand); $console->add($envWriteCommand); return $console; }
/** * {@inheritdoc} */ protected function describeApplication(Application $application, array $options = array()) { $describedNamespace = isset($options['namespace']) ? $options['namespace'] : null; $description = new ApplicationDescription($application, $describedNamespace); $this->write($application->getName() . "\n" . str_repeat('=', strlen($application->getName()))); foreach ($description->getNamespaces() as $namespace) { if (ApplicationDescription::GLOBAL_NAMESPACE !== $namespace['id']) { $this->write("\n\n"); $this->write('**' . $namespace['id'] . ':**'); } $this->write("\n\n"); $this->write(implode("\n", array_map(function ($commandName) { return '* ' . $commandName; }, $namespace['commands']))); } foreach ($description->getCommands() as $command) { $this->write("\n\n"); $this->write($this->describeCommand($command)); } }
/** * @param array $commands * * @return array */ private function sortCommands(array $commands) { $namespacedCommands = array(); foreach ($commands as $name => $command) { $key = $this->application->extractNamespace($name, 1); if (!$key) { $key = '_global'; } $namespacedCommands[$key][$name] = $command; } ksort($namespacedCommands); foreach ($namespacedCommands as &$commands) { ksort($commands); } return $namespacedCommands; }
/** * Creates a console application with all of the commands attached. * @return ConsoleApplication */ public function __construct() { parent::__construct("ImagickDemos", "1.0.0"); $statsCommand = new Command('statsRunner', 'Stats\\SimpleStats::run'); $statsCommand->setDescription("Run the stats collector and send the results to Librato."); $this->add($statsCommand); $taskCommand = new Command('imageRunner', 'ImagickDemo\\Queue\\ImagickTaskRunner::run'); $taskCommand->setDescription("Pull image request jobs off the queue and generated the images."); $this->add($taskCommand); $clearCacheCommand = new Command('clearCache', 'ImagickDemo\\Config\\APCCacheEnvReader::clearCache'); $clearCacheCommand->setDescription("Clear the apc cache."); $this->add($clearCacheCommand); $envWriteCommand = new Command('genEnvSettings', 'ImagickDemo\\Config\\EnvConfWriter::writeEnvFile'); $envWriteCommand->setDescription("Write an env setting bash script."); $envWriteCommand->addArgument('env', InputArgument::REQUIRED, 'Which environment the settings should be generated for.'); $envWriteCommand->addArgument('filename', InputArgument::REQUIRED, 'The file name that the env settings should be written to.'); $this->add($envWriteCommand); $clearRedisCommand = new Command('clearRedis', 'ImagickDemo\\Queue\\ImagickTaskQueue::clearStatusQueue'); $clearRedisCommand->setDescription("Clear the imagick task queue."); $this->add($clearRedisCommand); }
public function __construct() { parent::__construct('My Symfony application', 'v1.0'); $this->add(new DescriptorCommand1()); $this->add(new DescriptorCommand2()); }
} function uploadFile($filename, $dir) { echo "Need to upload the file {$filename} in the directory {$dir}" . PHP_EOL; } //Auryn needs scalars prefixed with a colon and we only //have scalars here. function lowrey($params) { $newParams = []; foreach ($params as $key => $value) { $newParams[':' . $key] = $value; } return $newParams; } $console = new Application(); $console->add(new AboutCommand()); $uploadCommand = new Command('upload', 'uploadFile'); $uploadCommand->addArgument('filename', InputArgument::REQUIRED, 'The name of the file to upload'); $uploadCommand->addOption('dir', null, InputArgument::OPTIONAL, 'Which directory to upload from', './'); $console->add($uploadCommand); $helloWorldCallable = function ($name) { echo "Hello world, and particularly {$name}" . PHP_EOL; }; $callableCommand = new Command('greet', $helloWorldCallable); $callableCommand->addArgument('name', InputArgument::REQUIRED, 'The name of the person to say hello to.'); $callableCommand->setDescription("Says hello to the world and one named person"); $console->add($callableCommand); try { $parsedCommand = $console->parseCommandLine(); } catch (\Exception $e) {
/** * Overwrites the constructor in order to set a different default command. */ public function __construct() { parent::__construct(); $command = new \FooCommand(); $this->add($command); $this->setDefaultCommand($command->getName()); }
public function testMergeApplicationDefinitionWithoutArgsThenWithArgsAddsArgs() { $application1 = new Application(); $application1->getDefinition()->addArguments(array(new InputArgument('foo'))); $application1->getDefinition()->addOptions(array(new InputOption('bar'))); $command = new \TestCommand(); $command->setApplication($application1); $command->setDefinition($definition = new InputDefinition(array())); $r = new \ReflectionObject($command); $m = $r->getMethod('mergeApplicationDefinition'); $m->setAccessible(true); $m->invoke($command, false); $this->assertTrue($command->getDefinition()->hasOption('bar'), '->mergeApplicationDefinition(false) merges the application and the commmand options'); $this->assertFalse($command->getDefinition()->hasArgument('foo'), '->mergeApplicationDefinition(false) does not merge the application arguments'); $m->invoke($command, true); $this->assertTrue($command->getDefinition()->hasArgument('foo'), '->mergeApplicationDefinition(true) merges the application arguments and the command arguments'); $m->invoke($command); $this->assertEquals(2, $command->getDefinition()->getArgumentCount(), '->mergeApplicationDefinition() does not try to merge twice the application arguments'); }
/** * {@inheritdoc} */ protected function describeApplication(Application $application, array $options = array()) { $describedNamespace = isset($options['namespace']) ? $options['namespace'] : null; $description = new ApplicationDescription($application, $describedNamespace); if (isset($options['raw_text']) && $options['raw_text']) { $width = $this->getColumnWidth($description->getCommands()); foreach ($description->getCommands() as $command) { $this->writeText(sprintf("%-{$width}s %s", $command->getName(), $command->getDescription()), $options); $this->writeText("\n"); } } else { $width = $this->getColumnWidth($description->getCommands()); $this->writeText($application->getHelp(), $options); $this->writeText("\n\n"); if ($describedNamespace) { $this->writeText(sprintf("<comment>Available commands for the \"%s\" namespace:</comment>", $describedNamespace), $options); } else { $this->writeText('<comment>Available commands:</comment>', $options); } // add commands by namespace foreach ($description->getNamespaces() as $namespace) { if (!$describedNamespace && ApplicationDescription::GLOBAL_NAMESPACE !== $namespace['id']) { $this->writeText("\n"); $this->writeText('<comment>' . $namespace['id'] . '</comment>', $options); } foreach ($namespace['commands'] as $name) { $this->writeText("\n"); $this->writeText(sprintf(" <info>%-{$width}s</info> %s", $name, $description->getCommand($name)->getDescription()), $options); } } $this->writeText("\n"); } }