Subversion-Projekte lars-tiefland.laravel_shop

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

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