1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* This file is part of Cecil. |
7
|
|
|
* |
8
|
|
|
* Copyright (c) Arnaud Ligny <[email protected]> |
9
|
|
|
* |
10
|
|
|
* For the full copyright and license information, please view the LICENSE |
11
|
|
|
* file that was distributed with this source code. |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
namespace Cecil\Command\ShowContent; |
15
|
|
|
|
16
|
|
|
use RecursiveFilterIterator; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Filters files by extension type. |
20
|
|
|
*/ |
21
|
|
|
class FileExtensionFilter extends RecursiveFilterIterator |
22
|
|
|
{ |
23
|
|
|
/** @var array */ |
24
|
|
|
protected $allowedExt = ['md', 'yml']; |
25
|
|
|
|
26
|
|
|
/** @var array */ |
27
|
|
|
protected $excludedDir = ['.git', '.cecil', '.cache', '_site', 'vendor', 'node_modules']; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @param \RecursiveIterator $iterator |
31
|
|
|
* @param string|array|null $extensions |
32
|
|
|
*/ |
33
|
|
|
public function __construct(\RecursiveIterator $iterator, $extensions = null) |
34
|
|
|
{ |
35
|
|
|
if (!\is_null($extensions)) { |
36
|
|
|
if (!\is_array($extensions)) { |
37
|
|
|
$extensions = [$extensions]; |
38
|
|
|
} |
39
|
|
|
$this->allowedExt = $extensions; |
40
|
|
|
} |
41
|
|
|
parent::__construct($iterator); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Get children with allowed extensions. |
46
|
|
|
*/ |
47
|
|
|
public function getChildren(): ?RecursiveFilterIterator |
48
|
|
|
{ |
49
|
|
|
return new self($this->getInnerIterator()->getChildren(), $this->allowedExt); // @phpstan-ignore-line |
|
|
|
|
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Valid file with allowed extensions. |
54
|
|
|
*/ |
55
|
|
|
public function accept(): bool |
56
|
|
|
{ |
57
|
|
|
/** @var \SplFileInfo $file */ |
58
|
|
|
$file = $this->current(); |
59
|
|
|
if ($file->isFile()) { |
60
|
|
|
return \in_array($file->getExtension(), $this->allowedExt); |
61
|
|
|
} |
62
|
|
|
if ($file->isDir()) { |
63
|
|
|
return !\in_array($file->getBasename(), $this->excludedDir); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
return true; |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
|