/**
  * Initialize environment setting
  */
 public function initEnv()
 {
     if ($this->_appEnv != null) {
         // Set default error template.
         ViewAbstract::setDefaultErrorTemplate($this->_appEnv);
         // call initialize environment method.
         $envInitMethod = NameManager::toMethod('init_' . $this->_appEnv);
         if (method_exists($this, $envInitMethod)) {
             $this->{$envInitMethod}();
         }
     }
 }
Example #2
0
 /**
  * Create and add elements to form from array
  * 
  * @param array $elementSettings
  */
 public function buildFromArray($elementSettings)
 {
     $this->_checkArgumentIsArray(__METHOD__, 1, $elementSettings);
     // search layout element from child nodes
     $searchedLayout = null;
     foreach ($this->getNodes() as $elem) {
         if ($elem instanceof FormLayoutAbstract) {
             $searchedLayout = $elem;
             break;
         }
     }
     // create form elements
     foreach ($elementSettings as $setting) {
         $type = strtolower($setting['type']);
         unset($setting['type']);
         $method = 'create' . ucfirst($type);
         $formElem = null;
         if ($type == 'select' || $type == 'radio' || $type == 'checkboxes') {
             $formElem = $this->{$method}(null, $setting['list']);
             unset($setting['list']);
         } else {
             $formElem = $this->{$method}();
         }
         foreach ($setting as $key => $val) {
             if ($key == 'layout' || $key == 'container') {
                 continue;
             }
             if ($key == 'caption') {
                 // set caption
                 $formElem->setCaption($val);
             } else {
                 // call setter method
                 $method = NameManager::toMethod('set_' . strtolower($key));
                 $formElem->{$method}($val);
             }
         }
         // create layout element and add form element to there
         $layout = null;
         if (isset($setting['layout'])) {
             if ($setting['layout'] instanceof FormLayoutAbstract) {
                 $layout = $setting['layout'];
             } else {
                 if ($setting['layout'] === true && $searchedLayout instanceof FormLayoutAbstract) {
                     $layout = $searchedLayout;
                 }
             }
         }
         // add child node of form
         if ($layout instanceof FormLayoutAbstract) {
             $layout->addFormElement($formElem);
         } else {
             if (isset($setting['container']) && $setting['container'] instanceof HtmlElement) {
                 $container = $setting['container'];
                 $container->addElement($formElem);
                 $this->addElement($container);
             } else {
                 $this->addFormElement($formElem);
             }
         }
     }
 }
Example #3
0
 /**
  * Routing the controller actions based on url, to run a class method in question.
  * 
  * @return void
  * @throws Exception
  * @throws NotFoundException
  * @throws MemberInaccessibleException
  */
 public function dispatch()
 {
     Loader::load('Request', 'core');
     Loader::load('ViewAbstract', 'core');
     $request = new Request();
     $response = new Response();
     $view = null;
     $viewInitialized = false;
     $subdir = '';
     try {
         // check system root setting.
         $sysRoot = PathManager::getSystemRoot();
         if ($sysRoot == null) {
             throw new Exception('Path of the system root has not been specified.');
         } else {
             if (!file_exists($sysRoot)) {
                 throw new Exception('Path of the system root does not exist.');
             }
         }
         $router = new Router();
         // initialize by Initializer.
         $initializer = $this->_searchIntializer();
         if ($initializer instanceof InitializerStandard) {
             $initializer->setDispatcher($this);
             $initializer->setRouter($router);
             $initializer->applyConfig();
             $initializer->initEnv();
             $initializer->initialize();
         }
         $params = $router->route($request->getBasePath());
         // create view class instance.
         Loader::load($this->_viewClassName, null, true, false);
         $viewClass = $this->_viewClassName;
         $view = new $viewClass();
         // set request instance to controller.
         $subdir = $params['subdir'];
         $request->setController($params['controller'], $subdir);
         $request->setAction($params['action']);
         $request->setParams($params['params']);
         $view->setRequest($request);
         $view->initialize();
         $viewInitialized = true;
         // create controller class instance.
         $controller = $this->_getControllerInstance($params['controller'], $params['subdir']);
         if ($controller == false) {
             throw new NotFoundException($params['controller'], '');
         }
         $controller->setAppEnv($this->_appEnv);
         $controller->setRequest($request);
         // set response instance to controller.
         $controller->setResponse($response);
         // set view to controller.
         $controller->setView($view);
         // get action method name.
         $reqestMethod = null;
         $actionMethod = '';
         if ($controller instanceof RestController) {
             $request->isRestAccess(true);
             $reqestMethod = $request->getMethod();
             if ($params['action'] == 'index' && $reqestMethod != 'GET') {
                 $restDefaultMethod = NameManager::convertActionToMethod($reqestMethod);
                 if (method_exists($controller, $restDefaultMethod)) {
                     $actionMethod = $restDefaultMethod;
                 }
             }
         }
         if ($actionMethod == '') {
             $actionMethod = NameManager::convertActionToMethod($params['action'], $reqestMethod);
         }
         // initialize validator
         $validatorClass = $this->_validatorClassName;
         $validator = new $validatorClass();
         $controller->setValidator($validator);
         $controller->initValidator();
         $controller->initialize();
         // Create plugin instance.
         $plugin = null;
         if ($this->_pluginEnabled) {
             $plugin = $this->_searchPlugin($params['subdir']);
             if ($plugin instanceof PluginAbstract) {
                 $plugin->setControllerInstance($controller);
                 $plugin->preProcess();
             }
         }
         $controller->preProcess();
         if ($controller->isDispatched() == false) {
             // Check action method exists.
             if (!method_exists($controller, $actionMethod)) {
                 $controllerParam = $params['controller'];
                 if ($params['subdir'] != null) {
                     $controllerParam = $params['subdir'] . '/' . $controllerParam;
                 }
                 throw new NotFoundException($controllerParam, $params['action']);
             } else {
                 if (!is_callable(array($controller, $actionMethod))) {
                     throw new MemberInaccessibleException($params['controller'], $params['action']);
                 }
             }
             $controller->{$actionMethod}();
         }
         $controller->postProcess();
         if ($this->_pluginEnabled) {
             if ($plugin instanceof PluginAbstract) {
                 $plugin->postProcess();
             }
         }
         if ($controller instanceof RestController) {
             if ($controller->isAutoRedirect() == true && $request->getMethod() != 'GET') {
                 $url = '';
                 if ($params['action'] != 'index') {
                     $url = $params['controller'] . '/' . $params['action'];
                 } else {
                     if ($params['controller'] != 'index') {
                         $url = $params['controller'];
                     }
                 }
                 $url = $request->getBaseUrl() . '/' . $url;
                 $response->redirect($url);
             }
         }
         $response->send();
         if ($response->isOutput() == false && $view->getRenderingEnabled() == true) {
             $viewScript = $this->_getViewScriptInstance($params['controller'], $params['subdir']);
             if ($viewScript instanceof ViewScript) {
                 if (method_exists($viewScript, $actionMethod)) {
                     $viewScript->setView($view);
                     $viewScript->setRequest($request);
                     $viewScript->{$actionMethod}();
                 }
             }
             $view->render();
         }
     } catch (Exception $e) {
         $handler = $this->_searchErrorHandler($subdir);
         if ($handler instanceof ErrorHandlerStandard) {
             $action = 'error';
             $actionMethod = '';
             $className = get_class($e);
             if ($className != 'Exception') {
                 $actionMethod = str_replace('Exception', '', $className);
                 $actionMethod = NameManager::toMethod($actionMethod);
             }
             if (method_exists($handler, $actionMethod)) {
                 $action = $actionMethod;
             }
             $handler->setException($e);
             $handler->setRequest($request);
             $handler->setResponse($response);
             if ($viewInitialized == true) {
                 $handler->setView($view);
             }
             if ($action == 'notFound') {
                 if ($this->_isSend404 == true) {
                     $response->setHttpStatus('404');
                     $response->send();
                 }
             }
             $handler->{$action}();
         } else {
             throw $e;
         }
     }
 }