a3e3f6cfcbe34d2b8a68264671ccdd9ea7fef439
[yaffs-website] / vendor / behat / behat / src / Behat / Behat / Gherkin / Cli / SyntaxController.php
1 <?php
2
3 /*
4  * This file is part of the Behat.
5  * (c) Konstantin Kudryashov <ever.zet@gmail.com>
6  *
7  * For the full copyright and license information, please view the LICENSE
8  * file that was distributed with this source code.
9  */
10
11 namespace Behat\Behat\Gherkin\Cli;
12
13 use Behat\Gherkin\Keywords\KeywordsDumper;
14 use Behat\Testwork\Cli\Controller;
15 use Symfony\Component\Console\Command\Command;
16 use Symfony\Component\Console\Formatter\OutputFormatterStyle;
17 use Symfony\Component\Console\Input\InputInterface;
18 use Symfony\Component\Console\Input\InputOption;
19 use Symfony\Component\Console\Output\OutputInterface;
20 use Symfony\Component\Translation\TranslatorInterface;
21
22 /**
23  * Prints example of the feature to present all available syntax keywords.
24  *
25  * @author Konstantin Kudryashov <ever.zet@gmail.com>
26  */
27 final class SyntaxController implements Controller
28 {
29     /**
30      * @var KeywordsDumper
31      */
32     private $keywordsDumper;
33     /**
34      * @var TranslatorInterface
35      */
36     private $translator;
37
38     /**
39      * Initializes controller.
40      *
41      * @param KeywordsDumper      $dumper
42      * @param TranslatorInterface $translator
43      */
44     public function __construct(KeywordsDumper $dumper, TranslatorInterface $translator)
45     {
46         $dumper->setKeywordsDumperFunction(array($this, 'dumpKeywords'));
47         $this->keywordsDumper = $dumper;
48         $this->translator = $translator;
49     }
50
51     /**
52      * Configures command to be executable by the controller.
53      *
54      * @param Command $command
55      */
56     public function configure(Command $command)
57     {
58         $command
59             ->addOption(
60                 '--story-syntax', null, InputOption::VALUE_NONE,
61                 "Print <comment>*.feature</comment> example." . PHP_EOL .
62                 "Use <info>--lang</info> to see specific language."
63             );
64     }
65
66     /**
67      * Executes controller.
68      *
69      * @param InputInterface  $input
70      * @param OutputInterface $output
71      *
72      * @return null|integer
73      */
74     public function execute(InputInterface $input, OutputInterface $output)
75     {
76         if (!$input->getOption('story-syntax')) {
77             return null;
78         }
79
80         $output->getFormatter()->setStyle('gherkin_keyword', new OutputFormatterStyle('green', null, array('bold')));
81         $output->getFormatter()->setStyle('gherkin_comment', new OutputFormatterStyle('yellow'));
82
83         $story = $this->keywordsDumper->dump($this->translator->getLocale());
84         $story = preg_replace('/^\#.*/', '<gherkin_comment>$0</gherkin_comment>', $story);
85         $output->writeln($story);
86         $output->writeln('');
87
88         return 0;
89     }
90
91     /**
92      * Keywords dumper.
93      *
94      * @param array $keywords keywords list
95      *
96      * @return string
97      */
98     public function dumpKeywords(array $keywords)
99     {
100         $dump = '<gherkin_keyword>' . implode('</gherkin_keyword>|<gherkin_keyword>', $keywords) . '</gherkin_keyword>';
101
102         if (1 < count($keywords)) {
103             return '[' . $dump . ']';
104         }
105
106         return $dump;
107     }
108 }