Skip to content

Commit edeb3bc

Browse files
committed
rework installer
1 parent f1a3b7e commit edeb3bc

File tree

2 files changed

+102
-10
lines changed

2 files changed

+102
-10
lines changed

src/Installer.php

+10-5
Original file line numberDiff line numberDiff line change
@@ -3,25 +3,30 @@
33
namespace nullref\core;
44

55

6-
use Composer\Installer\PackageEvent;
76
use nullref\core\components\ModuleInstaller;
87

98
class Installer extends ModuleInstaller
109
{
10+
public $moduleId = 'core';
1111

12-
public function install(PackageEvent $event)
12+
public function getModuleId()
13+
{
14+
return 'core';
15+
}
16+
17+
public function install()
1318
{
1419
$this->downloadComposer();
15-
parent::install($event);
20+
parent::install();
1621
}
1722

18-
public function uninstall(PackageEvent $event)
23+
public function uninstall()
1924
{
2025
$path = $this->getComposerPath();
2126
if (file_exists($path)) {
2227
@unlink($path);
2328
}
24-
parent::uninstall($event);
29+
parent::uninstall();
2530
}
2631

2732
protected function downloadComposer()

src/components/ModuleInstaller.php

+92-5
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,21 @@
55
use yii\base\Component;
66
use yii\db\Connection;
77
use yii\di\Instance;
8-
use Composer\Installer\PackageEvent;
98

109
/**
11-
*
10+
* @property string $moduleId
1211
*/
13-
class ModuleInstaller extends Component
12+
abstract class ModuleInstaller extends Component
1413
{
1514
/**
1615
* @var Connection|array|string
1716
*/
1817
public $db = 'db';
1918

19+
public abstract function getModuleId();
20+
21+
public $updateConfig = true;
22+
2023
public function init()
2124
{
2225
parent::init();
@@ -26,12 +29,16 @@ public function init()
2629

2730
public function install()
2831
{
29-
//do some stuff
32+
if ($this->updateConfig) {
33+
$this->addToConfig();
34+
}
3035
}
3136

3237
public function uninstall()
3338
{
34-
//do some stuff
39+
if ($this->updateConfig) {
40+
$this->removeFromConfig();
41+
}
3542
}
3643

3744
/**
@@ -63,6 +70,86 @@ public function createTable($table, $columns, $options = null)
6370
{
6471
$this->db->createCommand()->createTable($table, $columns, $options)->execute();
6572
}
73+
74+
75+
/**
76+
* Add module item to config
77+
*/
78+
protected function addToConfig()
79+
{
80+
$path = $this->getConfigPath();
81+
$config = require($path);
82+
83+
$config[$this->moduleId] = $this->getConfigArray();
84+
85+
$this->writeConfigFile($config);
86+
}
87+
88+
/**
89+
* @return array
90+
*/
91+
protected function getConfigArray()
92+
{
93+
return ['class' => str_replace('Installer', 'Module', static::class)];
94+
}
95+
96+
/**
97+
* Remove module item from config
98+
*/
99+
protected function removeFromConfig()
100+
{
101+
$path = $this->getConfigPath();
102+
$config = require($path);
103+
104+
if (isset($config[$this->moduleId])) {
105+
unset($config[$this->moduleId]);
106+
}
107+
$this->writeConfigFile($config);
108+
}
109+
110+
/**
111+
* Write config file
112+
* @param $var
113+
*/
114+
protected function writeConfigFile($var)
115+
{
116+
file_put_contents($this->getConfigPath(), '<?php' . PHP_EOL . 'return ' . $this->varExport($var) . ';');
117+
}
118+
119+
/**
120+
* @return bool|string
121+
*/
122+
protected function getConfigPath()
123+
{
124+
return \Yii::getAlias('@app/config/modules.php');
125+
}
126+
127+
/**
128+
* var_export as php 5.4
129+
* @param $var
130+
* @param string $indent
131+
* @return mixed|string
132+
*/
133+
protected function varExport($var, $indent = "")
134+
{
135+
switch (gettype($var)) {
136+
case "string":
137+
return '"' . addcslashes($var, "\\\$\"\r\n\t\v\f") . '"';
138+
case "array":
139+
$indexed = array_keys($var) === range(0, count($var) - 1);
140+
$r = [];
141+
foreach ($var as $key => $value) {
142+
$r[] = "$indent "
143+
. ($indexed ? "" : $this->varExport($key) . " => ")
144+
. $this->varExport($value, "$indent ");
145+
}
146+
return "[\n" . implode(",\n", $r) . $indent . "\n ]";
147+
case "boolean":
148+
return $var ? "true" : "false";
149+
default:
150+
return var_export($var, true);
151+
}
152+
}
66153
}
67154

68155

0 commit comments

Comments
 (0)