-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathPackageManagerApp.php
More file actions
187 lines (167 loc) · 6.73 KB
/
PackageManagerApp.php
File metadata and controls
187 lines (167 loc) · 6.73 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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
<?php
namespace axenox\PackageManager;
use exface\Core\Interfaces\AppInterface;
use exface\Core\Factories\DataSheetFactory;
use exface\Core\Interfaces\InstallerInterface;
use exface\Core\CommonLogic\Model\App;
use exface\Core\CommonLogic\Filemanager;
use exface\Core\Interfaces\Selectors\AliasSelectorInterface;
use exface\Core\CommonLogic\AppInstallers\AbstractSqlDatabaseInstaller;
use exface\Core\Facades\AbstractHttpFacade\HttpFacadeInstaller;
use exface\Core\Factories\FacadeFactory;
use axenox\PackageManager\Facades\PackagistFacade;
use axenox\PackageManager\Facades\UpdaterFacade;
/**
*
* @author Andrej Kabachnik
*
*/
class PackageManagerApp extends App
{
const FOLDER_NAME_MODEL = 'Model';
public function filemanager()
{
return $this->getWorkbench()->filemanager();
}
public function createAppFolder(AppInterface $app)
{
Filemanager::pathConstruct($app->getDirectoryAbsolutePath());
$this->createComposerJson($app);
return $this;
}
/**
*
* @param AppInterface $app
* @return array
*/
protected function createComposerJson(AppInterface $app)
{
$json = array(
"name" => mb_strtolower($app->getVendor() . '/' . str_replace($app->getVendor() . AliasSelectorInterface::ALIAS_NAMESPACE_DELIMITER, '', $app->getAliasWithNamespace())),
"require" => array(
"exface/core" => '^1.0'
),
"autoload" => [
"psr-4" => [
"\\" . str_replace(AliasSelectorInterface::ALIAS_NAMESPACE_DELIMITER, "\\", $app->getAliasWithNamespace()) . "\\" => ""
],
"exclude-from-classmap" => [
"/Config/",
"/Translations/",
"/Model/"
]
]
);
return $json;
}
/**
*
* @param AppInterface $app
* @return array
*/
public function getComposerJson(AppInterface $app)
{
$file_path = $this->getPathToComposerJson($app);
if (file_exists($file_path)) {
$json = json_decode(file_get_contents($file_path), true);
} else {
$json = $this->createComposerJson($app);
//$this->setComposerJson($app, $json);
}
return $json;
}
/**
*
* @param AppInterface $app
* @param array $json_object
* @return \axenox\PackageManager\PackageManagerApp
*/
public function setComposerJson(AppInterface $app, array $json_object)
{
$file_path = $this->getPathToComposerJson($app);
$this->filemanager()->dumpFile($file_path, json_encode($json_object, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES));
return $this;
}
public function getPathToComposerJson(AppInterface $app)
{
return $this->getPathToAppAbsolute($app) . DIRECTORY_SEPARATOR . 'composer.json';
}
/**
* Returns the path to the
*
* @param AppInterface $app
* @return string
*/
public function getPathToAppRelative(AppInterface $app = null, $base_path = '')
{
if (! $base_path) {
$base_path = Filemanager::FOLDER_NAME_VENDOR . DIRECTORY_SEPARATOR;
}
return $base_path . mb_strtolower($app ? $app->getVendor() . DIRECTORY_SEPARATOR . str_replace($app->getVendor() . AliasSelectorInterface::ALIAS_NAMESPACE_DELIMITER, '', $app->getAlias()) : '');
}
public function getPathToAppAbsolute(AppInterface $app = null, $base_path = '')
{
return $this->getWorkbench()->getInstallationPath() . DIRECTORY_SEPARATOR . $this->getPathToAppRelative($app, $base_path);
}
public function getInstalledVersion($app_alias)
{
$package_object = $this->getWorkbench()->model()->getObject('axenox.PackageManager.PACKAGE_INSTALLED');
$data_sheet = DataSheetFactory::createFromObject($package_object);
$data_sheet->getColumns()->addFromExpression('version');
$data_sheet->getFilters()->addConditionFromString('name', $this->getPackageNameFromAppAlias($app_alias));
$data_sheet->dataRead();
return $data_sheet->getCellValue('version', 0);
}
public static function getPackageNameFromAppAlias($app_alias)
{
return str_replace(AliasSelectorInterface::ALIAS_NAMESPACE_DELIMITER, '/', $app_alias);
}
public static function getAppAliasFromPackageName($package_name)
{
return str_replace('/', AliasSelectorInterface::ALIAS_NAMESPACE_DELIMITER, $package_name);
}
public function getCurrentAppVersion($app_alias)
{
$exface = $this->getWorkbench();
$ds = DataSheetFactory::createFromObjectIdOrAlias($exface, 'Axenox.PackageManager.PACKAGE_INSTALLED');
$ds->getFilters()->addConditionFromString('name', $this->getPackageNameFromAppAlias($app_alias));
$ds->getColumns()->addFromExpression('version');
$ds->dataRead();
return $ds->getCellValue('version', 0);
}
/**
* The installer of the package manager app will perform some additional actions like setting up composer.json to run the
* required postprocessing, etc.
*
* {@inheritdoc}
*
* @see App::getInstaller($injected_installer)
*/
public function getInstaller(InstallerInterface $injected_installer = null)
{
$installer = parent::getInstaller($injected_installer);
$installer->addInstaller(new PackageManagerInstaller($this->getSelector()));
$modelLoader = $this->getWorkbench()->model()->getModelLoader();
$modelConnection = $modelLoader->getDataConnection();
$installerClass = get_class($modelLoader->getInstaller()->getInstallers()[0]);
$schema_installer = new $installerClass($this->getSelector());
if ($schema_installer instanceof AbstractSqlDatabaseInstaller) {
$schema_installer
->setFoldersWithMigrations(['InitDB','Migrations'])
->setDataConnection($modelConnection)
->setFoldersWithStaticSql(['Views'])
->setMigrationsTableName('_migrations_packagemanager');
}
$installer->addInstaller($schema_installer);
// Packagist facade
$facadeInstaller = new HttpFacadeInstaller($this->getSelector());
$facadeInstaller->setFacade(FacadeFactory::createFromString(PackagistFacade::class, $this->getWorkbench()));
$installer->addInstaller($facadeInstaller);
// Updater facade
$facadeInstaller = new HttpFacadeInstaller($this->getSelector());
$facadeInstaller->setFacade(FacadeFactory::createFromString(UpdaterFacade::class, $this->getWorkbench()));
$installer->addInstaller($facadeInstaller);
return $installer;
}
}
?>