|
@@ -0,0 +1,83 @@
|
|
|
+<?php
|
|
|
+
|
|
|
+namespace Osinet\DemoBundle\Command;
|
|
|
+
|
|
|
+use Osinet\DemoBundle\Lucky;
|
|
|
+use Symfony\Component\Console\Command\Command;
|
|
|
+use Symfony\Component\Console\Input\InputInterface;
|
|
|
+use Symfony\Component\Console\Input\InputOption;
|
|
|
+use Symfony\Component\Console\Output\OutputInterface;
|
|
|
+
|
|
|
+class DemoCommand extends Command {
|
|
|
+ const NAME = 'osinet:demo:lucky';
|
|
|
+
|
|
|
+ const O_LUCKY = 'lucky';
|
|
|
+
|
|
|
+ protected $lucky;
|
|
|
+
|
|
|
+ public function __construct(Lucky $lucky, ?string $name = NULL) {
|
|
|
+ $this->lucky = $lucky;
|
|
|
+ parent::__construct($name);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ * Step 1: initialize variables used in the rest of the command methods.
|
|
|
+ *
|
|
|
+ * @param \Symfony\Component\Console\Input\InputInterface $input
|
|
|
+ * @param \Symfony\Component\Console\Output\OutputInterface $output
|
|
|
+ */
|
|
|
+ protected function initialize(InputInterface $input, OutputInterface $output) {
|
|
|
+
|
|
|
+ parent::initialize($input, $output);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ * Step 2: obtain options/arguments.
|
|
|
+ *
|
|
|
+ * Check if some of the options/arguments are missing and interactively ask
|
|
|
+ * the user for those values. This is the last place where you can ask for
|
|
|
+ * missing options/arguments.
|
|
|
+ *
|
|
|
+ * @param \Symfony\Component\Console\Input\InputInterface $input
|
|
|
+ * @param \Symfony\Component\Console\Output\OutputInterface $output
|
|
|
+ */
|
|
|
+ protected function interact(InputInterface $input, OutputInterface $output) {
|
|
|
+
|
|
|
+ parent::interact($input, $output);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ * Compile step: info hook.
|
|
|
+ *
|
|
|
+ * {@inheritdoc}
|
|
|
+ *
|
|
|
+ * Nothing in parent, so don't call it.
|
|
|
+ */
|
|
|
+ protected function configure() {
|
|
|
+ $this->setName(static::NAME)
|
|
|
+ ->setDescription('Draws a lucky number')
|
|
|
+ ->setHelp('Draws a pseudo-random number, or returns the one you pass')
|
|
|
+ ->addOption(static::O_LUCKY, 'l', InputOption::VALUE_OPTIONAL, 'A number to use', null);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ * Step 3: execute the command.
|
|
|
+ *
|
|
|
+ * @param \Symfony\Component\Console\Input\InputInterface $input
|
|
|
+ * @param \Symfony\Component\Console\Output\OutputInterface $output
|
|
|
+ *
|
|
|
+ * @return int|null|void
|
|
|
+ *
|
|
|
+ * Parent throws so do not invoke it.
|
|
|
+ */
|
|
|
+ protected function execute(InputInterface $input, OutputInterface $output) {
|
|
|
+
|
|
|
+ $value = $input->getOption(static::O_LUCKY);
|
|
|
+ if (isset($value)) {
|
|
|
+ $value = (int) $value;
|
|
|
+ }
|
|
|
+ $value = $this->lucky->number($value);
|
|
|
+ $output->writeln($value);
|
|
|
+ return $value;
|
|
|
+ }
|
|
|
+}
|