1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace JK\SamBundle\Watcher\Indexer; |
4
|
|
|
|
5
|
|
|
use Exception; |
6
|
|
|
use SplFileInfo; |
7
|
|
|
use Symfony\Component\Filesystem\Filesystem; |
8
|
|
|
use Symfony\Component\Finder\Finder; |
9
|
|
|
|
10
|
|
|
class FileIndexer implements FileIndexerInterface |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* @var integer[] |
14
|
|
|
*/ |
15
|
|
|
protected $index = []; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @var SplFileInfo[] |
19
|
|
|
*/ |
20
|
|
|
protected $changes = []; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Index a given directory : add each found files (according to the given extension) to the index. |
24
|
|
|
* |
25
|
|
|
* @param array $directories |
26
|
|
|
* @param array $extensions |
27
|
|
|
* |
28
|
|
|
* @throws Exception |
29
|
|
|
*/ |
30
|
|
|
public function index(array $directories, array $extensions = []) |
31
|
|
|
{ |
32
|
|
|
$fileSystem = new Filesystem(); |
33
|
|
|
$finder = new Finder(); |
34
|
|
|
|
35
|
|
|
// reset the change set |
36
|
|
|
$this->changes = []; |
37
|
|
|
clearstatcache(); |
38
|
|
|
|
39
|
|
|
foreach ($directories as $directory) { |
40
|
|
|
if (!$fileSystem->exists($directory)) { |
41
|
|
|
throw new Exception('The directory '.$directory.' does not exists'); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
// find all files in the given directory |
45
|
|
|
$finder |
46
|
|
|
->files() |
47
|
|
|
->in($directory) |
48
|
|
|
; |
49
|
|
|
|
50
|
|
|
// eventually filtering by file extension |
51
|
|
|
foreach ($extensions as $extension) { |
52
|
|
|
$finder->name('*.'.$extension); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
// index the results |
56
|
|
|
foreach ($finder as $file) { |
57
|
|
|
$this->add($file); |
58
|
|
|
} |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Add an entry to the indexer. If a entry already exists, it will also be added to the changes. |
65
|
|
|
* |
66
|
|
|
* @param SplFileInfo $splFileInfo |
67
|
|
|
* |
68
|
|
|
* @throws Exception |
69
|
|
|
*/ |
70
|
|
|
public function add(SplFileInfo $splFileInfo) |
71
|
|
|
{ |
72
|
|
|
$fileSystem = new Filesystem(); |
73
|
|
|
|
74
|
|
|
if (!$fileSystem->exists($splFileInfo->getRealPath())) { |
75
|
|
|
throw new Exception('Trying to add '.$splFileInfo->getRealPath().' missing file to the index'); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
// if the file is already present in the index, and its mtime has been modified, we add it to the change set |
79
|
|
|
if ($this->has($splFileInfo->getRealPath()) |
80
|
|
|
&& $splFileInfo->getMTime() !== $this->index[$splFileInfo->getRealPath()]) { |
81
|
|
|
$this->changes[$splFileInfo->getRealPath()] = $splFileInfo; |
82
|
|
|
} |
83
|
|
|
// add new or existing file to the index |
84
|
|
|
$this->index[$splFileInfo->getRealPath()] = $splFileInfo->getMTime(); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Return an existing index entry. |
89
|
|
|
* |
90
|
|
|
* @param string $entryName |
91
|
|
|
* |
92
|
|
|
* @return SplFileInfo |
93
|
|
|
* |
94
|
|
|
* @throws Exception |
95
|
|
|
*/ |
96
|
|
|
public function get($entryName) |
97
|
|
|
{ |
98
|
|
|
if (!$this->has($entryName)) { |
99
|
|
|
throw new Exception('Trying to get invalid index entry : '.$entryName); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
return new SplFileInfo($entryName); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Return true if the entry exists, false otherwise. |
107
|
|
|
* |
108
|
|
|
* @param $entryName |
109
|
|
|
* |
110
|
|
|
* @return bool |
111
|
|
|
*/ |
112
|
|
|
public function has($entryName) |
113
|
|
|
{ |
114
|
|
|
return array_key_exists($entryName, $this->index); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* Return true if the indexer has new changes since last index. |
119
|
|
|
* |
120
|
|
|
* @return bool |
121
|
|
|
*/ |
122
|
|
|
public function hasChangedEntries() |
123
|
|
|
{ |
124
|
|
|
return count($this->changes) > 0; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* Return the changed entry since last index. |
129
|
|
|
* |
130
|
|
|
* @return SplFileInfo[] |
131
|
|
|
*/ |
132
|
|
|
public function getChangedEntries() |
133
|
|
|
{ |
134
|
|
|
return $this->changes; |
135
|
|
|
} |
136
|
|
|
} |
137
|
|
|
|