Extending PHP 5.3 Closures with Serialization and Reflection[CP]
php  /  管理员 发布于 4年前   248
01.class SuperClosure {02. 03.protected $closure = NULL;04.protected $reflection = NULL;05. 06.public function __construct($function)07.{08.if ( ! $function instanceOf Closure)09.throw new InvalidArgumentException();10. 11.$this->closure = $function;12.$this->reflection = new ReflectionFunction($function);13.}14. 15.public function __invoke()16.{17.$args = func_get_args();18.return $this->reflection->invokeArgs($args);19.}20. 21.public function getClosure()22.{23.return $this->closure;24.}25.}
01.protected function _fetchCode()02.{03.// Open file and seek to the first line of the closure04.$file = new SplFileObject($this->reflection->getFileName());05.$file->seek($this->reflection->getStartLine()-1);06. 07.// Retrieve all of the lines that contain code for the closure08.$code = '';09.while ($file->key() < $this->reflection->getEndLine())10.{11.$code .= $file->current();12.$file->next();13.}14. 15.// Only keep the code defining that closure16.$begin = strpos($code, 'function');17.$end = strrpos($code, '}');18.$code = substr($code, $begin, $end - $begin + 1);19. 20.return $code;21.}
01.protected function _fetchUsedVariables()02.{03.// Make sure the use construct is actually used04.$use_index = stripos($this->code, 'use');05.if ( ! $use_index)06.return array();07. 08.// Get the names of the variables inside the use statement09.$begin = strpos($this->code, '(', $use_index) + 1;10.$end = strpos($this->code, ')', $begin);11.$vars = explode(',', substr($this->code, $begin, $end - $begin));12. 13.// Get the static variables of the function via reflection14.$static_vars = $this->reflection->getStaticVariables();15. 16.// Only keep the variables that appeared in both sets17.$used_vars = array();18.foreach ($vars as $var)19.{20.$var = trim($var, ' $&');21.$used_vars[$var] = $static_vars[$var];22.}23. 24.return $used_vars;25.}
001.class SuperClosure {002. 003.protected $closure = NULL;004.protected $reflection = NULL;005.protected $code = NULL;006.protected $used_variables = array();007. 008.public function __construct($function)009.{010.if ( ! $function instanceOf Closure)011.throw new InvalidArgumentException();012. 013.$this->closure = $function;014.$this->reflection = new ReflectionFunction($function);015.$this->code = $this->_fetchCode();016.$this->used_variables = $this->_fetchUsedVariables();017.}018. 019.public function __invoke()020.{021.$args = func_get_args();022.return $this->reflection->invokeArgs($args);023.}024. 025.public function getClosure()026.{027.return $this->closure;028.}029. 030.protected function _fetchCode()031.{032.// Open file and seek to the first line of the closure033.$file = new SplFileObject($this->reflection->getFileName());034.$file->seek($this->reflection->getStartLine()-1);035. 036.// Retrieve all of the lines that contain code for the closure037.$code = '';038.while ($file->key() < $this->reflection->getEndLine())039.{040.$code .= $file->current();041.$file->next();042.}043. 044.// Only keep the code defining that closure045.$begin = strpos($code, 'function');046.$end = strrpos($code, '}');047.$code = substr($code, $begin, $end - $begin + 1);048. 049.return $code;050.}051. 052.public function getCode()053.{054.return $this->code;055.}056. 057.public function getParameters()058.{059.return $this->reflection->getParameters();060.}061. 062.protected function _fetchUsedVariables()063.{064.// Make sure the use construct is actually used065.$use_index = stripos($this->code, 'use');066.if ( ! $use_index)067.return array();068. 069.// Get the names of the variables inside the use statement070.$begin = strpos($this->code, '(', $use_index) + 1;071.$end = strpos($this->code, ')', $begin);072.$vars = explode(',', substr($this->code, $begin, $end - $begin));073. 074.// Get the static variables of the function via reflection075.$static_vars = $this->reflection->getStaticVariables();076. 077.// Only keep the variables that appeared in both sets078.$used_vars = array();079.foreach ($vars as $var)080.{081.$var = trim($var, ' $&');082.$used_vars[$var] = $static_vars[$var];083.}084. 085.return $used_vars;086.}087. 088.public function getUsedVariables()089.{090.return $this->used_variables;091.}092. 093.public function __sleep()094.{095.return array('code', 'used_variables');096.}097. 098.public function __wakeup()099.{100.extract($this->used_variables);101. 102.eval('$_function = '.$this->code.';');103.if (isset($_function) AND $_function instanceOf Closure)104.{105.$this->closure = $_function;106.$this->reflection = new ReflectionFunction($_function);107.}108.else109.throw new Exception();110.}111.}
123 在
Clash for Windows作者删库跑路了,github已404中评论 按理说只要你在国内,所有的流量进出都在监控范围内,不管你怎么隐藏也没用,想搞你分..原梓番博客 在
在Laravel框架中使用模型Model分表最简单的方法中评论 好久好久都没看友情链接申请了,今天刚看,已经添加。..博主 在
佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 @1111老铁这个不行了,可以看看近期评论的其他文章..1111 在
佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 网站不能打开,博主百忙中能否发个APP下载链接,佛跳墙或极光..路人 在
php中使用hyperf框架调用讯飞星火大模型实现国内版chatgpt功能示例中评论 教程很详细,如果加个前端chatgpt对话页面就完美了..Copyright·© 2019 侯体宗版权所有· 粤ICP备20027696号