Example #1
0
 /**
  * @param string $url
  * @param string $method
  * @param array  $postData
  * @param array  $getData
  * @return string
  */
 public static function run($url = '/', $method = 'GET', $postData = [], $getData = [])
 {
     $Input = Loader::loadInput();
     $Input->clear();
     $Input->merge('post', $postData);
     $Input->merge('get', $getData);
     return Autoloader::resolve($method, $url);
 }
Example #2
0
 public static function initialize()
 {
     $constantMapping = (require_once Loader::APPPATH() . 'Config/Const.php');
     Kit::update(self::$constantMapping, $constantMapping);
     $constantMapping = (require_once Loader::APPPATH() . 'Config/Const-local.php');
     Kit::update(self::$constantMapping, $constantMapping);
     foreach (self::$constantMapping as $name => $value) {
         if (FALSE === defined($name)) {
             define($name, $value);
         }
     }
 }
Example #3
0
 protected function load_model()
 {
     $params = array();
     foreach (func_get_args() as $index => $n) {
         if ($index == 0) {
             $path = $n;
         } else {
             $params[] = $n;
         }
     }
     $name = Loader::getHandlerFromPath($path);
     return is_null($this->{$name}) ? $this->{$name} = Loader::model($path, $params) : $this->{$name};
 }
Example #4
0
 private final function execute($method_name, $arg_list, $call_parent = FALSE)
 {
     $execution_record = [];
     $execution_id = Debug::addExecutionRecord($execution_record);
     Debug::pushExecutionId($execution_id);
     try {
         $execution_record = $this->prepareExecutionRecord($method_name, $arg_list, $call_parent);
         $class_name = $execution_record['class'];
         $declaring_class_name = $execution_record['declaring_class'];
         $method_accessibility = $execution_record['method_accessibility'];
         $handler_prefix = $execution_record['handler_prefix'];
         $handler_suffix = $execution_record['handler_suffix'];
         Debug::updateExecutionRecord($execution_id, $execution_record);
         if (($count = Debug::countExecutionRecord()) > 3000000) {
             throw new UserException('Abnormal execution record count.', $count);
         }
         if (FALSE === $method_accessibility) {
             throw new UserException("Handler({$declaring_class_name} :: {$method_name}) is not accessible.", $execution_record);
         }
         $config_model_name = $this->loadConfig(Loader::getModelPath($declaring_class_name));
         // Method validateModelPrivilege should throw exception if the validation fails.
         $execution_record['validateModelPrivilege'] = $this->{$config_model_name}->validateModelPrivilege($handler_suffix, $method_name);
         $data_model_name = $this->loadData(Loader::getModelPath($declaring_class_name));
         // Method validateArgs should throw exception if the validation fails,
         // and it should load the config model and fetch the config info itself.
         $args_validation_result = $execution_record['validateArgs'] = $this->{$data_model_name}->validateArgs($handler_suffix, $method_name, $arg_list);
         // Now the validation passed.
         // Method sanitizeArgs should load the config model and fetch the config info itself.
         $args_sanitization_result = $execution_record['sanitizeArgs'] = $this->{$data_model_name}->sanitizeArgs($handler_suffix, $method_name, $arg_list, $args_validation_result);
         // @TODO: check it, can be called with context info?
         // so that XCollection will work correctly.
         // $method = new ReflectionMethod($declaring_class_name, $method_name);
         // $method->setAccessible(TRUE);
         $result = $execution_record['result'] = call_user_func_array([$declaring_class_name, $method_name], $args_sanitization_result);
         // Method validateResult should throw exception if the validation fails,
         // and it should load the config model and fetch the config info itself.
         $result_validation_result = $execution_record['validateResult'] = $this->{$data_model_name}->validateResult($handler_suffix, $method_name, $result);
         // Now the validation passed.
         // Method sanitizeResult should load the config model and fetch the config info itself.
         $result_sanitization_result = $execution_record['sanitizeResult'] = $this->{$data_model_name}->sanitizeResult($handler_suffix, $method_name, $result, $result_validation_result);
         $execution_record['success'] = TRUE;
         Debug::updateExecutionRecord($execution_id, $execution_record);
         Debug::popExecutionId($execution_id);
         return $result;
     } catch (Exception $e) {
         $execution_record['success'] = FALSE;
         Debug::updateExecutionRecord($execution_id, $execution_record);
         Debug::popExecutionId($execution_id);
         throw new UserException('Model execution failed.', $execution_record, $e);
     }
 }
Example #5
0
 public static final function trySetCurrentUserEntity()
 {
     $token = Loader::loadInput()->input('token');
     if (TRUE === Kit::isString($token) and '' !== $token) {
         $class_name = Loader::includeCore('User/User');
         try {
             self::$currentUser = $class_name::getCurrentUserEntity($token);
             self::$currentInstitution = self::$currentUser->getInstitution()->setReadOnly();
         } catch (Exception $e) {
             self::$currentUser = NULL;
             self::$currentInstitution = NULL;
         }
     }
 }
Example #6
0
 /**
  * @param string $APPPATH
  * @param string $RUNTIMEPATH
  * @param string $APPNAME
  */
 public static function initialize($APPPATH, $RUNTIMEPATH, $APPNAME)
 {
     $APPPATH = Kit::getRealPath($APPPATH);
     $ILEXPATH = Kit::getRealPath(__DIR__ . '/Base/');
     $RUNTIMEPATH = Kit::getRealPath($RUNTIMEPATH);
     /**
      * Loader::initialize() should be called before Constant::initialize(), 
      * because Loader::APPPATH() is called in Constant::initialize()
      */
     session_cache_expire(240);
     date_default_timezone_set('UTC');
     Loader::initialize($ILEXPATH, $APPPATH, $RUNTIMEPATH, $APPNAME);
     Constant::initialize();
     Debug::initialize();
 }
Example #7
0
 protected final function generateExecutionRecord($class_name, $method_name)
 {
     $class = new ReflectionClass($class_name);
     if (FALSE === $class->hasMethod($method_name)) {
         throw new UserException("Method({$method_name}) does not exist in class({$class_name}).");
     }
     $method = new ReflectionMethod($class_name, $method_name);
     if (TRUE === $method->isPublic()) {
         throw new UserException("Method({$method_name}) in class({$class_name}) is public.");
     }
     $declaring_class = $method->getDeclaringClass();
     $declaring_class_name = $declaring_class->getName();
     $methods_visibility = $declaring_class->getStaticProperties()['methodsVisibility'];
     $method_visibility = $this->getMethodVisibility($methods_visibility, $method_name);
     list($initiator_class_name, $initiator_type) = $this->getInitiatorNameAndType($method_name, $declaring_class);
     $method_accessibility = $this->getMethodAccessibility($method_visibility, $initiator_type);
     $handler_prefix = Loader::getHandlerPrefixFromPath($declaring_class_name);
     $handler_suffix = Loader::getHandlerSuffixFromPath($declaring_class_name);
     $execution_record = ['success' => FALSE, 'class' => $class_name, 'method' => $method_name, 'method_accessibility' => $method_accessibility, 'declaring_class' => $declaring_class_name, 'method_visibility' => $method_visibility, 'initiator_class' => $initiator_class_name, 'initiator_type' => $initiator_type, 'handler_prefix' => $handler_prefix, 'handler_suffix' => $handler_suffix];
     return $execution_record;
 }
Example #8
0
 public static function resolve($method, $url)
 {
     $Route = new Route\Route($method, $url);
     include Loader::APPPATH() . 'config/route.php';
     return $Route->result();
 }
 protected function __construct($collection_name)
 {
     Kit::ensureString($collection_name);
     try {
         $this->collectionName = $collection_name;
         $this->collection = Loader::loadMongoDB()->selectCollection($collection_name);
     } catch (Exception $e) {
         throw new UserException('Initializing collection failed.', $collection_name, $e);
     }
 }
Example #10
0
<?php

use Ilex\Core\Loader;
/** @var \Ilex\Route\Route $Route */
$Route->get('/', function () {
    echo 'Hello world!';
});
$Route->post('/user/(any)', function ($name) {
    /** @var \Ilex\Base\Model\sys\Input $Input */
    $Input = Loader::model('sys/Input');
    echo 'Hello ' . $Input->post('title', 'Guest') . ' ' . $name . '!';
});
$Route->get('/projects', 'Project');
$Route->get('/project/(num)', 'Project', 'view');
$Route->group('/planet', function ($Route) {
    /** @var \Ilex\Route\Route $Route */
    $Route->get('/', function () {
        echo 'Hello Cosmos!';
    });
    $Route->back();
});
$Route->controller('/about', 'About');
$Route->controller('/play', 'Play');
$Route->get('(all)', function ($url) {
    echo 'Oops, 404! "' . $url . '" does not exist.';
});
Example #11
0
 private function fitController($description, $handler)
 {
     if (!$this->getRestURI($description)) {
         return FALSE;
     }
     $function = RouteLib::getFunction($this->uri);
     if (is_array($function)) {
         $params = $function[1];
         $function = $function[0];
     } else {
         $params = array();
     }
     $controller = Loader::controller($handler);
     if (method_exists($controller, $this->method . $function)) {
         $fn = $this->method . $function;
     } elseif (method_exists($controller, $function)) {
         $fn = $function;
     } elseif (method_exists($controller, 'resolve')) {
         $fn = 'resolve';
         $params = array($this);
     } else {
         $this->_pop();
         return FALSE;
     }
     $this->end(call_user_func_array(array($controller, $fn), $params));
     return TRUE;
 }
Example #12
0
 protected final function loadCore($path)
 {
     $handler_name = Loader::getHandlerFromPath($path) . 'Core';
     return $this->{$handler_name} = Loader::loadCore($path);
 }
Example #13
0
 private final function includeEntityBulk()
 {
     $this->entityBulkClassName = Loader::includeEntityBulk($this->entityPath);
 }
Example #14
0
 /**
  * Extracts args and handles the request,
  * by choosing the appropriate handler,
  * and calling the appropriate method
  * if $description CAN fit $this->uri.
  * @param string      $description eg. '/project/(num)', '/(num)', '/', '/user/(any)', '(all)'
  * @param mixed       $handler     eg. 'Project',        $this,    an anonymous function
  * @param string|NULL $function    eg. 'view'
  * @param boolean     $is_time_consuming
  * @return boolean
  */
 private final function fitGeneral($description, $handler, $function = NULL, $is_time_consuming = FALSE)
 {
     /**
      * eg. $description : '/project/(id:num)' => '/project/([0-9]+?)'
      *     $this->uri   : '/project/12'
      *     $match_list  : ['/project/12', '12']
      */
     $pattern = $this->getPattern($description);
     // It will attempt to match the whole $this->uri string.
     $uri = rtrim($this->uri, '/');
     // $this->uri contains no GET args.
     if (1 === preg_match($pattern, $uri, $match_list)) {
         preg_match_all('@([^:\\(\\)]+):([^:\\(\\)]+)@', $description, $m, PREG_SET_ORDER);
         $mapping = [];
         foreach ($m as $value) {
             $mapping[$value[1]] = $value[2];
             // 'id' => 'num'
         }
         $Input = Loader::loadInput();
         foreach ($match_list as $key => $value) {
             // [0 => 12, 'id' => 12]
             if (TRUE === Kit::isInt($key)) {
                 unset($match_list[$key]);
             } elseif ('num' === $mapping[$key]) {
                 $Input->setInput($key, intval($value));
             } else {
                 $Input->setInput($key, $value);
             }
         }
         if (TRUE === Kit::isString($handler) or FALSE === $handler instanceof \Closure) {
             // $handler is a string or IS NOT an anonymous function, i.e., an instance.
             Kit::ensureString($function);
             $this->end(call_user_func_array([TRUE === Kit::isString($handler) ? Loader::loadService($handler) : $handler, $function], [$is_time_consuming]));
         } elseif (TRUE === is_callable($handler)) {
             // $handler is an anonymous function.
             $this->end(call_user_func_array($handler, [$is_time_consuming]));
         }
         // CAN FIT!
         return TRUE;
     } else {
         // CAN NOT FIT!
         return FALSE;
     }
 }
Example #15
0
 public function __construct()
 {
     $this->collection = Loader::db()->selectCollection($this->collectionName);
 }