Example #1
0
 /**
  * 激活插件
  *
  * @access public
  * @return void
  */
 public function activate($pluginName)
 {
     /** 获取插件入口 */
     list($pluginFileName, $className) = Typecho_Plugin::portal($pluginName, __TYPECHO_ROOT_DIR__ . '/' . __TYPECHO_PLUGIN_DIR__);
     $info = Typecho_Plugin::parseInfo($pluginFileName);
     /** 检测依赖信息 */
     list($version, $build) = explode('/', Typecho_Common::VERSION);
     if (Typecho_Plugin::checkDependence($build, $info['dependence'])) {
         /** 获取已激活插件 */
         $plugins = Typecho_Plugin::export();
         $activatedPlugins = $plugins['activated'];
         /** 载入插件 */
         require_once $pluginFileName;
         /** 判断实例化是否成功 */
         if (isset($activatedPlugins[$pluginName]) || !class_exists($className) || !method_exists($className, 'activate')) {
             throw new Typecho_Widget_Exception(_t('无法激活插件'), 500);
         }
         try {
             $result = call_user_func(array($className, 'activate'));
             Typecho_Plugin::activate($pluginName);
             $this->update(array('value' => serialize(Typecho_Plugin::export())), $this->db->sql()->where('name = ?', 'plugins'));
         } catch (Typecho_Plugin_Exception $e) {
             /** 截获异常 */
             $this->widget('Widget_Notice')->set($e->getMessage(), NULL, 'error');
             $this->response->goBack();
         }
         $form = new Typecho_Widget_Helper_Form();
         call_user_func(array($className, 'config'), $form);
         $personalForm = new Typecho_Widget_Helper_Form();
         call_user_func(array($className, 'personalConfig'), $personalForm);
         $options = $form->getValues();
         $personalOptions = $personalForm->getValues();
         if ($options && !$this->configHandle($pluginName, $options, true)) {
             self::configPlugin($pluginName, $options);
         }
         if ($personalOptions && !$this->personalConfigHandle($className, $personalOptions)) {
             self::configPlugin($pluginName, $personalOptions, true);
         }
     } else {
         $result = _t('<a href="%s">%s</a> 无法在此版本的typecho下正常工作', $info['link'], $info['title']);
     }
     /** 设置高亮 */
     $this->widget('Widget_Notice')->highlight('plugin-' . $pluginName);
     if (isset($result) && is_string($result)) {
         $this->widget('Widget_Notice')->set($result, NULL, 'notice');
     } else {
         $this->widget('Widget_Notice')->set(_t('插件已经被激活'), NULL, 'success');
     }
     $this->response->goBack();
 }