示例#1
0
 public function response($status = 0, $message = '', $payload = array())
 {
     if ($status instanceof \Painless\System\Workflow\Response) {
         $this->response = $status;
     } elseif (is_int($status)) {
         $this->response = \Painless::manufacture('response', $status, $message, $payload);
     }
     return $this->response;
 }
示例#2
0
 protected function processInternal($method, &$uri, $data = array(), $roles = array())
 {
     // Localize the variables
     $module = '';
     $controller = '';
     $param = array();
     // If it's an INTERNAL call, a URI must be given
     if (empty($uri)) {
         return FALSE;
     }
     // At this point, the URI has been split into an array. Pass it to
     // mapUri to map to the correct module and controller.
     list($module, $controller, $param, $contentType) = $this->mapUri($uri);
     // Manufacture a request
     $request = \Painless::manufacture('request', $method, $module, $controller, $param, $contentType, 'PainlessPHP Internal [v' . \Painless::VERSION . ']');
     // Append $data into request if needed
     if (!empty($data)) {
         $request->append($data);
     }
     return $request;
 }
示例#3
0
 public function execute($entry, $cmd = '', $data = array(), $role = array())
 {
     // Here we need to determine the entry point. There are only 3 of them:
     // HTTP (includes REST calls), CLI (including cron jobs) and APP. The
     // first two are fairly self-explanatory, but APP needs more explanation
     // on this front.
     //
     // When a request to APP is made, Painless would automatically convert it
     // to either HTTP or APP. First it'll look inside the app registry, and
     // check the app's path. If the path starts with a http://, it'll convert
     // the call to a HTTP call instead, and if not, it'll assume its a file
     // path and use APP as is.
     //
     //  Example:
     //      Painless::request( 'GET app://flight-plan/id/123' );
     //      will first search for the app's path inside the registry, and
     //      if it looks like this:
     //          $config['apps']['flight-plan'] = '/usr/local/web/htdocs/flight-plan';
     //      then it is a local call, and if it looks like this:
     //          $config['apps']['flight-plan'] = 'http://flight-plan.foo.com:8003';
     //      then it is a REST call.
     // Load the router into the Core registry
     $router = \Painless::load('system/common/router');
     // Localize the response variable
     $response = FALSE;
     // Log the entry point, which would return a sequence number
     $seq = $this->log('protocol', $entry, 0);
     // Send the command to the router to process, which will create a request
     // object containing all routing information (as well as some extra info
     // like agent string, content type, etc)
     if (\Painless::RUN_HTTP === $entry || \Painless::RUN_CLI === $entry || \Painless::RUN_APP === $entry || \Painless::RUN_INTERNAL) {
         // Send the router the command to receive a request
         $request = $router->process($entry, $cmd, $data, $role);
         // By now $cmd is an array. Change it back to a string
         $cmd = implode('/', $cmd);
         // Set this request as the active one
         $this->active = $request;
         // Log the request
         $this->log('request', $request, $seq);
         // If $request is FALSE, something baaaaadddddd has happened inside
         // the router. Use a 500 error response instead of dispatching it.
         if (FALSE === $request) {
             $response = \Painless::manufacture('response', 500, 'Fatal error when trying to process the command in router. See log for more details.');
         } elseif (empty($request->module)) {
             $response = \Painless::manufacture('response', 404, "Module not found for the command: [{$cmd}]");
         } elseif (empty($request->controller)) {
             $response = \Painless::manufacture('response', 404, "Controller not found for the command: [{$cmd}]");
         } else {
             $response = $router->dispatch($request);
         }
     } else {
         // Manufacture an empty request
         $request = \Painless::manufacture('request', '', '', '');
         // Set this request as the active one
         $this->active = $request;
         // Log the request
         $this->log('request', $request);
         // Manufacture a 500 error status response object
         $response = \Painless::manufacture('response', 500, 'Invalid entry point');
     }
     // Make sure $response is the correct type
     if (!$response instanceof \Painless\System\Workflow\Response) {
         $response = \Painless::manufacture('response', 500, 'Invalid returned response');
     }
     // Log the response object
     $this->log('response', $response, $seq);
     // Get the renderer
     $render = \Painless::load('system/common/render');
     // Process the request and response to get an output
     $response = $render->process($request, $response);
     // Log the response object generated by the renderer
     $this->log('output', $response, $seq);
     // Now we can safely assume that $response has all the necessary headers
     // and payload. We return this to the invoking agent to render it.
     return $response;
 }