public function __construct($config, $env = null) { $app = self::app(); if (!is_null($app)) { throw new Exception\StartupError('Only one application can be initialized'); } $this->_environ = $env; $config = $this->_loadConfig($config); if ($config == null || !$config instanceof Config_Abstract || $this->parseOptions($config->toArray()) != true) { throw new Exception\StartupError('Initialization of application config failed'); } //$this->parseOptions($config->toArray()); $this->_config = $config; //request initialization $request = new Request\Http(); if ($request == null) { throw new Exception\StartupError('Initialization of request failed'); } //dispatcher $this->_dispatcher = Dispatcher::getInstance(); if ($this->_dispatcher == null || !$this->_dispatcher instanceof Dispatcher) { throw new Exception\StartupError('Instantiation of dispatcher failed'); } $this->_dispatcher->setRequest($request); //loader initialization $loader = Loader::getInstance(isset($this->_options['local_library']) ? $this->_options['local_library'] : '', G::iniGet('yaf.library')); if ($loader == null || !$loader instanceof Loader) { throw new Exception\StartupError('Initialization of application auto loader failed'); } if (isset($this->_options['local_namespace']) && $this->_options['local_namespace'] != '') { $namespace = str_replace(array(',', ' '), array(':', ':'), $this->_options['local_namespace']); $loader->registerLocalNamespace($namespace); } self::$_app = $this; if (G::get('throwException') == false) { set_exception_handler(array($this, 'exceptionHandler')); } }
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; }
/** * Get a route from an array * * @param array $info * * @return Yaf_Route_Interface */ protected function _getRouteFromArray(array $info) { $useNamespace = G::iniGet('yaf.use_namespace'); if ($useNamespace) { $class = isset($info['type']) ? '\\Yaf\\Route\\' . ucfirst($info['type']) : '\\Yaf\\Route\\Static'; } else { $class = isset($info['type']) ? 'Yaf_Route_' . ucfirst($info['type']) : 'Yaf_Route_Static'; } try { $route = call_user_func(array($class, 'getInstance'), $info); } catch (\Exception $e) { return null; } return $route; }
/** * Processes a request and sets its controller and action. If * no route was possible, default route is set. * * @param Yaf_Request_Abstract * @return Yaf_Request_Abstract|boolean */ public function route(Request_Abstract $request) { $requestUri = $request->getRequestUri(); $baseuri = $request->getBaseUri(); if ($requestUri != '' && $baseuri != '' && stripos($requestUri, $baseuri) === 0) { $path = substr($requestUri, strlen($baseuri)); } else { $path = $requestUri; } $module = null; $controller = null; $action = null; $rest = null; $path = trim($path, Router::URI_DELIMITER); if ($path != '') { $path = explode(Router::URI_DELIMITER, $path); $path = array_filter($path, 'strlen'); if (Application::isModuleName($path[0])) { $module = $path[0]; array_shift($path); } if (count($path) && !empty($path[0])) { $controller = $path[0]; array_shift($path); } if (count($path) && !empty($path[0])) { $action = $path[0]; array_shift($path); } $rest = implode(Router::URI_DELIMITER, $path); $actionPrefer = G::iniGet('yaf.action_prefer'); if ($module == null && $controller == null && $action == null) { if ($actionPrefer == true) { $action = $rest; } else { $controller = $rest; } $rest = null; } elseif ($module == null && $action == null && $rest == null) { if ($actionPrefer == true) { $action = $controller; $controller = null; } } elseif ($controller == null && $action == null && $rest != null) { $controller = $module; $action = $rest; $module = null; $rest = null; } elseif ($action == null && $rest == null) { $action = $controller; $controller = $module; $module = null; } elseif ($controller == null && $action == null) { $controller = $module; $action = $rest; $module = null; $rest = null; } elseif ($action == null) { $action = $rest; $rest = null; } if ($module != null) { $request->setModuleName($module); } if ($controller != null) { $request->setControllerName($controller); } if ($action != null) { $request->setActionName($action); } $params = array(); if ($rest != null && trim($rest) != '') { $path = explode(Router::URI_DELIMITER, $rest); if (($numSegs = count($path)) != 0) { for ($i = 0; $i < $numSegs; $i = $i + 2) { $key = urldecode($path[$i]); $val = isset($path[$i + 1]) ? urldecode($path[$i + 1]) : null; $params[$key] = isset($params[$key]) ? array_merge((array) $params[$key], array($val)) : $val; } } $request->setParam($params); } } 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; }