Пример #1
0
 /**
  * Sends the body to output buffer.
  */
 public function flushBody()
 {
     $body = $this->body();
     if ($body instanceof \SplFileObject) {
         $body->fpassthru();
     } else {
         if (is_resource($body)) {
             fpassthru($body);
         } else {
             if (is_string($body) && @is_file($body)) {
                 $path = realpath(System::getPathname() . '/' . $body);
                 if ($path && is_readable($path)) {
                     readfile($path);
                 }
             } else {
                 echo $this->contentEncode($body);
             }
         }
     }
     return $this;
 }
Пример #2
0
 /**
  * Last step of task process. Responsible of taking request parameters and/or
  * any external input and process them. Tasks might also update the data store
  * in work instance to for reference of further tasks.
  *
  * @param {Request} $request The HTTP request parameter for post data, null for headless tasks.
  *
  * @return {Promise} The promise object about whether this task is successfully processed.
  */
 public function process()
 {
     $this->__deferred = new Deferred();
     // // note; execute $this->processScript.
     // $f = tmpfile(); fwrite($f, '<?php ' . $this->processScript);
     // $i = stream_get_meta_data($f); $i = $i['uri'];
     // call_user_func(
     //   function($request) { include(func_get_arg(1)); },
     //   $request, $i);
     // fclose($f); unset($f, $i);
     if (empty($this->extra['endpoint']['controller'])) {
         throw new FrameworkException('Task instance has no endpoint defined.');
     } else {
         // load the specified controller
         $controller = $this->extra['endpoint']['controller'];
         $basePath = ".private/modules/{$this->name}";
         require_once "{$basePath}/controllers/{$controller}.php";
         // note; change working directory to task based
         chdir($basePath);
         unset($basePath);
         $controller = new $controller($this);
         if (!$controller instanceof \prefw\ITaskProcessor) {
             throw new FrameworkException('Controller must implement ITaskProcessor interface!');
         } else {
             $ret = $controller->process();
             /*! note;
              *  Three kinds of acceptible return value,
              *    1. null type resolves immediately
              *    2. Exceptions rejects immediately
              *    3. Promise objects pipes results
              */
             if (@$ret === null) {
                 $this->resolve();
             } else {
                 if ($ret instanceof \core\Promise) {
                     $ret->then(array($this, 'resolve'), array($this, 'reject'));
                 }
             }
         }
         // note; revert back to project working directory
         chdir(System::getPathname());
     }
     return $this->__deferred->promise();
 }
Пример #3
0
 /**
  * Autoload PHP classes
  */
 public static function __autoload($name)
 {
     // Namespace path fix
     $name = str_replace('\\', '/', ltrim($name, '\\'));
     // Classname path fix
     // Note: Partially abide to PSR-0, ignoring starting and trailing underscores.
     $name = dirname($name) . '/' . preg_replace('/(\\w+)_(\\w+)/', '$1/$2', basename($name));
     // Look up current folder
     if (file_exists("./{$name}.php")) {
         require_once "./{$name}.php";
     } else {
         $lookupPaths = (array) self::$pathPrefixes;
         // Assumption: wildcards are always shorter than exact matches because "*" only has one character.
         $prefix = array_reduce(array_keys($lookupPaths), function ($result, $prefix) use(&$name) {
             // Wildcards
             if (strpos($prefix, '*') !== false) {
                 if (!preg_match('/^' . preg_replace('/\\\\\\*/', '.*', preg_quote($prefix)) . '/', $name)) {
                     unset($prefix);
                 }
             } else {
                 if (strpos($name, $prefix) === false) {
                     unset($prefix);
                 }
             }
             if (isset($prefix) && strlen($result) < strlen($prefix)) {
                 $result = $prefix;
             }
             return $result;
         });
         if (!$prefix) {
             return;
         }
         // Exact matches should remove prefix portion (PSR-4)
         if (strpos($prefix, '*') === false) {
             $name = trim(substr($name, strlen($prefix)), '/');
         }
         $lookupPaths = (array) @$lookupPaths[$prefix];
         foreach ($lookupPaths as $lookupPath) {
             $lookupPath = System::getPathname() . "/{$lookupPath}/{$name}.php";
             if (file_exists($lookupPath)) {
                 require_once $lookupPath;
             }
         }
     }
 }