add plugin

This commit is contained in:
Jaeger 2017-09-22 19:09:43 +08:00
parent 624f071a0d
commit 5422168c98
9 changed files with 155 additions and 8 deletions

58
src/Config.php Normal file
View File

@ -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]);
}
});
}
}

View File

@ -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);
}

View File

@ -35,9 +35,6 @@ use phpQueryObject;
* @method Elements filterCallback($callback,$_skipHistory) * @method Elements filterCallback($callback,$_skipHistory)
* @method Elements filter($selectors,$_skipHistory) * @method Elements filter($selectors,$_skipHistory)
* @method load($url,$data,$callback) * @method load($url,$data,$callback)
* @method css()
* @method show()
* @method hide()
* @method Elements trigger($type,$data) * @method Elements trigger($type,$data)
* @method Elements triggerHandler($type,$data) * @method Elements triggerHandler($type,$data)
* @method Elements bind($type,$data,$callback) * @method Elements bind($type,$data,$callback)
@ -199,4 +196,13 @@ class Elements
return trim($item->html()); return trim($item->html());
}); });
} }
/**
* @return phpQueryObject
*/
public function getElements(): phpQueryObject
{
return $this->elements;
}
} }

View File

@ -33,10 +33,10 @@ class Query
return $this->html; return $this->html;
} }
public function setHtml($html) public function setHtml($html, $charset = null)
{ {
$this->html = value($html); $this->html = value($html);
$this->document = phpQuery::newDocumentHTML($this->html); $this->document = phpQuery::newDocumentHTML($this->html,$charset);
return $this->ql; return $this->ql;
} }

View File

@ -12,6 +12,7 @@ use QL\Exceptions\ServiceNotFoundException;
use QL\Providers\EncodeServiceProvider; use QL\Providers\EncodeServiceProvider;
use Closure; use Closure;
use QL\Providers\HttpServiceProvider; use QL\Providers\HttpServiceProvider;
use QL\Providers\PluginServiceProvider;
use QL\Providers\SystemServiceProvider; use QL\Providers\SystemServiceProvider;
class Kernel class Kernel
@ -19,7 +20,8 @@ class Kernel
protected $providers = [ protected $providers = [
SystemServiceProvider::class, SystemServiceProvider::class,
HttpServiceProvider::class, HttpServiceProvider::class,
EncodeServiceProvider::class EncodeServiceProvider::class,
PluginServiceProvider::class
]; ];
protected $binds; protected $binds;
@ -37,6 +39,7 @@ class Kernel
public function bootstrap() public function bootstrap()
{ {
//注册服务提供者
$this->registerProviders(); $this->registerProviders();
return $this; return $this;
} }

View File

@ -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);
});
}
}

View File

@ -14,9 +14,13 @@ class SystemServiceProvider implements ServiceProviderContract
{ {
public function register(Kernel $kernel) public function register(Kernel $kernel)
{ {
$kernel->bind('html',function ($html){ $kernel->bind('html',function (...$args){
$this->setHtml($html); $this->setHtml(...$args);
return $this; return $this;
}); });
$kernel->bind('global',function (){
});
} }
} }

View File

@ -31,11 +31,13 @@ use QL\Dom\Query;
* @method QueryList encoding(string $outputEncoding,string $inputEncoding = null) * @method QueryList encoding(string $outputEncoding,string $inputEncoding = null)
* @method QueryList get($url,$args = null,$otherArgs = []) * @method QueryList get($url,$args = null,$otherArgs = [])
* @method QueryList post($url,$args = null,$otherArgs = []) * @method QueryList post($url,$args = null,$otherArgs = [])
* @method QueryList use($plugins,...$opt)
*/ */
class QueryList class QueryList
{ {
protected $query; protected $query;
protected $kernel; protected $kernel;
protected static $plugins = [];
/** /**
* QueryList constructor. * QueryList constructor.
@ -44,6 +46,7 @@ class QueryList
{ {
$this->query = new Query($this); $this->query = new Query($this);
$this->kernel = (new Kernel($this))->bootstrap(); $this->kernel = (new Kernel($this))->bootstrap();
Config::getInstance()->bootstrap($this);
} }
public function __call($name, $arguments) public function __call($name, $arguments)
@ -73,10 +76,19 @@ class QueryList
return $instance; return $instance;
} }
public static function config()
{
return Config::getInstance();
}
public function destruct() public function destruct()
{ {
phpQuery::$documents = []; phpQuery::$documents = [];
} }
public function bind(string $name,\Closure $provide)
{
$this->kernel->bind($name,$provide);
}
} }

View File

@ -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;
}
}