Skip to content

Commit

Permalink
Adding SelfDeletingTemporaryDirectory
Browse files Browse the repository at this point in the history
  • Loading branch information
spekulatius committed Jun 25, 2023
1 parent 36ce48a commit b51c32f
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 1 deletion.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@
],
"require": {
"php": "^8.1",
"illuminate/contracts": "^9.28|^10.0",
"spatie/laravel-package-tools": "^1.0",
"illuminate/contracts": "^9.28|^10.0"
"spatie/temporary-directory": "^2.0"
},
"require-dev": {
"laravel/pint": "^1.0",
Expand Down
16 changes: 16 additions & 0 deletions src/Helpers/SelfDeletingTemporaryDirectory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace Spekulatius\LaravelPowertools\Helpers;

use Spatie\TemporaryDirectory\TemporaryDirectory;

class SelfDeletingTemporaryDirectory extends TemporaryDirectory
{
public function __construct(string $location = '')
{
parent::__construct($location);

TemporaryDirectoryCleanupJob::dispatch($this)

Check failure on line 13 in src/Helpers/SelfDeletingTemporaryDirectory.php

View workflow job for this annotation

GitHub Actions / phpstan

Call to static method dispatch() on an unknown class Spekulatius\LaravelPowertools\Helpers\TemporaryDirectoryCleanupJob.

Check failure on line 13 in src/Helpers/SelfDeletingTemporaryDirectory.php

View workflow job for this annotation

GitHub Actions / phpstan

Class Spekulatius\LaravelPowertools\Helpers\TemporaryDirectoryCleanupJob was not found while trying to analyse it - discovering symbols is probably not configured properly.
->delay(config('powertools.temporary_directory_clean_up'));
}
}
34 changes: 34 additions & 0 deletions src/Jobs/TemporaryDirectoryCleanupJob.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

namespace Spekulatius\LaravelPowertools\Jobs;

use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Spekulatius\LaravelPowertools\Helpers\SelfDeletingTemporaryDirectory;

class TemporaryDirectoryCleanupJob implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

public function __construct(
protected SelfDeletingTemporaryDirectory $selfDeletingTemporaryDirectory,
protected array $context = [],
) {
}

public function handle(): void
{
try {
$this->selfDeletingTemporaryDirectory->delete();
} catch (\Exception $e) {
\Log::error('SelfDeletingTemporaryDirectory: Failed to delete temp dir: '.$e->getMessage(), [
'path' => $this->selfDeletingTemporaryDirectory->path(),
]);

throw $e;
}
}
}

0 comments on commit b51c32f

Please sign in to comment.