Przeglądaj źródła

Commands: create simple app and bundle commands with options.

Frederic G. MARAND 6 lat temu
rodzic
commit
1a0bfc74ac

+ 0 - 7
.idea/runConfigurations/Console.xml

@@ -1,7 +0,0 @@
-<component name="ProjectRunConfigurationManager">
-  <configuration default="false" name="Console" type="PhpLocalRunConfigurationType" factoryName="PHP Console" singleton="true" path="$PROJECT_DIR$/bin/console" scriptParameters="debug:container 'App\Osinet\DemoBundle\Controller\DemoController'">
-    <CommandLine workingDirectory="$PROJECT_DIR$" />
-    <option name="workingDirectory" value="$PROJECT_DIR$" />
-    <method />
-  </configuration>
-</component>

+ 7 - 0
.idea/runConfigurations/Console_debug_container.xml

@@ -0,0 +1,7 @@
+<component name="ProjectRunConfigurationManager">
+  <configuration default="false" name="Console debug:container" type="PhpLocalRunConfigurationType" factoryName="PHP Console" singleton="true" path="$PROJECT_DIR$/bin/console" scriptParameters="debug:container 'App\Osinet\DemoBundle\Controller\DemoController'">
+    <CommandLine workingDirectory="$PROJECT_DIR$" />
+    <option name="workingDirectory" value="$PROJECT_DIR$" />
+    <method />
+  </configuration>
+</component>

+ 11 - 0
.idea/runConfigurations/Console_list.xml

@@ -0,0 +1,11 @@
+<component name="ProjectRunConfigurationManager">
+  <configuration default="false" name="Console list" type="PhpLocalRunConfigurationType" factoryName="PHP Console" singleton="true" path="$PROJECT_DIR$/bin/console" scriptParameters="list">
+    <CommandLine workingDirectory="$PROJECT_DIR$">
+      <envs>
+        <env name="PHP_IDE_CONFIG" value="serverName=localhost" />
+      </envs>
+    </CommandLine>
+    <option name="workingDirectory" value="$PROJECT_DIR$" />
+    <method />
+  </configuration>
+</component>

+ 1 - 0
.idea/sf4-book.iml

@@ -5,6 +5,7 @@
       <sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" packagePrefix="App\" />
       <sourceFolder url="file://$MODULE_DIR$/tests" isTestSource="true" packagePrefix="App\Tests\" />
       <sourceFolder url="file://$MODULE_DIR$/src/Osinet" isTestSource="false" packagePrefix="Osinet\" />
+      <sourceFolder url="file://$MODULE_DIR$/var/cache/dev" isTestSource="false" />
       <excludeFolder url="file://$MODULE_DIR$/var" />
       <excludeFolder url="file://$MODULE_DIR$/vendor/composer" />
       <excludeFolder url="file://$MODULE_DIR$/vendor/doctrine/annotations" />

+ 57 - 0
src/Command/AppCommand.php

@@ -0,0 +1,57 @@
+<?php
+
+namespace App\Command;
+
+
+use Osinet\DemoBundle\Command\DemoCommand;
+use Osinet\DemoBundle\Lucky;
+use Symfony\Component\Console\Command\Command;
+use Symfony\Component\Console\Input\ArrayInput;
+use Symfony\Component\Console\Input\InputInterface;
+use Symfony\Component\Console\Input\InputOption;
+use Symfony\Component\Console\Output\NullOutput;
+use Symfony\Component\Console\Output\OutputInterface;
+
+class AppCommand extends Command {
+  const O_DELEGATE = 'delegate';
+
+  /**
+   * @var \Osinet\DemoBundle\Lucky
+   */
+  protected $lucky;
+
+  public function __construct(Lucky $lucky, ?string $name = NULL) {
+    $this->lucky = $lucky;
+    parent::__construct($name);
+  }
+
+  /**
+   * {@inheritdoc}
+   *
+   * Nothing in parent, so don't call it.
+   */
+  protected function configure() {
+    $this->setName('app:lucky')
+      ->setDescription('Draws a lucky number')
+      ->setHelp('Draws a pseudo-random number, or returns the one you pass')
+      ->addOption(static::O_DELEGATE, 'd', InputOption::VALUE_NONE, 'Delegate to demo bundle command');
+  }
+
+  protected function execute(InputInterface $input, OutputInterface $output) {
+    // Delegate to bundle commande.
+    if ($input->getOption(static::O_DELEGATE)) {
+      $bundleCommand = $this->getApplication()->find(DemoCommand::NAME);
+      $arguments = [];
+      $commandInput = new ArrayInput($arguments);
+      $commandOutput = new NullOutput();
+      // Pass no options/arguments, mask commmand output.
+      $value = $bundleCommand->run($commandInput, $commandOutput);
+    }
+    // Perform value selection directly.
+    else {
+      $value = $this->lucky->number();
+    }
+
+    $output->writeln($value);
+  }
+}

+ 83 - 0
src/Osinet/DemoBundle/Command/DemoCommand.php

@@ -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) {
+    // echo __METHOD__ . "\n";
+    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) {
+    // echo __METHOD__ . "\n";
+    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) {
+    // echo __METHOD__ . "\n";
+    $value = $input->getOption(static::O_LUCKY);
+    if (isset($value)) {
+      $value = (int) $value;
+    }
+    $value = $this->lucky->number($value);
+    $output->writeln($value);
+    return $value;
+  }
+}

+ 16 - 0
src/Osinet/DemoBundle/Lucky.php

@@ -0,0 +1,16 @@
+<?php
+
+namespace Osinet\DemoBundle;
+
+class Lucky {
+
+  /**
+   * @param int|null $value
+   *
+   * @return int
+   */
+  public function number(?int $value = null): int {
+    $number = (int) ($value ?? mt_rand(0, 100));
+    return $number;
+  }
+}

+ 6 - 0
src/Osinet/DemoBundle/Resources/config/services.yml

@@ -22,3 +22,9 @@ services:
     autoconfigure: true # Automatically registers your services as commands, event subscribers, etc.
 
   Osinet\DemoBundle\DemoCode: '@osinet_demo.democode'
+
+  Osinet\DemoBundle\Command\DemoCommand:
+    arguments:
+      - '@Osinet\DemoBundle\Lucky'
+    tags:
+      - { name: console.command }