|
1 <?php |
|
2 /** |
|
3 * Zend Framework |
|
4 * |
|
5 * LICENSE |
|
6 * |
|
7 * This source file is subject to the new BSD license that is bundled |
|
8 * with this package in the file LICENSE.txt. |
|
9 * It is also available through the world-wide-web at this URL: |
|
10 * http://framework.zend.com/license/new-bsd |
|
11 * If you did not receive a copy of the license and are unable to |
|
12 * obtain it through the world-wide-web, please send an email |
|
13 * to license@zend.com so we can send you a copy immediately. |
|
14 * |
|
15 * @category Zend |
|
16 * @package Zend_Serializer |
|
17 * @subpackage Adapter |
|
18 * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) |
|
19 * @license http://framework.zend.com/license/new-bsd New BSD License |
|
20 * @version $Id: PhpCode.php 20574 2010-01-24 17:39:14Z mabe $ |
|
21 */ |
|
22 |
|
23 /** @see Zend_Serializer_Adapter_AdapterAbstract */ |
|
24 require_once 'Zend/Serializer/Adapter/AdapterAbstract.php'; |
|
25 |
|
26 /** |
|
27 * @category Zend |
|
28 * @package Zend_Serializer |
|
29 * @subpackage Adapter |
|
30 * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) |
|
31 * @license http://framework.zend.com/license/new-bsd New BSD License |
|
32 */ |
|
33 class Zend_Serializer_Adapter_PhpCode extends Zend_Serializer_Adapter_AdapterAbstract |
|
34 { |
|
35 /** |
|
36 * Serialize PHP using var_export |
|
37 * |
|
38 * @param mixed $value |
|
39 * @param array $opts |
|
40 * @return string |
|
41 */ |
|
42 public function serialize($value, array $opts = array()) |
|
43 { |
|
44 return var_export($value, true); |
|
45 } |
|
46 |
|
47 /** |
|
48 * Deserialize PHP string |
|
49 * |
|
50 * Warning: this uses eval(), and should likely be avoided. |
|
51 * |
|
52 * @param string $code |
|
53 * @param array $opts |
|
54 * @return mixed |
|
55 * @throws Zend_Serializer_Exception on eval error |
|
56 */ |
|
57 public function unserialize($code, array $opts = array()) |
|
58 { |
|
59 $eval = @eval('$ret=' . $code . ';'); |
|
60 if ($eval === false) { |
|
61 $lastErr = error_get_last(); |
|
62 require_once 'Zend/Serializer/Exception.php'; |
|
63 throw new Zend_Serializer_Exception('eval failed: ' . $lastErr['message']); |
|
64 } |
|
65 return $ret; |
|
66 } |
|
67 } |