openssl signing
[pharext/pharext] / src / pharext / FilteredSourceDir.php
1 <?php
2
3 namespace pharext;
4
5 /**
6 * Generic filtered source directory
7 */
8 class FilteredSourceDir extends \FilterIterator implements SourceDir
9 {
10 /**
11 * The Packager command
12 * @var pharext\Command
13 */
14 private $cmd;
15
16 /**
17 * Base directory
18 * @var string
19 */
20 private $path;
21
22 /**
23 * Exclude filters
24 * @var array
25 */
26 private $filter = [".git/*", ".hg/*"];
27
28 /**
29 * @inheritdoc
30 * @see \pharext\SourceDir::__construct()
31 */
32 public function __construct(Command $cmd, $path) {
33 $this->cmd = $cmd;
34 $this->path = $path;
35 parent::__construct(
36 new \RecursiveIteratorIterator(
37 new \RecursiveDirectoryIterator($path,
38 \FilesystemIterator::KEY_AS_PATHNAME |
39 \FilesystemIterator::CURRENT_AS_FILEINFO |
40 \FilesystemIterator::SKIP_DOTS
41 )
42 )
43 );
44 foreach ([".gitignore", ".hgignore"] as $ignore) {
45 if (file_exists("$path/$ignore")) {
46 $this->filter = array_merge($this->filter,
47 array_map(function($pat) {
48 $pat = trim($pat);
49 if (substr($pat, -1) == '/') {
50 $pat .= '*';
51 }
52 return $pat;
53 }, file("$path/$ignore",
54 FILE_IGNORE_NEW_LINES |
55 FILE_SKIP_EMPTY_LINES
56 ))
57 );
58 }
59 }
60 }
61
62 /**
63 * @inheritdoc
64 * @see \pharext\SourceDir::getBaseDir()
65 */
66 public function getBaseDir() {
67 return $this->path;
68 }
69
70 /**
71 * Implements FilterIterator
72 * @see FilterIterator::accept()
73 */
74 public function accept() {
75 $fn = $this->key();
76 if (is_dir($fn)) {
77 if ($this->cmd->getArgs()->verbose) {
78 $this->info("Excluding %s\n", $fn);
79 }
80 return false;
81 }
82 $pl = strlen($this->path) + 1;
83 $pn = substr($this->key(), $pl);
84 foreach ($this->filter as $pat) {
85 if (fnmatch($pat, $pn)) {
86 if ($this->cmd->getArgs()->verbose) {
87 $this->info("Excluding %s\n", $pn);
88 }
89 return false;
90 }
91 }
92 if ($this->cmd->getArgs()->verbose) {
93 $this->info("Packaging %s\n", $pn);
94 }
95 return true;
96 }
97 }