123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262 |
- <?php
- namespace cmf\controller;
- use think\exception\ValidateException;
- use think\Request;
- use think\Config;
- use think\Loader;
- use think\exception\TemplateNotFoundException;
- class PluginBaseController extends BaseController
- {
-
- private $plugin;
-
- protected $beforeActionList = [];
-
- public function __construct(Request $request = null)
- {
- if (is_null($request)) {
- $request = Request::instance();
- }
- $this->request = $request;
- $this->getPlugin();
- $this->view = $this->plugin->getView();
- $siteInfo = cmf_get_site_info();
- $this->assign('site_info', $siteInfo);
-
- $this->_initialize();
-
- if ($this->beforeActionList) {
- foreach ($this->beforeActionList as $method => $options) {
- is_numeric($method) ?
- $this->beforeAction($options) :
- $this->beforeAction($method, $options);
- }
- }
- }
- public function getPlugin()
- {
- if (is_null($this->plugin)) {
- $pluginName = $this->request->param('_plugin');
- $pluginName = cmf_parse_name($pluginName, 1);
- $class = cmf_get_plugin_class($pluginName);
- $this->plugin = new $class;
- }
- return $this->plugin;
- }
-
- protected function _initialize()
- {
- }
-
- protected function beforeAction($method, $options = [])
- {
- if (isset($options['only'])) {
- if (is_string($options['only'])) {
- $options['only'] = explode(',', $options['only']);
- }
- if (!in_array($this->request->action(), $options['only'])) {
- return;
- }
- } elseif (isset($options['except'])) {
- if (is_string($options['except'])) {
- $options['except'] = explode(',', $options['except']);
- }
- if (in_array($this->request->action(), $options['except'])) {
- return;
- }
- }
- call_user_func([$this, $method]);
- }
-
- protected function fetch($template = '', $vars = [], $replace = [], $config = [])
- {
- $template = $this->parseTemplate($template);
-
- if (!is_file($template)) {
- throw new TemplateNotFoundException('template not exists:' . $template, $template);
- }
- return $this->view->fetch($template, $vars, $replace, $config);
- }
-
- private function parseTemplate($template)
- {
-
- $viewEngineConfig = Config::get('template');
- $path = $this->plugin->getThemeRoot();
- $depr = $viewEngineConfig['view_depr'];
- $data = $this->request->param();
- $controller = $data['_controller'];
- $action = $data['_action'];
- if (0 !== strpos($template, '/')) {
- $template = str_replace(['/', ':'], $depr, $template);
- $controller = Loader::parseName($controller);
- if ($controller) {
- if ('' == $template) {
-
- $template = str_replace('.', DS, $controller) . $depr . $action;
- } elseif (false === strpos($template, $depr)) {
- $template = str_replace('.', DS, $controller) . $depr . $template;
- }
- }
- } else {
- $template = str_replace(['/', ':'], $depr, substr($template, 1));
- }
- return $path . ltrim($template, '/') . '.' . ltrim($viewEngineConfig['view_suffix'], '.');
- }
-
- protected function display($content = '', $vars = [], $replace = [], $config = [])
- {
- return $this->view->display($content, $vars, $replace, $config);
- }
-
- protected function assign($name, $value = '')
- {
- $this->view->assign($name, $value);
- }
-
- protected function validateFailException($fail = true)
- {
- $this->failException = $fail;
- return $this;
- }
-
- protected function validate($data, $validate, $message = [], $batch = false, $callback = null)
- {
- if (is_array($validate)) {
- $v = Loader::validate();
- $v->rule($validate);
- } else {
- if (strpos($validate, '.')) {
-
- list($validate, $scene) = explode('.', $validate);
- }
- $v = Loader::validate('\\plugins\\' . cmf_parse_name($this->plugin->getName()) . '\\validate\\' . $validate . 'Validate');
- if (!empty($scene)) {
- $v->scene($scene);
- }
- }
-
- if ($batch || $this->batchValidate) {
- $v->batch(true);
- }
- if (is_array($message)) {
- $v->message($message);
- }
- if ($callback && is_callable($callback)) {
- call_user_func_array($callback, [$v, &$data]);
- }
- if (!$v->check($data)) {
- if ($this->failException) {
- throw new ValidateException($v->getError());
- } else {
- return $v->getError();
- }
- } else {
- return true;
- }
- }
- }
|