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