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