3
3
* Copyright © Magento, Inc. All rights reserved.
4
4
* See COPYING.txt for license details.
5
5
*/
6
+ declare (strict_types=1 );
7
+
6
8
namespace Magento \Cron \Console \Command ;
7
9
8
10
use Magento \Framework \Crontab \CrontabManagerInterface ;
19
21
*/
20
22
class CronInstallCommand extends Command
21
23
{
24
+ private const COMMAND_OPTION_FORCE = 'force ' ;
25
+ private const COMMAND_OPTION_NON_OPTIONAL = 'non-optional ' ;
26
+
22
27
/**
23
28
* @var CrontabManagerInterface
24
29
*/
@@ -44,19 +49,27 @@ public function __construct(
44
49
}
45
50
46
51
/**
47
- * { @inheritdoc}
52
+ * @inheritdoc
48
53
*/
49
54
protected function configure ()
50
55
{
51
56
$ this ->setName ('cron:install ' )
52
57
->setDescription ('Generates and installs crontab for current user ' )
53
- ->addOption ('force ' , 'f ' , InputOption::VALUE_NONE , 'Force install tasks ' );
58
+ ->addOption (self ::COMMAND_OPTION_FORCE , 'f ' , InputOption::VALUE_NONE , 'Force install tasks ' )
59
+ // @codingStandardsIgnoreStart
60
+ ->addOption (self ::COMMAND_OPTION_NON_OPTIONAL , 'd ' , InputOption::VALUE_NONE , 'Install only the non-optional (default) tasks ' );
61
+ // @codingStandardsIgnoreEnd
54
62
55
63
parent ::configure ();
56
64
}
57
65
58
66
/**
59
- * {@inheritdoc}
67
+ * Executes "cron:install" command.
68
+ *
69
+ * @param InputInterface $input
70
+ * @param OutputInterface $output
71
+ * @return int|null
72
+ * @throws LocalizedException
60
73
*/
61
74
protected function execute (InputInterface $ input , OutputInterface $ output )
62
75
{
@@ -65,8 +78,13 @@ protected function execute(InputInterface $input, OutputInterface $output)
65
78
return Cli::RETURN_FAILURE ;
66
79
}
67
80
81
+ $ tasks = $ this ->tasksProvider ->getTasks ();
82
+ if ($ input ->getOption (self ::COMMAND_OPTION_NON_OPTIONAL )) {
83
+ $ tasks = $ this ->extractNonOptionalTasks ($ tasks );
84
+ }
85
+
68
86
try {
69
- $ this ->crontabManager ->saveTasks ($ this -> tasksProvider -> getTasks () );
87
+ $ this ->crontabManager ->saveTasks ($ tasks );
70
88
} catch (LocalizedException $ e ) {
71
89
$ output ->writeln ('<error> ' . $ e ->getMessage () . '</error> ' );
72
90
return Cli::RETURN_FAILURE ;
@@ -76,4 +94,23 @@ protected function execute(InputInterface $input, OutputInterface $output)
76
94
77
95
return Cli::RETURN_SUCCESS ;
78
96
}
97
+
98
+ /**
99
+ * Returns an array of non-optional tasks
100
+ *
101
+ * @param array $tasks
102
+ * @return array
103
+ */
104
+ private function extractNonOptionalTasks (array $ tasks = []): array
105
+ {
106
+ $ defaultTasks = [];
107
+
108
+ foreach ($ tasks as $ taskCode => $ taskParams ) {
109
+ if (!$ taskParams ['optional ' ]) {
110
+ $ defaultTasks [$ taskCode ] = $ taskParams ;
111
+ }
112
+ }
113
+
114
+ return $ defaultTasks ;
115
+ }
79
116
}
0 commit comments