web-dev-qa-db-ja.com

例外: 'Closure'のシリアル化は許可されていません

だから私はあなたにみんなに見せなければならないものが正確にはわからない、あなたがより多くのコードが必要な場合はいつでも尋ねることをheしないでください:

したがって、このメソッドは、アプリケーションで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;
}

クロージャーが表示されています。このコードを使用するテストを実行すると、次のようになります。

Exception: Serialization of 'Closure' is not allowed 

したがって、この「閉鎖」に関連するすべてのテストは失敗します。だから私はここで皆さんに私がすべきことを尋ねています。

上記を明確にするために、私たちが送信するすべての電子メールは、その電子メールに関する情報をファイルの/ tmp/mail /ディレクトリ内のフォルダーに保存することを言っているだけです。

48
TheWebs

明らかに匿名関数はシリアル化できません。

$function = function () {
    return "ABC";
};
serialize($function); // would throw error

クロージャーを使用しているコードから:

$callback = function () // <---------------------- Issue
{
    return 'ZendMail_' . microtime(true) . '.tmp';
};

解決策1:通常の関数に置き換えます

function emailCallback() {
    return 'ZendMail_' . microtime(true) . '.tmp';
}
$callback = "emailCallback" ;

解決策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     }

同じアプローチを使用してコールバックを送信できます

$callback = array($this,"aMethodInYourClass");
38
Baba

PHPでは、直接クロージャのシリアル化は許可されていません。ただし、PHP Super Closure: https://github.com/jeremeamia/super_closure のようなpowefullクラスを使用できます。

このクラスは非常に使いやすく、キューマネージャーのlaravelフレームワークにバンドルされています。

Githubドキュメントから:

$helloWorld = new SerializableClosure(function ($name = 'World') use ($greeting) {
    echo "{$greeting}, {$name}!\n";
});

$serialized = serialize($helloWorld);
22
Ifnot

すでに述べたように、すぐに使用できるクロージャーはシリアル化できません。

ただし、__sleep()__wakeup()マジックメソッドおよびリフレクションを使用すると、手動でクロージャーをシリアル化できます。詳細については、 extending-php-5-3-closures-with-serialization-and-reflection を参照してください

これはリフレクションとphp関数evalを利用します。これによりCODEインジェクションの可能性が開かれることに注意してください。したがって、シリアル化する内容に注意してください。

12
ArjanW

グローバルを無効にする必要があります

 /**
 * @backupGlobals disabled
 */
3
Sampath Perera