3 * This file is part of the GlobalState package.
5 * (c) Sebastian Bergmann <sebastian@phpunit.de>
7 * For the full copyright and license information, please view the LICENSE
8 * file that was distributed with this source code.
11 namespace SebastianBergmann\GlobalState;
14 * Exports parts of a Snapshot as PHP code.
19 * @param Snapshot $snapshot
22 public function constants(Snapshot $snapshot)
26 foreach ($snapshot->constants() as $name => $value) {
28 'if (!defined(\'%s\')) define(\'%s\', %s);' . "\n",
31 $this->exportVariable($value)
39 * @param Snapshot $snapshot
42 public function iniSettings(Snapshot $snapshot)
46 foreach ($snapshot->iniSettings() as $key => $value) {
48 '@ini_set(%s, %s);' . "\n",
49 $this->exportVariable($key),
50 $this->exportVariable($value)
58 * @param mixed $variable
61 private function exportVariable($variable)
63 if (is_scalar($variable) || is_null($variable) ||
64 (is_array($variable) && $this->arrayOnlyContainsScalars($variable))) {
65 return var_export($variable, true);
68 return 'unserialize(' . var_export(serialize($variable), true) . ')';
75 private function arrayOnlyContainsScalars(array $array)
79 foreach ($array as $element) {
80 if (is_array($element)) {
81 $result = self::arrayOnlyContainsScalars($element);
82 } elseif (!is_scalar($element) && !is_null($element)) {
86 if ($result === false) {