-
Notifications
You must be signed in to change notification settings - Fork 199
Expand file tree
/
Copy pathArchiveStream.php
More file actions
66 lines (59 loc) · 1.69 KB
/
ArchiveStream.php
File metadata and controls
66 lines (59 loc) · 1.69 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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
<?php declare(strict_types=1);
/**
* This file implements an ArchiveStream, which creates a temporary tar archive
* from a directory and provides it as a PSR7 StreamInterface.
*
* @category PSR7
* @package Http
* @license http://www.gnu.org/licenses/gpl-3.0.txt GPLv3
* @link https://www.github.com/aces/Loris/
*
* @see https://www.php-fig.org/psr/psr-7/
*/
namespace LORIS\Http;
/**
* An ArchiveStream creates a temporary tar archive from a directory
* and provides it as a PSR7 StreamInterface. The temporary archive file
* is deleted when the stream is closed.
*
* @category PSR7
* @package Http
* @license http://www.gnu.org/licenses/gpl-3.0.txt GPLv3
* @link https://www.github.com/aces/Loris/
*/
class ArchiveStream extends FileStream
{
/**
* The path to the temporary archive file
*
* @var string
*/
private string $archivePath;
/**
* Constructor
*
* Creates a tar archive from the given directory and opens it as a stream.
* The archive file is deleted when the stream is closed.
*
* @param string $directoryPath The path to the directory to archive
*
* @throws \Exception if archive creation fails
*/
public function __construct(string $directoryPath)
{
$this->archivePath = sys_get_temp_dir() . '/' . uniqid() . '.tar';
$phar = new \PharData($this->archivePath);
$phar->buildFromDirectory($directoryPath);
parent::__construct($this->archivePath, 'r');
}
/**
* {@inheritdoc}
*/
public function close(): void
{
parent::close();
if (file_exists($this->archivePath)) {
unlink($this->archivePath);
}
}
}