Plugin.php 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. <?php
  2. namespace common\models;
  3. use common\behaviors\CacheInvalidateBehavior;
  4. use Yii;
  5. use yii\behaviors\TimestampBehavior;
  6. /**
  7. * This is the model class for table "{{%plugin}}".
  8. *
  9. * @property integer $id
  10. * @property string $name
  11. * @property string $title
  12. * @property integer $status
  13. * @property string $author
  14. * @property string $description
  15. * @property string $config
  16. * @property integer $created_at
  17. * @property integer $updated_at
  18. */
  19. class Plugin extends \yii\db\ActiveRecord
  20. {
  21. const STATUS_OPEN = 1;
  22. const STATUS_CLOSE = 0;
  23. /**
  24. * @inheritdoc
  25. */
  26. public static function tableName()
  27. {
  28. return '{{%plugin}}';
  29. }
  30. /**
  31. * @inheritdoc
  32. */
  33. public function rules()
  34. {
  35. return [
  36. [['id', 'name'], 'required'],
  37. [['status'], 'integer'],
  38. [['name'], 'string', 'max' => 50],
  39. [['config', 'description'], 'string'],
  40. ['status', 'default', 'value' => 1],
  41. [['id'], 'unique'],
  42. ];
  43. }
  44. /**
  45. * @inheritdoc
  46. */
  47. public function attributeLabels()
  48. {
  49. return [
  50. 'id' => 'ID',
  51. 'name' => '名称',
  52. 'status' => '是否启用',
  53. 'config' => '配置',
  54. 'created_at' => '创建时间',
  55. 'updated_at' => '更新时间',
  56. ];
  57. }
  58. public function behaviors()
  59. {
  60. return [
  61. TimestampBehavior::className(),
  62. [
  63. 'class' => CacheInvalidateBehavior::className(),
  64. 'keys' => [
  65. 'plugins',
  66. ]
  67. ]
  68. ];
  69. }
  70. public static function findOpenPlugins()
  71. {
  72. $plugins = Yii::$app->cache->get('plugins');
  73. if ($plugins === false) {
  74. $query = static::find();
  75. $plugins = $query->where(['status' => self::STATUS_OPEN])->all();
  76. Yii::$app->cache->set('plugins', $plugins, 0);
  77. }
  78. return $plugins;
  79. }
  80. public function getInstall()
  81. {
  82. return $this->isNewRecord ? false : true;
  83. }
  84. public function getOpen()
  85. {
  86. return $this->status == self::STATUS_OPEN;
  87. }
  88. }