Example #1
0
 /**
  * ServiceApi初始化
  */
 public function init()
 {
     Dispatcher::getInstance()->returnResponse(true);
     Dispatcher::getInstance()->disableView();
     $this->_config = Registry::get('config');
     $this->_initRequestData();
 }
Example #2
0
 function _initRoute(Dispatcher $dispatcher)
 {
     $router = $dispatcher->getRouter();
     $api = new Rewrite('/:module/:version/:method/:responseFormat', array('controller' => 'Api', 'action' => 'index'));
     $apiDefault = new Rewrite('/:method/:responseFormat', array('controller' => 'Api', 'action' => 'index'));
     /**
      * 前后端完全分离,前端靠 /env 这个uri中的内容判断线上还是开发环境,前端自己的开发环境有/env这个静态文件(svn中是忽略状态)
      *
      * @var \Yaf\Route\Rewrite $env
      */
     $env = new Rewrite('/staticEnv', array('controller' => 'Index', 'action' => 'staticEnv'));
     $router->addRoute('api', $apiDefault);
     $router->addRoute('apiDefault', $api);
     $router->addRoute('staticEnv', $env);
     $dispatcher->disableView();
 }
Example #3
0
 /**
  * 路由结束之后触发       此时路由一定正确完成, 否则这个事件不会触发
  *
  * @param Request_Abstract $request
  * @param Response_Abstract $response
  * @return mixed|void
  */
 public function routerShutdown(Request_Abstract $request, Response_Abstract $response)
 {
     $config = Registry::get("config")->smarty->toArray();
     $config['template_dir'] = $config['template_dir'] . $request->module . '/';
     $smarty = new Smarty\Adapter(null, $config);
     Dispatcher::getInstance()->setView($smarty);
 }
Example #4
0
 public function errorAction($exception)
 {
     //        $this->getView()->setLayout(null);
     // fallback views path to global when error occured in modules.
     $config = Yaf\Application::app()->getConfig();
     $this->getView()->setScriptPath($config->application->directory . "/views");
     $this->getView()->e = $exception;
     $this->getView()->e_class = get_class($exception);
     $this->getView()->e_string_trace = $exception->getTraceAsString();
     $params = $this->getRequest()->getParams();
     unset($params['exception']);
     $this->getView()->params = array_merge(array(), $params, $this->getRequest()->getPost(), $this->getRequest()->getQuery());
     $logFileName = 'error_' . date('Ymd');
     eYaf\Logger::getLogger($logFileName)->logException($exception);
     switch ($exception->getCode()) {
         case \YAF\ERR\AUTOLOAD_FAILED:
         case \YAF\ERR\NOTFOUND\MODULE:
         case \YAF\ERR\NOTFOUND\CONTROLLER:
         case \YAF\ERR\NOTFOUND\ACTION:
             header('HTTP/1.1 404 Not Found');
             break;
         case 401:
             $this->forward('Index', 'application', 'accessDenied');
             header('HTTP/1.1 401 Unauthorized');
             \Yaf\Dispatcher::getInstance()->disableView();
             echo $this->render('accessdenied');
             break;
         default:
             header("HTTP/1.1 500 Internal Server Error");
             break;
     }
     eYaf\Logger::stopLogging($logFileName);
 }
Example #5
0
 /**
  *
  * @param \Yaf\Dispatcher $dispatcher
  */
 protected function _initLayout(\Yaf\Dispatcher $dispatcher)
 {
     /*
      * layout allows boilerplate HTML to live in
      * /layouts/scripts rather than every script
      */
     $layout = new LayoutPlugin($this->_config->layout->file, $this->_config->layout->dir);
     /* Store a reference in the registry so values can be set later.
      *
      * This is a hack to make up for the lack of a getPlugin
      * method in the dispatcher.
      */
     \Yaf\Registry::set('Layout', $layout);
     /*add the plugin to the dispatcher*/
     $dispatcher->registerPlugin($layout);
 }
Example #6
0
 public function _initRoute(\Yaf\Dispatcher $dispatcher)
 {
     $router = \Yaf\Dispatcher::getInstance()->getRouter();
     //创建一个路由协议实例
     $route = new \Yaf\Route\Rewrite('exp/:ident', array('controller' => 'index', 'action' => 'index'));
     //使用路由器装载路由协议
     $router->addRoute('exp', $route);
 }
Example #7
0
 /**
  *
  * @return string
  */
 function getClientIp()
 {
     $request = Dispatcher::getInstance()->getRequest();
     if ($request->isCli()) {
     } else {
         return $request->getServer('REMOTE_ADDR');
     }
 }
Example #8
0
 /**
  * setup yaf
  */
 private function __setUpYafApplication()
 {
     $this->__setUpPHPIniVariables();
     // Import application and bootstrap.
     \Yaf\Loader::import(dirname(__DIR__) . '/public/index.php');
     $this->__setUpApplicationInit();
     \Yaf\Dispatcher::getInstance()->registerPlugin(new \YafUnit\Plugin\View());
     \Yaf\Registry::set('ApplicationInit', true);
 }
Example #9
0
 /**
  * @param Simple $View
  * @return bool
  */
 public function setView(Simple $View)
 {
     if (APPLICATION_IS_CLI) {
         return false;
     }
     $this->_coreView = $View;
     Dispatcher::getInstance()->setView($View);
     return true;
 }
Example #10
0
 /**
  * 初始化ServiceJob
  */
 public function init()
 {
     Dispatcher::getInstance()->disableView();
     if (strtolower($this->getRequest()->getMethod()) !== 'cli') {
         $sender = new SenderHttp();
         $sender->setStatus(503);
         $sender->send();
     }
 }
Example #11
0
 /**
  * 路由结束之后触发       此时路由一定正确完成, 否则这个事件不会触发
  *
  * @param Request_Abstract $request
  * @param Response_Abstract $response
  * @return mixed|void
  */
 public function routerShutdown(Request_Abstract $request, Response_Abstract $response)
 {
     if ($this->_checkModuleTpl($request->module)) {
         //只有这些模板才注入
         $config = Registry::get("config")->smarty->toArray();
         $config['template_dir'] = $config['template_dir'] . $request->module . '/';
         $smarty = new TheFairLib\Smarty\Adapter(null, $config);
         Dispatcher::getInstance()->setView($smarty);
     }
 }
Example #12
0
 public function init()
 {
     //do not call render for ajax request
     if ($this->getRequest()->isXmlHttpRequest()) {
         \Yaf\Dispatcher::getInstance()->autoRender(FALSE);
     }
     //use layout
     if ($this->layout) {
         $this->setLayout($this->layout);
     }
 }
Example #13
0
 /**
  * Constructor.
  *
  * @param array $config Configuration key-value pairs.
  */
 public function __construct($config)
 {
     // set template path
     $this->witty = new witty();
     //初始化目录
     $client = client::getDevice();
     $model = $client == 'pc' ? '/pc/' : '/mobile/';
     $tpl_dir = $config['tpl_dir'] . $model;
     $parse_file = $config['compile_dir'] . $model . \Yaf\Dispatcher::getInstance()->getRequest()->getControllerName() . '/';
     $this->witty->setTplDir($tpl_dir);
     $this->witty->setLayoutDir($tpl_dir . '/layout/');
     $this->witty->setCompileDir($parse_file);
     $this->witty->setCacheDir($config['cache_dir']);
     //$this->_options = $options;
 }
Example #14
0
 /**
  * 响应头
  * @param
  *    $type
  */
 public static function setHeader($type = '')
 {
     if (\Yaf\Dispatcher::getInstance()->getRequest()->isCli()) {
         return false;
     }
     switch ($type) {
         case 'json':
             header('Content-Type:application/json;charset=utf-8');
             header('Pragma: no-cache');
             header('Cache-Control: no-cache, no-store, max-age=0');
             header('Expires: 1L');
             break;
         default:
             header('Content-type:text/html;charset=utf-8');
             break;
     }
 }
Example #15
0
 /**
  * @brief 调试输出变量
  *
  * @param ...$params
  *
  * @return void
  */
 public static function vars(...$params)
 {
     $isCli = \Yaf\Dispatcher::getInstance()->getRequest()->isCli();
     echo $isCli ? '' : '<br>';
     echo '*****************************DEBUG::VARS******************************';
     echo $isCli ? PHP_EOL : '<br>' . PHP_EOL;
     echo $isCli ? '' : '<pre>' . PHP_EOL;
     foreach ($params as $item) {
         var_dump($item);
     }
     $trace = debug_backtrace();
     echo $isCli ? '' : '</pre>' . PHP_EOL;
     echo "from: [file: {$trace[0]['file']} line: {$trace[0]['line']}]";
     echo $isCli ? PHP_EOL : PHP_EOL . '<br>';
     echo '**********************************************************************';
     echo $isCli ? PHP_EOL : '<br>';
 }
Example #16
0
 /**
  * 导出API的api方法
  *
  * @access public
  * @param string $module 应用模块名
  * @param string $controller 对应模块内的控制器
  * @param string $action 对应控制器中的动作名
  * @param mixed $parameters 请求传递的参数
  * @return string API调用的响应正文
  */
 public function api($module, $controller, $action, $parameters)
 {
     try {
         $app = new Application(INI_PATH, 'product');
         $request = new RequestSimple('API', $module, $controller, $action, $parameters);
         $response = $app->bootstrap()->getDispatcher()->dispatch($request);
         return $response->getBody();
     } catch (Exception $e) {
         if (Application::app()->getConfig()->application->queue->log->switch) {
             $error = new ErrorLog($e, Dispatcher::getInstance()->getRequest());
             $error->errorLog();
         }
         $error = explode(ES, $e->getMessage(), 2);
         if (isset($error[1])) {
             return $error[1];
         }
     }
 }
Example #17
0
 /**
  * 生成指定路由类型url
  * 如果需要根据路由动态生成url,请调用Yaf_route_interface::assemble方法(注意这个assemble方法在低版本下有问题,参考github issue)
  *
  * @param $m
  * @param $c
  * @param $a
  * @param array $params
  */
 public static function url($c, $a = 'index', $m = 'default', array $params = [], $routerType = 'default')
 {
     if ($m == 'default') {
         $resquest = \Yaf\Dispatcher::getInstance()->getRequest();
         $m = strtolower($resquest->getModuleName());
     }
     /**
      * 默认静态路由方式
      */
     if ($routerType == 'default') {
         $queryString = '';
         foreach ($params as $key => $item) {
             $queryString .= '/' . $key . '/' . $item;
         }
         return '/' . implode('/', [$m, $c, $a]) . $queryString;
     }
     //TODO: 按需求补全其它路由
     throw new \Yaf\Exception\RouterFailed('无效的路由类型' . $routerType . '无法生成URL');
 }
Example #18
0
 public function __construct($views = null, $extraParams = array())
 {
     $this->_smarty = new \Smarty();
     foreach ($extraParams as $key => $value) {
         $this->_smarty->{$key} = $value;
     }
     //根据不同的模块设置不同的模版路径
     $dispatcher = Dispatcher::getInstance();
     $arrRequest = $dispatcher->getRequest();
     if ($views == null) {
         $this->_views = "views";
     } else {
         $this->_views = $views;
     }
     if (empty($arrRequest->module) or 'index' == strtolower($arrRequest->module)) {
         $this->_script_path = APPLICATION_PATH . '/application/' . $this->_views . '/' . strtolower($arrRequest->controller);
         $this->setScriptPath(APPLICATION_PATH . '/application/' . $this->_views . '/' . strtolower($arrRequest->controller));
     } else {
         $this->_script_path = APPLICATION_PATH . '/application/modules/' . $arrRequest->module . '/' . $this->_views;
         $this->setScriptPath(APPLICATION_PATH . '/application/modules/' . $arrRequest->module . '/' . $this->_views);
     }
 }
Example #19
0
 function dispatchLoopStartup(Request_Abstract $request, Response_Abstract $response)
 {
     $dispatcher = Dispatcher::getInstance();
     $controllerName = strtolower($request->getControllerName());
     $isApi = 0 === strpos($controllerName, 'api');
     $isCli = 0 === strpos($controllerName, 'cli_');
     // db
     \Zend_Db_Table::setDefaultAdapter(AbstractModel::getInstance()->getMultiDb()->getDefaultDb());
     // view
     if ($isApi) {
         $dispatcher->disableView();
     } else {
         if ($response instanceof Http) {
             $response->setHeader('Content-Type', 'text/html; charset=UTF-8');
         }
         $dispatcher->setView(SmartyView::getInstance());
     }
     // cli
     if ($isCli && !$request->isCli()) {
         $request->setControllerName('Error')->setActionName('error403');
     }
 }
Example #20
0
 protected function checkUserInput($param, $method)
 {
     /* @var \Yaf\Request\Http $request */
     $request = Dispatcher::getInstance()->getRequest();
     if ($method != null && $method != $request->getMethod()) {
         return new ApiResponse(Code::FAIL_REQUEST_METHOD, $method . ' support only');
     }
     if ($param) {
         foreach ($param as $k => $v) {
             if (is_int($k) && $k <= self::TYPE_MAX) {
                 switch ($k) {
                     case self::LONG:
                         if (!is_long($v)) {
                             return new ApiResponse(Code::FAIL_PARAMETER_TYPE, $v . ' should be long');
                         }
                         break;
                     case self::INT:
                         if (!is_int($v)) {
                             return new ApiResponse(Code::FAIL_PARAMETER_TYPE, $v . ' should be integer');
                         }
                         break;
                     case self::UNSIGNED_INT:
                         if (!is_int($v) || $v < 0) {
                             return new ApiResponse(Code::FAIL_PARAMETER_TYPE, $v . ' should be unsigned integer');
                         }
                         break;
                     case self::STRING:
                         if (!is_string($v)) {
                             return new ApiResponse(Code::FAIL_PARAMETER_TYPE, $v . ' should be string');
                         }
                         break;
                     case self::FLOAT:
                         if (!is_float($v)) {
                             return new ApiResponse(Code::FAIL_PARAMETER_TYPE, $v . ' should be float');
                         }
                         break;
                     case self::DOUBLE:
                         if (!is_double($v)) {
                             return new ApiResponse(Code::FAIL_PARAMETER_TYPE, $v . ' should be float');
                         }
                         break;
                     case self::FILE:
                         if ($request->getFiles($v) === null) {
                             return new ApiResponse(Code::FAIL_PARAMETER_TYPE, $v . ' should be file');
                         }
                         break;
                     default:
                         return new ApiResponse(Code::FAIL_PARAMETER_TYPE, 'type of ' . $v . ' not support');
                 }
             } else {
                 if (ApiRequest::getParam($v) === null) {
                     return new ApiResponse(Code::FAIL_PARAMETER_MISSING, 'parameter ' . $v . ' missing');
                 }
             }
         }
     }
     return null;
 }
Example #21
0
 public function init()
 {
     Dispatcher::getInstance()->returnResponse(true);
     Dispatcher::getInstance()->disableView();
 }
Example #22
0
 /**
  * ServiceApi初始化
  */
 public function init()
 {
     Dispatcher::getInstance()->returnResponse(true);
     Dispatcher::getInstance()->disableView();
     $this->_output_format = $this->getRequest()->getParam('format');
 }
Example #23
0
 /**
  * Create a HTML page link.
  *
  * @param  int     $page
  * @param  string  $text
  * @param  string  $class
  * @return string
  */
 protected function link($page, $text, $class)
 {
     $query = '?page=' . $page . $this->appendage($this->appends);
     $uri = \Yaf\Dispatcher::getInstance()->getRequest()->getRequestUri();
     return Html::linkTo($text, $uri . $query, compact('class'));
 }
Example #24
0
 public function _initLayout(Dispatcher $dispatcher)
 {
     /*layout allows boilerplate HTML to live in /views/layout rather than every script*/
     $layout = new LayoutPlugin('layout/layout.html');
     /* Store a reference in the registry so values can be set later.
      * This is a hack to make up for the lack of a getPlugin
      * method in the dispatcher.
      */
     Registry::set('layout', $layout);
     /*add the plugin to the dispatcher*/
     $dispatcher->registerPlugin($layout);
 }
Example #25
0
 /**
  * 测试redis pub/sub
  */
 public function subAction()
 {
     Dispatcher::getInstance()->disableView();
     $redis = Registry::get('mount')->get('redis')->getResource();
     $redis->subscribe(array('log'), array($this, 'echoData'));
 }
 /**
  * 通过调度器initView方法可以返回\Core\View核心视图对象
  * @return \Yaf\View_Interface  $view   视图对象
  */
 public function getView()
 {
     return \Yaf\Dispatcher::getInstance()->initView(null);
 }
Example #27
0
 public function init()
 {
     \Yaf\Dispatcher::getInstance()->disableView();
 }
Example #28
0
 public function _initPlugin(Dispatcher $dispatcher)
 {
     $dispatcher->registerPlugin(new ApiPluginBase());
 }
Example #29
0
 /**
  * 初始化控制器
  */
 public function init()
 {
     //禁止自动加载模板,需要手工指定模板路径
     Dispatcher::getInstance()->autoRender(false);
 }
 /**
  * 关闭模板的默认渲染设置
  *
  * @return bool
  */
 public function disableView()
 {
     return \Yaf\Dispatcher::getInstance()->disableView();
 }