|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* File: Purge.php |
|
7
|
|
|
* |
|
8
|
|
|
* @author Maciej Sławik <[email protected]> |
|
9
|
|
|
* @copyright Copyright (C) 2019 Lizard Media (http://lizardmedia.pl) |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace LizardMedia\VarnishWarmer\Controller\Adminhtml; |
|
13
|
|
|
|
|
14
|
|
|
use LizardMedia\VarnishWarmer\Api\ProgressHandler\ProgressBarRendererInterface; |
|
15
|
|
|
use LizardMedia\VarnishWarmer\Api\ProgressHandler\QueueProgressLoggerInterface; |
|
16
|
|
|
use LizardMedia\VarnishWarmer\Api\VarnishActionManagerInterface; |
|
17
|
|
|
use LizardMedia\VarnishWarmer\Block\Adminhtml\PurgeSingle\Form\Edit\Form; |
|
18
|
|
|
use Magento\Backend\App\Action; |
|
19
|
|
|
use Magento\Backend\App\Action\Context; |
|
20
|
|
|
use Magento\Framework\App\Filesystem\DirectoryList; |
|
21
|
|
|
use Magento\Framework\App\ResponseInterface; |
|
22
|
|
|
use Magento\Framework\Controller\Result\Redirect; |
|
23
|
|
|
use Magento\Framework\Message\Factory; |
|
24
|
|
|
use Magento\Framework\Message\ManagerInterface; |
|
25
|
|
|
use Magento\Framework\Message\MessageInterface; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* Class Purge |
|
29
|
|
|
* @package LizardMedia\VarnishWarmer\Controller\Adminhtml |
|
30
|
|
|
* @SuppressWarnings(PHPMD.LongVariable) |
|
31
|
|
|
*/ |
|
32
|
|
|
abstract class Purge extends Action |
|
33
|
|
|
{ |
|
34
|
|
|
/** |
|
35
|
|
|
* @var ManagerInterface |
|
36
|
|
|
*/ |
|
37
|
|
|
protected $messageManager; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @var DirectoryList |
|
41
|
|
|
*/ |
|
42
|
|
|
protected $directoryList; |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* @var VarnishActionManagerInterface |
|
46
|
|
|
*/ |
|
47
|
|
|
protected $varnishActionManager; |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* @var QueueProgressLoggerInterface |
|
51
|
|
|
*/ |
|
52
|
|
|
protected $queueProgressLogger; |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* @var ProgressBarRendererInterface |
|
56
|
|
|
*/ |
|
57
|
|
|
protected $queueProgressBarRenderer; |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* @var Factory |
|
61
|
|
|
*/ |
|
62
|
|
|
private $messageFactory; |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* Purge constructor. |
|
66
|
|
|
* @param Context $context |
|
67
|
|
|
* @param DirectoryList $directoryList |
|
68
|
|
|
* @param VarnishActionManagerInterface $varnishActionManager |
|
69
|
|
|
* @param QueueProgressLoggerInterface $queueProgressLogger |
|
70
|
|
|
* @param ProgressBarRendererInterface $queueProgressBarRenderer |
|
71
|
|
|
* @param Factory $messageFactory |
|
72
|
|
|
* @SuppressWarnings(PHPMD.LongVariable) |
|
73
|
|
|
*/ |
|
74
|
|
|
public function __construct( |
|
75
|
|
|
Context $context, |
|
76
|
|
|
DirectoryList $directoryList, |
|
77
|
|
|
VarnishActionManagerInterface $varnishActionManager, |
|
78
|
|
|
QueueProgressLoggerInterface $queueProgressLogger, |
|
79
|
|
|
ProgressBarRendererInterface $queueProgressBarRenderer, |
|
80
|
|
|
Factory $messageFactory |
|
81
|
|
|
) { |
|
82
|
|
|
parent::__construct($context); |
|
83
|
|
|
$this->messageManager = $context->getMessageManager(); |
|
84
|
|
|
$this->directoryList = $directoryList; |
|
85
|
|
|
$this->varnishActionManager = $varnishActionManager; |
|
86
|
|
|
$this->queueProgressLogger = $queueProgressLogger; |
|
87
|
|
|
$this->queueProgressBarRenderer = $queueProgressBarRenderer; |
|
88
|
|
|
$this->messageFactory = $messageFactory; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* @return Redirect |
|
93
|
|
|
*/ |
|
94
|
|
|
public function execute(): Redirect |
|
95
|
|
|
{ |
|
96
|
|
|
if (!$this->isLocked()) { |
|
97
|
|
|
$this->runCommand(); |
|
98
|
|
|
$this->addProcessNotification(); |
|
99
|
|
|
} else { |
|
100
|
|
|
$this->addProcessLockWarning(); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
return $this->getRedirect(); |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
/** |
|
107
|
|
|
* @return string |
|
108
|
|
|
*/ |
|
109
|
|
|
abstract protected function getCliCommand(): string; |
|
110
|
|
|
|
|
111
|
|
|
/** |
|
112
|
|
|
* @return Redirect |
|
113
|
|
|
*/ |
|
114
|
|
|
protected function getRedirect(): Redirect |
|
115
|
|
|
{ |
|
116
|
|
|
$resultRedirect = $this->resultRedirectFactory->create(); |
|
117
|
|
|
$resultRedirect->setPath('admin/dashboard/index'); |
|
118
|
|
|
return $resultRedirect; |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
/** |
|
122
|
|
|
* @return void |
|
123
|
|
|
*/ |
|
124
|
|
|
protected function addProcessNotification(): void |
|
125
|
|
|
{ |
|
126
|
|
|
$this->messageManager->addNoticeMessage(__('LizardMedia: cache is purged in background, it may take a while.')); |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
/** |
|
130
|
|
|
* @return void |
|
131
|
|
|
*/ |
|
132
|
|
|
protected function addProcessLockWarning(): void |
|
133
|
|
|
{ |
|
134
|
|
|
$this->messageManager->addMessage( |
|
135
|
|
|
$this->messageFactory->create( |
|
136
|
|
|
MessageInterface::TYPE_WARNING, |
|
137
|
|
|
sprintf( |
|
138
|
|
|
__( |
|
139
|
|
|
'LizardMedia: cache is already being purged in background ' |
|
140
|
|
|
. '(started at: %s), ' |
|
141
|
|
|
. 'cannot run again until it finishes. <br />%s' |
|
142
|
|
|
), |
|
143
|
|
|
$this->varnishActionManager->getLockMessage(), |
|
144
|
|
|
$this->queueProgressBarRenderer->getProgressHtml($this->queueProgressLogger->getProgressData()) |
|
145
|
|
|
) |
|
146
|
|
|
) |
|
147
|
|
|
); |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
/** |
|
151
|
|
|
* @return void |
|
152
|
|
|
*/ |
|
153
|
|
|
protected function runCommand(): void |
|
154
|
|
|
{ |
|
155
|
|
|
$additionalParams = $this->getAdditionalParams(); |
|
156
|
|
|
$baseDir = $this->directoryList->getRoot(); |
|
157
|
|
|
$cliCommand = $this->getCliCommand(); |
|
158
|
|
|
$cmd = "nohup {$baseDir}/bin/magento {$cliCommand}{$additionalParams}>/dev/null 2>&1 &"; |
|
159
|
|
|
exec($cmd); |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
|
|
/** |
|
163
|
|
|
* @return bool |
|
164
|
|
|
*/ |
|
165
|
|
|
protected function isLocked(): bool |
|
166
|
|
|
{ |
|
167
|
|
|
return $this->varnishActionManager->isLocked(); |
|
168
|
|
|
} |
|
169
|
|
|
|
|
170
|
|
|
/** |
|
171
|
|
|
* @return string |
|
172
|
|
|
*/ |
|
173
|
|
|
protected function getAdditionalParams(): string |
|
174
|
|
|
{ |
|
175
|
|
|
$additionalParams = ''; |
|
176
|
|
|
|
|
177
|
|
|
if ($this->getRequest()->getParam(Form::STORE_VIEW_FORM_PARAM)) { |
|
178
|
|
|
$additionalParams .= ' --store=' . $this->getRequest()->getParam(Form::STORE_VIEW_FORM_PARAM); |
|
179
|
|
|
} |
|
180
|
|
|
|
|
181
|
|
|
return $additionalParams; |
|
182
|
|
|
} |
|
183
|
|
|
} |
|
184
|
|
|
|