Example #1
0
 /**
  * This function retrieves all the services for the controller and then executes them.
  * @param \System\Web\Controller The controller to process the services for.
  */
 private static final function processServices(\System\Web\Controller $controller)
 {
     //get all the registered services for this controller
     $services = new \System\Collection\Vector();
     $controller->deployServices($services);
     $serviceResults = new \System\Collection\Map();
     //process all the registered services
     foreach ($services as $service) {
         if ($service instanceof \System\Web\ServiceEntry) {
             //this is an optimization. A ServiceEntry needs to define on which actionName it needs to fire.
             //for instance: <actioncontainer>-><actionfield> must be a specific <actionname> to fire the service.
             //the service sets the value to check for, so the servicemanagement can distinguish if it is needed or not
             switch ($service->getActionType()) {
                 case \System\Web\ServiceEntry::ACTIONTYPE_POST:
                     $actionContainer = new \System\HTTP\Request\Post();
                     break;
                 case \System\Web\ServiceEntry::ACTIONTYPE_GET:
                     $actionContainer = new \System\HTTP\Request\Get();
                     break;
                 case \System\Web\ServiceEntry::ACTIONTYPE_SESSION:
                     $actionContainer = new \System\HTTP\Storage\Session();
                     break;
                 case \System\Web\ServiceEntry::ACTIONTYPE_COOKIE:
                     $actionContainer = new \System\HTTP\Storage\Cookie();
                     break;
                 default:
                     throw new \InvalidArgumentException('Unknown ACTIONTYPE given. Please check the serviceentries');
             }
             //do the actual matching and check if we need to fire this service call
             $actionType = $service->getActionField();
             $actionValue = (string) $actionContainer->{$actionType};
             if ($actionValue == $service->getActionName()) {
                 if (is_callable($service->getCallback())) {
                     $serviceResult = new \System\Collection\Map();
                     //automatically call the validation of handles
                     \System\Web\Service::validateHandles();
                     //passing parameters to a static method implies call by reference. thus the params are passed by reference
                     $isMatch = call_user_func_array($service->getCallback(), array($serviceResult, $controller->getDefaultDb()));
                     if (is_bool($isMatch) && $isMatch) {
                         $serviceResults[$service->getCallback()] = $serviceResult;
                         //we raise the proper event
                         $event = new \System\Event\Event\OnServiceExecutedEvent();
                         $callBack = $service->getCallback();
                         $event->setServiceCallback($callBack);
                         $event->setServiceResult($serviceResult);
                         $event->raise($controller);
                     }
                 } else {
                     throw new \InvalidArgumentException('Cannot execute the callback in the given service. Please check the callback');
                 }
             }
         } else {
             throw new \InvalidArgumentException('The given entry in the \'deployServices\' method is not a \\System\\Web\\ServiceEntry.');
         }
     }
     //we store the service results in the controller
     $controller->setServiceResults($serviceResults);
 }