vendor/winzou/state-machine-bundle/Command/winzouStateMachineDebugCommand.php line 13

Open in your IDE?
  1. <?php
  2. namespace winzou\Bundle\StateMachineBundle\Command;
  3. use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
  4. use Symfony\Component\Console\Helper\TableSeparator;
  5. use Symfony\Component\Console\Input\InputArgument;
  6. use Symfony\Component\Console\Input\InputInterface;
  7. use Symfony\Component\Console\Output\OutputInterface;
  8. use Symfony\Component\Console\Helper\Table;
  9. use Symfony\Component\Console\Question\ChoiceQuestion;
  10. class winzouStateMachineDebugCommand extends ContainerAwareCommand
  11. {
  12.     /**
  13.      * @var array
  14.      */
  15.     protected $config;
  16.     /**
  17.      * {@inheritdoc}
  18.      */
  19.     public function configure()
  20.     {
  21.         $this->addArgument('key'InputArgument::REQUIRED'A state machine key');
  22.         $this->setName('debug:winzou:state-machine');
  23.     }
  24.     /**
  25.      * {@inheritdoc}
  26.      */
  27.     protected function initialize(InputInterface $inputOutputInterface $output)
  28.     {
  29.         $this->config $this->getContainer()->getParameter('sm.configs');
  30.         if (empty($this->config)) {
  31.             throw new \RuntimeException('The is no state machines configured.');
  32.         }
  33.     }
  34.     /**
  35.      * {@inheritdoc}
  36.      */
  37.     protected function interact(InputInterface $inputOutputInterface $output)
  38.     {
  39.         if (null !== $input->getArgument('key')) {
  40.             return;
  41.         }
  42.         $choices array_map(function ($name$config) {
  43.             return $name "\t(" $config['class'] . ' - ' $config['graph'] . ')';
  44.         }, array_keys($this->config), $this->config);
  45.         $question = new ChoiceQuestion(
  46.             '<question>Which state machine would you like to know about?</question>',
  47.             $choices,
  48.             0
  49.         );
  50.         $question->setErrorMessage('State Machine %s does not exists.');
  51.         $choice $this->getHelper('question')->ask($input$output$question);
  52.         $choice substr($choice0strpos($choice"\t"));
  53.         $output->writeln('<info>You have just selected: '.$choice.'</info>');
  54.         $input->setArgument('key'$choice);
  55.     }
  56.     /**
  57.      * {@inheritdoc}
  58.      */
  59.     public function execute(InputInterface $inputOutputInterface $output)
  60.     {
  61.         $key $input->getArgument('key');
  62.         if (!array_key_exists($key$this->config)) {
  63.             throw new \RuntimeException("The provided state machine key is not configured.");
  64.         }
  65.         $config $this->config[$key];
  66.         $this->printStates($config['states'], $output);
  67.         $this->printTransitions($config['transitions'], $output);
  68.         $this->printCallbacks($config['callbacks'], $output);
  69.     }
  70.     /**
  71.      * @param array           $states
  72.      * @param OutputInterface $output
  73.      */
  74.     protected function printStates(array $statesOutputInterface $output)
  75.     {
  76.         $table = new Table($output);
  77.         $table->setHeaders(array('Configured States:'));
  78.         foreach ($states as $state) {
  79.             $table->addRow(array($state));
  80.         }
  81.         $table->render();
  82.     }
  83.     /**
  84.      * @param array           $transitions
  85.      * @param OutputInterface $output
  86.      */
  87.     protected function printTransitions(array $transitionsOutputInterface $output)
  88.     {
  89.         $table = new Table($output);
  90.         $table->setHeaders(array('Transition''From(s)''To'));
  91.         end($transitions);
  92.         $lastTransition key($transitions);
  93.         reset($transitions);
  94.         foreach ($transitions as $name => $transition) {
  95.             $table->addRow(array($nameimplode("\n"$transition['from']), $transition['to']));
  96.             if ($name !== $lastTransition) {
  97.                 $table->addRow(new TableSeparator());
  98.             }
  99.         }
  100.         $table->render();
  101.     }
  102.     /**
  103.      * @param array           $allCallbacks
  104.      * @param OutputInterface $output
  105.      */
  106.     protected function printCallbacks(array $allCallbacksOutputInterface $output)
  107.     {
  108.         foreach ($allCallbacks as $type => $callbacks) {
  109.             $table = new Table($output);
  110.             $table->setHeaders(array(ucfirst($type) . ' Callback''On''Do''Args'));
  111.             end($callbacks);
  112.             $lastCallback key($callbacks);
  113.             reset($callbacks);
  114.             foreach ($callbacks as $name => $callback) {
  115.                 $table->addRow(array($nameimplode("\n"$callback['on']), implode("\n"$callback['do']), implode("\n"$callback['args'])));
  116.                 if ($name !== $lastCallback) {
  117.                     $table->addRow(new TableSeparator());
  118.                 }
  119.             }
  120.             $table->render();
  121.         }
  122.     }
  123. }