39181925ca5dad03948d7eb1ab49c169ad3d9607
[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 Symfony\Component\Console\Command\Command;
14 use Drupal\Core\Extension\ModuleHandlerInterface;
15 use Drupal\Core\Lock\LockBackendInterface;
16 use Drupal\Core\State\StateInterface;
17 use Drupal\Console\Core\Command\Shared\CommandTrait;
18 use Drupal\Console\Core\Style\DrupalStyle;
19 use Drupal\Console\Core\Utils\ChainQueue;
20
21 class ExecuteCommand extends Command
22 {
23     use CommandTrait;
24
25     /**
26      * @var ModuleHandlerInterface
27      */
28     protected $moduleHandler;
29
30     /**
31      * @var LockBackendInterface
32      */
33     protected $lock;
34
35     /**
36      * @var StateInterface
37      */
38     protected $state;
39
40     /**
41      * @var ChainQueue
42      */
43     protected $chainQueue;
44
45     /**
46      * DebugCommand constructor.
47      *
48      * @param ModuleHandlerInterface $moduleHandler
49      * @param LockBackendInterface   $lock
50      * @param StateInterface         $state
51      * @param ChainQueue             $chainQueue
52      */
53     public function __construct(
54         ModuleHandlerInterface $moduleHandler,
55         LockBackendInterface $lock,
56         StateInterface $state,
57         ChainQueue $chainQueue
58     ) {
59         $this->moduleHandler = $moduleHandler;
60         $this->lock = $lock;
61         $this->state = $state;
62         $this->chainQueue = $chainQueue;
63         parent::__construct();
64     }
65
66     /**
67      * {@inheritdoc}
68      */
69     protected function configure()
70     {
71         $this
72             ->setName('cron:execute')
73             ->setDescription($this->trans('commands.cron.execute.description'))
74             ->addArgument(
75                 'module',
76                 InputArgument::IS_ARRAY | InputArgument::OPTIONAL,
77                 $this->trans('commands.common.options.module')
78             );
79     }
80
81     /**
82      * {@inheritdoc}
83      */
84     protected function execute(InputInterface $input, OutputInterface $output)
85     {
86         $io = new DrupalStyle($input, $output);
87         $modules = $input->getArgument('module');
88
89         if (!$this->lock->acquire('cron', 900.0)) {
90             $io->warning($this->trans('commands.cron.execute.messages.lock'));
91
92             return 1;
93         }
94
95         if ($modules === null || in_array('all', $modules)) {
96             $modules = $this->moduleHandler->getImplementations('cron');
97         }
98
99         foreach ($modules as $module) {
100             if (!$this->moduleHandler->implementsHook($module, 'cron')) {
101                 $io->warning(
102                     sprintf(
103                         $this->trans('commands.cron.execute.messages.module-invalid'),
104                         $module
105                     )
106                 );
107                 continue;
108             }
109             try {
110                 $io->info(
111                     sprintf(
112                         $this->trans('commands.cron.execute.messages.executing-cron'),
113                         $module
114                     )
115                 );
116                 $this->moduleHandler->invoke($module, 'cron');
117             } catch (\Exception $e) {
118                 watchdog_exception('cron', $e);
119                 $io->error($e->getMessage());
120             }
121         }
122
123         $this->state->set('system.cron_last', REQUEST_TIME);
124         $this->lock->release('cron');
125
126         $this->chainQueue->addCommand('cache:rebuild', ['cache' => 'all']);
127
128         $io->success($this->trans('commands.cron.execute.messages.success'));
129
130         return 0;
131     }
132 }