1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
//require ('../modulechecks/vendor/autoload.php'); |
4
|
|
|
|
5
|
|
|
use Symfony\Component\Yaml\Yaml; |
6
|
|
|
|
7
|
|
|
class ConfigYML extends Object |
8
|
|
|
{ |
9
|
|
|
public function __construct($gitHubModuleInstance) |
10
|
|
|
{ |
11
|
|
|
if (! $gitHubModuleInstance) { |
12
|
|
|
user_error("ConfigYML needs an instance of GitHubModule"); |
13
|
|
|
} |
14
|
|
|
$this->gitHubModuleInstance = $gitHubModuleInstance; |
|
|
|
|
15
|
|
|
$this->moduleName = $gitHubModuleInstance->ModuleName; |
|
|
|
|
16
|
|
|
$this->yaml_data = null; |
|
|
|
|
17
|
|
|
$folder = GitHubModule::Config()->get('absolute_temp_folder'); |
18
|
|
|
|
19
|
|
|
$this->filename = $folder . '/' . $this->moduleName . '/_config/config.yml'; |
|
|
|
|
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
public function reWrite() |
23
|
|
|
{ |
24
|
|
|
if (! $this->readYMLFromFile()) { |
25
|
|
|
return false; |
26
|
|
|
} |
27
|
|
|
if (! $this->writeYAMLToFile()) { |
28
|
|
|
return false; |
29
|
|
|
} |
30
|
|
|
return true; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
public function readYMLFromFile() |
34
|
|
|
{ |
35
|
|
|
GeneralMethods::output_to_screen("reading config yml ... ", 'updating'); |
36
|
|
|
|
37
|
|
|
if (! file_exists($this->filename)) { |
38
|
|
|
GeneralMethods::output_to_screen("<li>Unable to load: " . $this->filename, 'updated') ; |
39
|
|
|
//UpdateModules::$unsolvedItems[$this->gitHubModuleInstance->ModuleName] = "Unable to load " . $this->filename; |
40
|
|
|
|
41
|
|
|
UpdateModules::addUnsolvedProblem($this->gitHubModuleInstance->ModuleName, "Unable to load " . $this->filename); |
42
|
|
|
return false; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
try { |
46
|
|
|
$this->yaml_data = Yaml::parse(file_get_contents($this->filename)); |
|
|
|
|
47
|
|
|
} catch (Exception $e) { |
48
|
|
|
GeneralMethods::output_to_screen("<li>Unable to parse the YAML string: " .$e->getMessage(). " <li>", 'updated') ; |
49
|
|
|
|
50
|
|
|
//UpdateModules::$unsolvedItems[$this->gitHubModuleInstance->ModuleName] = "Unable to parse the YAML string: " .$e->getMessage(); |
51
|
|
|
|
52
|
|
|
UpdateModules::addUnsolvedProblem($this->gitHubModuleInstance->ModuleName, "Unable to parse the YAML string: " .$e->getMessage()); |
53
|
|
|
|
54
|
|
|
//trigger_error ("Error in YML file"); |
55
|
|
|
|
56
|
|
|
$this->replaceFaultyYML(); |
57
|
|
|
|
58
|
|
|
return false; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
|
62
|
|
|
return $this->yaml_data; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
|
66
|
|
|
public function replaceFaultyYML() |
67
|
|
|
{ |
68
|
|
|
return false; |
69
|
|
|
|
70
|
|
|
/**function broken do not use**/ |
71
|
|
|
|
72
|
|
|
|
73
|
|
|
if (file_exists($this->filename)) { |
|
|
|
|
74
|
|
|
$rawYML = file_get_contents($this->filename); |
75
|
|
|
|
76
|
|
|
$lines = explode("\n", $rawYML); |
77
|
|
|
|
78
|
|
|
$replacment = ''; |
79
|
|
|
|
80
|
|
|
foreach ($lines as $index=>$line) { |
81
|
|
|
if (strpos($line, "After:") !==false) { |
82
|
|
|
$replacment = "After:"; |
83
|
|
|
$listitems = explode(',', $line); |
84
|
|
|
//print_r ($listitems); |
85
|
|
|
foreach ($listitems as $item) { |
86
|
|
|
if (! trim($item)) { |
87
|
|
|
continue; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
$item = str_replace('After: ', '', $item); |
91
|
|
|
$replacment .= ' - '. trim($item) . ""; |
92
|
|
|
} |
93
|
|
|
$lines[$index] = $replacment; |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
$newYML = implode('', $lines); |
97
|
|
|
|
98
|
|
|
GeneralMethods::output_to_screen("Updating config.YML to correct syntax ... ", 'updating'); |
99
|
|
|
|
100
|
|
|
$file = fopen($this->filename, "w"); |
101
|
|
|
|
102
|
|
|
|
103
|
|
|
|
104
|
|
|
file_put_contents($this->filename, $newYML); |
105
|
|
|
} else { |
106
|
|
|
return false; |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
public function writeYAMLToFile() |
110
|
|
|
{ |
111
|
|
|
GeneralMethods::output_to_screen("Writing config yml ... ", 'updating'); |
112
|
|
|
|
113
|
|
|
if (!$this->yaml_data) { |
114
|
|
|
return false; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
$yaml = Yaml::dump($this->yaml_data); |
118
|
|
|
file_put_contents($this->filename, $yaml); |
119
|
|
|
return true; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
private function catchFopenWarning() |
123
|
|
|
{ |
124
|
|
|
} |
125
|
|
|
} |
126
|
|
|
|