Exemplo n.º 1
0
 public function __construct($config, $env = null)
 {
     $app = self::app();
     if (!is_null($app)) {
         throw new Yaf_Exception_StartupError('Only one application can be initialized');
     }
     $this->_environ = $env;
     $config = $this->_loadConfig($config);
     if ($config == null || !$config instanceof Yaf_Config_Abstract || $this->parseOptions($config->toArray()) != true) {
         throw new Yaf_Exception_StartupError('Initialization of application config failed');
     }
     //$this->parseOptions($config->toArray());
     $this->_config = $config;
     //request initialization
     $request = new Yaf_Request_Http();
     if ($request == null) {
         throw new Yaf_Exception_StartupError('Initialization of request failed');
     }
     //dispatcher
     $this->_dispatcher = Yaf_Dispatcher::getInstance();
     if ($this->_dispatcher == null || !$this->_dispatcher instanceof Yaf_Dispatcher) {
         throw new Yaf_Exception_StartupError('Instantiation of dispatcher failed');
     }
     $this->_dispatcher->setRequest($request);
     //loader initialization
     $loader = Yaf_Loader::getInstance(isset($this->_options['local_library']) ? $this->_options['local_library'] : '', Yaf_G::iniGet('yaf.library'));
     if ($loader == null || !$loader instanceof Yaf_Loader) {
         throw new Yaf_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 (Yaf_G::get('throwException') == false) {
         set_exception_handler(array($this, 'exceptionHandler'));
     }
 }
Exemplo n.º 2
0
 /**
  * Get a route from an array
  *
  * @param  array $info
  * @return Yaf_Route_Interface
  */
 protected function _getRouteFromArray(array $info)
 {
     $useNamespace = Yaf_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;
 }
Exemplo n.º 3
0
 /**
  * Processes a request and sets its controller and action based on a
  * supervar value.
  *
  * @param  Yaf_Request_Abstract
  *
  * @return Yaf_Request_Abstract|boolean
  */
 public function route(Yaf_Request_Abstract $request)
 {
     $requestUri = $request->getQuery($this->_varName);
     if ($requestUri == null || $requestUri == '') {
         return false;
     }
     $module = null;
     $controller = null;
     $action = null;
     $rest = null;
     $path = trim($requestUri, Yaf_Router::URI_DELIMITER);
     if ($path != '' && $path != '/') {
         $path = explode(Yaf_Router::URI_DELIMITER, $path);
         if (Yaf_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(Yaf_Router::URI_DELIMITER, $path);
         $actionPrefer = Yaf_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(Yaf_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;
 }
Exemplo n.º 4
0
 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;
 }
Exemplo n.º 5
0
 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;
 }