插件开发指导


    安装querylist

    1. composer create-project jaeger/querylist

    querylist/Ext目录可用于存放QueryList扩展

    1. <?php
    2. /**
    3. * QueryList的Hello扩展演示
    4. */
    5. namespace QL\Ext;
    6. class Hello extends AQuery
    7. {
    8. /**
    9. * 必须要实现run()方法
    10. */
    11. public function run(array $args)
    12. {
    13. //getInstance()方法用于获取任意类的实例,默认获取QueryList实例
    14. $ql = $this->getInstance();
    15. //设置QueryList对象的html属性
    16. $ql->html = $this->getHtml($args['url']);
    17. //返回QueryList对象
    18. return $ql;
    19. }
    20. * 自定义一个抓取网页源码的方法
    21. */
    22. public function getHtml($url)
    23. {
    24. return file_get_contents($url);
    25. }
    26. }

    输出结果:

    1. Array
    2. (
    3. [0] => Array
    4. (
    5. [title] => 百度一下,你就知道
    6. )
    7. )

    扩展之间还可以继承,下面的Login扩展继承了Request扩展并重写了run()方法:

    1. <?php
    2. namespace QL\Ext;
    3. /**
    4. * @Author: Jaeger <hj.q@qq.com>
    5. * @version 1.0
    6. * 模拟登陆扩展
    7. */
    8. {
    9. private $http;
    10. public function run(array $args)
    11. {
    12. $this->http = $this->hq($args);
    13. $this->html = $this->http->result;
    14. return $this;
    15. }
    16. public function get($url,$callback = null,$args = null)
    17. {
    18. $result = $this->http->get($url);
    19. return $this->getQL($result,$callback,$args);
    20. }
    21. public function post($url,$data=array(),$callback = null,$args = null)
    22. {
    23. $result = $this->http->post($url,$data);
    24. return $this->getQL($result,$callback,$args);
    25. }
    26. private function getQL($html,$callback = null,$args = null)
    27. {
    28. if(is_callable($callback)){
    29. $result = call_user_func($callback,$result,$args);
    30. }
    31. $ql = $this->getInstance();
    32. $ql->html = $html;
    33. return $ql;
    34. }