-
-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathLocalFilesystem.php
More file actions
49 lines (38 loc) · 1.37 KB
/
LocalFilesystem.php
File metadata and controls
49 lines (38 loc) · 1.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
<?php
declare(strict_types=1);
/*
* This file is part of the community-maintained Playwright PHP project.
* It is not affiliated with or endorsed by Microsoft.
*
* (c) 2025-Present - Playwright PHP - https://github.com/playwright-php
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Playwright\Media\Filesystem;
use Playwright\Exception\RuntimeException;
final readonly class LocalFilesystem implements FilesystemInterface
{
public function __construct(private string $directory)
{
$this->ensureDirectoryExists($this->directory);
}
public function write(string $path, string $content): string
{
if (!str_contains($path, $this->directory)) {
$path = $this->directory.DIRECTORY_SEPARATOR.$path;
}
$this->ensureDirectoryExists(dirname($path));
file_put_contents($path, $content) ?: throw new RuntimeException(sprintf('Failed to write screenshot to file: %s', $path));
return $path;
}
private function ensureDirectoryExists(string $directory): void
{
if (is_dir($directory)) {
return;
}
if (!mkdir($directory, 0755, true) && !is_dir($directory)) {
throw new RuntimeException(sprintf('Failed to create screenshot directory: %s', $directory));
}
}
}