From 5422168c988dd16a76b54ff1d3823be46a4d5d2c Mon Sep 17 00:00:00 2001
From: Jaeger <JaegerCode@gmail.com>
Date: Fri, 22 Sep 2017 19:09:43 +0800
Subject: [PATCH] add plugin

---
 src/Config.php                          | 58 +++++++++++++++++++++++++
 src/Contracts/PluginContract.php        | 15 +++++++
 src/Dom/Elements.php                    | 12 +++--
 src/Dom/Query.php                       |  4 +-
 src/Kernel.php                          |  5 ++-
 src/Providers/PluginServiceProvider.php | 23 ++++++++++
 src/Providers/SystemServiceProvider.php |  8 +++-
 src/QueryList.php                       | 12 +++++
 src/Services/PluginService.php          | 26 +++++++++++
 9 files changed, 155 insertions(+), 8 deletions(-)
 create mode 100644 src/Config.php
 create mode 100644 src/Contracts/PluginContract.php
 create mode 100644 src/Providers/PluginServiceProvider.php
 create mode 100644 src/Services/PluginService.php

diff --git a/src/Config.php b/src/Config.php
new file mode 100644
index 0000000..cb73d99
--- /dev/null
+++ b/src/Config.php
@@ -0,0 +1,58 @@
+<?php
+/**
+ * Created by PhpStorm.
+ * User: Jaeger <JaegerCode@gmail.com>
+ * Date: 2017/9/22
+ */
+
+namespace QL;
+
+
+class Config
+{
+    protected static $instance = null;
+
+    protected $plugins;
+
+    /**
+     * Config constructor.
+     */
+    public function __construct()
+    {
+        $this->plugins = collect();
+    }
+
+
+    public static function getInstance()
+    {
+        self::$instance || self::$instance = new self();
+        return self::$instance;
+    }
+
+    public function use($plugins,...$opt)
+    {
+        if(is_string($plugins)){
+            $this->plugins->push([$plugins,$opt]);
+        }else{
+            $this->plugins = $this->plugins->merge($plugins);
+        }
+        return $this;
+    }
+
+    public function bootstrap(QueryList $queryList)
+    {
+        $this->installPlugins($queryList);
+    }
+
+    protected function installPlugins(QueryList $queryList)
+    {
+        $this->plugins->each(function($plugin) use($queryList){
+            if(is_string($plugin)){
+                $queryList->use($plugin);
+            }else{
+                $queryList->use($plugin[0],...$plugin[1]);
+            }
+        });
+    }
+
+}
\ No newline at end of file
diff --git a/src/Contracts/PluginContract.php b/src/Contracts/PluginContract.php
new file mode 100644
index 0000000..6fe165e
--- /dev/null
+++ b/src/Contracts/PluginContract.php
@@ -0,0 +1,15 @@
+<?php
+/**
+ * Created by PhpStorm.
+ * User: Jaeger <JaegerCode@gmail.com>
+ * Date: 2017/9/22
+ */
+
+namespace QL\Contracts;
+
+use QL\QueryList;
+
+interface PluginContract
+{
+    public static function install(QueryList $queryList,...$opt);
+}
\ No newline at end of file
diff --git a/src/Dom/Elements.php b/src/Dom/Elements.php
index d778f96..8ba893b 100644
--- a/src/Dom/Elements.php
+++ b/src/Dom/Elements.php
@@ -35,9 +35,6 @@ use phpQueryObject;
  * @method Elements filterCallback($callback,$_skipHistory)
  * @method Elements filter($selectors,$_skipHistory)
  * @method  load($url,$data,$callback)
- * @method  css()
- * @method  show()
- * @method  hide()
  * @method Elements trigger($type,$data)
  * @method Elements triggerHandler($type,$data)
  * @method Elements bind($type,$data,$callback)
@@ -199,4 +196,13 @@ class Elements
             return trim($item->html());
         });
     }
+
+    /**
+     * @return phpQueryObject
+     */
+    public function getElements(): phpQueryObject
+    {
+        return $this->elements;
+    }
+
 }
\ No newline at end of file
diff --git a/src/Dom/Query.php b/src/Dom/Query.php
index 3562d69..f245eed 100644
--- a/src/Dom/Query.php
+++ b/src/Dom/Query.php
@@ -33,10 +33,10 @@ class Query
         return $this->html;
     }
 
-    public function setHtml($html)
+    public function setHtml($html, $charset = null)
     {
         $this->html = value($html);
-        $this->document = phpQuery::newDocumentHTML($this->html);
+        $this->document = phpQuery::newDocumentHTML($this->html,$charset);
         return $this->ql;
     }
 
diff --git a/src/Kernel.php b/src/Kernel.php
index 4285abc..a7e1c38 100644
--- a/src/Kernel.php
+++ b/src/Kernel.php
@@ -12,6 +12,7 @@ use QL\Exceptions\ServiceNotFoundException;
 use QL\Providers\EncodeServiceProvider;
 use Closure;
 use QL\Providers\HttpServiceProvider;
+use QL\Providers\PluginServiceProvider;
 use QL\Providers\SystemServiceProvider;
 
 class Kernel
@@ -19,7 +20,8 @@ class Kernel
     protected $providers = [
         SystemServiceProvider::class,
         HttpServiceProvider::class,
-        EncodeServiceProvider::class
+        EncodeServiceProvider::class,
+        PluginServiceProvider::class
     ];
 
     protected $binds;
@@ -37,6 +39,7 @@ class Kernel
 
     public function bootstrap()
     {
+        //注册服务提供者
         $this->registerProviders();
         return $this;
     }
diff --git a/src/Providers/PluginServiceProvider.php b/src/Providers/PluginServiceProvider.php
new file mode 100644
index 0000000..f20cbcf
--- /dev/null
+++ b/src/Providers/PluginServiceProvider.php
@@ -0,0 +1,23 @@
+<?php
+/**
+ * Created by PhpStorm.
+ * User: Jaeger <JaegerCode@gmail.com>
+ * Date: 2017/9/22
+ */
+
+namespace QL\Providers;
+
+use QL\Contracts\ServiceProviderContract;
+use QL\Kernel;
+use QL\Services\PluginService;
+
+class PluginServiceProvider implements ServiceProviderContract
+{
+    public function register(Kernel $kernel)
+    {
+        $kernel->bind('use',function ($plugins,...$opt){
+            return PluginService::install($this,$plugins,...$opt);
+        });
+    }
+
+}
\ No newline at end of file
diff --git a/src/Providers/SystemServiceProvider.php b/src/Providers/SystemServiceProvider.php
index f504c31..6f3e32d 100644
--- a/src/Providers/SystemServiceProvider.php
+++ b/src/Providers/SystemServiceProvider.php
@@ -14,9 +14,13 @@ class SystemServiceProvider implements ServiceProviderContract
 {
     public function register(Kernel $kernel)
     {
-        $kernel->bind('html',function ($html){
-            $this->setHtml($html);
+        $kernel->bind('html',function (...$args){
+            $this->setHtml(...$args);
             return $this;
         });
+
+        $kernel->bind('global',function (){
+
+        });
     }
 }
\ No newline at end of file
diff --git a/src/QueryList.php b/src/QueryList.php
index 3fd61e3..766fed8 100644
--- a/src/QueryList.php
+++ b/src/QueryList.php
@@ -31,11 +31,13 @@ use QL\Dom\Query;
  * @method QueryList encoding(string $outputEncoding,string $inputEncoding = null)
  * @method QueryList get($url,$args = null,$otherArgs = [])
  * @method QueryList post($url,$args = null,$otherArgs = [])
+ * @method QueryList use($plugins,...$opt)
  */
 class QueryList
 {
     protected $query;
     protected $kernel;
+    protected static $plugins = [];
 
     /**
      * QueryList constructor.
@@ -44,6 +46,7 @@ class QueryList
     {
         $this->query = new Query($this);
         $this->kernel = (new Kernel($this))->bootstrap();
+        Config::getInstance()->bootstrap($this);
     }
 
     public function __call($name, $arguments)
@@ -73,10 +76,19 @@ class QueryList
         return $instance;
     }
 
+    public static function config()
+    {
+        return Config::getInstance();
+    }
+
     public function destruct()
     {
         phpQuery::$documents = [];
     }
 
+    public function bind(string $name,\Closure $provide)
+    {
+        $this->kernel->bind($name,$provide);
+    }
 
 }
\ No newline at end of file
diff --git a/src/Services/PluginService.php b/src/Services/PluginService.php
new file mode 100644
index 0000000..e44fbe5
--- /dev/null
+++ b/src/Services/PluginService.php
@@ -0,0 +1,26 @@
+<?php
+/**
+ * Created by PhpStorm.
+ * User: Jaeger <JaegerCode@gmail.com>
+ * Date: 2017/9/22
+ */
+
+namespace QL\Services;
+
+use QL\QueryList;
+
+class PluginService
+{
+    public static function install(QueryList $queryList, $plugins, ...$opt)
+    {
+        if(is_array($plugins))
+        {
+            foreach ($plugins as $plugin) {
+                $plugin::install($queryList);
+            }
+        }else{
+            $plugins::install($queryList,...$opt);
+        }
+        return $queryList;
+    }
+}
\ No newline at end of file