Skip to content

Commit 0de68b3

Browse files
committed
update flush method to work with context folders
1 parent 081bb2c commit 0de68b3

2 files changed

Lines changed: 54 additions & 2 deletions

File tree

src/Handlers/FileHandler.php

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,20 +116,42 @@ public function forget(string $class, string $property, ?string $context = null)
116116
*/
117117
public function flush()
118118
{
119-
// Get all settings files
119+
// Delete all .php files in main directory (null context files)
120120
$files = glob($this->path . '*.php', GLOB_NOSORT);
121121

122122
if ($files === false) {
123123
throw new RuntimeException('Unable to read settings directory: ' . $this->path);
124124
}
125125

126-
// Delete all files
127126
foreach ($files as $file) {
128127
if (! unlink($file)) {
129128
throw new RuntimeException('Unable to delete settings file: ' . $file);
130129
}
131130
}
132131

132+
// Delete all context subdirectories and their contents
133+
$directories = glob($this->path . '*', GLOB_ONLYDIR | GLOB_NOSORT);
134+
135+
if ($directories !== false) {
136+
foreach ($directories as $directory) {
137+
// Delete all files inside the directory
138+
$contextFiles = glob($directory . '/*.php', GLOB_NOSORT);
139+
140+
if ($contextFiles !== false) {
141+
foreach ($contextFiles as $file) {
142+
if (! unlink($file)) {
143+
throw new RuntimeException('Unable to delete settings file: ' . $file);
144+
}
145+
}
146+
}
147+
148+
// Remove the empty directory
149+
if (! rmdir($directory)) {
150+
throw new RuntimeException('Unable to delete directory: ' . $directory);
151+
}
152+
}
153+
}
154+
133155
// Clear local storage and hydration tracking
134156
parent::flush();
135157
$this->hydrated = [];

tests/FileHandlerTest.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,36 @@ public function testFlushRemovesAllFiles()
188188
$this->assertSame('Settings Test', $this->settings->get('Example.siteName'));
189189
}
190190

191+
public function testFlushRemovesFilesAndContextDirectories()
192+
{
193+
// Create files in main directory (null context)
194+
$this->settings->set('Example.siteName', 'Main');
195+
$this->settings->set('Example.siteEmail', 'main@example.com');
196+
197+
// Create files in context subdirectories
198+
$this->settings->set('Example.siteName', 'Production', 'production');
199+
$this->settings->set('Example.siteTitle', 'Prod Site', 'production');
200+
$this->settings->set('Example.siteName', 'Testing', 'testing');
201+
202+
$mainFiles = glob($this->path . '*.php', GLOB_NOSORT);
203+
$this->assertNotEmpty($mainFiles);
204+
205+
$directories = glob($this->path . '*', GLOB_ONLYDIR | GLOB_NOSORT);
206+
$this->assertCount(2, $directories); // production and testing
207+
208+
$this->settings->flush();
209+
210+
$mainFiles = glob($this->path . '*.php', GLOB_NOSORT);
211+
$this->assertEmpty($mainFiles);
212+
213+
$directories = glob($this->path . '*', GLOB_ONLYDIR | GLOB_NOSORT);
214+
$this->assertEmpty($directories);
215+
216+
// Should be back to default values
217+
$this->assertSame('Settings Test', $this->settings->get('Example.siteName'));
218+
$this->assertSame('Settings Test', $this->settings->get('Example.siteName', 'production'));
219+
}
220+
191221
public function testSetWithContext()
192222
{
193223
$this->settings->set('Example.siteName', 'Banana', 'environment:test');

0 commit comments

Comments
 (0)