grithin / phpbase
PHP 基础工具。
5.0.9
2021-07-20 16:27 UTC
Requires
- php: >=7
- ext-json: *
- ext-mbstring: *
README
提供其他 grithin/php* 项目使用的 PHP 工具。
工具
数组
# get arbitrarily deep item using a path (like lodash.get) $user_input = [ 'name'=> [ 'first'=>'bob', 'last'=>'bobl', ] ]; $x = Arrays::get($user_input, 'name.first'); #> 'bob' #+ flatten array picking one value { $user_input = [ 'name'=> [ 'first'=>'bob', 'last'=>'bobl', ] ]; $x = Arrays::flatten_values($user_input); ppe($x); #> {"name": "bob"} #+ } #+ flatten structured array { $user_input = [ 'name'=> [ 'first'=>'bob', 'last'=>'bobl', ] ]; $x = Arrays::flatten($user_input); ppe($x); /* {"name_first": "bob", "name_last": "bobl"} */ #+ } #+ pick and ensure $user_input = [ 'first_name'=>'bob', 'last_name'=>'bobl', 'unwanted'=>'blah' ]; $pick = ['first_name', 'last_name', 'middle_name']; $x = Arrays::pick_default($user_input, $pick, 'N/A'); /* {"first_name": "bob", "last_name": "bobl", "middle_name": "N\/A"} */ #+ } #+ rekey and exclude { $user_input = [ 'first_name'=>'bob', 'last_name'=>'bobl', 'unwanted'=>'blah' ]; $map = ['first_name'=>'FirstName', 'last_name'=>'LastName']; $x = Arrays::map_only($user_input, $map); ppe($x); /* {"FirstName": "bob", "LastName": "bobl"} */ #+ }
文件
主要功能是文件包含函数 inc
、req
、incOnce
、reqOnce
。它们允许包含跟踪、上下文隔离、变量注入、变量提取和全局注入。
示例文件 bob.php
$bill = [$bob] $bill[] = 'monkey' return 'blue'
用法
Files::inc('bob.php') #< 'blue' # Using variable injection and variable extraction Files::inc('bob.php',['bob'=>'sue'], ['extract'=>['bill']]) #< ['sue', 'monkey']
Bench
简单的基准测试工具
$bench = new \Grithin\Bench(); for($i=0; $i<10000; $i++){ mt_rand(0,100000) + mt_rand(0,100000); } $bench->mark(); for($i=0; $i<10000; $i++){ mt_rand(0,100000) + mt_rand(0,100000); } $bench->end_out();
...
"value": {
"intervals": [
{
"time": 0.0035231113433838,
"mem.change": 808
},
{
"time": 0.0028860569000244,
"mem.change": 776
}
],
"summary": {
"mem": {
"start": 403168,
"end": 404752,
"diff": 1584
},
"time": 0.0064091682434082
}
}
...
Memoized
Memoized 特性添加了可以解释方法调用的魔法方法,如果调用以 memoize_
或 memoized_
前缀开头,Memoized 将处理缓存。
class Test{ use \Grithin\Traits\Memoized; public function random(){ return microtime() } } $Test = new Test; $x = $Test->memoized_random(); $y = $Test->memoized_random(); $x == $y; # > true
如果需要重新缓存已缓存的函数,可以将其前缀为 memoize_
# ... $x = $Test->memoized_random(); $y = $Test->memoize_random(); $x == $y; # > false
Memoized 将函数参数作为键,因此如果函数参数不同,它们将生成不同的缓存 - 而这与使用的参数无关。
# ... $x = $Test->memoized_random(1); $y = $Test->memoized_random(2); $x == $y; # > false
Memoized 可以与静态方法一起使用
class Test{ use \Grithin\Traits\Memoized; static function random(){ return microtime(); } } $x = Test::memoized_random(); $y = Test::memoized_random(); $x == $y; # > true
在某些情况下,可能希望知道一个方法是否目前正在缓存。假设我们有一个返回用户数据的 user_get
函数,在该函数内部还有一个返回与用户相关联的位置数据的 location_get
函数。假设有两个场景需要从 user_get 获取输出
- 需要最新
user_get
数据的代码 - 可以预期
user_get
调用在其运行期间是相同数据的代码
Memoized 提供了一种检查当前函数是否在缓存链中的方法。例如,对于级别缓存 $this->memoizing()
;对于静态缓存,Memoized::static_memoizing()
。
一个可能看起来是这样的 user_get
class User{ use \Grithin\Traits\Memoized; public user_get($id){ $user_data = Db::get('...'); if($this->memoizing()){ # function has been called with a memoize_ or memoized_ prefix $location = Location::memoized_get($user_data['location']); }else{ $location = Location::get($user_data['location']); } } }
最后,一个函数可以使用 ->caller_requested_memoized()
和 ::static_caller_requested_memoized()
检查它是否被直接调用以进行缓存。
调试
错误处理和打印输出
错误处理设置
# optionally configure Debug::configure([ 'log_file'=>__DIR__.'/log/'.date('Ymd').'.log', 'err_file'=>__DIR__.'/log/'.date('Ymd').'.err', ]); set_error_handler(['\Grithin\Debug','handleError'], E_ALL & ~E_DEPRECATED & ~E_STRICT & ~E_NOTICE); set_exception_handler(['\Grithin\Debug','handleException']);
输出
打印任何变量
$bob = ['bob']; $sue = ['sue']; $bob[] = &$sue; $sue[] = &$bob; $sue[] = 'moe'; Debug::out($sue); echo 'bob';
输出
{
"file": "\/test.php",
"line": 37,
"i": 1,
"value": []
}bob
如果在 Web 服务器中运行,它将使用 <pre>
封装
Debug::quit();
将执行 ::out()
然后 exit
。