Conf.php 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. <?php
  2. namespace app\models;
  3. use Yii;
  4. /**
  5. * This is the model class for table "conf".
  6. *
  7. * @property integer $id
  8. * @property integer $user_id
  9. * @property string $name
  10. * @property string $conf
  11. * @property integer $level
  12. * @property integer $status
  13. * @property integer $at
  14. */
  15. class Conf extends \yii\db\ActiveRecord
  16. {
  17. const CONF_TPL = 'conf_tpl';
  18. // 有效状态
  19. const STATUS_VALID = 1;
  20. // 测试环境
  21. const LEVEL_TEST = 1;
  22. // 模拟线上环境
  23. const LEVEL_SIMU = 2;
  24. // 线上环境
  25. const LEVEL_PROD = 3;
  26. public static $LEVEL = [
  27. self::LEVEL_TEST => 'test',
  28. self::LEVEL_SIMU => 'simu',
  29. self::LEVEL_PROD => 'prod',
  30. ];
  31. public $context;
  32. /**
  33. * @inheritdoc
  34. */
  35. public static function tableName()
  36. {
  37. return 'conf';
  38. }
  39. /**
  40. * @inheritdoc
  41. */
  42. public function rules()
  43. {
  44. return [
  45. [['user_id', 'name', 'conf', 'level', 'created_at'], 'required'],
  46. [['user_id', 'level', 'status', 'created_at', 'level'], 'integer'],
  47. [['name', 'conf'], 'string', 'max' => 20],
  48. ];
  49. }
  50. /**
  51. * @inheritdoc
  52. */
  53. public function attributeLabels()
  54. {
  55. return [
  56. 'id' => 'ID',
  57. 'user_id' => 'user_id',
  58. 'name' => 'Name',
  59. 'conf' => 'Conf',
  60. 'level' => 'Level',
  61. 'status' => 'Status',
  62. 'created_at' => 'created_at',
  63. ];
  64. }
  65. public static function getConfigFile($id) {
  66. $conf = static::findOne($id);
  67. if (!$conf) {
  68. throw new \Exception('找不到此任务号');
  69. };
  70. return static::getConf($conf->conf);
  71. }
  72. public static function getConf($name) {
  73. return sprintf("%s/%s/%s.yml",
  74. rtrim(\Yii::$app->basePath, '/'), trim(Yii::$app->params['config.dir'], '/'), $name);
  75. }
  76. public static function saveConfContext($name, $context) {
  77. $file = static::getConf($name);
  78. return file_put_contents($file, $context);
  79. }
  80. public static function getConfContext($name) {
  81. $file = static::getConf($name);
  82. if (file_exists($file)) {
  83. return file_get_contents($file);
  84. }
  85. return '';
  86. }
  87. }