예제 #1
0
 public static function fix_files_array($files)
 {
     // Multiple values for post-keys indexes
     if (isset($files['name'], $files['tmp_name'], $files['size'], $files['type'], $files['error'])) {
         return FilesParameter::_move_indexes_right($files);
     }
     // Re order pre-keys indexes
     array_walk($files, function (&$sub) {
         if (is_array($sub)) {
             $sub = FilesParameter::fix_files_array($sub);
         }
     });
     return $files;
 }
예제 #2
0
 /**
  * Create request object from superglobal $GLOBALS
  *
  * @param $globals The $GLOBALS
  * @return HttpRequest
  */
 public static function createFromGlobals(array $globals)
 {
     $request = new self();
     if (isset($globals['_POST'])) {
         $request->bodyParameters = $globals['_POST'];
     }
     if (isset($globals['_GET'])) {
         $request->queryParameters = $globals['_GET'];
     }
     if (isset($globals['_REQUEST'])) {
         $request->parameters = $globals['_REQUEST'];
     }
     if (isset($globals['_COOKIE'])) {
         $request->cookieParameters = $globals['_COOKIE'];
     }
     if (isset($globals['_SESSION'])) {
         $request->sessionParameters = $globals['_SESSION'];
     }
     if (isset($globals['_FILES'])) {
         $request->files = FilesParameter::fix_files_array($globals['_FILES']);
     }
     if (isset($globals['_SERVER'])) {
         $request->serverParameters = $globals['_SERVER'];
     }
     return $request;
 }
예제 #3
0
 /**
  * Constructing Action objects
  *
  * When 'request' object is provided, 'files' will be ignored.
  *
  * @param array $args        The request arguments
  * @param mixed $options     Can be ArrayAccess or array
  */
 public function __construct(array $args = array(), $options = array())
 {
     // try to get service container or create a new one.
     // we use service container to get:
     //   1. MessagePool
     //   2. CsrfTokenProvider
     if (isset($options['services'])) {
         $this->services = $options['services'];
     } else {
         $this->services = new ServiceContainer();
     }
     if (isset($options['current_user'])) {
         $this->currentUser = $options['current_user'];
     } else {
         if (isset($this->services['current_user'])) {
             $this->currentUser = $this->services['current_user'];
         }
     }
     if (isset($options['csrf'])) {
         $this->csrf = $options['csrf'];
     } else {
         if (isset($this->services['csrf'])) {
             $this->csrf = $this->services['csrf'];
         }
     }
     if (isset($options['message_pool'])) {
         $this->messagePool = $options['message_pool'];
     } else {
         if (isset($this->services['message_pool'])) {
             $this->messagePool = $this->services['message_pool'];
         }
     }
     // save parent action
     if (isset($options['parent'])) {
         $this->parent = $options['parent'];
     }
     // backward compatible request object
     // ActionRequest
     if (isset($options['request'])) {
         $this->request = $options['request'];
         if (isset($options['files'])) {
             trigger_error('"files" is ignored because you passed action request object');
         }
     } else {
         // Create request object manually
         if (isset($options['files'])) {
             $files = FilesParameter::fix_files_array($options['files']);
             $this->request = new ActionRequest($args, $files);
         } else {
             if (isset($this->services['action_request'])) {
                 // fallback to action_request defiend in service
                 $this->request = $this->services['action_request'];
             } else {
                 if (isset($_FILES)) {
                     // if not, always fix $_FILES
                     $files = FilesParameter::fix_files_array($_FILES);
                     $this->request = new ActionRequest($args, $files);
                 } else {
                     // When rendering Action with view, we probably won't have this request object.
                     $this->request = new ActionRequest($args, []);
                 }
             }
         }
     }
     $this->result = new Result();
     $this->mixins = $this->mixins();
     $this->preinit();
     foreach ($this->mixins as $mixin) {
         $mixin->preinit();
     }
     // initialize parameter objects
     $this->schema();
     // intiailize schema from mixin classes later,
     // so that we can use mixin to override the default options.
     foreach ($this->mixins as $mixin) {
         $mixin->schema();
     }
     // use the schema definitions to filter arguments
     $this->args = $this->filterArguments($args);
     // See if we need to render the input names with relationship ID and
     // index?
     if ($relationId = $this->arg('__nested')) {
         $this->setParamNamesWithIndex($relationId);
     }
     $this->loadParamValues();
     // action & parameters initialization
     // ===================================
     //
     // call the parameter preinit method to initialize
     // foreach is always faster than array_map
     foreach ($this->params as $param) {
         $param->preinit($this->args);
     }
     // call the parameter init method
     foreach ($this->params as $param) {
         $param->init($this->args);
     }
     // user-defined init script
     $this->init();
     // call the parameter init method
     foreach ($this->params as $param) {
         $param->postinit($this->args);
     }
     $this->postinit();
     foreach ($this->mixins as $mixin) {
         $mixin->postinit();
     }
     // save request arguments
     $this->result->args($this->args);
 }