Plugin.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: NODELOG
  5. * Date: 16/7/4
  6. * Time: 下午12:28
  7. */
  8. namespace plugins;
  9. use common\components\PackageInfo;
  10. use common\models\Plugin as PluginModel;
  11. use common\modules\rbac\models\Menu;
  12. use ReflectionClass;
  13. use Yii;
  14. use yii\base\BootstrapInterface;
  15. use yii\base\InvalidParamException;
  16. use yii\web\View;
  17. abstract class Plugin extends PackageInfo implements BootstrapInterface
  18. {
  19. public $aliases = [];
  20. /**
  21. * @var string 模块所属应用ID(frontend,backend,wechat,api)
  22. */
  23. public $app = 'backend';
  24. private $_model;
  25. /**
  26. * @return Plugin
  27. */
  28. public function getModel()
  29. {
  30. if ($this->_model == null) {
  31. $model = PluginModel::findOne($this->getPackage());
  32. if ($model == null) {
  33. $model = new PluginModel();
  34. $model->loadDefaultValues();
  35. $model->id = $this->getPackage();
  36. }
  37. $this->_model = $model;
  38. }
  39. return $this->_model;
  40. }
  41. /**
  42. * 在菜单插件管理下添加一个新菜单
  43. * @param $name
  44. * @param $route
  45. * @throws \yii\db\Exception
  46. */
  47. public function addMenu($name, $route)
  48. {
  49. $id = \Yii::$app->db->createCommand('SELECT `id` FROM {{%menu}} WHERE `name`="插件" AND `parent` IS NULL')->queryScalar();
  50. if (!$id) {
  51. $model = new Menu();
  52. $model->name = '插件';
  53. $model->route = '';
  54. $model->parent = 24;
  55. $model->save();
  56. $id = $model->id;
  57. }
  58. $model = new Menu();
  59. $model->name = $name;
  60. $model->route = $route;
  61. $model->parent = $id;
  62. $model->save();
  63. }
  64. /**
  65. * 删除一个插件管理下的子菜单
  66. * @param $name
  67. * @throws \yii\db\Exception
  68. */
  69. public function deleteMenu($name)
  70. {
  71. \Yii::$app->db->createCommand("DELETE FROM {{%menu}} WHERE `name`='{$name}'")->execute();
  72. }
  73. public function install()
  74. {
  75. return true;
  76. }
  77. public function uninstall()
  78. {
  79. return true;
  80. }
  81. /**
  82. * 各插件在系统bootstrap阶段执行,前台执行frontend方法,后台执行backend方法.
  83. * 比如插件要在后台添加一个控制器,则可以这样写
  84. * ```
  85. public function backend($app)
  86. {
  87. $app->controllerMap['donation'] = [
  88. 'class' => '\plugins\donation\controllers\AdminController',
  89. 'viewPath' => '@plugins/donation/views/admin'
  90. ];
  91. }
  92. * ```
  93. * @param \yii\base\Application $app
  94. */
  95. public function bootstrap($app)
  96. {
  97. if ($this->hasMethod($app->id)) {
  98. call_user_func([$this, $app->id], $app);
  99. }
  100. }
  101. private $_view;
  102. /**
  103. * Returns the view object that can be used to render views or view files.
  104. * The [[render()]] and [[renderFile()]] methods will use
  105. * this view object to implement the actual view rendering.
  106. * If not set, it will default to the "view" application component.
  107. * @return \yii\web\View the view object that can be used to render views or view files.
  108. */
  109. public function getView()
  110. {
  111. if ($this->_view === null) {
  112. $this->_view = Yii::$app->getView();
  113. }
  114. return $this->_view;
  115. }
  116. /**
  117. * Sets the view object to be used by this widget.
  118. * @param View $view the view object that can be used to render views or view files.
  119. */
  120. public function setView($view)
  121. {
  122. $this->_view = $view;
  123. }
  124. /**
  125. * Renders a view.
  126. * The view to be rendered can be specified in one of the following formats:
  127. *
  128. * - path alias (e.g. "@app/views/site/index");
  129. * - absolute path within application (e.g. "//site/index"): the view name starts with double slashes.
  130. * The actual view file will be looked for under the [[Application::viewPath|view path]] of the application.
  131. * - absolute path within module (e.g. "/site/index"): the view name starts with a single slash.
  132. * The actual view file will be looked for under the [[Module::viewPath|view path]] of the currently
  133. * active module.
  134. * - relative path (e.g. "index"): the actual view file will be looked for under [[viewPath]].
  135. *
  136. * If the view name does not contain a file extension, it will use the default one `.php`.
  137. *
  138. * @param string $view the view name.
  139. * @param array $params the parameters (name-value pairs) that should be made available in the view.
  140. * @return string the rendering result.
  141. * @throws InvalidParamException if the view file does not exist.
  142. */
  143. public function render($view, $params = [])
  144. {
  145. return $this->getView()->render($view, $params, $this);
  146. }
  147. /**
  148. * Renders a view file.
  149. * @param string $file the view file to be rendered. This can be either a file path or a path alias.
  150. * @param array $params the parameters (name-value pairs) that should be made available in the view.
  151. * @return string the rendering result.
  152. * @throws InvalidParamException if the view file does not exist.
  153. */
  154. public function renderFile($file, $params = [])
  155. {
  156. return $this->getView()->renderFile($file, $params, $this);
  157. }
  158. /**
  159. * Returns the directory containing the view files for this widget.
  160. * The default implementation returns the 'views' subdirectory under the directory containing the widget class file.
  161. * @return string the directory containing the view files for this widget.
  162. */
  163. public function getViewPath()
  164. {
  165. $class = new ReflectionClass($this);
  166. return dirname($class->getFileName()) . DIRECTORY_SEPARATOR . 'views';
  167. }
  168. }