ClearController.php 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. <?php
  2. /**
  3. * Author: lf
  4. * Blog: https://blog.feehi.com
  5. * Email: job@feehi.com
  6. * Created at: 2016-04-13 12:49
  7. */
  8. namespace backend\controllers;
  9. use Yii;
  10. use yii\helpers\FileHelper;
  11. class ClearController extends \yii\web\Controller
  12. {
  13. /**
  14. * remove all backend cache
  15. *
  16. * @auth - item group=其他 category=缓存 description-get=清除后台缓存 sort=720 method=get
  17. * @return string
  18. * @throws \yii\base\ErrorException
  19. */
  20. public function actionBackend()
  21. {
  22. FileHelper::removeDirectory(Yii::getAlias('@runtime/cache'));
  23. $paths = [Yii::getAlias('@admin/assets'), Yii::getAlias('@backend/web/assets')];
  24. $this->deleteFiles($paths);
  25. Yii::$app->getSession()->setFlash('success', Yii::t('app', 'Success'));
  26. return $this->render('clear');
  27. }
  28. /**
  29. * remove all frontend cache
  30. *
  31. * @auth - item group=其他 category=缓存 description-get=清除前台缓存 sort=721 method=get
  32. * @return string
  33. * @throws \yii\base\ErrorException
  34. */
  35. public function actionFrontend()
  36. {
  37. FileHelper::removeDirectory(Yii::getAlias('@frontend/runtime/cache'));
  38. $paths = [Yii::getAlias('@frontend/web/assets')];
  39. $this->deleteFiles($paths);
  40. Yii::$app->getSession()->setFlash('success', Yii::t('app', 'Success'));
  41. return $this->render('clear');
  42. }
  43. /**
  44. * @param array $paths
  45. * @throws \yii\base\ErrorException
  46. */
  47. private function deleteFiles(array $paths)
  48. {
  49. foreach ($paths as $path) {
  50. $fp = opendir($path);
  51. while (false !== ($file = readdir($fp))) {
  52. if (! in_array($file, ['.', '..', '.gitignore'])) {
  53. FileHelper::removeDirectory($path . DIRECTORY_SEPARATOR . $file);
  54. }
  55. }
  56. }
  57. }
  58. }