Exemplo n.º 1
0
 /**
  * 加载路由设置
  * @param \Yaf\Dispatcher $dispatcher
  */
 public function _initRoute(Yaf\Dispatcher $dispatcher)
 {
     if (defined('CONFIG_PATH') && is_file(CONFIG_PATH . 'routing.ini')) {
         $config = new Yaf\Config\Ini(CONFIG_PATH . 'routing.ini');
         $dispatcher->getRouter()->addConfig($config);
     }
 }
Exemplo n.º 2
0
 public function _initLayout(Yaf\Dispatcher $dispatcher)
 {
     $event = $this->profiler->startEvent('Init layout');
     if (!$dispatcher->getRequest()->isXmlHttpRequest()) {
         $layout = new Layout($this->config->layout->dir);
         $dispatcher->setView($layout);
     } else {
         //
     }
     $this->profiler->endEvent($event);
 }
Exemplo n.º 3
0
 public function _initPlugin(Yaf\Dispatcher $dispatcher)
 {
     //开启debug控制台输出
     if (APP_DEBUG) {
         $dispatcher->registerPlugin(new DebugPlugin());
     }
     //开启布局支持
     $dispatcher->registerPlugin(new LayoutPlugin());
     //开启性能分析
     if (APP_ANALYZE && function_exists('xhprof_enable')) {
         $dispatcher->registerPlugin(new XhprofPlugin());
     }
 }
Exemplo n.º 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());
     eYaf\Logger::getLogger()->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();
 }
Exemplo n.º 5
0
 public function errorAction(E $exception)
 {
     if (Registry::get('config')->environment != 'pro') {
         echo '<pre>';
         print_r($exception);
     }
     Yaf\Dispatcher::getInstance()->autoRender(false);
     switch ($exception->getCode()) {
         case \YAF\ERR\NOTFOUND\MODULE:
         case \YAF\ERR\NOTFOUND\CONTROLLER:
         case \YAF\ERR\NOTFOUND\ACTION:
         case \YAF\ERR\NOTFOUND\VIEW:
         case 404:
             header("Content-type: text/html; charset=utf-8");
             header("status: 404 Not Found");
             $this->display("404");
             break;
         default:
             header("Content-type: text/html; charset=utf-8");
             header("status: 500 Internal Server Error");
             if (Registry::get('config')->environment == 'pro') {
                 $this->display("500");
             } else {
                 echo $exception->getMessage();
             }
             break;
     }
 }
Exemplo n.º 6
0
 public function _initRouter($dispatcher)
 {
     $router = Yaf\Dispatcher::getInstance()->getRouter();
     #$route = new Yaf\Route\Simple("m", "c", "a");
     #$router->addRoute("name", $route);
     $route = new Yaf\Route\Regex('/\\/product\\/([a-zA-Z]+)/', ['controller' => 'index', 'action' => 'test'], [1 => 'ident']);
     $router->addRoute('product', $route);
 }
Exemplo n.º 7
0
 public function setUp()
 {
     $this->application = Yaf\Registry::get('Application');
     if ($this->application) {
         return;
     }
     $this->application = new Yaf\Application(APPLICATION_PATH . "/config/application.ini", APPLICATION_ENVIRONMENT);
     $this->application->bootstrap();
     Yaf\Registry::set('Application', $this->application);
     Yaf\Dispatcher::getInstance()->setView(PHPUnit_MockYafView::getInstance());
 }
Exemplo n.º 8
0
 public function _initHooks(Yaf\Dispatcher $dispatcher)
 {
     //注册Hooks
     $dispatcher->registerPlugin(new \Hook\RequestPlugin());
     $dispatcher->registerPlugin(new \Hook\AuthenticatePlugin());
     $dispatcher->registerPlugin(new \Hook\AuthorizePlugin());
     $dispatcher->registerPlugin(new \Hook\ApiRoutePlugin());
     $dispatcher->registerPlugin(new \Hook\ResponsePlugin());
     $dispatcher->registerPlugin(new \Hook\PostEventPlugin());
 }
Exemplo n.º 9
0
 public function _initView(Yaf\Dispatcher $dispatcher)
 {
     $view = new \SlatePF\Extras\ExtrasView();
     $path = $view->getScriptPath();
     // plain text TEST
     $view->on('txt', function ($file, $data) use($path) {
         return file_get_contents($path . $file) . ' THIS IS JUST A TEST';
     });
     // twig
     $view->on('twig', function ($file, $data) use($path) {
         $loader = new Twig_Loader_Filesystem($path);
         $twig = new Twig_Environment($loader);
         return $twig->loadTemplate($file)->render($data);
     });
     //protobuf
     $view->on('pb', function ($file, $data) use($path) {
         include $path . $file;
         exit;
     });
     $dispatcher->disableView();
     // disable auto-render
     $dispatcher->setView($view);
 }
Exemplo n.º 10
0
 public function _initRest(Yaf\Dispatcher $dispatcher)
 {
     $dispatcher->disableView();
     //如果不是cli就调用rest路由
     if (!$dispatcher->getRequest()->isCli()) {
         $router = $dispatcher->getRouter();
         $route = new RestRoute();
         $router->addRoute("rest", $route);
         //设置模板目录
         $view_engine = new Yaf\View\Simple(APPLICATION_PATH . '/application/views');
         $dispatcher->setView($view_engine);
         //启用权限控制插件
         $auth = new AuthPlugin();
         $dispatcher->registerPlugin($auth);
     } else {
         $command = new CommandLinePlugin();
         $dispatcher->registerPlugin($command);
     }
 }
Exemplo n.º 11
0
 public function _initView(Yaf\Dispatcher $dispatcher)
 {
     $view = new \views\wittyAdapter(\Yaf\Registry::get("config")->witty);
     $dispatcher->setView($view);
 }
Exemplo n.º 12
0
 public function _initPluginUser(Yaf\Dispatcher $dispatcher)
 {
     $system = new SystemPlugin();
     $dispatcher->registerPlugin($system);
 }
Exemplo n.º 13
0
 public function _initPlugin(Yaf\Dispatcher $dispatcher)
 {
     //注册一个插件
     $objSamplePlugin = new SamplePlugin();
     $dispatcher->registerPlugin($objSamplePlugin);
 }
Exemplo n.º 14
0
 public function updateAction($id)
 {
     $resource = $this->getResource($id);
     if (null === $resource) {
         return $this->forwardTo404();
     }
     $model = $this->get_model_name();
     if ($resource->updateAttributes($this->get_resource_params($model))) {
         $this->redirect($this->get_index_url());
         return false;
     } else {
         Yaf\Dispatcher::getInstance()->disableView();
         echo $this->render('edit', array('resource' => $resource, 'index_url' => $this->get_index_url()));
         return false;
     }
 }
Exemplo n.º 15
0
 public function _initLayout(Yaf\Dispatcher $dispatcher)
 {
     $layout = new Layout($this->config->application->layout->directory);
     $dispatcher->setView($layout);
 }
Exemplo n.º 16
0
 public function getView()
 {
     return Yaf\Dispatcher::getInstance()->initView(APPLICATION_VIEWS);
 }
Exemplo n.º 17
0
<?php

/**
 * cli方式执行请求
 * eg:
 * php cli.php "request_uri=/index/index/testcli/p1/helloworld/p2/god" 
 */
define('APPLICATION_PATH', dirname(dirname(__FILE__)));
require APPLICATION_PATH . '/vendor/autoload.php';
$application = new Yaf\Application(APPLICATION_PATH . "/conf/application.ini");
$request = new Yaf\Request\Simple();
Yaf\Dispatcher::getInstance()->dispatch($request);
Exemplo n.º 18
0
<?php

$dis = Yaf\Dispatcher::getInstance();
//Initialize Routes for Admin module
$routes = new Yaf\Config\Ini(__DIR__ . "/config" . "/routes.ini");
$dis->getRouter()->addConfig($routes->admin);
Exemplo n.º 19
0
 /**
  * Using twig as templating engine
  *
  * @param Yaf_Dispatcher $dispatcher
  */
 protected function _initView(Yaf\Dispatcher $dispatcher)
 {
     $view = new Cooltime\View\Twig(APPLICATION_PATH . "/application/views", Yaf\Application::app()->getConfig()->twig->toArray());
     $dispatcher->setView($view);
 }
Exemplo n.º 20
0
 /**
  * 初始化插件
  *
  * @param Yaf_Dispatcher $dispatcher
  */
 public function _initPlugin(Yaf\Dispatcher $dispatcher)
 {
     $site = new SitePlugin();
     $dispatcher->registerPlugin($site);
 }
Exemplo n.º 21
0
 /**
  * Get the current page from the request query string.
  *
  * @param  int  $total
  * @param  int  $per_page
  * @return int
  */
 public static function page($total, $per_page)
 {
     $query = Yaf\Dispatcher::getInstance()->getRequest()->getQuery();
     $page = isset($query['page']) ? (int) $query['page'] : 1;
     // The page will be validated and adjusted if it is less than one or greater
     // than the last page. For example, if the current page is not an integer or
     // less than one, one will be returned. If the current page is greater than
     // the last page, the last page will be returned.
     if (is_numeric($page) and $page > ($last = ceil($total / $per_page))) {
         return $last > 0 ? $last : 1;
     }
     return static::valid($page) ? $page : 1;
 }
Exemplo n.º 22
0
 public function _initConfig()
 {
     Yaf\Dispatcher::getInstance()->autoRender(FALSE);
     // 关闭自动加载模板
 }
Exemplo n.º 23
0
 /**
  * 加载插件
  * @param \Yaf\Dispatcher $dispatcher
  */
 public function _initPlugin(Yaf\Dispatcher $dispatcher)
 {
     $dispatcher->registerPlugin(new TplPlugin());
     $dispatcher->registerPlugin(new SystemPlugin());
 }
Exemplo n.º 24
0
 /**
  * 初始化模板引擎
  */
 public function _initView(Yaf\Dispatcher $dispatcher)
 {
     $dispatcher->disableView();
 }