/**
  * Handles methods that do not exist.
  *
  * @param   string  method name
  * @param   array   arguments
  * @return  void
  */
 public function __call($method, $args)
 {
     // get(Db/Mem/Tt/Fs)Instance($routeStamp)
     if (substr($method, 0, 3) == 'get' && substr($method, -8) == 'Instance') {
         $baseDriverKey = substr($method, 3, strlen($method) - 11);
         if ($this->configObject->isExistsDriverKey($baseDriverKey) == FALSE) {
             throw new ServRouteInstanceException(_('Instance.driver_not_supported:') . $baseDriverKey, 500);
         }
         // 路由驱动
         $routeDriver = 'ServRoute_' . $baseDriverKey . '_Driver';
         // 实例驱动
         $instanceDriver = 'ServInstance_' . $baseDriverKey . '_Driver';
         if (!Lemon::auto_load($routeDriver)) {
             throw new ServRouteInstanceException(_('Instance.route_driver_not_found:') . $baseDriverKey, 500);
         }
         if (!Lemon::auto_load($instanceDriver)) {
             throw new ServRouteInstanceException(_('Instance.instance_driver_not_found:') . $baseDriverKey, 500);
         }
         // 实例池
         $driverInstancePoolName = $baseDriverKey;
         if (!array_key_exists($driverInstancePoolName, $this->instancePools)) {
             $this->instancePools[$driverInstancePoolName] = array();
         }
         // fast but unflexible
         $thisRouteClassInstance = new $routeDriver($this->configObject->getDriverSetup($baseDriverKey), $args);
         // slow but flexible
         //$rc = new ReflectionClass($routeDriver);$thisRouteClassInstance = $rc->newInstanceArgs($args);
         if (!$thisRouteClassInstance instanceof ServRoute_Driver || !is_subclass_of($thisRouteClassInstance, 'ServRoute_Driver')) {
             throw new ServRouteInstanceException(_('Instance.route_driver_implements:') . $driverKey, 500);
         }
         // 路由key
         $routeKey = $thisRouteClassInstance->getRouteKey();
         // 初始化Pool中的routeKey
         !isset($this->instancePools[$driverInstancePoolName][$routeKey]) && ($this->instancePools[$driverInstancePoolName][$routeKey] = false);
         if ($this->instancePools[$driverInstancePoolName][$routeKey] !== FALSE && $this->instancePools[$driverInstancePoolName][$routeKey]->isAvailable() == TRUE) {
             // 池中有对应的实例并且可用
             return $this->instancePools[$driverInstancePoolName][$routeKey];
         }
         // 设定池中的实例
         $thisInstanceClassInstance = new $instanceDriver($thisRouteClassInstance);
         if (!$thisInstanceClassInstance instanceof ServInstance_Driver || !is_subclass_of($thisInstanceClassInstance, 'ServInstance_Driver')) {
             throw new ServRouteInstanceException(_('Instance.instance_driver_implements:') . $driverKey, 500);
         }
         $this->instancePools[$driverInstancePoolName][$routeKey] = $thisInstanceClassInstance;
         if ($this->instancePools[$driverInstancePoolName][$routeKey] !== FALSE) {
             $this->instancePools[$driverInstancePoolName][$routeKey]->isAvailable() or $this->instancePools[$driverInstancePoolName][$routeKey]->setup();
             if ($this->instancePools[$driverInstancePoolName][$routeKey]->isAvailable() != TRUE) {
                 throw new ServRouteInstanceException(_('Instance.getInstance Failed,Service Not Available.'), 500);
             }
             // 池中有对应的实例并且可用
             return $this->instancePools[$driverInstancePoolName][$routeKey];
         }
         throw new ServRouteInstanceException(_('Instance.getInstance Failed,critical error'), 500);
     } else {
         throw new ServRouteInstanceException(_('Unknown Method'), 500);
     }
 }
Esempio n. 2
0
 /**
  * Loads the configured driver and validates it.
  *
  * @param   array|string  custom configuration or config group name
  * @return  void
  */
 public function __construct($config = FALSE)
 {
     if (is_string($config)) {
         $name = $config;
         // Test the config group name
         if (($config = Lemon::config('cache.' . $config)) === NULL) {
             throw new LemonRuntimeException('cache.undefined_group ' . $name, 500);
         }
     }
     if (is_array($config)) {
         // Append the default configuration options
         $config += Lemon::config('cache.default');
     } else {
         // Load the default group
         $config = Lemon::config('cache.default');
     }
     // Cache the config in the object
     $this->config = $config;
     // Set driver name
     $driver = 'Cache_' . ucfirst($this->config['driver']) . '_Driver';
     // Load the driver
     if (!Lemon::auto_load($driver)) {
         throw new LemonRuntimeException('core.driver_not_found ' . $this->config['driver'], 500);
     }
     // Initialize the driver
     $this->driver = new $driver($this->config['params']);
     // Validate the driver
     if (!$this->driver instanceof Cache_Driver) {
         throw new LemonRuntimeException('core.driver_implements ' . $this->config['driver'], 500);
     }
     if (Cache::$loaded !== TRUE) {
         $this->config['requests'] = (int) $this->config['requests'];
         if ($this->config['requests'] > 0 and mt_rand(1, $this->config['requests']) === 1) {
             // Do garbage collection
             $this->driver->delete_expired();
         }
         // Cache has been loaded once
         Cache::$loaded = TRUE;
     }
 }
Esempio n. 3
0
 /**
  * Create a new session.
  *
  * @param   array  variables to set after creation
  * @return  void
  */
 public function create($vars = NULL, $_session_id = NULL)
 {
     // Destroy any current sessions
     $this->destroy();
     if (Session::$config['driver'] !== 'native') {
         // Set driver name
         $driver = 'Session_' . ucfirst(Session::$config['driver']) . '_Driver';
         // Load the driver
         if (!Lemon::auto_load($driver)) {
             throw new LemonRuntimeException('core.driver_not_found ' . Session::$config['driver'], 500);
         }
         // Initialize the driver
         Session::$driver = new $driver();
         // Validate the driver
         if (!Session::$driver instanceof Session_Driver) {
             throw new LemonRuntimeException('core.driver_implements ' . Session::$config['driver'], 500);
         }
         // Register non-native driver as the session handler
         session_set_save_handler(array(Session::$driver, 'open'), array(Session::$driver, 'close'), array(Session::$driver, 'read'), array(Session::$driver, 'write'), array(Session::$driver, 'destroy'), array(Session::$driver, 'gc'));
     }
     // Validate the session name
     if (!preg_match('~^(?=.*[a-z])[a-z0-9_]++$~iD', Session::$config['name'])) {
         throw new LemonRuntimeException('session.invalid_session_name ' . Session::$config['name'], 500);
     }
     // Name the session, this will also be the name of the cookie
     session_name(Session::$config['name']);
     // Set the session cookie parameters
     session_set_cookie_params(Session::$config['expiration'], Lemon::config('cookie.path'), Lemon::config('cookie.domain'), Lemon::config('cookie.secure'), Lemon::config('cookie.httponly'));
     // Start the session!
     if ($_session_id !== NULL) {
         //log::write('dbglog','got_sessionid not null '.$_session_id.PHP_EOL,__FILE__,__LINE__);
         session_id($_session_id);
     }
     session_start();
     // Put session_id in the session variable
     $_SESSION['session_id'] = session_id();
     // Set defaults
     if (!isset($_SESSION['_kf_flash_'])) {
         $_SESSION['total_hits'] = 0;
         $_SESSION['_kf_flash_'] = array();
         $_SESSION['user_agent'] = Lemon::$user_agent;
         $_SESSION['ip_address'] = $this->input->ip_address();
     }
     // Set up flash variables
     Session::$flash =& $_SESSION['_kf_flash_'];
     // Increase total hits
     $_SESSION['total_hits'] += 1;
     // Validate data only on hits after one
     if ($_SESSION['total_hits'] > 1) {
         // Validate the session
         foreach (Session::$config['validate'] as $valid) {
             switch ($valid) {
                 // Check user agent for consistency
                 case 'user_agent':
                     if ($_SESSION[$valid] !== Lemon::$user_agent) {
                         return $this->create(NULL, $_session_id);
                     }
                     break;
                     // Check ip address for consistency
                 // Check ip address for consistency
                 case 'ip_address':
                     if ($_SESSION[$valid] !== $this->input->{$valid}()) {
                         return $this->create(NULL, $_session_id);
                     }
                     break;
                     // Check expiration time to prevent users from manually modifying it
                 // Check expiration time to prevent users from manually modifying it
                 case 'expiration':
                     if (time() - $_SESSION['last_activity'] > ini_get('session.gc_maxlifetime')) {
                         return $this->create(NULL, $_session_id);
                     }
                     break;
             }
         }
     }
     // Expire flash keys
     $this->expire_flash();
     // Update last activity
     $_SESSION['last_activity'] = time();
     // Set the new data
     Session::set($vars);
 }