Plug-in development guidance



QueryList plug-in development guide


List the last before explaining Overall directory structure, assuming that the www directory is the current project directory:

www
├── querylist
│   ├── Ext
│   │   └── Hello.php
│   ├── QueryList.php
│   └── vendor
│       
└── testHello.php

All source code for the demonstration is packaged and downloaded: Full download

1. Download the QueryList project to the local

Install querylist

composer create-project jaeger/querylist

Then go to the querylist directory and execute the following command to install AQuery ,AQuery is the base class of all plug-ins. The plug-in must inherit AQuery and implement the run() method.

composer require jaeger/querylist-ext-aquery

2. Create a new Ext directory under the querylist directory

querylist/Ext directory can be used to store QueryListExtension

3. Create a new extension file Hello.php
<?php
/**
 * QueryList的Hello扩展演示
 */
namespace QL\Ext;
class Hello extends AQuery
{
    /**
     * 必须要实现run()方法
     */
    public function run(array $args)
    {
        //getInstance()方法用于获取任意类的实例,默认获取QueryList实例
        $ql = $this->getInstance();
        //设置QueryList对象的html属性
        $ql->html = $this->getHtml($args['url']);
        //返回QueryList对象
        return $ql;
    }
    /**
     * 自定义一个抓取网页源码的方法
     */
    public function getHtml($url)
    {
      return file_get_contents($url);
    }
}
<?php
require 'querylist/vendor/autoload.php';
use QL\QueryList;
$ql = QueryList::run('Hello',[
    'url' => 'http://www.baidu.com'
    ]);
$data = $ql->setQuery([
    'title'=>['title','text']
    ])->data;
print_r($data);
输出结果;
Array
(
    [0] => Array
        (
            [title] => 百度一下,你就知道
        )
)

in the

querylist/Ext directory Attached below are some existing plug-in source codes to enhance understanding

The following is the source code of the Request extension:

<?php
namespace QL\Ext;
/**
 * @Author: Jaeger <hj.q@qq.com>
 * @version         1.0
 * 网络操作扩展
 */
class Request extends AQuery
{
    protected function hq(array $args)
    {
        $args = array(
            'http' => isset($args['http'])?$args['http']:$args,
            'callback' => isset($args['callback'])?$args['callback']:'',
            'args' =>  isset($args['args'])?$args['args']:''
            );
        $http = $this->getInstance('QL\Ext\Lib\Http');
        $http->initialize($args['http']);
        $http->execute();
        if(!empty($args['callback'])){
            $http->result = call_user_func($args['callback'],$http->result,$args['args']);
        }
        return $http;
    }
    public function run(array $args)
    {
        $http = $this->hq($args);
        $ql = $this->getInstance();
        $ql->html = $http->result;
        return $ql;
    }
}

There are also extensions between It can be inherited. The following Login extension inherits the Request extension and overrides the run() method:

<?php
namespace QL\Ext;
/**
 * @Author: Jaeger <hj.q@qq.com>
 * @version         1.0
 * 模拟登陆扩展
 */
class Login extends Request
{
    private $http;
    public $html;
    public function run(array $args)
    {
        $this->http = $this->hq($args);
        $this->html = $this->http->result;
        return $this;
    }
    public function get($url,$callback = null,$args = null)
    {
        $result = $this->http->get($url);
        return $this->getQL($result,$callback,$args);
    }
    public function post($url,$data=array(),$callback = null,$args = null)
    {
        $result = $this->http->post($url,$data);
        return $this->getQL($result,$callback,$args);
    }
    private function getQL($html,$callback = null,$args = null)
    {
        if(is_callable($callback)){
            $result = call_user_func($callback,$result,$args);
        }
        $ql = $this->getInstance();
        $ql->html = $html;
        return $ql;
    }
}