1. Go to this page and download the library: Download hehex/hehep-hcontainer library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
use hehe\core\hcontainer\annotation\Bean;
/**
* @bean("user",_onProxy=true)
*/
class UserBean
{
}
use hehe\core\hcontainer\annotation\Bean;
use hehe\core\hcontainer\aop\annotation\After;
/**
* @bean("user")
* @After("hcontainer\tests\common\LogBehavior@log",pointcut=".+Action")
*/
class UserBean
{
/**
* @After("hcontainer\tests\common\LogBehavior")
*/
public function doAfter(){}
}
use hehe\core\hcontainer\annotation\Bean;
use hehe\core\hcontainer\annotation\Ref;
/**
* @bean("user")
*/
class UserBean
{
/**
* $annRole 为代理对象
* @Ref("role","_lazy"=>true)
* @var RoleBean
*/
public $annRole;
}
use hehe\core\hcontainer\ann\base\AnnotationProcessor;
class BeanProcessor extends AnnotationProcessor
{
// 自定义注解处理方法
protected $annotationHandlers = [
'Ref'=>'handleRefAnnotation'
];
// 实现以下方法即可
// 统一处理类,方法,类类型注解 $target_type = class,method,property
public function handleAnnotation($annotation,string $class,string $target,string $target_type):void{}
// 处理注解类
public function handleAnnotationClass($annotation,string $class):void{}
// 处理注解类方法
public function handleAnnotationMethod($annotation,string $class,string $method):void{}
// 处理注解类属性
public function handleAnnotationProperty($annotation,string $class,string $property):void{}
// 独立处理注解@Ref
public function handleRefAnnotation($annotation,string $class,string $target,string $target_type)
{
}
// 扫码结束后调用此方法
public function handleProcessorFinish()
{
// 注册bean信息到容器
}
}
use hehe\core\hcontainer\ContainerManager;
$hcontainer = new ContainerManager();
// 注册优先处理器
$hcontainer->addFirstProcessor("hehe\core\hcontainer\annotation\BeanProcessor");
// 注册重置处理器[旧注解器类,新注解处理器类]
$hcontainer->addCustomProcessors(["hehe\core\hcontainer\annotation\BeanProcessor","hehe\core\hcontainer\annotation\NewBeanProcessor"]);
use hehe\core\hcontainer\ann\base\Annotation;
use hehe\core\hcontainer\ann\base\BaseAnnotation;
/**
* @Annotation("hehe\core\hcontainer\annotation\BeanProcessor")
*/
class Bean extends BaseAnnotation
{
public $_id;
public $_scope;
public $_single;
public function __construct($value = null,bool $_scope = null,bool $_single = null,string $_id = null)
{
$this->injectArgParams(func_get_args(),'_id');
}
}
namespace app\behaviors;
use hehe\core\hcontainer\aop\base\PointcutContext;
class LogBehavior
{
// 默认调用方法
public function handle(PointcutContext $pointcutCtx)
{
$pointcutCtx->advice;// 通知点位置
$pointcutCtx->target;// 调用的对象
$pointcutCtx->method;// 被调用的方法
$pointcutCtx->parameters;// 被调用方法的参数
$pointcutCtx->methodResult;// 执行被调用方法后返回的结果
$pointcutCtx->exception;// 执行被调用方法后发生异常后,抛出的异常对象
}
// 方法1
public function handle1(PointcutContext $pointcutCtx)
{
// 业务行为代码
}
// 静态方法1
public static function handle2(PointcutContext $pointcutCtx)
{
// 业务行为代码
}
}
namespace app\beans;
use hehe\core\hcontainer\annotation\Bean;
use hehe\core\hcontainer\aop\annotation\Advice;
use hehe\core\hcontainer\aop\annotation\After;
use hehe\core\hcontainer\aop\annotation\Before;
use hehe\core\hcontainer\aop\annotation\Around;
use hehe\core\hcontainer\aop\annotation\AfterThrowing;
/**
* @bean("user")
* 匹配以"Action"结尾的方法名,并在调用目标方法之后切入"hcontainer\tests\common\LogBehavior@log"业务行为
* @After("hcontainer\tests\common\LogBehavior@log",pointcut=".+Action")
*/
class UserBean
{
/**
* 在执行方法之后执行“LogBehavior” 类的handle方法
* @After("hcontainer\tests\common\LogBehavior")
*/
public function doAfter($user,$msg = '')
{
return $msg;
}
/**
* 在执行方法之前执行“LogBehavior”类的handle方法
* @Before("hcontainer\tests\common\LogBehavior")
*/
public function doBefore($user,$msg = '')
{
return $msg;
}
/**
* 在执行方法之前与之后执行“LogBehavior”类的handle方法
* @Around("hcontainer\tests\common\LogBehavior")
*/
public function doAround($user,$msg = '')
{
return $msg;
}
/**
* 在执行方法时发生异常,则执行"LogBehavior"
* @AfterThrowing("hcontainer\tests\common\LogBehavior")
*/
public function doAfterThrowing($user,$msg = '')
{
throw new Exception($msg);
}
/**
* 在执行方法之后以对象的方式调用“LogBehavior”的"log"方法
* @After("hcontainer\tests\common\LogBehavior@handle1")
*/
public function doNewMethod($user,$msg = '')
{
return $msg;
}
/**
* 在执行方法之后以类的方式调用“LogBehavior”的静态"log"方法
* @After("hcontainer\tests\common\LogBehavior@@handle2")
*/
public function doNew2Method($user,$msg = '')
{
return $msg;
}
// 以下两方法被类切面拦截
public function do1Action($user,$msg = '')
{
return $msg;
}
public function do2Action($user,$msg = '')
{
return $msg;
}
}
class BeanProcessor extends AnnotationProcessor
{
//
}
namespace hehe\core\hcontainer\annotation;
use hehe\core\hcontainer\annotation\Annotation;
/**
* @Annotation("hehe\core\hcontainer\annotation\BeanProcessor")
*/
class Ref
{
public $ref;
public function __construct($value = null,bool $lazy = null,string $ref = null)
{
// 无需处理构造参数,调用injectArgParams将构造参数直接赋值给注解器属性
// 如需处理构造参数,通过$this->getArgParams(func_get_args(),'ref') 获取格式化后的构造参数
$this->injectArgParams(func_get_args(),'ref');
}
}
namespace admin\service;
use hehe\core\hcontainer\annotation\Bean;
use hehe\core\hcontainer\annotation\Ref;
/**
* @Bean(id="user")
*/
class User
{
public $name;
/**
* role 值为Role bean对象
* @Ref("Role");
*/
public $role;
}
namespace hehe\core\hcontainer\annotation;
use hehe\core\hcontainer\ann\base\BaseAnnotation;
use hehe\core\hcontainer\annotation\Annotation;
use Attribute;
#[Annotation("hehe\core\hcontainer\annotation\BeanProcessor")]
#[Attribute]
class Ref extends BaseAnnotation
{
public $ref;
public function __construct($value = null,bool $lazy = null,string $ref = null)
{
// 无需处理构造参数,调用injectArgParams将构造参数直接赋值给注解器属性
$this->injectArgParams(func_get_args(),'ref');
}
}
namespace admin\service;
use hehe\core\hcontainer\annotation\Bean;
use hehe\core\hcontainer\annotation\Ref;
#[Bean("user")]
class User
{
/**
* role值为Role bean对象
*/
#[Ref("role",_lazy:true)]
public $role;
#[After("hcontainer\\tests\common\LogBehavior@@log2")]
public function ok()
{
return 'ok';
}
}
use hehe\core\hcontainer\annotation\Bean;
/**
* @Bean("user")
* @Bean("user",_onProxy=>true)
* @Bean(_id="user")
*/
class User
{
}
use hehe\core\hcontainer\annotation\Proxy;
/**
* @Proxy()
*/
class User
{
}
use hehe\core\hcontainer\annotation\Ref;
class User
{
/**
* @Ref('role'),
* @Ref('role',_lazy=true)
*/
protected $role;
}
use hehe\core\hcontainer\aop\annotation\After;
class User
{
/**
* 注册用户
* @After("hcontainer\tests\common\LogBehavior")
*/
public function add(array $user)
{
return 'ok';
}
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.