5 * Contains \Drupal\Console\Core\EventSubscriber\ShowTipsListener.
8 namespace Drupal\Console\Core\EventSubscriber;
10 use Symfony\Component\Console\ConsoleEvents;
11 use Symfony\Component\Console\Event\ConsoleCommandEvent;
12 use Symfony\Component\EventDispatcher\EventSubscriberInterface;
13 use Symfony\Component\Console\Command\Command;
14 use Drupal\Console\Core\Utils\TranslatorManagerInterface;
15 use Drupal\Console\Core\Style\DrupalStyle;
18 * Class ShowTipsListener
20 * @package Drupal\Console\Core\EventSubscriber
22 class ShowTipsListener implements EventSubscriberInterface
25 * @var TranslatorManagerInterface
27 protected $translator;
30 * ShowGenerateChainListener constructor.
32 * @param TranslatorManagerInterface $translator
34 public function __construct(
35 TranslatorManagerInterface $translator
37 $this->translator = $translator;
41 * @param ConsoleCommandEvent $event
43 public function showTips(ConsoleCommandEvent $event)
45 /* @var Command $command */
46 $command = $event->getCommand();
47 $input = $command->getDefinition();
48 /* @var DrupalStyle $io */
49 $io = new DrupalStyle($event->getInput(), $event->getOutput());
51 $learning = $input->getOption('learning');
53 // pick randomly one of the tips (5 tips as maximum).
54 $tips = $this->getTip($command->getName());
56 if ($learning && $tips) {
57 $io->commentBlock($tips);
65 private function getTip($commandName)
67 $get_tip = $this->translator
68 ->trans('commands.'.str_replace(':', '.', $commandName).'.tips.0.tip');
69 preg_match("/^commands./", $get_tip, $matches, null, 0);
70 if (!empty($matches)) {
75 $get_tip = $this->translator
76 ->trans('commands.'.str_replace(':', '.', $commandName).'.tips.' . $n . '.tip');
77 preg_match("/^commands./", $get_tip, $matches, null, 0);
79 if (empty($matches)) {
82 return $this->getTip($commandName);
89 public static function getSubscribedEvents()
91 return [ConsoleEvents::COMMAND => 'showTips'];