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

orchestral / testbench-core / 15022132429

14 May 2025 01:34PM UTC coverage: 89.705% (-2.9%) from 92.612%
15022132429

Pull #347

github

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

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

1429 of 1593 relevant lines covered (89.7%)

62.79 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
        $filesystem = new Filesystem;
×
65

NEW
66
        if ($this->filesystem->missing($filename)) {
×
NEW
67
            throw new RuntimeException("The file [{$filename}] does not exist.");
×
68
        }
69

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

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

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

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

NEW
99
        $envContent = $this->filesystem->get($filename);
×
100

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

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

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

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

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

NEW
131
        $lineToAdd = $shouldQuote ? $lineToAddVariations[0] : $lineToAddVariations[2];
×
132

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

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

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

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

NEW
151
                return $envLines;
×
152
            }
153

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

NEW
159
                $envLines[$index] = $lineToAdd;
×
160

NEW
161
                return $envLines;
×
162
            }
163
        }
164

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

NEW
170
            return array_merge($envLines, [$lineToAdd]);
×
171
        }
172

NEW
173
        return array_merge(
×
NEW
174
            \array_slice($envLines, 0, $lastPrefixIndex + 1),
×
NEW
175
            [$lineToAdd],
×
NEW
176
            \array_slice($envLines, $lastPrefixIndex + 1)
×
NEW
177
        );
×
178
    }
179
}
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