cwola/method-interceptor 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

cwola/method-interceptor

最新稳定版本:v0.1.0

Composer 安装命令:

composer require cwola/method-interceptor

包简介

Intercept class method.

README 文档

README

Experimental: This is an experimental version.

Intercept class method.

Installation

composer require cwola/method-interceptor

Usage

Interceptor.php

<?php

// include autoloader...

use Cwola\MethodInterceptor;

#[MethodInterceptor\Attribute\Interceptable]
class Foo {
    use MethodInterceptor\UseIntercept;

    public function __construct() {
        $this->__addInterceptor(new InterceptTimer, new FilterRunOnly);
        $this->__addInterceptor(new InterceptGreet);
    }

    public function run() :bool {
        $this->message('Hello');
        return true;
    }

    protected function message(string $message) {
        $this->privateMessage(
            $this->bold($message)
        );
    }

    #[MethodInterceptor\Attribute\DoNotIntercept]
    protected function bold(string $message) :string {
        return '**' . $message . '**';
    }

    private function privateMessage(string $message) {
        echo $message . PHP_EOL;
    }
}

class InterceptTimer implements MethodInterceptor\Contracts\Visitor {
    protected array $timers = [];

    public function enterMethod(string $name, ...$args) :void {
        $timers[$name] = new StopWatch;
    }

    public function leaveMethod(string $name, ...$args) :void {
        echo 'TIME : ' . $timers[$name]->stop()->time() . PHP_EOL;
    }
}

class InterceptGreet implements MethodInterceptor\Contracts\Visitor {
    public function enterMethod(string $name, ...$args) :void {
        echo 'ENTER : ' . $name . PHP_EOL;
    }

    public function leaveMethod(string $name, ...$args) :void {
        echo 'LEAVE : ' . $name . PHP_EOL;
    }
}

class FilterRunOnly implements MethodInterceptor\Contracts\Filter {
    public function test(string $name, ...$args) :bool {
        return $name === 'run';
    }
}

test.php

<?php

// include autoloader...

$foo = new Foo;

$foo->run();
// output:
//
// [ENTER] : run
// [ENTER] : message
// [ENTER] : privateMessage
// **Hello**
// [LEAVE] : privateMessage
// [LEAVE] : message
// TIME : xxx
// [LEAVE] : run
//

Licence

MIT

统计信息

  • 总下载量: 4
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 2
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2022-09-13

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固