• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

orchestral / testbench-core / 15022240188

14 May 2025 01:39PM UTC coverage: 89.761% (-2.9%) from 92.612%
15022240188

Pull #347

github

web-flow
Merge e5cc2f891 into b5e6e18eb
Pull Request #347: Add `Orchestra\Testbench\Foundation\Console\WriteEnvironmentVariable` action

0 of 49 new or added lines in 1 file covered. (0.0%)

1429 of 1592 relevant lines covered (89.76%)

62.83 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/src/Foundation/Console/Actions/WriteEnvironmentVariable.php
1
<?php
2

3
namespace Orchestra\Testbench\Foundation\Console\Actions;
4

5
use Illuminate\Console\View\Components\Factory as ComponentsFactory;
6
use Illuminate\Filesystem\Filesystem;
7
use RuntimeException;
8

9
/**
10
 * @api
11
 */
12
class WriteEnvironmentVariable
13
{
14
    /**
15
     * Construct a new action instance.
16
     *
17
     * @param  \Illuminate\Filesystem\Filesystem  $filesystem
18
     * @param  \Illuminate\Console\View\Components\Factory|null  $components
19
     * @param  bool  $force
20
     * @param  string|null  $workingPath
21
     */
22
    public function __construct(
23
        public Filesystem $filesystem,
24
        public ?ComponentsFactory $components = null,
25
        public bool $force = false,
26
        public ?string $workingPath = null
NEW
27
    ) {}
×
28

29
    /**
30
     * Handle the action.
31
     *
32
     * @param  array<string, mixed>  $variables
33
     * @param  string|false|null  $filename
34
     * @param  bool  $overwrite
35
     * @return void
36
     *
37
     * @throws \RuntimeException
38
     * @throws \Illuminate\Contracts\Filesystem\FileNotFoundException
39
     */
40
    public function handle(array $variables, string|false|null $filename, bool $overwrite = false): void
41
    {
NEW
42
        if (! \is_string($filename)) {
×
NEW
43
            return;
×
44
        }
45

NEW
46
        $this->writeVariables($variables, $filename, $overwrite);
×
47
    }
48

49
    /**
50
     * Write an array of key-value pairs to the environment file.
51
     *
52
     * @laravel-overrides
53
     *
54
     * @param  array<string, mixed>  $variables
55
     * @param  string  $filename
56
     * @param  bool  $overwrite
57
     * @return void
58
     *
59
     * @throws \RuntimeException
60
     * @throws \Illuminate\Contracts\Filesystem\FileNotFoundException
61
     */
62
    protected function writeVariables(array $variables, string $filename, bool $overwrite = false): void
63
    {
NEW
64
        if ($this->filesystem->missing($filename)) {
×
NEW
65
            throw new RuntimeException("The file [{$filename}] does not exist.");
×
66
        }
67

NEW
68
        $lines = explode(PHP_EOL, $this->filesystem->get($filename));
×
69

NEW
70
        foreach ($variables as $key => $value) {
×
NEW
71
            $lines = $this->addVariableToEnvContents($key, $value, $lines, $overwrite);
×
72
        }
73

NEW
74
        $this->filesystem->put($filename, implode(PHP_EOL, $lines));
×
75
    }
76

77
    /**
78
     * Write a single key-value pair to the environment file.
79
     *
80
     * @laravel-overrides
81
     *
82
     * @param  string  $key
83
     * @param  mixed  $value
84
     * @param  string  $filename
85
     * @param  bool  $overwrite
86
     * @return void
87
     *
88
     * @throws \RuntimeException
89
     * @throws \Illuminate\Contracts\Filesystem\FileNotFoundException
90
     */
91
    protected function writeVariable(string $key, mixed $value, string $filename, bool $overwrite = false): void
92
    {
NEW
93
        if ($this->filesystem->missing($filename)) {
×
NEW
94
            throw new RuntimeException("The file [{$filename}] does not exist.");
×
95
        }
96

NEW
97
        $envContent = $this->filesystem->get($filename);
×
98

NEW
99
        $lines = explode(PHP_EOL, $envContent);
×
NEW
100
        $lines = $this->addVariableToEnvContents($key, $value, $lines, $overwrite);
×
101

NEW
102
        $this->filesystem->put($filename, implode(PHP_EOL, $lines));
×
103
    }
104

105
    /**
106
     * Add a variable to the environment file contents.
107
     *
108
     * @laravel-overrides
109
     *
110
     * @param  string  $key
111
     * @param  mixed  $value
112
     * @param  array  $envLines
113
     * @param  bool  $overwrite
114
     * @return array
115
     */
116
    protected function addVariableToEnvContents(string $key, mixed $value, array $envLines, bool $overwrite): array
117
    {
NEW
118
        $prefix = explode('_', $key)[0].'_';
×
NEW
119
        $lastPrefixIndex = -1;
×
120

NEW
121
        $shouldQuote = preg_match('/^[a-zA-z0-9]+$/', $value) === 0;
×
122

NEW
123
        $lineToAddVariations = [
×
NEW
124
            $key.'='.(\is_string($value) ? '"'.addslashes($value).'"' : $value),
×
NEW
125
            $key.'='.(\is_string($value) ? "'".addslashes($value)."'" : $value),
×
NEW
126
            $key.'='.$value,
×
NEW
127
        ];
×
128

NEW
129
        $lineToAdd = $shouldQuote ? $lineToAddVariations[0] : $lineToAddVariations[2];
×
130

NEW
131
        if ($value === '') {
×
NEW
132
            $lineToAdd = $key.'=';
×
133
        }
134

NEW
135
        foreach ($envLines as $index => $line) {
×
NEW
136
            if (str_starts_with($line, $prefix)) {
×
NEW
137
                $lastPrefixIndex = $index;
×
138
            }
139

NEW
140
            if (\in_array($line, $lineToAddVariations)) {
×
141
                // This exact line already exists, so we don't need to add it again.
NEW
142
                return $envLines;
×
143
            }
144

NEW
145
            if ($line === $key.'=') {
×
146
                // If the value is empty, we can replace it with the new value.
NEW
147
                $envLines[$index] = $lineToAdd;
×
148

NEW
149
                return $envLines;
×
150
            }
151

NEW
152
            if (str_starts_with($line, $key.'=')) {
×
NEW
153
                if (! $overwrite) {
×
NEW
154
                    return $envLines;
×
155
                }
156

NEW
157
                $envLines[$index] = $lineToAdd;
×
158

NEW
159
                return $envLines;
×
160
            }
161
        }
162

NEW
163
        if ($lastPrefixIndex === -1) {
×
NEW
164
            if (\count($envLines) && $envLines[\count($envLines) - 1] !== '') {
×
NEW
165
                $envLines[] = '';
×
166
            }
167

NEW
168
            return array_merge($envLines, [$lineToAdd]);
×
169
        }
170

NEW
171
        return array_merge(
×
NEW
172
            \array_slice($envLines, 0, $lastPrefixIndex + 1),
×
NEW
173
            [$lineToAdd],
×
NEW
174
            \array_slice($envLines, $lastPrefixIndex + 1)
×
NEW
175
        );
×
176
    }
177
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc