The*_*ebs 49 php serialization closures exception
所以我不确定我要向你们展示什么,如果你需要更多代码,请不要犹豫,问:
所以这个方法将在我们的应用程序中为Zend设置initMailer:
protected function _initMailer()
{
if ('testing' !== APPLICATION_ENV) {
$this->bootstrap('Config');
$options = $this->getOptions();
$mail = new Zend_Application_Resource_Mail($options['mail']);
}elseif ('testing' === APPLICATION_ENV) {
//change the mail transport only if dev or test
if (APPLICATION_ENV <> 'production') {
$callback = function()
{
return 'ZendMail_' . microtime(true) .'.tmp';
};
$mail = new Zend_Mail_Transport_File(
array('path' => '/tmp/mail/',
'callback'=>$callback
)
);
Zend_Mail::setDefaultTransport($mail);
}
}
return $mail;
}
Run Code Online (Sandbox Code Playgroud)
你可以看到它所在的闭包.当我运行任何使用这段代码的测试时,我得到:
Exception: Serialization of 'Closure' is not allowed
Run Code Online (Sandbox Code Playgroud)
因此,与这种"封闭"有关的所有测试都失败了.所以我在这里问你们应该做些什么.
为了澄清上述情况,所有人都在说我们发送的任何电子邮件都要将有关该电子邮件的信息存储在文件的/ tmp/mail /目录下的文件夹中.
Bab*_*aba 38
显然匿名函数无法序列化.
例
$function = function () {
return "ABC";
};
serialize($function); // would throw error
Run Code Online (Sandbox Code Playgroud)
从你的代码中你使用Closure:
$callback = function () // <---------------------- Issue
{
return 'ZendMail_' . microtime(true) . '.tmp';
};
Run Code Online (Sandbox Code Playgroud)
解决方案1:替换为正常功能
例
function emailCallback() {
return 'ZendMail_' . microtime(true) . '.tmp';
}
$callback = "emailCallback" ;
Run Code Online (Sandbox Code Playgroud)
解决方案2:通过数组变量调用间接方法
如果你看一下 http://docs.mnkras.com/libraries_23rdparty_2_zend_2_mail_2_transport_2file_8php_source.html
public function __construct($options = null)
63 {
64 if ($options instanceof Zend_Config) {
65 $options = $options->toArray();
66 } elseif (!is_array($options)) {
67 $options = array();
68 }
69
70 // Making sure we have some defaults to work with
71 if (!isset($options['path'])) {
72 $options['path'] = sys_get_temp_dir();
73 }
74 if (!isset($options['callback'])) {
75 $options['callback'] = array($this, 'defaultCallback'); <- here
76 }
77
78 $this->setOptions($options);
79 }
Run Code Online (Sandbox Code Playgroud)
您可以使用相同的方法发送回调
$callback = array($this,"aMethodInYourClass");
Run Code Online (Sandbox Code Playgroud)
Ifn*_*not 22
PHP不允许直接关闭序列化.但你可以像PHP Super Closure一样使用powefull类:https://github.com/jeremeamia/super_closure
这个类非常简单易用,并且捆绑在队列管理器的laravel框架中.
从github文档:
$helloWorld = new SerializableClosure(function ($name = 'World') use ($greeting) {
echo "{$greeting}, {$name}!\n";
});
$serialized = serialize($helloWorld);
Run Code Online (Sandbox Code Playgroud)
Arj*_*anW 12
如前所述:开箱即用的封装无法序列化.
但是,使用__sleep(),__wakeup()神奇的方法和反思U可以手动进行关闭序列化.有关更多详细信息,请参阅extend-php-5-3-closures-with-serialization-and-reflection
这使用了反射和php函数eval.请注意,这可以开启CODE注入的可能性,因此请注意您要序列化的内容.
| 归档时间: |
|
| 查看次数: |
81308 次 |
| 最近记录: |