CustomLog.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. <?php
  2. /**
  3. * Author: lf
  4. * Blog: https://blog.feehi.com
  5. * Email: job@feehi.com
  6. * Created at: 2017-09-20 23:04
  7. */
  8. namespace backend\components;
  9. use yii;
  10. use common\models\AdminUser;
  11. use yii\base\ErrorException;
  12. class CustomLog extends \yii\base\Event
  13. {
  14. const EVENT_AFTER_CREATE = 1;
  15. const EVENT_AFTER_DELETE = 2;
  16. const EVENT_CUSTOM = 3;
  17. public $description = null;
  18. private $adminUserName = null;
  19. public function init()
  20. {
  21. parent::init();
  22. /** @var AdminUser $identity */
  23. $components = Yii::$app->coreComponents();
  24. if( !isset($components['user']) ){//cli(console)模式
  25. $this->adminUserName = "command(console)";
  26. }else {
  27. $identity = yii::$app->getUser()->getIdentity();
  28. $this->adminUserName = $identity->username;
  29. }
  30. }
  31. public function getDescription()
  32. {
  33. switch ($this->name){
  34. case self::EVENT_AFTER_CREATE:
  35. $description = $this->create();
  36. break;
  37. case self::EVENT_AFTER_DELETE:
  38. $description = $this->delete();
  39. break;
  40. case self::EVENT_CUSTOM:
  41. $description = $this->custom();
  42. break;
  43. default:
  44. throw new ErrorException("None exists event");
  45. }
  46. $this->setDescription($description);
  47. return $this->description;
  48. }
  49. public function setDescription($description)
  50. {
  51. $this->description = $description;
  52. }
  53. private function create()
  54. {
  55. $class = $this->sender->className();
  56. $template = $description = '{{%ADMIN_USER%}} [ ' . $this->adminUserName . ' ] {{%BY%}} ' . $class . " {{%CREATED%}} {{%RECORD%}}: ";
  57. if( $this->description !== null ){
  58. return $template . $this->description;
  59. }
  60. switch ($this->sender->className()){
  61. default:
  62. $str = "<br>";
  63. foreach ($this->sender->activeAttributes() as $field) {
  64. $value = $this->sender->$field;
  65. if( is_array($value) ) $value = implode(',', $value);
  66. $str .= $this->sender->getAttributeLabel($field) . '(' . $field . ') => ' . $value . ',<br>';
  67. }
  68. $str = substr($str, 0, -5);
  69. }
  70. return $template . $str;
  71. }
  72. private function delete()
  73. {
  74. $class = $this->sender->className();
  75. $template = '{{%ADMIN_USER%}} [ ' . $this->adminUserName . ' ] {{%BY%}} ' . $class . " {{%DELETED%}} {{%RECORD%}}: ";
  76. if( $this->description !== null ){
  77. return $template . $this->description;
  78. }
  79. switch ($this->sender->className()){
  80. default:
  81. $str = "<br>";
  82. foreach ($this->sender->activeAttributes() as $field) {
  83. $value = $this->sender->$field;
  84. if( is_array($value) ) $value = implode(',', $value);
  85. $str .= $this->sender->getAttributeLabel($field) . '(' . $field . ') => ' . $value . ',<br>';
  86. }
  87. $str = substr($str, 0, -5);
  88. }
  89. return $template . $str;
  90. }
  91. private function custom()
  92. {
  93. $class= $this->sender->className();
  94. $template = '{{%ADMIN_USER%}} [ ' . $this->adminUserName . ' ] {{%BY%}} ' . $class;
  95. if ($this->description !== null){
  96. return $template . $this->description;
  97. }
  98. switch ($this->sender->className()){
  99. default:
  100. throw new ErrorException("EVENT_CUSTOM must set description property");
  101. }
  102. }
  103. }