-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathPackageManagerInstaller.php
More file actions
119 lines (105 loc) · 5.25 KB
/
PackageManagerInstaller.php
File metadata and controls
119 lines (105 loc) · 5.25 KB
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
<?php
namespace axenox\PackageManager;
use exface\Core\CommonLogic\AppInstallers\AbstractAppInstaller;
use exface\Core\Exceptions\UnexpectedValueException;
class PackageManagerInstaller extends AbstractAppInstaller
{
/**
*
* {@inheritDoc}
* @see \exface\Core\Interfaces\InstallerInterface::install()
*/
public function install(string $source_absolute_path) : \Iterator
{
$idt = $this->getOutputIndentation();
$root_composer_json_path = $this->getWorkbench()->filemanager()->getPathToBaseFolder() . DIRECTORY_SEPARATOR . 'composer.json';
if (! file_exists($root_composer_json_path)) {
yield $idt . 'Root composer.json not found under "' . $root_composer_json_path . '" - automatic installation of apps will not work! See the package manager docs for solutions.' . PHP_EOL;
return;
}
try {
$root_composer_json = $this->parseComposerJson($root_composer_json_path);
} catch (\Throwable $e) {
yield $idt . 'ERROR: ' . $e->getMessage() . ' - automatic installation of apps will not work! See the package manager docs for solutions.';
}
$result = '';
$changes = 0;
if (! isset($root_composer_json['autoload']['psr-0']["axenox\\PackageManager"])) {
$root_composer_json['autoload']['psr-0']["axenox\\PackageManager"] = "vendor/";
$changes ++;
}
// Package install/update scripts
if (! is_array($root_composer_json['scripts']['post-package-install']) || ! in_array("axenox\\PackageManager\\StaticInstaller::composerFinishPackageInstall", $root_composer_json['scripts']['post-package-install'])) {
$root_composer_json['scripts']['post-package-install'][] = "axenox\\PackageManager\\StaticInstaller::composerFinishPackageInstall";
$changes ++;
}
if (! is_array($root_composer_json['scripts']['post-package-update']) || ! in_array("axenox\\PackageManager\\StaticInstaller::composerFinishPackageUpdate", $root_composer_json['scripts']['post-package-update'])) {
$root_composer_json['scripts']['post-package-update'][] = "axenox\\PackageManager\\StaticInstaller::composerFinishPackageUpdate";
$changes ++;
}
// Overall install/update scripts
if (! is_array($root_composer_json['scripts']['post-update-cmd']) || ! in_array("axenox\\PackageManager\\StaticInstaller::composerFinishUpdate", $root_composer_json['scripts']['post-update-cmd'])) {
$root_composer_json['scripts']['post-update-cmd'][] = "axenox\\PackageManager\\StaticInstaller::composerFinishUpdate";
$changes ++;
}
if (! is_array($root_composer_json['scripts']['post-install-cmd']) || ! in_array("axenox\\PackageManager\\StaticInstaller::composerFinishInstall", $root_composer_json['scripts']['post-install-cmd'])) {
$root_composer_json['scripts']['post-install-cmd'][] = "axenox\\PackageManager\\StaticInstaller::composerFinishInstall";
$changes ++;
}
if ($changes > 0) {
$this->getWorkbench()->filemanager()->dumpFile($root_composer_json_path, json_encode($root_composer_json, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES));
yield $idt . "Configured root composer.json for automatic app installation" . PHP_EOL;
} else {
yield $idt . "Checked root composer.json" . PHP_EOL;
}
}
public function update($source_absolute_path)
{
return $this->install();
}
public function uninstall() : \Iterator
{
return 'Uninstall not implemented for' . $this->getSelectorInstalling()->getAliasWithNamespace() . '!';
}
/**
*
* {@inheritDoc}
* @see \exface\Core\Interfaces\InstallerInterface::backup()
*/
public function backup(string $destination_absolute_path) : \Iterator
{
return new \EmptyIterator();
}
protected function parseComposerJson($root_composer_json_path)
{
$root_composer_json = json_decode(file_get_contents($root_composer_json_path), true);
if (! is_array($root_composer_json)) {
switch (json_last_error()) {
case JSON_ERROR_NONE:
$error = 'No errors';
break;
case JSON_ERROR_DEPTH:
$error = 'Maximum stack depth exceeded';
break;
case JSON_ERROR_STATE_MISMATCH:
$error = 'Underflow or the modes mismatch';
break;
case JSON_ERROR_CTRL_CHAR:
$error = 'Unexpected control character found';
break;
case JSON_ERROR_SYNTAX:
$error = 'Syntax error, malformed JSON';
break;
case JSON_ERROR_UTF8:
$error = 'Malformed UTF-8 characters, possibly incorrectly encoded';
break;
default:
$error = 'Unknown error';
break;
}
throw new UnexpectedValueException('Cannot parse root composer.json under "' . $root_composer_json_path . '": ' . $error);
}
return $root_composer_json;
}
}
?>