Passed
Push — master ( 3f67b7...07fe76 )
by f
02:48
created

UnifiedArchive::isFileExists()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 2
Bugs 0 Features 1
Metric Value
eloc 1
c 2
b 0
f 1
dl 0
loc 3
ccs 0
cts 0
cp 0
rs 10
cc 1
nc 1
nop 1
crap 2
1
<?php
2
namespace wapmorgan\UnifiedArchive;
3
4
use InvalidArgumentException;
5
use wapmorgan\UnifiedArchive\Drivers\BasicDriver;
6
use wapmorgan\UnifiedArchive\Exceptions\ArchiveExtractionException;
7
use wapmorgan\UnifiedArchive\Exceptions\ArchiveModificationException;
8
use wapmorgan\UnifiedArchive\Exceptions\EmptyFileListException;
9
use wapmorgan\UnifiedArchive\Exceptions\FileAlreadyExistsException;
10
use wapmorgan\UnifiedArchive\Exceptions\NonExistentArchiveFileException;
11
use wapmorgan\UnifiedArchive\Exceptions\UnsupportedArchiveException;
12
use wapmorgan\UnifiedArchive\Exceptions\UnsupportedOperationException;
13
14
/**
15
 * Class which represents archive in one of supported formats.
16
 */
17
class UnifiedArchive
18
{
19
    const VERSION = '1.2.0';
20
21
    /** @var string Type of current archive */
22
    protected $format;
23
24
    /** @var BasicDriver Adapter for current archive */
25
    protected $archive;
26
27
    /** @var array List of files in current archive */
28
    protected $files;
29
30
    /** @var int Number of files in archive */
31
    protected $filesQuantity;
32
33
    /** @var int Cumulative size of uncompressed files */
34
    protected $uncompressedFilesSize;
35
36
    /** @var int Cumulative size of compressed files */
37
    protected $compressedFilesSize;
38
39
    /** @var int Total size of archive file */
40
    protected $archiveSize;
41
    /**
42
     * @var null
43
     */
44
    protected $password;
45
46
    /**
47
     * Creates a UnifiedArchive instance for passed archive
48
     *
49
     * @param string $fileName Archive filename
50
     * @param null $password
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $password is correct as it would always require null to be passed?
Loading history...
51
     * @return UnifiedArchive|null Returns UnifiedArchive in case of successful reading of the file
52
     * @throws UnsupportedOperationException
53
     */
54 23
    public static function open($fileName, $password = null)
55
    {
56 23
        if (!file_exists($fileName) || !is_readable($fileName)) {
57
            throw new InvalidArgumentException('Could not open file: ' . $fileName);
58
        }
59
60 23
        $format = Formats::detectArchiveFormat($fileName);
61 23
        if (!Formats::canOpen($format)) {
62
            return null;
63
        }
64
65 23
        return new static($fileName, $format, $password);
66
    }
67
68
    /**
69
     * Checks whether archive can be opened with current system configuration
70
     *
71
     * @param string $fileName Archive filename
72
     * @return bool
73
     */
74 21
    public static function canOpen($fileName)
75
    {
76 21
        $format = Formats::detectArchiveFormat($fileName);
77 21
        return $format !== false && Formats::canOpen($format);
0 ignored issues
show
Bug introduced by
It seems like $format can also be of type true; however, parameter $format of wapmorgan\UnifiedArchive\Formats::canOpen() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

77
        return $format !== false && Formats::canOpen(/** @scrutinizer ignore-type */ $format);
Loading history...
78
    }
79
80
    /**
81
     * Opens the file as one of supported formats
82
     *
83
     * @param string $fileName Archive filename
84
     * @param string $format Archive type
85
     * @param string|null $password
86
     */
87 23
    public function __construct($fileName, $format, $password = null)
88
    {
89 23
        $driver = Formats::getFormatDriver($format);
90 23
        if ($driver === false) {
91
            throw new \RuntimeException('Driver for '.$format.' ('.$fileName.') is not found');
92
        }
93
94 23
        $this->format = $format;
95 23
        $this->archiveSize = filesize($fileName);
96 23
        $this->password = $password;
0 ignored issues
show
Documentation Bug introduced by
It seems like $password can also be of type string. However, the property $password is declared as type null. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
97
98
        /** @var BasicDriver archive */
99 23
        $this->archive = new $driver($fileName, $format, $password);
100 23
        $this->scanArchive();
101 23
    }
102
103
    /**
104
     * Rescans array after modification
105
     */
106 23
    protected function scanArchive()
107
    {
108 23
        $information = $this->archive->getArchiveInformation();
109 23
        $this->files = $information->files;
110 23
        $this->compressedFilesSize = $information->compressedFilesSize;
111 23
        $this->uncompressedFilesSize = $information->uncompressedFilesSize;
112 23
        $this->filesQuantity = count($information->files);
113 23
    }
114
115
    /**
116
     * Closes archive
117
     */
118 23
    public function __destruct()
119
    {
120 23
        unset($this->archive);
121 23
    }
122
123
    /**
124
     * Returns an instance of class implementing PclZipOriginalInterface
125
     * interface.
126
     *
127
     * @return PclzipZipInterface Returns an instance of a class implementing PclZipOriginalInterface
128
     */
129
    public function getPclZipInterface()
130
    {
131
        return new PclzipZipInterface($this);
132
    }
133
134
    /**
135
     * @return string
136
     */
137
    public function getDriverType()
138
    {
139
        return get_class($this->archive);
140
    }
141
142
    /**
143
     * @return BasicDriver
144
     */
145
    public function getDriver()
146
    {
147
        return $this->archive;
148
    }
149
150
    /**
151
     * Returns size of archive file in bytes
152
     *
153
     * @return int
154
     */
155
    public function getSize()
156
    {
157
        return $this->archiveSize;
158
    }
159
160
    /**
161
     * Returns type of archive
162
     *
163
     * @return string One of Format class constants
164
     */
165
    public function getFormat()
166
    {
167
        return $this->format;
168
    }
169
170
    /**
171
     * Returns mime type of archive
172
     *
173
     * @return string Mime Type
174
     */
175
    public function getMimeType()
176
    {
177
        return Formats::getFormatMimeType($this->format);
178
    }
179
180
    /**
181
     * @return string|null
182
     */
183
    public function getComment()
184
    {
185
        return $this->archive->getComment();
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->archive->getComment() targeting wapmorgan\UnifiedArchive...sicDriver::getComment() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
186
    }
187
188
    /**
189
     * @param string|null $comment
190
     * @return string|null
191
     */
192
    public function setComment($comment)
193
    {
194
        return $this->archive->setComment($comment);
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->archive->setComment($comment) targeting wapmorgan\UnifiedArchive...sicDriver::setComment() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
195
    }
196
197
    /**
198
     * Counts number of files
199
     *
200
     * @return int
201
     */
202 7
    public function countFiles()
203
    {
204 7
        return $this->filesQuantity;
205
    }
206
207
    /**
208
     * Counts cumulative size of all uncompressed data (bytes)
209
     *
210
     * @return int
211
     */
212 7
    public function countUncompressedFilesSize()
213
    {
214 7
        return $this->uncompressedFilesSize;
215
    }
216
217
    /**
218
     * Counts cumulative size of all compressed data (in bytes)
219
     *
220
     * @return int
221
     */
222
    public function countCompressedFilesSize()
223
    {
224
        return $this->compressedFilesSize;
225
    }
226
227
    /**
228
     * Checks that file exists in archive
229
     *
230
     * @param string $fileName File name in archive
231
     * @return bool
232
     */
233 9
    public function hasFile($fileName)
234
    {
235 9
        return in_array($fileName, $this->files, true);
236
    }
237
238
    /**
239
     * Returns list of files, excluding folders.
240
     *
241
     * Paths is present in unix-style (with forward slash - /).
242
     *
243
     * @param string|null $filter
244
     * @return array List of files
245
     */
246 7
    public function getFileNames($filter = null)
247
    {
248 7
        if ($filter === null)
249 7
            return $this->files;
250
251
        $result = [];
252
        foreach ($this->files as $file) {
253
            if (fnmatch($filter, $file))
254
                $result[] = $file;
255
        }
256
        return $result;
257
    }
258
259
    /**
260
     * Returns file metadata of file in archive
261
     *
262
     * @param string $fileName File name in archive
263
     * @return ArchiveEntry
264
     * @throws NonExistentArchiveFileException
265
     */
266 7
    public function getFileData($fileName)
267
    {
268 7
        if (!in_array($fileName, $this->files, true)) {
269
            throw new NonExistentArchiveFileException('File ' . $fileName . ' does not exist in archive');
270
        }
271
272 7
        return $this->archive->getFileData($fileName);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->archive->getFileData($fileName) could also return false which is incompatible with the documented return type wapmorgan\UnifiedArchive\ArchiveEntry. Did you maybe forget to handle an error condition?

If the returned type also contains false, it is an indicator that maybe an error condition leading to the specific return statement remains unhandled.

Loading history...
273
    }
274
275
    /**
276
     * Returns full file content as string
277
     *
278
     * @param string $fileName File name in archive
279
     * @return string
280
     * @throws NonExistentArchiveFileException
281
     */
282 9
    public function getFileContent($fileName)
283
    {
284 9
        if (!in_array($fileName, $this->files, true)) {
285
            throw new NonExistentArchiveFileException('File ' . $fileName . ' does not exist in archive');
286
        }
287
288 9
        return $this->archive->getFileContent($fileName);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->archive->getFileContent($fileName) could also return false which is incompatible with the documented return type string. Did you maybe forget to handle an error condition?

If the returned type also contains false, it is an indicator that maybe an error condition leading to the specific return statement remains unhandled.

Loading history...
289
    }
290
291
    /**
292
     * Returns a resource for reading file from archive
293
     *
294
     * @param string $fileName File name in archive
295
     * @return resource
296
     * @throws NonExistentArchiveFileException
297
     */
298 7
    public function getFileStream($fileName)
299
    {
300 7
        if (!in_array($fileName, $this->files, true)) {
301
            throw new NonExistentArchiveFileException('File ' . $fileName . ' does not exist in archive');
302
        }
303
304 7
        return $this->archive->getFileStream($fileName);
305
    }
306
307
    /**
308
     * Unpacks files to disk
309
     *
310
     * @param string $outputFolder Extraction output dir
311
     * @param string|array|null $files One file or files list or null to extract all content.
312
     * @param bool $expandFilesList Whether paths like 'src/' should be expanded to all files inside 'src/' dir or not.
313
     * @return int Number of extracted files
314
     * @throws EmptyFileListException
315
     * @throws ArchiveExtractionException
316
     */
317
    public function extractFiles($outputFolder, $files = null, $expandFilesList = false)
318
    {
319
        if ($files !== null) {
320
            if (is_string($files)) {
321
                $files = [$files];
322
            }
323
324
            if ($expandFilesList) {
325
                $files = static::expandFileList($this->files, $files);
326
            }
327
328
            if (empty($files)) {
329
                throw new EmptyFileListException('Files list is empty!');
330
            }
331
332
            return $this->archive->extractFiles($outputFolder, $files);
333
        }
334
335
        return $this->archive->extractArchive($outputFolder);
336
    }
337
338
    /**
339
     * Updates existing archive by removing files from it
340
     *
341
     * Only 7zip and zip types support deletion.
342
     * @param string|string[] $fileOrFiles
343
     * @param bool $expandFilesList
344
     *
345
     * @return bool|int
346
     * @throws EmptyFileListException
347
     * @throws UnsupportedOperationException
348
     * @throws ArchiveModificationException
349
     */
350 2
    public function deleteFiles($fileOrFiles, $expandFilesList = false)
351
    {
352 2
        $fileOrFiles = is_string($fileOrFiles) ? [$fileOrFiles] : $fileOrFiles;
353
354 2
        if ($expandFilesList && $fileOrFiles !== null) {
355
            $fileOrFiles = static::expandFileList($this->files, $fileOrFiles);
356
        }
357
358 2
        if (empty($fileOrFiles)) {
359
            throw new EmptyFileListException('Files list is empty!');
360
        }
361
362 2
        $result = $this->archive->deleteFiles($fileOrFiles);
363 2
        $this->scanArchive();
364
365 2
        return $result;
366
    }
367
368
    /**
369
     * Updates existing archive by adding new files
370
     *
371
     * @param string[] $fileOrFiles See [[archiveFiles]] method for file list format.
372
     * @return int|bool Number of added files
373
     * @throws ArchiveModificationException
374
     * @throws EmptyFileListException
375
     * @throws UnsupportedOperationException
376
     */
377 2
    public function addFiles($fileOrFiles)
378
    {
379 2
        $files_list = static::createFilesList($fileOrFiles);
380
381 2
        if (empty($files_list))
382
            throw new EmptyFileListException('Files list is empty!');
383
384 2
        $result = $this->archive->addFiles($files_list);
0 ignored issues
show
Bug introduced by
It seems like $files_list can also be of type boolean; however, parameter $files of wapmorgan\UnifiedArchive...BasicDriver::addFiles() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

384
        $result = $this->archive->addFiles(/** @scrutinizer ignore-type */ $files_list);
Loading history...
385 2
        $this->scanArchive();
386 2
        return $result;
387
    }
388
389
    /**
390
     * Adds file into archive
391
     *
392
     * @param string $file File name to be added
393
     * @param string|null $inArchiveName If not passed, full path will be preserved.
394
     * @return bool
395
     * @throws ArchiveModificationException
396
     * @throws EmptyFileListException
397
     * @throws UnsupportedOperationException
398
     */
399 2
    public function addFile($file, $inArchiveName = null)
400
    {
401 2
        if (!is_file($file))
402
            throw new InvalidArgumentException($file.' is not a valid file to add in archive');
403
404 2
        return ($inArchiveName !== null
405 2
                ? $this->addFiles([$inArchiveName => $file])
406 2
                : $this->addFiles([$file])) === 1;
407
    }
408
409
    /**
410
     * @param string $inArchiveName
411
     * @param string $content
412
     * @return bool
413
     * @throws ArchiveModificationException
414
     * @throws UnsupportedOperationException
415
     */
416
    public function addFileFromString($inArchiveName, $content)
417
    {
418
        $result = $this->archive->addFileFromString($inArchiveName, $content);
419
        $this->scanArchive();
420
        return $result;
421
    }
422
423
    /**
424
     * Adds directory contents to archive
425
     *
426
     * @param string $directory
427
     * @param string|null $inArchivePath If not passed, full paths will be preserved.
428
     * @return bool
429
     * @throws ArchiveModificationException
430
     * @throws EmptyFileListException
431
     * @throws UnsupportedOperationException
432
     */
433
    public function addDirectory($directory, $inArchivePath = null)
434
    {
435
        if (!is_dir($directory) || !is_readable($directory))
436
            throw new InvalidArgumentException($directory.' is not a valid directory to add in archive');
437
438
        return ($inArchivePath !== null
439
                ? $this->addFiles([$inArchivePath => $directory])
440
                : $this->addFiles([$inArchivePath])) > 0;
441
    }
442
443
    /**
444
     * Prepare files list for archiving
445
     *
446
     * @param string $fileOrFiles File of list of files. See [[archiveFiles]] for details.
447
     * @param string $archiveName File name of archive. See [[archiveFiles]] for details.
448
     * @return array An array containing entries:
449
     * - totalSize (int) - size in bytes for all files
450
     * - numberOfFiles (int) - quantity of files
451
     * - files (array) - list of files prepared for archiving
452
     * - type (string) - prepared format for archive. One of class constants
453
     * @throws EmptyFileListException
454
     * @throws UnsupportedArchiveException
455
     */
456 2
    public static function prepareForArchiving($fileOrFiles, $archiveName)
457
    {
458 2
        $archiveType = Formats::detectArchiveFormat($archiveName, false);
459
460 2
        if ($archiveType === false)
461
            throw new UnsupportedArchiveException('Could not detect archive type for name "'.$archiveName.'"');
462
463 2
        $files_list = static::createFilesList($fileOrFiles);
464
465 2
        if (empty($files_list))
466
            throw new EmptyFileListException('Files list is empty!');
467
468 2
        $totalSize = 0;
469 2
        foreach ($files_list as $fn) {
470 2
            $totalSize += filesize($fn);
471
        }
472
473
        return [
474 2
            'totalSize' => $totalSize,
475 2
            'numberOfFiles' => count($files_list),
0 ignored issues
show
Bug introduced by
It seems like $files_list can also be of type boolean; however, parameter $value of count() does only seem to accept Countable|array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

475
            'numberOfFiles' => count(/** @scrutinizer ignore-type */ $files_list),
Loading history...
476 2
            'files' => $files_list,
477 2
            'type' => $archiveType,
478
        ];
479
    }
480
481
    /**
482
     * Creates an archive with passed files list
483
     *
484
     * @param string|string[]|array<string,string> $fileOrFiles List of files. Can be one of three formats:
485
     *                             1. A string containing path to file or directory.
486
     *                                  File will have it's basename.
487
     *                                  `UnifiedArchive::archiveFiles('/etc/php.ini', 'archive.zip)` will store
488
     * file with 'php.ini' name.
489
     *                                  Directory contents will be stored in archive root.
490
     *                                  `UnifiedArchive::archiveFiles('/var/log/', 'archive.zip')` will store all
491
     * directory contents in archive root.
492
     *                             2. An array with strings containing pats to files or directories.
493
     *                                  Files and directories will be stored with full paths.
494
     *                                  `UnifiedArchive::archiveFiles(['/etc/php.ini', '/var/log/'], 'archive.zip)`
495
     * will preserve full paths.
496
     *                             3. An array with strings where keys are strings.
497
     *                                  Files will have name from key.
498
     *                                  Directories contents will have prefix from key.
499
     *                                  `UnifiedArchive::archiveFiles(['doc.txt' => 'very_long_name_of_document.txt',
500
     *  'static' => '/var/www/html/static/'], 'archive.zip')`
501
     *
502
     * @param string $archiveName File name of archive. Type of archive will be determined by it's name.
503
     * @param int $compressionLevel Level of compression
504
     * @param null $password
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $password is correct as it would always require null to be passed?
Loading history...
505
     * @return int Count of stored files is returned.
506
     * @throws FileAlreadyExistsException
507
     * @throws UnsupportedOperationException
508
     */
509 2
    public static function archiveFiles($fileOrFiles, $archiveName, $compressionLevel = BasicDriver::COMPRESSION_AVERAGE, $password = null)
510
    {
511 2
        if (file_exists($archiveName))
512
            throw new FileAlreadyExistsException('Archive '.$archiveName.' already exists!');
513
514 2
        $info = static::prepareForArchiving($fileOrFiles, $archiveName);
0 ignored issues
show
Bug introduced by
It seems like $fileOrFiles can also be of type array<string,string> and string[]; however, parameter $fileOrFiles of wapmorgan\UnifiedArchive...::prepareForArchiving() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

514
        $info = static::prepareForArchiving(/** @scrutinizer ignore-type */ $fileOrFiles, $archiveName);
Loading history...
515
516 2
        if (!Formats::canCreate($info['type']))
517
            throw new UnsupportedArchiveException('Unsupported archive type: '.$info['type'].' of archive '.$archiveName);
518
519 2
        if ($password !== null && !Formats::canEncrypt($info['type']))
0 ignored issues
show
introduced by
The condition $password !== null is always false.
Loading history...
520
            throw new UnsupportedOperationException('Archive type '.$info['type'].' can not be encrypted');
521
522
        /** @var BasicDriver $handler_class */
523 2
        $driver = Formats::getFormatDriver($info['type'], true);
524
525 2
        return $driver::createArchive($info['files'], $archiveName, $compressionLevel, $password);
526
    }
527
528
    /**
529
     * Creates an archive with one file
530
     *
531
     * @param string $file
532
     * @param string $archiveName
533
     * @param int $compressionLevel Level of compression
534
     * @param null $password
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $password is correct as it would always require null to be passed?
Loading history...
535
     * @return bool
536
     * @throws FileAlreadyExistsException
537
     * @throws UnsupportedOperationException
538
     */
539
    public static function archiveFile($file, $archiveName, $compressionLevel = BasicDriver::COMPRESSION_AVERAGE, $password = null)
540
    {
541
        if (!is_file($file)) {
542
            throw new InvalidArgumentException($file . ' is not a valid file to archive');
543
        }
544
545
        return static::archiveFiles($file, $archiveName, $compressionLevel, $password) === 1;
546
    }
547
548
    /**
549
     * Creates an archive with full directory contents
550
     *
551
     * @param string $directory
552
     * @param string $archiveName
553
     * @param int $compressionLevel Level of compression
554
     * @param null $password
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $password is correct as it would always require null to be passed?
Loading history...
555
     * @return bool
556
     * @throws FileAlreadyExistsException
557
     * @throws UnsupportedOperationException
558
     */
559
    public static function archiveDirectory($directory, $archiveName, $compressionLevel = BasicDriver::COMPRESSION_AVERAGE, $password = null)
560
    {
561
        if (!is_dir($directory) || !is_readable($directory))
562
            throw new InvalidArgumentException($directory.' is not a valid directory to archive');
563
564
        return static::archiveFiles($directory, $archiveName, $compressionLevel, $password) > 0;
565
    }
566
567
    /**
568
     * Expands files list
569
     * @param $archiveFiles
570
     * @param $files
571
     * @return array
572
     */
573
    protected static function expandFileList($archiveFiles, $files)
574
    {
575
        $newFiles = [];
576
        foreach ($files as $file) {
577
            foreach ($archiveFiles as $archiveFile) {
578
                if (fnmatch($file.'*', $archiveFile)) {
579
                    $newFiles[] = $archiveFile;
580
                }
581
            }
582
        }
583
        return $newFiles;
584
    }
585
586
    /**
587
     * @param string|array $nodes
588
     * @return array|bool
589
     */
590 4
    protected static function createFilesList($nodes)
591
    {
592 4
        $files = [];
593
594
        // passed an extended list
595 4
        if (is_array($nodes)) {
596 2
            foreach ($nodes as $destination => $source) {
597 2
                if (is_numeric($destination))
598
                    $destination = $source;
599
600 2
                $destination = rtrim($destination, '/\\*');
601
602
                // if is directory
603 2
                if (is_dir($source))
604
                    static::importFilesFromDir(rtrim($source, '/\\*').'/*',
605
                        !empty($destination) ? $destination.'/' : null, true, $files);
606 2
                else if (is_file($source))
607 2
                    $files[$destination] = $source;
608
            }
609
610 2
        } else if (is_string($nodes)) { // passed one file or directory
0 ignored issues
show
introduced by
The condition is_string($nodes) is always true.
Loading history...
611
            // if is directory
612 2
            if (is_dir($nodes))
613 2
                static::importFilesFromDir(rtrim($nodes, '/\\*').'/*', null, true,
614 2
                    $files);
615
            else if (is_file($nodes))
616
                $files[basename($nodes)] = $nodes;
617
        }
618
619 4
        return $files;
620
    }
621
622
    /**
623
     * @param string $source
624
     * @param string|null $destination
625
     * @param bool $recursive
626
     * @param array $map
627
     */
628 2
    protected static function importFilesFromDir($source, $destination, $recursive, &$map)
629
    {
630
        // $map[$destination] = rtrim($source, '/*');
631
        // do not map root archive folder
632
633 2
        if ($destination !== null)
634 2
            $map[$destination] = null;
635
636 2
        foreach (glob($source, GLOB_MARK) as $node) {
637 2
            if (in_array(substr($node, -1), ['/', '\\'], true) && $recursive) {
638 2
                static::importFilesFromDir(str_replace('\\', '/', $node).'*',
639 2
                    $destination.basename($node).'/', $recursive, $map);
640 2
            } elseif (is_file($node) && is_readable($node)) {
641 2
                $map[$destination.basename($node)] = $node;
642
            }
643
        }
644 2
    }
645
646
    /**
647
     * Checks whether archive can be opened with current system configuration
648
     *
649
     * @param string $fileName Archive filename
650
     * @deprecated See {UnifiedArchive::canOpen()}
651
     * @return bool
652
     */
653
    public static function canOpenArchive($fileName)
654
    {
655
        return static::canOpen($fileName);
656
    }
657
658
    /**
659
     * Checks whether specific archive type can be opened with current system configuration
660
     *
661
     * @deprecated See {{Formats::canOpen()}}
662
     * @param string $type One of predefined archive types (class constants)
663
     * @return bool
664
     */
665
    public static function canOpenType($type)
666
    {
667
        return Formats::canOpen($type);
668
    }
669
670
    /**
671
     * Checks whether specified archive can be created
672
     *
673
     * @deprecated See {{Formats::canCreate()}}
674
     * @param string $type One of predefined archive types (class constants)
675
     * @return bool
676
     */
677
    public static function canCreateType($type)
678
    {
679
        return Formats::canCreate($type);
680
    }
681
682
    /**
683
     * Returns type of archive
684
     *
685
     * @deprecated See {{UnifiedArchive::getArchiveFormat()}}
686
     * @return string One of class constants
687
     */
688
    public function getArchiveType()
689
    {
690
        return $this->getFormat();
691
    }
692
693
    /**
694
     * Detect archive type by its filename or content
695
     *
696
     * @deprecated See {{Formats::detectArchiveFormat()}}
697
     * @param string $fileName Archive filename
698
     * @param bool $contentCheck Whether archive type can be detected by content
699
     * @return string|bool One of UnifiedArchive type constants OR false if type is not detected
700
     */
701
    public static function detectArchiveType($fileName, $contentCheck = true)
702
    {
703
        return Formats::detectArchiveFormat($fileName, $contentCheck);
704
    }
705
706
    /**
707
     * Returns a resource for reading file from archive
708
     *
709
     * @deprecated See {{UnifiedArchive::getFileStream}}
710
     * @param string $fileName File name in archive
711
     * @return resource
712
     * @throws NonExistentArchiveFileException
713
     */
714
    public function getFileResource($fileName)
715
    {
716
        return $this->getFileStream($fileName);
717
    }
718
719
    /**
720
     * Returns type of archive
721
     *
722
     * @deprecated See {{UnifiedArchive::getFormat}}
723
     * @return string One of class constants
724
     */
725
    public function getArchiveFormat()
726
    {
727
        return $this->getFormat();
728
    }
729
730
    /**
731
     * Checks that file exists in archive
732
     *
733
     * @deprecated See {{UnifiedArchive::hasFile}}
734
     * @param string $fileName File name in archive
735
     * @return bool
736
     */
737
    public function isFileExists($fileName)
738
    {
739
        return $this->hasFile($fileName);
740
    }
741
742
    /**
743
     * Returns size of archive file in bytes
744
     *
745
     * @deprecated See {{UnifiedArchive::getSize}}
746
     * @return int
747
     */
748
    public function getArchiveSize()
749
    {
750
        return $this->getSize();
751
    }
752
}
753