* * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ namespace Behat\Behat\Gherkin\Cli; use Behat\Gherkin\Keywords\KeywordsDumper; use Behat\Testwork\Cli\Controller; use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Formatter\OutputFormatterStyle; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Output\OutputInterface; use Symfony\Component\Translation\TranslatorInterface; /** * Prints example of the feature to present all available syntax keywords. * * @author Konstantin Kudryashov */ final class SyntaxController implements Controller { /** * @var KeywordsDumper */ private $keywordsDumper; /** * @var TranslatorInterface */ private $translator; /** * Initializes controller. * * @param KeywordsDumper $dumper * @param TranslatorInterface $translator */ public function __construct(KeywordsDumper $dumper, TranslatorInterface $translator) { $dumper->setKeywordsDumperFunction(array($this, 'dumpKeywords')); $this->keywordsDumper = $dumper; $this->translator = $translator; } /** * Configures command to be executable by the controller. * * @param Command $command */ public function configure(Command $command) { $command ->addOption( '--story-syntax', null, InputOption::VALUE_NONE, "Print *.feature example." . PHP_EOL . "Use --lang to see specific language." ); } /** * Executes controller. * * @param InputInterface $input * @param OutputInterface $output * * @return null|integer */ public function execute(InputInterface $input, OutputInterface $output) { if (!$input->getOption('story-syntax')) { return null; } $output->getFormatter()->setStyle('gherkin_keyword', new OutputFormatterStyle('green', null, array('bold'))); $output->getFormatter()->setStyle('gherkin_comment', new OutputFormatterStyle('yellow')); $story = $this->keywordsDumper->dump($this->translator->getLocale()); $story = preg_replace('/^\#.*/', '$0', $story); $output->writeln($story); $output->writeln(''); return 0; } /** * Keywords dumper. * * @param array $keywords keywords list * * @return string */ public function dumpKeywords(array $keywords) { $dump = '' . implode('|', $keywords) . ''; if (1 < count($keywords)) { return '[' . $dump . ']'; } return $dump; } }