QueryList/src/QueryList.php

123 lines
3.0 KiB
PHP
Raw Normal View History

2017-09-19 02:33:38 +08:00
<?php
/**
* QueryList
*
* 一个基于phpQuery的通用列表采集类
*
* @author Jaeger
* @email JaegerCode@gmail.com
* @link https://github.com/jae-jae/QueryList
* @version 4.0.0
*
*/
2017-09-22 01:51:46 +08:00
namespace QL;
use phpQuery;
use QL\Dom\Query;
2018-12-10 19:23:15 +08:00
use Tightenco\Collect\Support\Collection;
2017-09-25 14:15:26 +08:00
use Closure;
2018-12-10 19:23:15 +08:00
use QL\Services\MultiRequestService;
2017-09-22 01:51:46 +08:00
/**
* Class QueryList
* @package QL
*
2017-09-25 14:15:26 +08:00
* @method string getHtml()
2017-09-22 01:51:46 +08:00
* @method QueryList setHtml($html)
2017-09-22 02:38:46 +08:00
* @method QueryList html($html)
2017-09-22 01:51:46 +08:00
* @method Dom\Elements find($selector)
* @method QueryList rules(array $rules)
* @method QueryList range($range)
* @method QueryList removeHead()
2017-09-25 14:15:26 +08:00
* @method QueryList query(Closure $callback = null)
* @method Collection getData(Closure $callback = null)
2018-10-15 18:52:12 +08:00
* @method Array queryData(Closure $callback = null)
2017-09-25 14:15:26 +08:00
* @method QueryList setData(Collection $data)
2017-09-22 01:51:46 +08:00
* @method QueryList encoding(string $outputEncoding,string $inputEncoding = null)
2017-09-22 02:38:46 +08:00
* @method QueryList get($url,$args = null,$otherArgs = [])
* @method QueryList post($url,$args = null,$otherArgs = [])
2018-12-10 01:27:48 +08:00
* @method QueryList postJson($url,$args = null,$otherArgs = [])
2018-12-10 19:23:15 +08:00
* @method MultiRequestService multiRequest($urls)
2017-09-22 19:09:43 +08:00
* @method QueryList use($plugins,...$opt)
2018-12-10 01:27:48 +08:00
* @method QueryList pipe(Closure $callback = null)
2017-09-22 01:51:46 +08:00
*/
2017-09-19 02:33:38 +08:00
class QueryList
{
2017-09-22 01:51:46 +08:00
protected $query;
2017-09-21 02:20:28 +08:00
protected $kernel;
2017-12-15 11:05:32 +08:00
protected static $instance = null;
2017-09-19 02:33:38 +08:00
/**
* QueryList constructor.
*/
public function __construct()
{
2017-09-22 01:51:46 +08:00
$this->query = new Query($this);
2017-09-21 13:12:20 +08:00
$this->kernel = (new Kernel($this))->bootstrap();
2017-09-22 19:09:43 +08:00
Config::getInstance()->bootstrap($this);
2017-09-19 02:33:38 +08:00
}
2017-09-21 13:12:20 +08:00
public function __call($name, $arguments)
{
2017-09-22 01:51:46 +08:00
if(method_exists($this->query,$name)){
$result = $this->query->$name(...$arguments);
}else{
$result = $this->kernel->getService($name)->call($this,...$arguments);
}
return $result;
2017-09-19 19:06:16 +08:00
}
2017-09-22 01:51:46 +08:00
public static function __callStatic($name, $arguments)
2017-09-19 19:06:16 +08:00
{
2018-12-07 00:35:58 +08:00
$instance = new self();
2017-09-22 01:51:46 +08:00
return $instance->$name(...$arguments);
2017-09-19 19:06:16 +08:00
}
2017-09-22 01:51:46 +08:00
public function __destruct()
2017-09-19 19:06:16 +08:00
{
2017-09-22 01:51:46 +08:00
$this->destruct();
2017-09-19 19:06:16 +08:00
}
2017-10-09 01:48:56 +08:00
/**
2017-12-15 11:05:32 +08:00
* Get the QueryList single instance
2017-10-09 01:48:56 +08:00
*
* @return QueryList
*/
2017-09-22 01:51:46 +08:00
public static function getInstance()
2017-09-19 19:06:16 +08:00
{
2017-12-15 11:05:32 +08:00
self::$instance || self::$instance = new self();
return self::$instance;
2017-09-19 19:06:16 +08:00
}
2017-10-09 01:48:56 +08:00
/**
* Get the Config instance
* @return null|Config
*/
2017-09-22 19:09:43 +08:00
public static function config()
{
return Config::getInstance();
}
2017-10-09 01:48:56 +08:00
/**
* Destruction of resources
*/
2017-09-22 01:51:46 +08:00
public function destruct()
2017-09-19 19:06:16 +08:00
{
2017-09-22 01:51:46 +08:00
phpQuery::$documents = [];
2017-09-19 19:06:16 +08:00
}
2017-10-09 01:48:56 +08:00
/**
* Bind a custom method to the QueryList object
*
* @param string $name Invoking the name
* @param Closure $provide Called method
* @return $this
*/
2017-09-25 14:15:26 +08:00
public function bind(string $name,Closure $provide)
2017-09-22 19:09:43 +08:00
{
$this->kernel->bind($name,$provide);
2017-09-25 14:15:26 +08:00
return $this;
2017-09-22 19:09:43 +08:00
}
2017-09-19 19:06:16 +08:00
2017-09-19 02:33:38 +08:00
}