1 | <?php |
||
2 | |||
3 | class ComposerJson extends Object |
||
4 | { |
||
5 | /** |
||
6 | * |
||
7 | * @var array|null |
||
8 | */ |
||
9 | protected $jsonData = null; |
||
10 | |||
11 | public function __construct($gitHubModuleInstance) |
||
12 | { |
||
13 | if (! $gitHubModuleInstance) { |
||
14 | user_error("CheckComposerJson needs an instance of GitHubModule"); |
||
15 | } |
||
16 | $this->gitHubModuleInstance = $gitHubModuleInstance; |
||
0 ignored issues
–
show
Bug
Best Practice
introduced
by
![]() |
|||
17 | $this->moduleName = $gitHubModuleInstance->ModuleName; |
||
0 ignored issues
–
show
|
|||
18 | } |
||
19 | |||
20 | private function readJsonFromFile() |
||
21 | { |
||
22 | $folder = GitHubModule::Config()->get('absolute_temp_folder'); |
||
23 | $filename = $folder . '/' . $this->moduleName . '/composer.json'; |
||
24 | set_error_handler(array($this, 'catchFopenWarning'), E_WARNING); |
||
25 | $json = file_get_contents($filename); |
||
26 | restore_error_handler(); |
||
27 | if ($json) { |
||
28 | $this->jsonData = json_decode($json, true); |
||
29 | } else { |
||
30 | UpdateModules::addUnsolvedProblem($this->moduleName, 'Could not open composer.json file...'); |
||
31 | } |
||
32 | return (is_array($this->jsonData)); |
||
33 | } |
||
34 | |||
35 | |||
36 | /** |
||
37 | * @return array |
||
38 | */ |
||
39 | public function getJsonData() |
||
40 | { |
||
41 | return $this->jsonData; |
||
42 | } |
||
43 | |||
44 | /** |
||
45 | * @param array $array [description] |
||
46 | */ |
||
47 | public function setJsonData(array $array) |
||
48 | { |
||
49 | $this->jsonData = $array; |
||
50 | } |
||
51 | |||
52 | |||
53 | |||
54 | public function updateJsonFile() |
||
55 | { |
||
56 | if (! is_array($this->jsonData)) { |
||
57 | $this->readJsonFromFile(); |
||
58 | } |
||
59 | |||
60 | |||
61 | if (is_array($this->jsonData)) { |
||
62 | GeneralMethods::output_to_screen("<li> Updating composer.json </li>"); |
||
63 | $composerUpdates = ClassInfo::subclassesFor('UpdateComposer'); |
||
64 | |||
65 | //remove base class |
||
66 | array_shift($composerUpdates); |
||
67 | |||
68 | //get all updates and if they exists then get the ones that we need to do ... |
||
69 | $limitedComposerUpdates = $this->Config()->get('updates'); |
||
70 | if ($limitedComposerUpdates === 'none') { |
||
71 | $composerUpdates = []; |
||
72 | } elseif (is_array($limitedComposerUpdates) && count($limitedComposerUpdates)) { |
||
73 | $composerUpdates = array_intersect($composerUpdates, $limitedComposerUpdates); |
||
74 | } |
||
75 | |||
76 | |||
77 | foreach ($composerUpdates as $composerUpdate) { |
||
78 | $obj = $composerUpdate::create($this); |
||
79 | $obj->run(); |
||
80 | } |
||
81 | if ($this->writeJsonToFile()) { |
||
82 | GeneralMethods::output_to_screen("<li> Updated JSON </li>"); |
||
83 | } else { |
||
84 | UpdateModules::addUnsolvedProblem($this->moduleName, 'Could not write JSON'); |
||
85 | } |
||
86 | } else { |
||
87 | //UpdateModules::$unsolvedItems[$this->moduleName] = 'No composer.json'; |
||
88 | |||
89 | UpdateModules::addUnsolvedProblem($this->moduleName, 'No composer.json'); |
||
90 | } |
||
91 | } |
||
92 | |||
93 | |||
94 | protected function writeJsonToFile() |
||
95 | { |
||
96 | if (! is_array($this->jsonData)) { //if not loaded |
||
97 | return false; |
||
98 | } |
||
99 | |||
100 | $folder = GitHubModule::Config()->get('absolute_temp_folder'); |
||
101 | $filename = $folder . '/' . $this->gitHubModuleInstance->ModuleName . '/composer.json'; |
||
102 | $value = file_put_contents($filename, json_encode($this->jsonData, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES)); |
||
103 | |||
104 | return $value ? true : false; |
||
105 | } |
||
106 | |||
107 | public function getDescription() |
||
108 | { |
||
109 | if (! is_array($this->jsonData)) { //if not loaded |
||
110 | return 'no json data'; |
||
111 | } |
||
112 | |||
113 | return isset($this->jsonData['description']) ? $this->jsonData['description'] : 'description tba'; |
||
114 | } |
||
115 | |||
116 | |||
117 | private function catchFopenWarning() |
||
118 | { |
||
119 | user_error('Can not open composer file ....'); |
||
120 | } |
||
121 | } |
||
122 |