1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace app\backend\actions; |
4
|
|
|
|
5
|
|
|
use Symfony\Component\Finder\Iterator\RecursiveDirectoryIterator; |
6
|
|
|
use yii\base\Action; |
7
|
|
|
use yii\base\Module; |
8
|
|
|
use yii\caching\Cache; |
9
|
|
|
|
10
|
|
|
class FlushCacheAction extends Action |
11
|
|
|
{ |
12
|
|
|
|
13
|
|
|
public $view = 'message'; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Recursive flush all app cache |
17
|
|
|
* @param null|Module $current Current Module |
18
|
|
|
* @return string execute message |
19
|
|
|
*/ |
20
|
|
|
protected function flushCache(Module $current = null) |
21
|
|
|
{ |
22
|
|
|
$message = ''; |
23
|
|
|
if ($current === null) { |
24
|
|
|
$current = \Yii::$app; |
25
|
|
|
} |
26
|
|
|
$modules = $current->getModules(); |
27
|
|
|
foreach ($modules as $moduleName => $module) { |
28
|
|
|
if (is_array($module)) { |
29
|
|
|
$module = $current->getModule($moduleName, true); |
30
|
|
|
} |
31
|
|
|
if ($module instanceof Module) { |
32
|
|
|
$message .= $this->flushCache($module); |
33
|
|
|
} |
34
|
|
|
} |
35
|
|
|
$components = $current->getComponents(); |
36
|
|
|
foreach ($components as $componentName => $component) { |
37
|
|
|
if (is_array($component)) { |
38
|
|
|
$component = $current->get($componentName); |
39
|
|
|
} |
40
|
|
|
if ($component instanceof Cache) { |
41
|
|
|
$message .= $component->flush() ? |
42
|
|
|
'<p>' . \Yii::t( |
43
|
|
|
'app', |
44
|
|
|
'{currentModuleName} {componentName} is flushed', |
45
|
|
|
[ |
46
|
|
|
'currentModuleName' => $current->className(), |
|
|
|
|
47
|
|
|
'componentName' => $component->className(), |
|
|
|
|
48
|
|
|
] |
49
|
|
|
) . '</p>' : |
50
|
|
|
''; |
51
|
|
|
} |
52
|
|
|
} |
53
|
|
|
return $message; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Flush webroot/assets/ |
58
|
|
|
* @return string execute message |
59
|
|
|
*/ |
60
|
|
|
protected function flushAssets() |
61
|
|
|
{ |
62
|
|
|
$message = ''; |
63
|
|
|
$except = [\Yii::getAlias('@webroot/assets/.gitignore'), \Yii::getAlias('@webroot/assets/index.html')]; |
64
|
|
|
$dir = \Yii::getAlias('@webroot/assets'); |
65
|
|
|
$it = new \RecursiveDirectoryIterator($dir, \RecursiveDirectoryIterator::SKIP_DOTS); |
66
|
|
|
/* @var RecursiveDirectoryIterator[] $files */ |
67
|
|
|
$files = new \RecursiveIteratorIterator($it, \RecursiveIteratorIterator::CHILD_FIRST); |
68
|
|
|
$hasErrors = false; |
69
|
|
|
if (stristr(PHP_OS, 'WIN') === false) { |
70
|
|
|
foreach ($files as $file) { |
71
|
|
|
if (!in_array($file->getRealPath(), $except)) { |
72
|
|
|
if ($file->isDir() && $file->isLink() === false) { |
73
|
|
|
$result = @rmdir($file->getRealPath()); |
74
|
|
|
} elseif ($file->isLink() === true) { |
75
|
|
|
$result = @unlink($file->getPath() . DIRECTORY_SEPARATOR . $file->getFilename()); |
76
|
|
|
} else { |
77
|
|
|
$result = @unlink($file->getRealPath()); |
78
|
|
|
} |
79
|
|
|
if (!$result) { |
80
|
|
|
$hasErrors = true; |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
$message .= $hasErrors |
86
|
|
|
? '<p>' . \Yii::t('app', 'Some assets are not flushed') . '</p>' |
87
|
|
|
: '<p>' . \Yii::t('app', 'Assets are flushed') . '</p>'; |
88
|
|
|
return $message; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
public function run() |
92
|
|
|
{ |
93
|
|
|
$message = $this->flushCache(); |
94
|
|
|
$message .= $this->flushAssets(); |
95
|
|
|
return $this->controller->renderPartial($this->view, ['message' => $message]); |
96
|
|
|
} |
97
|
|
|
} |
This method has been deprecated. The supplier of the class has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.