Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Public Member Functions
CrontabManager Class Reference
Inheritance diagram for CrontabManager:
CrontabManagerInterface

Public Member Functions

 __construct (ShellInterface $shell, Filesystem $filesystem)
 
 getTasks ()
 
 saveTasks (array $tasks)
 
 removeTasks ()
 

Additional Inherited Members

- Data Fields inherited from CrontabManagerInterface
const TASKS_BLOCK_START = '#~ MAGENTO START'
 
const TASKS_BLOCK_END = '#~ MAGENTO END'
 

Detailed Description

Manager works with cron tasks

Definition at line 18 of file CrontabManager.php.

Constructor & Destructor Documentation

◆ __construct()

__construct ( ShellInterface  $shell,
Filesystem  $filesystem 
)
Parameters
ShellInterface$shell
Filesystem$filesystem

Definition at line 34 of file CrontabManager.php.

37  {
38  $this->shell = $shell;
39  $this->filesystem = $filesystem;
40  }

Member Function Documentation

◆ getTasks()

getTasks ( )

{#- Get list of Magento Tasks

Returns
array
Exceptions
LocalizedException
}

Implements CrontabManagerInterface.

Definition at line 69 of file CrontabManager.php.

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  }
$pattern
Definition: website.php:22

◆ removeTasks()

removeTasks ( )

{Remove Magento Tasks form crontab

Returns
void
Exceptions
LocalizedException
}

Exceptions
LocalizedException

Implements CrontabManagerInterface.

Definition at line 124 of file CrontabManager.php.

125  {
126  $this->checkSupportedOs();
127  $content = $this->getCrontabContent();
128  $content = $this->cleanMagentoSection($content);
129  $this->save($content);
130  }

◆ saveTasks()

saveTasks ( array  $tasks)

{Save Magento Tasks to crontab

Parameters
array$tasks
Returns
void
Exceptions
LocalizedException
}

Implements CrontabManagerInterface.

Definition at line 87 of file CrontabManager.php.

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  }
$baseDir
Definition: autoload.php:9

The documentation for this class was generated from the following file: