Browse Source

build: parseContent, parseHeaders

Hugh Harlequin 2 years ago
parent
commit
5c20df9f24
1 changed files with 115 additions and 4 deletions
  1. 115 4
      src/Builder.php

+ 115 - 4
src/Builder.php

@@ -5,13 +5,17 @@ use Hugh\Curl\Builder as CurlBuilder;
 class Builder {
 class Builder {
     private $mCurl = null;
     private $mCurl = null;
     private $curls = [];
     private $curls = [];
+    private $options = [];
 
 
     public function __construct() {
     public function __construct() {
         $this->mCurl = curl_multi_init();
         $this->mCurl = curl_multi_init();
     }
     }
-    public function add(CurlBuilder $curl) {
+    public function add($objectAndOptions) {
+        list($curl, $options, $packageOption) = $objectAndOptions;
         curl_multi_add_handle($this->mCurl, $curl);
         curl_multi_add_handle($this->mCurl, $curl);
         $this->curls[] = $curl;
         $this->curls[] = $curl;
+        $this->options[] = $options;
+        $this->packageOptions[] = $packageOption;
         return $this;
         return $this;
     }
     }
     public function go() {
     public function go() {
@@ -21,12 +25,119 @@ class Builder {
                 curl_multi_select($this->mCurl);
                 curl_multi_select($this->mCurl);
             }
             }
         } while ($active && $status == CURLM_OK);
         } while ($active && $status == CURLM_OK);
-        curl_multi_close($this->mCurl);
         $result = [];
         $result = [];
-        foreach ($this->curls as $curl) {
-            $result[] = curl_multi_getcontent($curl);
+        foreach ($this->curls as $key => $curl) {
+            $result[] = $this->parseContent($key);
         }
         }
+        curl_multi_close($this->mCurl);
+
         return $result;
         return $result;
     }
     }
+    protected function parseContent($key)
+    {
+        $response = curl_multi_getcontent($this->curls[$key]);
+        $curlOptions = $this->options[$key];
+        $curlObject = $this->curls[$key];
+        $packageOptions = $this->packageOptions[$key];
+//        curl_close( $curlObject );
+
+        if( $packageOptions[ 'saveFile' ] ) {
+            // Save to file if a filename was specified
+            $file = fopen($packageOptions[ 'saveFile' ], 'w');
+            fwrite($file, $response);
+            fclose($file);
+        } else if( $packageOptions[ 'asJsonResponse' ] ) {
+            // Decode the request if necessary
+            $response = json_decode($response, $packageOptions[ 'returnAsArray' ]);
+        }
+
+        if( $packageOptions[ 'enableDebug' ] ) {
+            fclose( $debugFile );
+        }
+        return $this->returnResponse( $response, $key );
+    }
+    /**
+     * @param   string $headerString    Response header string
+     * @return mixed
+     */
+    protected function parseHeaders($headerString)
+    {
+        $headers = array_filter(array_map(function ($x) {
+            $arr = array_map('trim', explode(':', $x, 2));
+            if( count($arr) == 2 ) {
+                return [ $arr[ 0 ] => $arr[ 1 ] ];
+            }
+        }, array_filter(array_map('trim', explode("\r\n", $headerString)))));
+
+        $results = array();
+
+        foreach( $headers as $values ) {
+            if( !is_array($values) ) {
+                continue;
+            }
+
+            $key = array_keys($values)[ 0 ];
+            if( isset($results[ $key ]) ) {
+                $results[ $key ] = array_merge(
+                    (array) $results[ $key ],
+                    array( array_values($values)[ 0 ] )
+                );
+            } else {
+                $results = array_merge(
+                    $results,
+                    $values
+                );
+            }
+        }
 
 
+        return $results;
+    }
+
+    /**
+     * @param   mixed $content          Content of the request
+     * @param   array $responseData     Additional response information
+     * @param   string $header          Response header string
+     * @return mixed
+     */
+    protected function returnResponse($content, $key)
+    {
+        $packageOptions = $this->packageOptions[$key];
+        $curlOptions = $this->options[$key];
+        $curlObject = $this->curls[$key];
+        if( !$packageOptions[ 'responseObject' ] && !$packageOptions[ 'responseArray' ] ) {
+            return $content;
+        }
+
+        $responseHeader = null;
+        if( $curlOptions[ 'HEADER' ] ) {
+            $headerSize = curl_getinfo( $curlObject, CURLINFO_HEADER_SIZE );
+            $responseHeader = substr( $content, 0, $headerSize );
+            $content = substr( $content, $headerSize );
+        }
+
+        // Capture additional request information if needed
+        $responseData = array();
+        if( $packageOptions[ 'responseObject' ] || $packageOptions[ 'responseArray' ] ) {
+            $responseData = curl_getinfo( $curlObject );
+
+            if( curl_errno($curlObject) ) {
+                $responseData[ 'errorMessage' ] = curl_error($curlObject);
+            }
+        }
+        curl_multi_remove_handle($this->mCurl, $curlObject);
+        curl_close($curlObject);
+
+        $object = new \stdClass();
+        $object->content = $content;
+        $object->status = $responseData[ 'http_code' ];
+        $object->contentType = $responseData[ 'content_type' ];
+        if( array_key_exists('errorMessage', $responseData) ) {
+            $object->error = $responseData[ 'errorMessage' ];
+        }
+
+        if( $curlOptions[ 'HEADER' ] ) {
+            $object->headers = $this->parseHeaders( $responseHeader );
+        }
+        return $packageOptions[ 'responseObject' ] ? $object : (array) $object;
+    }
 }
 }