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

orchestral / workbench / 15024607357

14 May 2025 03:20PM UTC coverage: 88.386% (-6.1%) from 94.516%
15024607357

Pull #93

github

web-flow
Merge f7eb78fa5 into 52584df60
Pull Request #93: Add `Orchestra\Workbench\Actions\WriteEnvironmentVariables`

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

586 of 663 relevant lines covered (88.39%)

16.18 hits per line

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

0.0
/src/Actions/WriteEnvironmentVariables.php
1
<?php
2

3
namespace Orchestra\Workbench\Actions;
4

5
use Illuminate\Contracts\Filesystem\FileNotFoundException;
6
use Illuminate\Filesystem\Filesystem;
7
use RuntimeException;
8

9
/**
10
 * @api
11
 */
12
class WriteEnvironmentVariables
13
{
14
    /**
15
     * Construct a new action instance.
16
     */
17
    public function __construct(
18
        public Filesystem $filesystem,
19
        public string|false|null $filename,
NEW
20
    ) {}
×
21

22
    /**
23
     * Handle the action.
24
     *
25
     * @param  array<string, mixed>  $variables
26
     *
27
     * @throws \RuntimeException
28
     * @throws \Illuminate\Contracts\Filesystem\FileNotFoundException
29
     */
30
    public function handle(array $variables, bool $overwrite = false): void
31
    {
NEW
32
        if (! \is_string($this->filename)) {
×
NEW
33
            throw new FileNotFoundException;
×
34
        }
35

NEW
36
        $this->writeVariables($variables, $this->filename, $overwrite);
×
37
    }
38

39
    /**
40
     * Write an array of key-value pairs to the environment file.
41
     *
42
     * @laravel-overrides
43
     *
44
     * @param  array<string, mixed>  $variables
45
     *
46
     * @throws \RuntimeException
47
     * @throws \Illuminate\Contracts\Filesystem\FileNotFoundException
48
     */
49
    protected function writeVariables(array $variables, string $filename, bool $overwrite = false): void
50
    {
NEW
51
        if ($this->filesystem->missing($filename)) {
×
NEW
52
            throw new RuntimeException("The file [{$filename}] does not exist.");
×
53
        }
54

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

NEW
57
        foreach ($variables as $key => $value) {
×
NEW
58
            $lines = $this->addVariableToEnvContents($key, $value, $lines, $overwrite);
×
59
        }
60

NEW
61
        $this->filesystem->put($filename, implode(PHP_EOL, $lines));
×
62
    }
63

64
    /**
65
     * Add a variable to the environment file contents.
66
     *
67
     * @laravel-overrides
68
     */
69
    protected function addVariableToEnvContents(string $key, mixed $value, array $envLines, bool $overwrite): array
70
    {
NEW
71
        $prefix = explode('_', $key)[0].'_';
×
NEW
72
        $lastPrefixIndex = -1;
×
73

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

NEW
76
        $lineToAddVariations = [
×
NEW
77
            $key.'='.(\is_string($value) ? '"'.addslashes($value).'"' : $value),
×
NEW
78
            $key.'='.(\is_string($value) ? "'".addslashes($value)."'" : $value),
×
NEW
79
            $key.'='.$value,
×
NEW
80
        ];
×
81

NEW
82
        $lineToAdd = $shouldQuote ? $lineToAddVariations[0] : $lineToAddVariations[2];
×
83

NEW
84
        if ($value === '') {
×
NEW
85
            $lineToAdd = $key.'=';
×
86
        }
87

NEW
88
        foreach ($envLines as $index => $line) {
×
NEW
89
            if (str_starts_with($line, $prefix)) {
×
NEW
90
                $lastPrefixIndex = $index;
×
91
            }
92

NEW
93
            if (\in_array($line, $lineToAddVariations)) {
×
94
                // This exact line already exists, so we don't need to add it again.
NEW
95
                return $envLines;
×
96
            }
97

NEW
98
            if ($line === $key.'=') {
×
99
                // If the value is empty, we can replace it with the new value.
NEW
100
                $envLines[$index] = $lineToAdd;
×
101

NEW
102
                return $envLines;
×
103
            }
104

NEW
105
            if (str_starts_with($line, $key.'=')) {
×
NEW
106
                if (! $overwrite) {
×
NEW
107
                    return $envLines;
×
108
                }
109

NEW
110
                $envLines[$index] = $lineToAdd;
×
111

NEW
112
                return $envLines;
×
113
            }
114
        }
115

NEW
116
        if ($lastPrefixIndex === -1) {
×
NEW
117
            if (\count($envLines) && $envLines[\count($envLines) - 1] !== '') {
×
NEW
118
                $envLines[] = '';
×
119
            }
120

NEW
121
            return array_merge($envLines, [$lineToAdd]);
×
122
        }
123

NEW
124
        return array_merge(
×
NEW
125
            \array_slice($envLines, 0, $lastPrefixIndex + 1),
×
NEW
126
            [$lineToAdd],
×
NEW
127
            \array_slice($envLines, $lastPrefixIndex + 1)
×
NEW
128
        );
×
129
    }
130
}
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