示例#1
0
文件: Application.php 项目: fem/spof
 /**
  * Factory to get a view by module name. If the desired view is not available then an alternative view might get
  * returned.
  *
  * @internal
  *
  * @throws \Exception
  *
  * @param string $module module name
  * @param string $action
  *
  * @return view\AbstractView
  */
 private function handleRequest($module, $action)
 {
     // there will always be something to view (otherwise we should get a nice error message)
     $viewName = $this->getClassByModule($module);
     /** @var \FeM\sPof\view\AbstractView $view */
     $view = null;
     try {
         // for an action, there is a controller to handle it
         if ($action) {
             AbstractController::createAndRunAction($module, $action);
         }
         if (!class_exists($viewName)) {
             // if file exists -> the class name does not match the filename
             if (file_exists('view/' . $module . 'View.php')) {
                 throw new \FeM\sPof\exception\ClassNotFoundException(__('"%sView" requested, but missing definition in file "view/%sView.php", maybe a typo in the ' . 'class name?', $module, $module));
             }
             // file doesn't exist -> file probably need to get renamed
             if (!file_exists('view/' . $module . 'View.php')) {
                 throw new \FeM\sPof\exception\ClassNotFoundException(__('"%sView" requested, but could not find the associated file "view/%sView.php"', $module, $module));
             }
         }
         // class does exist, so get us a instance of it
         $view = new $viewName();
         $view->executeShow();
     } catch (\Exception $e) {
         Logger::getInstance()->warning(__("Could not instanciate class of type '%s'. Trying to find alternatives", $viewName));
         // try to call the static implementation, we need this workaround, because this method is called staticly,
         // directly on this class, so no late state binding, try to workaround by directly calling it and otherwise
         // use local fallback
         if (method_exists($viewName, 'handleException')) {
             $viewName::handleException($e);
         } else {
             $viewName = $this->getClassByModule($this->defaultModule);
             if (method_exists($viewName, 'handleException')) {
                 $viewName::handleException($e);
             }
         }
     }
     // we got a view we can proceed
     if ($view instanceof view\AbstractView) {
         return $view;
     }
     // try to call the static implementation, we need this workaround, because this method is called staticly,
     // directly on this class, so no late state binding, try to workaround by directly calling it and otherwise
     // use local fallback
     if (method_exists($viewName, 'handleNoViewFound')) {
         return $viewName::handleNoViewFound();
     } else {
         $viewName = $this->getClassByModule($this->defaultModule);
         if (method_exists($viewName, 'handleNoViewFound')) {
             return $viewName::handleNoViewFound();
         }
     }
     if (empty($module)) {
         self::death(_("\nNo module given, abort."));
     } else {
         self::death(__("\nCould not find a suitable view component for module '%s'.", $module));
     }
 }