From 042c10cdea1aa94f88143861b29a0212bb1fcc2b Mon Sep 17 00:00:00 2001 From: Jaeger Date: Fri, 22 Sep 2017 02:38:46 +0800 Subject: [PATCH] add Http service --- composer.json | 3 ++- src/Dom/Query.php | 2 +- src/Kernel.php | 4 ++++ src/Providers/HttpServiceProvider.php | 27 ++++++++++++++++++++++++ src/Providers/SystemServiceProvider.php | 22 +++++++++++++++++++ src/QueryList.php | 3 +++ src/Services/HttpService.php | 28 +++++++++++++++++++++++++ 7 files changed, 87 insertions(+), 2 deletions(-) create mode 100644 src/Providers/HttpServiceProvider.php create mode 100644 src/Providers/SystemServiceProvider.php create mode 100644 src/Services/HttpService.php diff --git a/composer.json b/composer.json index 9f248ae..b4965b7 100644 --- a/composer.json +++ b/composer.json @@ -6,7 +6,8 @@ "require": { "PHP":">=7.0", "jaeger/phpquery-single": "^0.9", - "tightenco/collect": "^5.5" + "tightenco/collect": "^5.5", + "jaeger/g-http": "^1.1" }, "suggest":{ "monolog/monolog":"Log support" diff --git a/src/Dom/Query.php b/src/Dom/Query.php index ad32c46..ba4b256 100644 --- a/src/Dom/Query.php +++ b/src/Dom/Query.php @@ -35,7 +35,7 @@ class Query public function setHtml($html) { - $this->html = $html; + $this->html = value($html); $this->document = phpQuery::newDocumentHTML($this->html); return $this->ql; } diff --git a/src/Kernel.php b/src/Kernel.php index c2c004b..4285abc 100644 --- a/src/Kernel.php +++ b/src/Kernel.php @@ -11,10 +11,14 @@ use QL\Contracts\ServiceProviderContract; use QL\Exceptions\ServiceNotFoundException; use QL\Providers\EncodeServiceProvider; use Closure; +use QL\Providers\HttpServiceProvider; +use QL\Providers\SystemServiceProvider; class Kernel { protected $providers = [ + SystemServiceProvider::class, + HttpServiceProvider::class, EncodeServiceProvider::class ]; diff --git a/src/Providers/HttpServiceProvider.php b/src/Providers/HttpServiceProvider.php new file mode 100644 index 0000000..9d041d2 --- /dev/null +++ b/src/Providers/HttpServiceProvider.php @@ -0,0 +1,27 @@ + + * Date: 2017/9/22 + */ + +namespace QL\Providers; + + +use QL\Contracts\ServiceProviderContract; +use QL\Kernel; +use QL\Services\HttpService; + +class HttpServiceProvider implements ServiceProviderContract +{ + public function register(Kernel $kernel) + { + $kernel->bind('get',function (...$args){ + return HttpService::get($this,...$args); + }); + + $kernel->bind('post',function (...$args){ + return HttpService::post($this,...$args); + }); + } +} \ No newline at end of file diff --git a/src/Providers/SystemServiceProvider.php b/src/Providers/SystemServiceProvider.php new file mode 100644 index 0000000..f504c31 --- /dev/null +++ b/src/Providers/SystemServiceProvider.php @@ -0,0 +1,22 @@ + + * Date: 2017/9/22 + */ + +namespace QL\Providers; + +use QL\Contracts\ServiceProviderContract; +use QL\Kernel; + +class SystemServiceProvider implements ServiceProviderContract +{ + public function register(Kernel $kernel) + { + $kernel->bind('html',function ($html){ + $this->setHtml($html); + return $this; + }); + } +} \ No newline at end of file diff --git a/src/QueryList.php b/src/QueryList.php index b81b042..3fd61e3 100644 --- a/src/QueryList.php +++ b/src/QueryList.php @@ -22,12 +22,15 @@ use QL\Dom\Query; * * @method QueryList getHtml() * @method QueryList setHtml($html) + * @method QueryList html($html) * @method Dom\Elements find($selector) * @method QueryList rules(array $rules) * @method QueryList range($range) * @method QueryList removeHead() * @method \Illuminate\Support\Collection query($callback = null) * @method QueryList encoding(string $outputEncoding,string $inputEncoding = null) + * @method QueryList get($url,$args = null,$otherArgs = []) + * @method QueryList post($url,$args = null,$otherArgs = []) */ class QueryList { diff --git a/src/Services/HttpService.php b/src/Services/HttpService.php new file mode 100644 index 0000000..6dba7b7 --- /dev/null +++ b/src/Services/HttpService.php @@ -0,0 +1,28 @@ + + * Date: 2017/9/22 + */ + +namespace QL\Services; + +use Jaeger\GHttp; +use QL\QueryList; + +class HttpService +{ + public static function get(QueryList $ql,$url,$args = null,$otherArgs = []) + { + $html = GHttp::get($url,$args,$otherArgs); + $ql->setHtml($html); + return $ql; + } + + public static function post(QueryList $ql,$url,$args = null,$otherArgs = []) + { + $html = GHttp::post($url,$args,$otherArgs); + $ql->setHtml($html); + return $ql; + } +} \ No newline at end of file