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

orchestral / canvas-core / 15363389568

31 May 2025 11:50AM UTC coverage: 92.308% (+1.1%) from 91.176%
15363389568

Pull #14

github

crynobone
wip

Signed-off-by: Mior Muhammad Zaki <crynobone@gmail.com>
Pull Request #14: Move Workbench actions

53 of 57 new or added lines in 3 files covered. (92.98%)

84 of 91 relevant lines covered (92.31%)

1.64 hits per line

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

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

3
namespace Orchestra\Canvas\Core\Actions;
4

5
use Illuminate\Contracts\Filesystem\FileNotFoundException;
6
use Illuminate\Filesystem\Filesystem;
7
use Orchestra\Sidekick\Env;
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,
20
    ) {}
5✔
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
    {
32
        if (! \is_string($this->filename)) {
5✔
33
            throw new FileNotFoundException;
2✔
34
        }
35

36
        $this->writeVariables($variables, $this->filename, $overwrite);
3✔
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 \Illuminate\Contracts\Filesystem\FileNotFoundException
47
     */
48
    protected function writeVariables(array $variables, string $filename, bool $overwrite = false): void
49
    {
50
        if ($this->filesystem->missing($filename)) {
3✔
51
            throw new FileNotFoundException("The file [{$filename}] does not exist.");
1✔
52
        }
53

54
        $lines = explode(PHP_EOL, $this->filesystem->get($filename));
2✔
55

56
        foreach ($variables as $key => $value) {
2✔
57
            $lines = $this->addVariableToEnvContents($key, $value, $lines, $overwrite);
2✔
58
        }
59

60
        $this->filesystem->put($filename, implode(PHP_EOL, $lines));
2✔
61
    }
62

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

73
        $shouldQuote = \is_string($value) && preg_match('/^[a-zA-z0-9]+$/', $value) === 0;
2✔
74

75
        $lineToAddVariations = [
2✔
76
            $key.'='.(\is_string($value) ? '"'.addslashes($value).'"' : Env::encode($value)),
2✔
77
            $key.'='.(\is_string($value) ? "'".addslashes($value)."'" : Env::encode($value)),
2✔
78
            $key.'='.Env::encode($value),
2✔
79
        ];
2✔
80

81
        $lineToAdd = $shouldQuote ? $lineToAddVariations[0] : $lineToAddVariations[2];
2✔
82

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

87
        foreach ($envLines as $index => $line) {
2✔
88
            if (str_starts_with($line, $prefix)) {
2✔
89
                $lastPrefixIndex = $index;
2✔
90
            }
91

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

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

NEW
101
                return $envLines;
×
102
            }
103

104
            if (str_starts_with($line, $key.'=')) {
2✔
105
                if (! $overwrite) {
2✔
106
                    return $envLines;
1✔
107
                }
108

109
                $envLines[$index] = $lineToAdd;
1✔
110

111
                return $envLines;
1✔
112
            }
113
        }
114

115
        if ($lastPrefixIndex === -1) {
2✔
116
            if (\count($envLines) && $envLines[\count($envLines) - 1] !== '') {
1✔
117
                $envLines[] = '';
1✔
118
            }
119

120
            return array_merge($envLines, [$lineToAdd]);
1✔
121
        }
122

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