public function __construct() { $defaultRoute = Yaf_G::get('default_route'); if ($defaultRoute != null) { $this->addRoute('_default', $this->_getRouteFromArray($defaultRoute)); } else { $this->addRoute('_default', new Yaf_Route_Static()); } }
public function __construct($method = null, $module = null, $controller = null, $action = null, $params = array()) { if (!is_array($params)) { throw new Yaf_Exception_TypeError('Expects the params is an array'); } if ($method == null) { if (isset($_SERVER['REQUEST_METHOD'])) { $method = $_SERVER['REQUEST_METHOD']; } else { $sapiType = php_sapi_name(); if (strtolower($sapiType) == 'cli' || substr($sapiType, 0, 3) == 'cgi') { $method = 'CLI'; } else { $method = 'unknown'; } } } $this->method = $method; if ($module != null || $action != null || $controller != null) { $this->setActionName($action); $this->setControllerName($controller); $this->setModuleName($module); $this->setRouted(true); } else { if ($module == null) { $this->setModuleName(Yaf_G::get('default_module')); } if ($controller == null) { $this->setControllerName(Yaf_G::get('default_controller')); } if ($action == null) { $this->setActionName(Yaf_G::get('default_action')); } } if ($params) { $this->setParam($params); } }
/** * 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 Yaf_Exception_TypeError('Expected an array of application configure'); } $options = array_change_key_case($options, CASE_LOWER); if (!isset($options['application'])) { throw new Yaf_Exception_TypeError('Expected an array of application configure'); } $options = $options['application']; if (!empty($options['directory'])) { Yaf_G::set('directory', preg_replace("/" . preg_quote(DIRECTORY_SEPARATOR, "/") . "\$/", "", $options['directory'])); } else { throw new Yaf_Exception_StartupError('Expected a directory entry in application configures'); } if (!empty($options['ext'])) { Yaf_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'] = Yaf_G::get('directory') . DIRECTORY_SEPARATOR . Yaf_Loader::YAF_LIBRARY_DIRECTORY_NAME; } if (!empty($options['view']) && is_array($options['view']) && !empty($options['view']['ext']) && is_string($options['view']['ext'])) { Yaf_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'])) { Yaf_G::set('default_module', $options['dispatcher']['defaultModule']); } else { Yaf_G::set('default_module', Yaf_Router::YAF_ROUTER_DEFAULT_MODULE); } if (!empty($options['dispatcher']['defaultController']) && is_string($options['dispatcher']['defaultController'])) { Yaf_G::set('default_controller', $options['dispatcher']['defaultController']); } else { Yaf_G::set('default_controller', Yaf_Router::YAF_ROUTER_DEFAULT_CONTROLLER); } if (!empty($options['dispatcher']['defaultAction']) && is_string($options['dispatcher']['defaultAction'])) { Yaf_G::set('default_action', $options['dispatcher']['defaultAction']); } else { Yaf_G::set('default_action', Yaf_Router::YAF_ROUTER_DEFAULT_ACTION); } if (isset($options['dispatcher']['throwException'])) { Yaf_G::set('throwException', (bool) $options['dispatcher']['throwException']); } if (isset($options['dispatcher']['catchException'])) { Yaf_G::set('catchException', (bool) $options['dispatcher']['catchException']); } if (isset($options['dispatcher']['defaultRoute']) && is_array($options['dispatcher']['defaultRoute'])) { Yaf_G::set('default_route', $options['dispatcher']['defaultRoute']); } } else { Yaf_G::set('default_module', Yaf_Router::YAF_ROUTER_DEFAULT_MODULE); Yaf_G::set('default_controller', Yaf_Router::YAF_ROUTER_DEFAULT_CONTROLLER); Yaf_G::set('default_action', Yaf_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[] = Yaf_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 (Yaf_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) . '.' . Yaf_G::get('ext'); } else { $file .= str_replace('_', DIRECTORY_SEPARATOR, $className) . '.' . Yaf_G::get('ext'); } return $file; }
/** * 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) { Yaf_G::set('throwException', (bool) $flag); return $this; } return Yaf_G::get('throwException'); }
private function getController($appDir, $module, $controller) { $controllerDir = ''; if ($this->_default_module == $module) { $controllerDir = $appDir . DIRECTORY_SEPARATOR . Yaf_Loader::YAF_CONTROLLER_DIRECTORY_NAME; } else { $controllerDir = $appDir . DIRECTORY_SEPARATOR . Yaf_Loader::YAF_MODULE_DIRECTORY_NAME . DIRECTORY_SEPARATOR . $module . DIRECTORY_SEPARATOR . Yaf_Loader::YAF_CONTROLLER_DIRECTORY_NAME; } $nameSeparator = Yaf_G::iniGet('yaf.name_separator'); if (Yaf_G::iniGet('yaf.name_suffix') == true) { $classname = $controller . $nameSeparator . 'Controller'; } else { $classname = 'Controller' . $nameSeparator . $controller; } if (!@class_exists($classname, false)) { if (!Yaf_Loader::getInstance()->internal_autoload($controller, $controllerDir)) { throw new Yaf_Exception_LoadFailed_Controller('Could not find controller script ' . $controllerDir . DIRECTORY_SEPARATOR . $controller . '.' . Yaf_G::get('ext')); } } if (!class_exists($classname, false)) { throw new Yaf_Exception_LoadFailed('Could not find class ' . $classname . ' in controller script ' . $controllerDir); } return $classname; }
/** * 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 . '.' . Yaf_G::get('view_ext'); $controller = $request->getControllerName(); if ($controller != null) { $controller = str_replace('_', DIRECTORY_SEPARATOR, strtolower($controller)); } $script = $controller . DIRECTORY_SEPARATOR . $script; return $script; }