Security update for Core, with self-updated composer
[yaffs-website] / vendor / drupal / console / src / Command / Cron / ExecuteCommand.php
1 <?php
2
3 /**
4  * @file
5  * Contains \Drupal\Console\Command\Cron\ExecuteCommand.
6  */
7
8 namespace Drupal\Console\Command\Cron;
9
10 use Symfony\Component\Console\Input\InputArgument;
11 use Symfony\Component\Console\Input\InputInterface;
12 use Symfony\Component\Console\Output\OutputInterface;
13 use Drupal\Console\Core\Command\Command;
14 use Drupal\Core\Extension\ModuleHandlerInterface;
15 use Drupal\Core\Lock\LockBackendInterface;
16 use Drupal\Core\State\StateInterface;
17
18 class ExecuteCommand extends Command
19 {
20     /**
21      * @var ModuleHandlerInterface
22      */
23     protected $moduleHandler;
24
25     /**
26      * @var LockBackendInterface
27      */
28     protected $lock;
29
30     /**
31      * @var StateInterface
32      */
33     protected $state;
34
35     /**
36      * DebugCommand constructor.
37      *
38      * @param ModuleHandlerInterface $moduleHandler
39      * @param LockBackendInterface   $lock
40      * @param StateInterface         $state
41      */
42     public function __construct(
43         ModuleHandlerInterface $moduleHandler,
44         LockBackendInterface $lock,
45         StateInterface $state
46     ) {
47         $this->moduleHandler = $moduleHandler;
48         $this->lock = $lock;
49         $this->state = $state;
50         parent::__construct();
51     }
52
53     /**
54      * {@inheritdoc}
55      */
56     protected function configure()
57     {
58         $this
59             ->setName('cron:execute')
60             ->setDescription($this->trans('commands.cron.execute.description'))
61             ->addArgument(
62                 'module',
63                 InputArgument::IS_ARRAY | InputArgument::OPTIONAL,
64                 $this->trans('commands.common.options.module')
65             )
66             ->setAliases(['croe']);
67     }
68
69     /**
70      * {@inheritdoc}
71      */
72     protected function execute(InputInterface $input, OutputInterface $output)
73     {
74         $modules = $input->getArgument('module');
75
76         if (!$this->lock->acquire('cron', 900.0)) {
77             $this->getIo()->warning($this->trans('commands.cron.execute.messages.lock'));
78
79             return 1;
80         }
81
82         if ($modules === null || in_array('all', $modules)) {
83             $modules = $this->moduleHandler->getImplementations('cron');
84         }
85
86         foreach ($modules as $module) {
87             if (!$this->moduleHandler->implementsHook($module, 'cron')) {
88                 $this->getIo()->warning(
89                     sprintf(
90                         $this->trans('commands.cron.execute.messages.module-invalid'),
91                         $module
92                     )
93                 );
94                 continue;
95             }
96             try {
97                 $this->getIo()->info(
98                     sprintf(
99                         $this->trans('commands.cron.execute.messages.executing-cron'),
100                         $module
101                     )
102                 );
103                 $this->moduleHandler->invoke($module, 'cron');
104             } catch (\Exception $e) {
105                 watchdog_exception('cron', $e);
106                 $this->getIo()->error($e->getMessage());
107             }
108         }
109
110         $this->state->set('system.cron_last', REQUEST_TIME);
111         $this->lock->release('cron');
112
113         $this->getIo()->success($this->trans('commands.cron.execute.messages.success'));
114
115         return 0;
116     }
117 }