Chris@0
|
1 <?php
|
Chris@0
|
2
|
Chris@0
|
3 namespace Drupal\Core\Archiver;
|
Chris@0
|
4
|
Chris@0
|
5 /**
|
Chris@0
|
6 * Defines a archiver implementation for .tar files.
|
Chris@0
|
7 */
|
Chris@0
|
8 class Tar implements ArchiverInterface {
|
Chris@0
|
9
|
Chris@0
|
10 /**
|
Chris@0
|
11 * The underlying ArchiveTar instance that does the heavy lifting.
|
Chris@0
|
12 *
|
Chris@0
|
13 * @var \Drupal\Core\Archiver\ArchiveTar
|
Chris@0
|
14 */
|
Chris@0
|
15 protected $tar;
|
Chris@0
|
16
|
Chris@0
|
17 /**
|
Chris@0
|
18 * Constructs a Tar object.
|
Chris@0
|
19 *
|
Chris@0
|
20 * @param string $file_path
|
Chris@0
|
21 * The full system path of the archive to manipulate. Only local files
|
Chris@0
|
22 * are supported. If the file does not yet exist, it will be created if
|
Chris@0
|
23 * appropriate.
|
Chris@0
|
24 *
|
Chris@0
|
25 * @throws \Drupal\Core\Archiver\ArchiverException
|
Chris@0
|
26 */
|
Chris@0
|
27 public function __construct($file_path) {
|
Chris@0
|
28 $this->tar = new ArchiveTar($file_path);
|
Chris@0
|
29 }
|
Chris@0
|
30
|
Chris@0
|
31 /**
|
Chris@0
|
32 * {@inheritdoc}
|
Chris@0
|
33 */
|
Chris@0
|
34 public function add($file_path) {
|
Chris@0
|
35 $this->tar->add($file_path);
|
Chris@0
|
36
|
Chris@0
|
37 return $this;
|
Chris@0
|
38 }
|
Chris@0
|
39
|
Chris@0
|
40 /**
|
Chris@0
|
41 * {@inheritdoc}
|
Chris@0
|
42 */
|
Chris@0
|
43 public function remove($file_path) {
|
Chris@0
|
44 // @todo Archive_Tar doesn't have a remove operation
|
Chris@0
|
45 // so we'll have to simulate it somehow, probably by
|
Chris@0
|
46 // creating a new archive with everything but the removed
|
Chris@0
|
47 // file.
|
Chris@0
|
48
|
Chris@0
|
49 return $this;
|
Chris@0
|
50 }
|
Chris@0
|
51
|
Chris@0
|
52 /**
|
Chris@0
|
53 * {@inheritdoc}
|
Chris@0
|
54 */
|
Chris@0
|
55 public function extract($path, array $files = []) {
|
Chris@0
|
56 if ($files) {
|
Chris@0
|
57 $this->tar->extractList($files, $path);
|
Chris@0
|
58 }
|
Chris@0
|
59 else {
|
Chris@0
|
60 $this->tar->extract($path);
|
Chris@0
|
61 }
|
Chris@0
|
62
|
Chris@0
|
63 return $this;
|
Chris@0
|
64 }
|
Chris@0
|
65
|
Chris@0
|
66 /**
|
Chris@0
|
67 * {@inheritdoc}
|
Chris@0
|
68 */
|
Chris@0
|
69 public function listContents() {
|
Chris@0
|
70 $files = [];
|
Chris@0
|
71 foreach ($this->tar->listContent() as $file_data) {
|
Chris@0
|
72 $files[] = $file_data['filename'];
|
Chris@0
|
73 }
|
Chris@0
|
74 return $files;
|
Chris@0
|
75 }
|
Chris@0
|
76
|
Chris@0
|
77 /**
|
Chris@0
|
78 * Retrieves the tar engine itself.
|
Chris@0
|
79 *
|
Chris@0
|
80 * In some cases it may be necessary to directly access the underlying
|
Chris@0
|
81 * Archive_Tar object for implementation-specific logic. This is for advanced
|
Chris@0
|
82 * use only as it is not shared by other implementations of ArchiveInterface.
|
Chris@0
|
83 *
|
Chris@0
|
84 * @return Archive_Tar
|
Chris@0
|
85 * The Archive_Tar object used by this object.
|
Chris@0
|
86 */
|
Chris@0
|
87 public function getArchive() {
|
Chris@0
|
88 return $this->tar;
|
Chris@0
|
89 }
|
Chris@0
|
90
|
Chris@0
|
91 }
|