public function __construct() { $defaultRoute = G::get('default_route'); if ($defaultRoute != null) { $this->addRoute('_default', $this->_getRouteFromArray($defaultRoute)); } else { $this->addRoute('_default', new Route_Static()); } }
/** * Set the throwExceptions flag and retrieve current status * * Set whether exceptions encounted in the dispatch loop should be thrown * or caught and trapped in the response object. * * Default behaviour is to trap them in the response object; call this * method to have them thrown. * * Passing no value will return the current value of the flag; passing a * boolean true or false value will set the flag and return the current * object instance. * * @param boolean $flag Defaults to null (return flag state) * @return boolean|Yaf_Dispatcher Used as a setter, * returns object; as a getter, returns boolean */ public function throwException($flag = null) { if ($flag !== null) { G::set('throwException', (bool) $flag); return $this; } return G::get('throwException'); }
/** * Construct view script path * * Used by render() and display to determine the path to the view script. * * @param string $action Defaults to action registered in request object * @return string * @throws InvalidArgumentException with bad $action */ protected function getViewScript($action = null) { $request = $this->getRequest(); if (null === $action) { $action = $request->getActionName(); } elseif (!is_string($action)) { throw new \InvalidArgumentException('Invalid action for view rendering'); } $action = str_replace('_', DIRECTORY_SEPARATOR, strtolower($action)); $script = $action . '.' . G::get('view_ext'); $controller = $request->getControllerName(); if ($controller != null) { $controller = str_replace('_', DIRECTORY_SEPARATOR, strtolower($controller)); } $script = $controller . DIRECTORY_SEPARATOR . $script; return $script; }
/** * Parse application options * * @param array $options * @throws Yaf_Exception When no bootstrap path is provided * @throws Yaf_Exception When invalid bootstrap information are provided * @return Yaf_Application */ protected function parseOptions(array $options) { if (!is_array($options)) { throw new Exception\TypeError('Expected an array of application configure'); } $options = array_change_key_case($options, CASE_LOWER); if (!isset($options['application'])) { throw new Exception\TypeError('Expected an array of application configure'); } $options = $options['application']; if (!empty($options['directory'])) { G::set('directory', preg_replace("/" . preg_quote(DIRECTORY_SEPARATOR, "/") . "\$/", "", $options['directory'])); } else { throw new Exception\StartupError('Expected a directory entry in application configures'); } if (!empty($options['ext'])) { G::set('ext', $options['ext']); } if (!empty($options['bootstrap']) && is_string($options['bootstrap'])) { $this->_options['bootstrap'] = $options['bootstrap']; } if (!empty($options['library'])) { if (is_string($options['library'])) { $this->_options['local_library'] = $options['library']; } elseif (is_array($options['library'])) { if (!empty($options['library']['directory']) && is_string($options['library']['directory'])) { $this->_options['local_library'] = $options['library']['directory']; } if (!empty($options['library']['namespace']) && is_string($options['library']['namespace'])) { $this->_options['local_namespace'] = $options['library']['namespace']; } } } else { $this->_options['local_library'] = G::get('directory') . DIRECTORY_SEPARATOR . Loader::YAF_LIBRARY_DIRECTORY_NAME; } if (!empty($options['view']) && is_array($options['view']) && !empty($options['view']['ext']) && is_string($options['view']['ext'])) { G::set('view_ext', $options['view']['ext']); } if (!empty($options['baseUri']) && is_string($options['baseUri'])) { $this->_options['baseUri'] = $options['baseUri']; } else { $this->_options['baseUri'] = $_SERVER['PHP_SELF']; } if (!empty($options['dispatcher']) && is_array($options['dispatcher'])) { if (!empty($options['dispatcher']['defaultModule']) && is_string($options['dispatcher']['defaultModule'])) { G::set('default_module', $options['dispatcher']['defaultModule']); } else { G::set('default_module', Router::YAF_ROUTER_DEFAULT_MODULE); } if (!empty($options['dispatcher']['defaultController']) && is_string($options['dispatcher']['defaultController'])) { G::set('default_controller', $options['dispatcher']['defaultController']); } else { G::set('default_controller', Router::YAF_ROUTER_DEFAULT_CONTROLLER); } if (!empty($options['dispatcher']['defaultAction']) && is_string($options['dispatcher']['defaultAction'])) { G::set('default_action', $options['dispatcher']['defaultAction']); } else { G::set('default_action', Router::YAF_ROUTER_DEFAULT_ACTION); } if (isset($options['dispatcher']['throwException'])) { G::set('throwException', (bool) $options['dispatcher']['throwException']); } if (isset($options['dispatcher']['catchException'])) { G::set('catchException', (bool) $options['dispatcher']['catchException']); } if (isset($options['dispatcher']['defaultRoute']) && is_array($options['dispatcher']['defaultRoute'])) { G::set('default_route', $options['dispatcher']['defaultRoute']); } } else { G::set('default_module', Router::YAF_ROUTER_DEFAULT_MODULE); G::set('default_controller', Router::YAF_ROUTER_DEFAULT_CONTROLLER); G::set('default_action', Router::YAF_ROUTER_DEFAULT_ACTION); $this->_options['throwException'] = true; $this->_options['catchException'] = true; } if (!empty($options['modules']) && is_string($options['modules'])) { $modules = preg_split("/,/", $options['modules']); foreach ($modules as $module) { $this->_modules[] = trim($module); } } if (empty($this->_modules)) { $this->_modules[] = G::get('default_module'); } return true; }
private function resolveClass($class) { // Autodiscover the path from the class name // Implementation is PHP namespace-aware, and based on // Framework Interop Group reference implementation: // http://groups.google.com/group/php-standards/web/psr-0-final-proposal $className = ltrim($class, '\\'); $file = ''; $namespace = ''; if (($lastNsPos = strripos($className, '\\')) !== false) { $namespace = substr($className, 0, $lastNsPos); $className = substr($className, $lastNsPos + 1); $file = str_replace('\\', DIRECTORY_SEPARATOR, $namespace) . DIRECTORY_SEPARATOR; } if (G::iniGet('yaf.lowcase_path') == true) { $file = strtolower($file); $segments = explode('_', $className); foreach ($segments as $key => &$value) { if ($key != count($segments) - 1) { $value = strtolower($value); } } $file .= implode(DIRECTORY_SEPARATOR, $segments) . '.' . G::get('ext'); } else { $file .= str_replace('_', DIRECTORY_SEPARATOR, $className) . '.' . G::get('ext'); } return $file; }
private function getController($appDir, $module, $controller) { $controllerDir = ''; if ($this->_default_module == $module) { $controllerDir = $appDir . DIRECTORY_SEPARATOR . Loader::YAF_CONTROLLER_DIRECTORY_NAME; } else { $controllerDir = $appDir . DIRECTORY_SEPARATOR . Loader::YAF_MODULE_DIRECTORY_NAME . DIRECTORY_SEPARATOR . $module . DIRECTORY_SEPARATOR . Loader::YAF_CONTROLLER_DIRECTORY_NAME; } $nameSeparator = G::iniGet('yaf.name_separator'); if (G::iniGet('yaf.name_suffix') == true) { $classname = $controller . $nameSeparator . 'Controller'; } else { $classname = 'Controller' . $nameSeparator . $controller; } if (!@class_exists($classname, false)) { if (!Loader::getInstance()->internal_autoload($controller, $controllerDir)) { throw new Exception\LoadFailed\Controller('Could not find controller script ' . $controllerDir . DIRECTORY_SEPARATOR . $controller . '.' . G::get('ext')); } } if (!class_exists($classname, false)) { throw new Exception\LoadFailed\LoadFailed('Could not find class ' . $classname . ' in controller script ' . $controllerDir); } return $classname; }