Module.php 2.5 KB

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