blob: 43f6f38839eecfd39a30e14dd1db58ad98a0be91 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
|
<?php
namespace Icinga\Module\Fileshipper\ProvidedHook\Director;
use Exception;
use Icinga\Application\Config;
use Icinga\Module\Director\Hook\ShipConfigFilesHook;
use RecursiveDirectoryIterator;
use RecursiveIteratorIterator;
use RegexIterator;
class ShipConfigFiles extends ShipConfigFilesHook
{
/**
* @return array
*/
public function fetchFiles()
{
$files = [];
foreach ($this->getDirectories() as $key => $cfg) {
$target = $cfg->get('target');
try {
foreach ($this->listFiles($cfg->get('source'), $cfg->get('extensions')) as $file) {
try {
$files["$target/$file"] = file_get_contents($cfg->get('source') . '/' . $file);
} catch (Exception $e) {
$files["$target/$file"] = '/* ' . $e->getMessage() . ' */';
}
}
} catch (Exception $e) {
$files["$target/ERROR.txt"] = '/* ' . $e->getMessage() . ' */';
}
}
return $files;
}
/**
* @param $folder
* @param $extensions
* @return array
*/
protected function listFiles($folder, $extensions)
{
if (! $extensions) {
$pattern = '/^[^\.].+\.conf$/';
} else {
$exts = [];
foreach (preg_split('/\s+/', $extensions, -1, PREG_SPLIT_NO_EMPTY) as $ext) {
$exts[] = preg_quote($ext, '/');
}
$pattern = '/^[^\.].+(?:' . implode('|', $exts) . ')$/';
}
$dir = new RecursiveDirectoryIterator($folder);
$ite = new RecursiveIteratorIterator($dir);
$files = new RegexIterator($ite, $pattern, RegexIterator::GET_MATCH);
$fileList = [];
$start = strlen($folder) + 1;
foreach ($files as $file) {
foreach ($file as $f) {
$fileList[] = substr($f, $start);
}
}
return $fileList;
}
/**
* @return Config
*/
protected function getDirectories()
{
return Config::module('fileshipper', 'directories');
}
}
|