Skip to content

Commit c8ad145

Browse files
authored
Cleanup of the cache and log directory after kernel shutdown (#59)
* feat: Add cleanup after shutdown * docs(changelog): Add cleanup of cache and log directory * fix: Reorder requires * fix: Fix tests * fix: Fix CS * fix: Make tests compatible with older phpunit version
1 parent 25420f8 commit c8ad145

File tree

5 files changed

+60
-13
lines changed

5 files changed

+60
-13
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,12 @@
22

33
The change log describes what is "Added", "Removed", "Changed" or "Fixed" between each release.
44

5+
## 2.0.0
6+
7+
### Added
8+
9+
- Cleanup of the cache and log directory after kernel shutdown
10+
511
## 1.8.1
612

713
### Fixed

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
"require": {
1313
"php": "^7.2 || ^8.0",
1414
"symfony/dependency-injection": "^4.4 || ^5.3 || ^6.0",
15+
"symfony/filesystem": "^4.4 || ^5.3 || ^6.0",
1516
"symfony/framework-bundle": "^4.4 || ^5.3 || ^6.0",
1617
"symfony/http-kernel": "^4.4 || ^5.3 || ^6.0",
1718
"symfony/yaml": "^4.4 || ^5.3 || ^6.0"

src/TestKernel.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use Symfony\Component\Config\Loader\LoaderInterface;
99
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
1010
use Symfony\Component\DependencyInjection\ContainerBuilder;
11+
use Symfony\Component\Filesystem\Filesystem;
1112
use Symfony\Component\HttpKernel\Bundle\BundleInterface;
1213
use Symfony\Component\HttpKernel\Kernel;
1314
use Symfony\Component\Routing\Loader\Configurator\RoutingConfigurator;
@@ -179,4 +180,22 @@ protected function configureRoutes($routes): void
179180
$routes->import($routingFile);
180181
}
181182
}
183+
184+
public function shutdown(): void
185+
{
186+
parent::shutdown();
187+
188+
$cacheDirectory = $this->getCacheDir();
189+
$logDirectory = $this->getLogDir();
190+
191+
$filesystem = new Filesystem();
192+
193+
if ($filesystem->exists($cacheDirectory)) {
194+
$filesystem->remove($cacheDirectory);
195+
}
196+
197+
if ($filesystem->exists($logDirectory)) {
198+
$filesystem->remove($logDirectory);
199+
}
200+
}
182201
}

tests/Functional/BundleInitializationTest.php

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,7 @@
33
namespace Nyholm\BundleTest\Tests\Functional;
44

55
use Nyholm\BundleTest\TestKernel;
6-
use Symfony\Bundle\FrameworkBundle\FrameworkBundle;
76
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
8-
use Symfony\Component\HttpKernel\KernelInterface;
97

108
/**
119
* @author Tobias Nyholm <[email protected]>
@@ -17,17 +15,6 @@ protected static function getKernelClass(): string
1715
return TestKernel::class;
1816
}
1917

20-
protected static function createKernel(array $options = []): KernelInterface
21-
{
22-
/**
23-
* @var TestKernel $kernel
24-
*/
25-
$kernel = parent::createKernel($options);
26-
$kernel->addTestBundle(FrameworkBundle::class);
27-
28-
return $kernel;
29-
}
30-
3118
public function testRegisterBundle(): void
3219
{
3320
$kernel = self::bootKernel();
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
namespace Nyholm\BundleTest\Tests\Functional;
4+
5+
use Nyholm\BundleTest\TestKernel;
6+
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
7+
use Symfony\Component\Filesystem\Filesystem;
8+
9+
/**
10+
* @author Jason Schilling <[email protected]>
11+
*/
12+
class BundleShutdownTest extends KernelTestCase
13+
{
14+
protected static function getKernelClass(): string
15+
{
16+
return TestKernel::class;
17+
}
18+
19+
public function testCleanupTemporaryDirectories(): void
20+
{
21+
$kernel = self::bootKernel();
22+
$cacheDirectory = $kernel->getCacheDir();
23+
$logDirectory = $kernel->getLogDir();
24+
$filesystem = new Filesystem();
25+
26+
self::assertTrue($filesystem->exists($cacheDirectory));
27+
self::assertTrue($filesystem->exists($logDirectory));
28+
29+
self::ensureKernelShutdown();
30+
31+
self::assertFalse($filesystem->exists($cacheDirectory));
32+
self::assertFalse($filesystem->exists($logDirectory));
33+
}
34+
}

0 commit comments

Comments
 (0)