PhpCode.php 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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-2015 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id$
  21. */
  22. /** @see Zend_Serializer_Adapter_AdapterAbstract */
  23. require_once 'Zend/Serializer/Adapter/AdapterAbstract.php';
  24. /**
  25. * @category Zend
  26. * @package Zend_Serializer
  27. * @subpackage Adapter
  28. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  29. * @license http://framework.zend.com/license/new-bsd New BSD License
  30. */
  31. class Zend_Serializer_Adapter_PhpCode extends Zend_Serializer_Adapter_AdapterAbstract
  32. {
  33. /**
  34. * Serialize PHP using var_export
  35. *
  36. * @param mixed $value
  37. * @param array $opts
  38. * @return string
  39. */
  40. public function serialize($value, array $opts = array())
  41. {
  42. return var_export($value, true);
  43. }
  44. /**
  45. * Deserialize PHP string
  46. *
  47. * Warning: this uses eval(), and should likely be avoided.
  48. *
  49. * @param string $code
  50. * @param array $opts
  51. * @return mixed
  52. * @throws Zend_Serializer_Exception on eval error
  53. */
  54. public function unserialize($code, array $opts = array())
  55. {
  56. $eval = @eval('$ret=' . $code . ';');
  57. if ($eval === false) {
  58. $lastErr = error_get_last();
  59. require_once 'Zend/Serializer/Exception.php';
  60. throw new Zend_Serializer_Exception('eval failed: ' . $lastErr['message']);
  61. }
  62. return $ret;
  63. }
  64. }