Skip to content

Commit e76c5f4

Browse files
Changing the location of temporary files to the system’s temporary directory
1 parent 26fb014 commit e76c5f4

File tree

5 files changed

+79
-31
lines changed

5 files changed

+79
-31
lines changed

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@
1818
"illuminate/database": "^11.0 || ^12.0",
1919
"illuminate/filesystem": "^11.0 || ^12.0",
2020
"illuminate/support": "^11.0 || ^12.0",
21-
"laravel/prompts": ">=0.3.6"
21+
"laravel/prompts": ">=0.3.6",
22+
"spatie/temporary-directory": "^2.3"
2223
},
2324
"require-dev": {
2425
"dragon-code/codestyler": "^6.3",

src/Exceptions/OpenFeedException.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,14 @@
55
namespace DragonCode\LaravelFeed\Exceptions;
66

77
use RuntimeException;
8+
use Throwable;
89

910
// @codeCoverageIgnoreStart
1011
class OpenFeedException extends RuntimeException
1112
{
12-
public function __construct(string $path)
13+
public function __construct(string $path, Throwable $e)
1314
{
14-
parent::__construct("Unable to open file for writing: [$path]");
15+
parent::__construct("Unable to open file for writing: [$path]", previous: $e);
1516
}
1617
}
1718
// @codeCoverageIgnoreEnd
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace DragonCode\LaravelFeed\Exceptions;
6+
7+
use Exception;
8+
9+
class ResourceMetaException extends Exception
10+
{
11+
public function __construct()
12+
{
13+
parent::__construct('Unable to get a link to the file from the resource.');
14+
}
15+
}

src/Services/FilesystemService.php

Lines changed: 53 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -5,40 +5,49 @@
55
namespace DragonCode\LaravelFeed\Services;
66

77
use DragonCode\LaravelFeed\Exceptions\OpenFeedException;
8+
use DragonCode\LaravelFeed\Exceptions\ResourceMetaException;
89
use DragonCode\LaravelFeed\Exceptions\WriteFeedException;
910
use Illuminate\Filesystem\Filesystem as File;
11+
use Illuminate\Support\Str;
12+
use RuntimeException;
13+
use Spatie\TemporaryDirectory\TemporaryDirectory;
14+
use Throwable;
1015

1116
use function dirname;
1217
use function fclose;
1318
use function fopen;
1419
use function fwrite;
1520
use function is_resource;
21+
use function microtime;
22+
use function stream_get_meta_data;
1623

1724
class FilesystemService
1825
{
1926
public function __construct(
2027
protected File $file,
28+
protected TemporaryDirectory $directory,
2129
) {}
2230

2331
/**
2432
* @return resource
2533
*/
26-
public function open(string $path) // @pest-ignore-type
34+
public function createDraft(string $filename) // @pest-ignore-type
2735
{
28-
$path = $this->draft($path);
36+
$temp = $this->draftPath($filename);
2937

30-
$this->ensureFileDelete($path);
31-
$this->ensureDirectory($path);
38+
try {
39+
$resource = fopen($temp, 'ab');
3240

33-
$resource = fopen($path, 'ab');
41+
if ($resource === false) {
42+
// @codeCoverageIgnoreStart
43+
throw new RuntimeException($temp);
44+
// @codeCoverageIgnoreEnd
45+
}
3446

35-
if ($resource === false) {
36-
// @codeCoverageIgnoreStart
37-
throw new OpenFeedException($path);
38-
// @codeCoverageIgnoreEnd
47+
return $resource;
48+
} catch (Throwable $e) {
49+
throw new OpenFeedException($temp, $e);
3950
}
40-
41-
return $resource;
4251
}
4352

4453
/**
@@ -58,16 +67,21 @@ public function append($resource, string $content, string $path): void // @pest-
5867
*/
5968
public function release($resource, string $path): void // @pest-ignore-type
6069
{
70+
$temp = $this->getMetaPath($resource);
71+
6172
$this->close($resource);
6273

6374
if ($this->file->exists($path)) {
6475
$this->file->delete($path);
6576
}
6677

67-
$this->file->move(
68-
$this->draft($path),
69-
$path
78+
$this->file->ensureDirectoryExists(
79+
dirname($path)
7080
);
81+
82+
$this->file->move($temp, $path);
83+
84+
$this->cleanTemporaryDirectory($temp);
7185
}
7286

7387
/**
@@ -84,20 +98,37 @@ public function close($resource): void // @pest-ignore-type
8498
fclose($resource);
8599
}
86100

87-
protected function ensureFileDelete(string $path): void
101+
protected function cleanTemporaryDirectory(string $filename): void
88102
{
89-
if ($this->file->exists($path)) {
90-
$this->file->delete($path);
91-
}
103+
$this->file->deleteDirectory(
104+
dirname($filename)
105+
);
106+
}
107+
108+
protected function draftPath(string $filename): string
109+
{
110+
return $this->directory
111+
->name($this->temporaryFilename($filename))
112+
->create()
113+
->path((string) microtime(true));
92114
}
93115

94-
protected function ensureDirectory(string $path): void
116+
protected function temporaryFilename(string $filename): string
95117
{
96-
$this->file->ensureDirectoryExists(dirname($path));
118+
return Str::of($filename)
119+
->prepend('feeds_draft_')
120+
->append('_', microtime(true))
121+
->slug('_')
122+
->toString();
97123
}
98124

99-
protected function draft(string $path): string
125+
/**
126+
* @param resource $file
127+
*/
128+
protected function getMetaPath($file): string
100129
{
101-
return $path . '.draft';
130+
$meta = stream_get_meta_data($file);
131+
132+
return $meta['uri'] ?? throw new ResourceMetaException;
102133
}
103134
}

src/Services/GeneratorService.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@ public function feed(Feed $feed, ?OutputStyle $output = null): void
3434
try {
3535
$this->started($feed);
3636

37-
$file = $this->openFile(
38-
$path = $feed->path()
37+
$file = $this->createDraft(
38+
$feed->filename()
3939
);
4040

4141
$this->performHeader($file, $feed);
@@ -45,11 +45,11 @@ public function feed(Feed $feed, ?OutputStyle $output = null): void
4545
$this->performItem($file, $feed, $output);
4646
$this->performFooter($file, $feed);
4747

48-
$this->release($file, $path);
48+
$this->release($file, $feed->path());
4949

5050
$this->setLastActivity($feed);
5151

52-
$this->finished($feed, $path);
52+
$this->finished($feed, $feed->path());
5353
} catch (Throwable $e) {
5454
throw new FeedGenerationException(get_class($feed), $e);
5555
}
@@ -142,9 +142,9 @@ protected function release($file, string $path): void // @pest-ignore-type
142142
$this->filesystem->release($file, $path);
143143
}
144144

145-
protected function openFile(string $path) // @pest-ignore-type
145+
protected function createDraft(string $filename) // @pest-ignore-type
146146
{
147-
return $this->filesystem->open($path);
147+
return $this->filesystem->createDraft($filename);
148148
}
149149

150150
protected function setLastActivity(Feed $feed): void

0 commit comments

Comments
 (0)