コード例 #1
0
ファイル: mcontext.php プロジェクト: joshuacoddingyou/php
 public function __construct($request)
 {
     $this->isCore = false;
     if (is_string($request)) {
         $path = $request;
         $this->url = $path;
     } else {
         $this->request = $request;
         if ($this->request->querystring != '') {
             parse_str($this->request->querystring, $this->vars);
         }
         $path = $this->request->getPathInfo();
         $this->url = $this->request->path;
     }
     mtrace('Context path: ' . $path);
     $pathParts = explode('/', $path);
     $app = array_shift($pathParts);
     if ($app != '') {
         if ($app == 'core') {
             $this->isCore = true;
             $app = array_shift($pathParts);
         }
         $this->app = $app;
         $part = array_shift($pathParts);
         // check for module
         $namespace = $this->getNamespace($this->app, $part);
         if ($part && Manager::existsNS($namespace)) {
             $this->module = $part;
             $part = array_shift($pathParts);
         } else {
             $this->module = '';
         }
         // check for controller/component/service
         $ctlr = $part;
         $controller = $component = $service = '';
         while ($part && ($controller == '' && $component == '' && $service == '')) {
             $namespace = $this->getNamespace($this->app, $this->module, '', 'controllers');
             $ns = $namespace . $part . 'Controller.php';
             if (Manager::existsNS($ns)) {
                 $controller = $part;
                 $part = array_shift($pathParts);
             } else {
                 $namespace = $this->getNamespace($this->app, $this->module, '', 'services');
                 $ns = $namespace . $part . 'Service.php';
                 if (Manager::existsNS($ns)) {
                     $service = $part;
                     $part = array_shift($pathParts);
                 } else {
                     $namespace = $this->getNamespace($this->app, $this->module, '', 'components');
                     $ns = $namespace . $part . '.php';
                     if (Manager::existsNS($ns)) {
                         $component = $part;
                         $part = array_shift($pathParts);
                     } else {
                         $part = array_shift($pathParts);
                     }
                 }
             }
         }
     } else {
         $this->app = Manager::getOptions('startup');
         $controller = 'main';
     }
     if ($controller) {
         $this->controller = $controller;
     } elseif ($service) {
         $this->service = $service;
     } elseif ($component) {
         $this->component = $component;
     } else {
         throw new ENotFoundException(_M("App: [%s], Module: [%s], Controller: [%s] : Not found!", array($this->app, $this->module, $ctlr)));
     }
     $this->action = $part ?: ($component == '' ? 'main' : '');
     $this->actionTokens[0] = $this->controller;
     $this->actionTokens[1] = $this->action;
     $this->currentToken = 1 + ($this->module ? 1 : 0);
     if ($n = count($pathParts)) {
         for ($i = 0; $i < $n; $i++) {
             $this->actionTokens[$i + 2] = $this->vars[$pathParts[$i]] = $pathParts[$i];
         }
     }
     $this->id = $this->vars['item'] ?: $this->actionTokens[2];
     if ($this->id !== '') {
         $_REQUEST['id'] = $this->id;
     }
     Manager::getInstance()->application = $this->app;
     mtrace('Context app: ' . $this->app);
     mtrace('Context module: ' . $this->module);
     mtrace('Context controller: ' . $this->controller);
     mtrace('Context service: ' . $this->service);
     mtrace('Context component: ' . $this->component);
     mtrace('Context action: ' . $this->action);
     mtrace('Context id: ' . $this->id);
 }