Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
CrontabManager.php
Go to the documentation of this file.
1 <?php
8 
14 
19 {
23  private $shell;
24 
28  private $filesystem;
29 
34  public function __construct(
35  ShellInterface $shell,
36  Filesystem $filesystem
37  ) {
38  $this->shell = $shell;
39  $this->filesystem = $filesystem;
40  }
41 
45  private function getTasksBlockStart()
46  {
47  $tasksBlockStart = self::TASKS_BLOCK_START;
48  if (defined('BP')) {
49  $tasksBlockStart .= ' ' . md5(BP);
50  }
51  return $tasksBlockStart;
52  }
53 
57  private function getTasksBlockEnd()
58  {
59  $tasksBlockEnd = self::TASKS_BLOCK_END;
60  if (defined('BP')) {
61  $tasksBlockEnd .= ' ' . md5(BP);
62  }
63  return $tasksBlockEnd;
64  }
65 
69  public function getTasks()
70  {
71  $this->checkSupportedOs();
72  $content = $this->getCrontabContent();
73  $pattern = '!(' . $this->getTasksBlockStart() . ')(.*?)(' . $this->getTasksBlockEnd() . ')!s';
74 
75  if (preg_match($pattern, $content, $matches)) {
76  $tasks = trim($matches[2], PHP_EOL);
77  $tasks = explode(PHP_EOL, $tasks);
78  return $tasks;
79  }
80 
81  return [];
82  }
83 
87  public function saveTasks(array $tasks)
88  {
89  if (!$tasks) {
90  throw new LocalizedException(new Phrase('The list of tasks is empty. Add tasks and try again.'));
91  }
92 
93  $this->checkSupportedOs();
94  $baseDir = $this->filesystem->getDirectoryRead(DirectoryList::ROOT)->getAbsolutePath();
95  $logDir = $this->filesystem->getDirectoryRead(DirectoryList::LOG)->getAbsolutePath();
96 
97  foreach ($tasks as $key => $task) {
98  if (empty($task['expression'])) {
99  $tasks[$key]['expression'] = '* * * * *';
100  }
101 
102  if (empty($task['command'])) {
103  throw new LocalizedException(new Phrase("The command shouldn't be empty. Enter and try again."));
104  }
105 
106  $tasks[$key]['command'] = str_replace(
107  ['{magentoRoot}', '{magentoLog}'],
108  [$baseDir, $logDir],
109  $task['command']
110  );
111  }
112 
113  $content = $this->getCrontabContent();
114  $content = $this->cleanMagentoSection($content);
115  $content = $this->generateSection($content, $tasks);
116 
117  $this->save($content);
118  }
119 
124  public function removeTasks()
125  {
126  $this->checkSupportedOs();
127  $content = $this->getCrontabContent();
128  $content = $this->cleanMagentoSection($content);
129  $this->save($content);
130  }
131 
139  private function generateSection($content, $tasks = [])
140  {
141  if ($tasks) {
142  // Add EOL symbol to previous line if not exist.
143  if (substr($content, -strlen(PHP_EOL)) !== PHP_EOL) {
144  $content .= PHP_EOL;
145  }
146 
147  $content .= $this->getTasksBlockStart() . PHP_EOL;
148  foreach ($tasks as $task) {
149  $content .= $task['expression'] . ' ' . PHP_BINARY . ' ' . $task['command'] . PHP_EOL;
150  }
151  $content .= $this->getTasksBlockEnd() . PHP_EOL;
152  }
153 
154  return $content;
155  }
156 
163  private function cleanMagentoSection($content)
164  {
165  $content = preg_replace(
166  '!' . preg_quote($this->getTasksBlockStart()) . '.*?'
167  . preg_quote($this->getTasksBlockEnd() . PHP_EOL) . '!s',
168  '',
169  $content
170  );
171 
172  return $content;
173  }
174 
182  private function getCrontabContent()
183  {
184  try {
185  $content = (string)$this->shell->execute('crontab -l');
186  } catch (LocalizedException $e) {
187  return '';
188  }
189 
190  return $content;
191  }
192 
200  private function save($content)
201  {
202  $content = str_replace(['%', '"'], ['%%', '\"'], $content);
203 
204  try {
205  $this->shell->execute('echo "' . $content . '" | crontab -');
206  } catch (LocalizedException $e) {
207  throw new LocalizedException(
208  new Phrase('Error during saving of crontab: %1', [$e->getPrevious()->getMessage()]),
209  $e
210  );
211  }
212  }
213 
222  private function checkSupportedOs()
223  {
224  if (stripos(PHP_OS, 'WIN') === 0) {
225  throw new LocalizedException(
226  new Phrase('Your operating system is not supported to work with this command')
227  );
228  }
229  }
230 }
$baseDir
Definition: autoload.php:9
$pattern
Definition: website.php:22
if(!file_exists($installConfigFile)) if(!defined('TESTS_INSTALLATION_DB_CONFIG_FILE')) $shell
Definition: bootstrap.php:46
const BP
Definition: autoload.php:14
__construct(ShellInterface $shell, Filesystem $filesystem)
$filesystem