Chris@0
|
1 <?php
|
Chris@0
|
2 /*
|
Chris@14
|
3 * This file is part of sebastian/global-state.
|
Chris@0
|
4 *
|
Chris@0
|
5 * (c) Sebastian Bergmann <sebastian@phpunit.de>
|
Chris@0
|
6 *
|
Chris@0
|
7 * For the full copyright and license information, please view the LICENSE
|
Chris@0
|
8 * file that was distributed with this source code.
|
Chris@0
|
9 */
|
Chris@0
|
10
|
Chris@14
|
11 declare(strict_types=1);
|
Chris@14
|
12
|
Chris@0
|
13 namespace SebastianBergmann\GlobalState;
|
Chris@0
|
14
|
Chris@0
|
15 /**
|
Chris@0
|
16 * Exports parts of a Snapshot as PHP code.
|
Chris@0
|
17 */
|
Chris@0
|
18 class CodeExporter
|
Chris@0
|
19 {
|
Chris@14
|
20 public function constants(Snapshot $snapshot): string
|
Chris@0
|
21 {
|
Chris@0
|
22 $result = '';
|
Chris@0
|
23
|
Chris@0
|
24 foreach ($snapshot->constants() as $name => $value) {
|
Chris@14
|
25 $result .= \sprintf(
|
Chris@0
|
26 'if (!defined(\'%s\')) define(\'%s\', %s);' . "\n",
|
Chris@0
|
27 $name,
|
Chris@0
|
28 $name,
|
Chris@0
|
29 $this->exportVariable($value)
|
Chris@0
|
30 );
|
Chris@0
|
31 }
|
Chris@0
|
32
|
Chris@0
|
33 return $result;
|
Chris@0
|
34 }
|
Chris@0
|
35
|
Chris@14
|
36 public function globalVariables(Snapshot $snapshot): string
|
Chris@14
|
37 {
|
Chris@14
|
38 $result = '$GLOBALS = [];' . PHP_EOL;
|
Chris@14
|
39
|
Chris@14
|
40 foreach ($snapshot->globalVariables() as $name => $value) {
|
Chris@14
|
41 $result .= \sprintf(
|
Chris@14
|
42 '$GLOBALS[%s] = %s;' . PHP_EOL,
|
Chris@14
|
43 $this->exportVariable($name),
|
Chris@14
|
44 $this->exportVariable($value)
|
Chris@14
|
45 );
|
Chris@14
|
46 }
|
Chris@14
|
47
|
Chris@14
|
48 return $result;
|
Chris@14
|
49 }
|
Chris@14
|
50
|
Chris@14
|
51 public function iniSettings(Snapshot $snapshot): string
|
Chris@0
|
52 {
|
Chris@0
|
53 $result = '';
|
Chris@0
|
54
|
Chris@0
|
55 foreach ($snapshot->iniSettings() as $key => $value) {
|
Chris@14
|
56 $result .= \sprintf(
|
Chris@0
|
57 '@ini_set(%s, %s);' . "\n",
|
Chris@0
|
58 $this->exportVariable($key),
|
Chris@0
|
59 $this->exportVariable($value)
|
Chris@0
|
60 );
|
Chris@0
|
61 }
|
Chris@0
|
62
|
Chris@0
|
63 return $result;
|
Chris@0
|
64 }
|
Chris@0
|
65
|
Chris@14
|
66 private function exportVariable($variable): string
|
Chris@0
|
67 {
|
Chris@14
|
68 if (\is_scalar($variable) || \is_null($variable) ||
|
Chris@14
|
69 (\is_array($variable) && $this->arrayOnlyContainsScalars($variable))) {
|
Chris@14
|
70 return \var_export($variable, true);
|
Chris@0
|
71 }
|
Chris@0
|
72
|
Chris@14
|
73 return 'unserialize(' . \var_export(\serialize($variable), true) . ')';
|
Chris@0
|
74 }
|
Chris@0
|
75
|
Chris@14
|
76 private function arrayOnlyContainsScalars(array $array): bool
|
Chris@0
|
77 {
|
Chris@0
|
78 $result = true;
|
Chris@0
|
79
|
Chris@0
|
80 foreach ($array as $element) {
|
Chris@14
|
81 if (\is_array($element)) {
|
Chris@0
|
82 $result = self::arrayOnlyContainsScalars($element);
|
Chris@14
|
83 } elseif (!\is_scalar($element) && !\is_null($element)) {
|
Chris@0
|
84 $result = false;
|
Chris@0
|
85 }
|
Chris@0
|
86
|
Chris@0
|
87 if ($result === false) {
|
Chris@0
|
88 break;
|
Chris@0
|
89 }
|
Chris@0
|
90 }
|
Chris@0
|
91
|
Chris@0
|
92 return $result;
|
Chris@0
|
93 }
|
Chris@0
|
94 }
|