EventBuild.php 2.2 KB
Newer Older
lzc828's avatar
lzc828 已提交
1 2
<?php

lzc828's avatar
lzc828 已提交
3
namespace One\Database\Pgsql;
lzc828's avatar
lzc828 已提交
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88

class EventBuild extends CacheBuild
{
    private $events = [];

    public function __construct($connection, $model, $model_name, $table)
    {
        parent::__construct($connection, $model, $model_name, $table);
        $this->events = $this->model->events();
    }

    protected function getData($all = false)
    {
        if ($this->callBefre(__FUNCTION__, $id) !== false) {
            $ret = parent::getData($all);
            $this->callAfter(__FUNCTION__, $ret, $id);
            return $ret;
        }
    }

    public function find($id = null)
    {
        if ($this->callBefre(__FUNCTION__, $id) !== false) {
            $ret = parent::find($id);
            $this->callAfter(__FUNCTION__, $ret, $id);
            return $ret;
        }
    }

    public function findAll()
    {
        if ($this->callBefre(__FUNCTION__) !== false) {
            $ret = parent::findAll();
            $this->callAfter(__FUNCTION__, $ret);
            return $ret;
        }
    }

    public function update($data)
    {
        if ($this->callBefre(__FUNCTION__, $data) !== false) {
            $ret = parent::update($data);
            $this->callAfter(__FUNCTION__, $ret, $data);
            return $ret;
        }
    }

    public function delete()
    {
        if ($this->callBefre(__FUNCTION__) !== false) {
            $ret = parent::delete();
            $this->callAfter(__FUNCTION__, $ret);
            return $ret;
        }
    }

    public function insert($data, $is_mulit = false)
    {
        if ($this->callBefre(__FUNCTION__, $data) !== false) {
            $ret = parent::insert($data, $is_mulit);
            $this->callAfter(__FUNCTION__, $ret, $data);
            return $ret;
        }
    }

    private function callBefre($name, & $arg = null)
    {
        $key = 'before' . ucfirst($name);
        if (isset($this->events[$key])) {
            return $this->events[$key]($this, $arg);
        } else {
            return true;
        }
    }

    private function callAfter($name, & $result, & $arg = null)
    {
        $key = 'after' . ucfirst($name);
        if (isset($this->events[$key])) {
            $this->events[$key]($result, $arg);
        }
    }


}