phar_metadata_write4.phpt 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. --TEST--
  2. Phar with object in metadata
  3. --EXTENSIONS--
  4. phar
  5. --INI--
  6. phar.require_hash=0
  7. phar.readonly=0
  8. --FILE--
  9. <?php
  10. class EchoesOnWakeup {
  11. public function __wakeup() {
  12. echo "In __wakeup " . spl_object_id($this) . "\n";
  13. }
  14. public function __destruct() {
  15. echo "In __destruct " . spl_object_id($this) . "\n";
  16. }
  17. }
  18. class ThrowsOnSerialize {
  19. public function __sleep() {
  20. throw new RuntimeException("In sleep");
  21. }
  22. }
  23. $fname = __DIR__ . '/' . basename(__FILE__, '.php') . '.phar.php';
  24. $pname = 'phar://' . $fname;
  25. $file = "<?php __HALT_COMPILER(); ?>";
  26. $files = array();
  27. $files['a'] = array('cont' => 'a', 'meta' => new EchoesOnWakeup());
  28. include 'files/phar_test.inc';
  29. foreach($files as $name => $cont) {
  30. var_dump(file_get_contents($pname.'/'.$name));
  31. }
  32. unset($files);
  33. $phar = new Phar($fname);
  34. echo "Loading metadata for 'a' without allowed_classes\n";
  35. var_dump($phar['a']->getMetadata(['allowed_classes' => []]));
  36. echo "Loading metadata for 'a' with allowed_classes\n";
  37. var_dump($phar['a']->getMetadata(['allowed_classes' => true]));
  38. unset($phar);
  39. // NOTE: Phar will use the cached value of metadata if setMetaData was called on that Phar path before.
  40. // Save the writes to the phar and use a different file path.
  41. $fname_new = "$fname.copy.php";
  42. copy($fname, $fname_new);
  43. $phar = new Phar($fname_new);
  44. echo "Loading metadata from 'a' from the new phar\n";
  45. var_dump($phar['a']->getMetadata());
  46. echo "Loading metadata from 'a' from the new phar with unserialize options\n";
  47. var_dump($phar['a']->getMetadata(['allowed_classes' => true]));
  48. // PharEntry->setMetaData will do the following:
  49. // 1. serialize, checking for exceptions
  50. // 2. free the original data, checking for exceptions or the data getting set from destructors or error handlers.
  51. // 3. set the new data.
  52. try {
  53. var_dump($phar['a']->setMetadata(new ThrowsOnSerialize()));
  54. } catch (RuntimeException $e) {
  55. echo "Caught {$e->getMessage()} at {$e->getFile()}:{$e->getLine()}\n";
  56. unset($e);
  57. }
  58. var_dump($phar['a']->getMetadata([]));
  59. var_dump($phar['a']->getMetadata(['allowed_classes' => false]));
  60. ?>
  61. --CLEAN--
  62. <?php
  63. unlink(__DIR__ . '/' . basename(__FILE__, '.clean.php') . '.phar.php');
  64. unlink(__DIR__ . '/' . basename(__FILE__, '.clean.php') . '.phar.php.copy.php');
  65. ?>
  66. --EXPECTF--
  67. In __destruct 1
  68. string(1) "a"
  69. Loading metadata for 'a' without allowed_classes
  70. object(__PHP_Incomplete_Class)#3 (1) {
  71. ["__PHP_Incomplete_Class_Name"]=>
  72. string(14) "EchoesOnWakeup"
  73. }
  74. Loading metadata for 'a' with allowed_classes
  75. In __wakeup 2
  76. object(EchoesOnWakeup)#2 (0) {
  77. }
  78. In __destruct 2
  79. Loading metadata from 'a' from the new phar
  80. In __wakeup 3
  81. object(EchoesOnWakeup)#3 (0) {
  82. }
  83. In __destruct 3
  84. Loading metadata from 'a' from the new phar with unserialize options
  85. In __wakeup 2
  86. object(EchoesOnWakeup)#2 (0) {
  87. }
  88. In __destruct 2
  89. Caught In sleep at %sphar_metadata_write4.php:12
  90. In __wakeup 3
  91. object(EchoesOnWakeup)#3 (0) {
  92. }
  93. In __destruct 3
  94. object(__PHP_Incomplete_Class)#4 (1) {
  95. ["__PHP_Incomplete_Class_Name"]=>
  96. string(14) "EchoesOnWakeup"
  97. }