定制 raphhh/trex-reflection 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

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

raphhh/trex-reflection

Composer 安装命令:

composer require raphhh/trex-reflection

包简介

Reflection helpers for callables and types

README 文档

README

Latest Stable Version Build Status Scrutinizer Quality Score Code Coverage Dependency Status Total Downloads Reference Status License

PHP tool to reflect callables an types.

Installation

Use composer command:

$ composer require raphhh/trex-reflection

Documentation

CallableReflection

You can use CallableReflection to inspect and call a callable, like a callback or a Closure for example.

All kind of callable

You can know which kind of callable is given.

Closure
$reflect = new CallableReflection(function(){});
$reflect->isClosure(); //true
Function
$reflect = CallableReflection('in_array')
$reflect->isFunction(); //true
Static method
$reflect = new CallableReflection('\DateTime::createFromFormat');
$reflect->isMethod(); //true
$reflect->isStaticMethod(); //true
$reflect = new CallableReflection(array('\DateTime', 'createFromFormat'));
$reflect->isMethod(); //true
$reflect->isStaticMethod(); //true
Instance method
$reflect = new CallableReflection(array(new \DateTime(), 'modify'));
$reflect->isMethod(); //true
$reflect->isInstanceMethod(); //true
Invoked object
class Bar{
    function __invoke(){}
}

$reflect = new CallableReflection(new Bar());
$reflect->isInvokedObject(); //true

Retrieve contexts

You can retrieve the callable, like the object or the method name for example.

Closure
$reflect = new CallableReflection(function(){});
$reflect->getClosure(); //closure
Function
$reflect = new CallableReflection('in_array')
$reflect->getFunctionName(); //'in_array'
Static method
$reflect = new CallableReflection('\DateTime::createFromFormat');
$reflect->getClassName(); //'DateTime'
$reflect->getMethodName(); //'createFromFormat'
$reflect = new CallableReflection(array('\DateTime', 'createFromFormat'));
$reflect->getClassName(); //'DateTime'
$reflect->getMethodName(); //'createFromFormat'
Instance method
$reflect = new CallableReflection(array(new \DateTime(), 'modify'));
$reflect->getClassName(); //'DateTime'
$reflect->getObject(); //DateTime instance
$reflect->getMethodName(); //'modify'
Invoked object
class Bar{
    function __invoke(){}
}

$reflect = new CallableReflection(new Bar());
$reflect->getClassName(); //'Bar'
$reflect->getObject(); //Bar instance

Invoke callable

You can invoke every kind of callable in a same way.

With a list of args

This method calls the method and give to it all its args.

$reflect = new CallableReflection('in_array')
$reflect->invoke(1, [0, 1]); //true

With an array of args

This method allows to map each value of an array with every params of a function. Useful to use dynamic args with func_get_args().

$reflect = new CallableReflection('in_array')
$reflect->invoke([1, [0, 1]]); //true

With a map of args

This method allows to map the keys of an array with the name of the params of a function. So, the order of the args has no importance.

$closure = function($arg1, $arg2){
    return [$arg1, $arg2];
}

$reflect = new CallableReflection($closure)
$reflect->invokeA(['arg2' => 'arg2', 'arg1' => 'arg1'])); //['arg1', 'arg2']

Retrieve the associated reflection class

Retrieve the ReflectionFunctionAbstract of the callable.

For a function or a closure
$reflect = new CallableReflection('in_array');
$reflect->getReflector(); //ReflectionFunction
For a method
$reflect = new CallableReflection(array('\DateTime', 'createFromFormat'));
$reflect->getReflector(); //ReflectionMethod
For a class

Note that for a class, we get a ReflectionMethod for the __invoke method of the current object, and not a ReflectionClass.

class Bar{
    function __invoke(){}
}

$reflect = new CallableReflection(new Bar());
$reflect->getReflector(); //ReflectionMethod

TypeReflection

Reflection on the type of a variable or function.

What is a type?

A type is a string defined by a specific value related to one of the PHP types.

$typeReflection = new TypeReflection('int');
$typeReflection->getType(); //"int"
$typeReflection->getStandardizedType(); //"integer"

If you work with a variable, you can use TypeReflection::createFromVariable($variable)

$foo = new stdClass();
$typeReflection = TypeReflection::createFromVariable($foo);
$typeReflection->getType(); //"stdClass"
$typeReflection->getStandardizedType(); //"object"

Note the TypeReflection is case insensitive.

Standardized types

TypeReflection standardizes types with following values:

  • void
  • mixed
  • null
  • boolean
  • string
  • integer
  • float
  • number
  • scalar
  • array
  • object
  • resource
  • callable
  • unknown type

Valid any type

Recognition
$typeReflection = new TypeReflection('string');
$typeReflection->isValid(); //true
$typeReflection = new TypeReflection('foo');
$typeReflection->isValid(); //false
boolean
$typeReflection = new TypeReflection('bool');
$typeReflection->isBoolean(); //true
$typeReflection = new TypeReflection('boolean');
$typeReflection->isBoolean(); //true
string
$typeReflection = new TypeReflection('string');
$typeReflection->isString(); //true
integer
$typeReflection = new TypeReflection('int');
$typeReflection->isInteger(); //true
$typeReflection = new TypeReflection('integer');
$typeReflection->isInteger(); //true
$typeReflection = new TypeReflection('long');
$typeReflection->isInteger(); //true
float
$typeReflection = new TypeReflection('float');
$typeReflection->isFloat(); //true
$typeReflection = new TypeReflection('double');
$typeReflection->isFloat(); //true
$typeReflection = new TypeReflection('real');
$typeReflection->isFloat(); //true
number

Any integer or float value.

scalar

Any boolean, string or number value.

array
$typeReflection = new TypeReflection('array');
$typeReflection->isArray(); //true
$typeReflection = new TypeReflection('int[]');
$typeReflection->isArray(); //true
object
$typeReflection = new TypeReflection('object');
$typeReflection->isObject(); //true
$typeReflection = new TypeReflection('Datetime');
$typeReflection->isObject(); //true
resource
$typeReflection = new TypeReflection('resource');
$typeReflection->isResource(); //true
callable
$typeReflection = new TypeReflection('callable');
$typeReflection->isCallable(); //true
void
$typeReflection = new TypeReflection('void');
$typeReflection->isVoid(); //true
null
$typeReflection = new TypeReflection('null');
$typeReflection->isNull(); //true
mixed
$typeReflection = new TypeReflection('mixed');
$typeReflection->isMixed(); //true

Retrieve a standard notation

$typeReflection = new TypeReflection('bool');
$typeReflection->getStandardizedType(); //boolean
$typeReflection = new TypeReflection('int');
$typeReflection->getStandardizedType(); //integer
$typeReflection = new TypeReflection('real');
$typeReflection->getStandardizedType(); //float
$typeReflection = new TypeReflection('int[]');
$typeReflection->getStandardizedType(); //array
$typeReflection = new TypeReflection('Datetime');
$typeReflection->getStandardizedType(); //object

raphhh/trex-reflection 适用场景与选型建议

raphhh/trex-reflection 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 343.88k 次下载、GitHub Stars 达 14, 最近一次更新时间为 2015 年 01 月 20 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「reflection」 「type」 「callable」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 raphhh/trex-reflection 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 raphhh/trex-reflection 我们能提供哪些服务?
定制开发 / 二次开发

基于 raphhh/trex-reflection 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 343.88k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 14
  • 点击次数: 25
  • 依赖项目数: 10
  • 推荐数: 0

GitHub 信息

  • Stars: 14
  • Watchers: 3
  • Forks: 3
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2015-01-20