1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Yawik project. |
5
|
|
|
* |
6
|
|
|
* For the full copyright and license information, please view the LICENSE |
7
|
|
|
* file that was distributed with this source code. |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
|
11
|
|
|
namespace Yawik\Composer; |
12
|
|
|
|
13
|
|
|
use Composer\Composer; |
14
|
|
|
use Composer\DependencyResolver\Operation\InstallOperation; |
15
|
|
|
use Composer\DependencyResolver\Operation\UpdateOperation; |
16
|
|
|
use Composer\DependencyResolver\Operation\UninstallOperation; |
17
|
|
|
use Composer\EventDispatcher\EventSubscriberInterface; |
18
|
|
|
use Composer\Installer\PackageEvent; |
19
|
|
|
use Composer\IO\IOInterface; |
20
|
|
|
use Composer\Package\PackageInterface; |
21
|
|
|
use Composer\Plugin\PluginInterface; |
22
|
|
|
use Composer\Script\Event as ScriptEvent; |
23
|
|
|
use Symfony\Component\Console\Input\ArgvInput; |
24
|
|
|
use Symfony\Component\Console\Output\ConsoleOutput; |
25
|
|
|
use Symfony\Component\Console\Style\SymfonyStyle; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Class Plugin |
29
|
|
|
* @package Yawik\Composer |
30
|
|
|
* @author Anthonius Munthi <[email protected]> |
31
|
|
|
* @since 0.32.0 |
32
|
|
|
* @TODO Create more documentation for methods |
33
|
|
|
*/ |
34
|
|
|
class Plugin implements PluginInterface, EventSubscriberInterface |
35
|
|
|
{ |
36
|
|
|
const YAWIK_MODULE_TYPE = 'yawik-module'; |
37
|
|
|
|
38
|
|
|
const SCAN_TYPE_INSTALL = 'install'; |
39
|
|
|
const SCAN_TYPE_REMOVE = 'remove'; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @var Composer |
43
|
|
|
*/ |
44
|
|
|
private $composer; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @var IOInterface |
48
|
|
|
*/ |
49
|
|
|
private $output; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* An array list of available modules |
53
|
|
|
* |
54
|
|
|
* @var array |
55
|
|
|
*/ |
56
|
|
|
private $installed = []; |
57
|
|
|
|
58
|
|
|
private $uninstalled = []; |
59
|
|
|
|
60
|
|
|
private $projectPath; |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @var AssetsInstaller |
64
|
|
|
*/ |
65
|
|
|
private $assetsInstaller; |
66
|
|
|
|
67
|
5 |
|
public function __construct($projectPath=null) |
68
|
|
|
{ |
69
|
5 |
|
$this->projectPath = is_null($projectPath) ? getcwd():$projectPath; |
70
|
5 |
|
} |
71
|
|
|
|
72
|
4 |
|
public function activate(Composer $composer, IOInterface $io) |
73
|
|
|
{ |
74
|
4 |
|
$this->composer = $composer; |
75
|
4 |
|
$this->output = $io; |
76
|
4 |
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Define AssetsInstaller to use |
80
|
|
|
* This very usefull during testing process |
81
|
|
|
* @param $installer |
82
|
|
|
*/ |
83
|
5 |
|
public function setAssetsInstaller($installer) |
84
|
|
|
{ |
85
|
5 |
|
$this->assetsInstaller = $installer; |
86
|
5 |
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Provide composer event listeners. |
90
|
|
|
* |
91
|
|
|
* @return array |
92
|
|
|
*/ |
93
|
1 |
|
public static function getSubscribedEvents() |
94
|
|
|
{ |
95
|
|
|
return [ |
96
|
1 |
|
'post-autoload-dump' => 'onPostAutoloadDump', |
97
|
|
|
'post-package-install' => 'onPostPackageInstall', |
98
|
|
|
'post-package-update' => 'onPostPackageUpdate', |
99
|
|
|
'pre-package-uninstall' => 'onPrePackageUninstall' |
100
|
|
|
]; |
101
|
|
|
} |
102
|
|
|
|
103
|
4 |
|
public function onPostAutoloadDump() |
104
|
|
|
{ |
105
|
4 |
|
if (count($this->installed) > 0) { |
106
|
2 |
|
$this->getAssetsInstaller()->install($this->installed); |
107
|
|
|
} |
108
|
4 |
|
if (count($this->uninstalled) > 0) { |
109
|
1 |
|
$this->getAssetsInstaller()->uninstall($this->uninstalled); |
110
|
|
|
} |
111
|
|
|
|
112
|
4 |
|
$this->getAssetsInstaller()->fixDirPermissions(); |
113
|
4 |
|
} |
114
|
|
|
|
115
|
2 |
|
public function onPostPackageInstall(PackageEvent $event) |
116
|
|
|
{ |
117
|
2 |
|
$package = $event->getOperation()->getPackage(); |
|
|
|
|
118
|
2 |
|
$this->scanModules($package, static::SCAN_TYPE_INSTALL); |
119
|
2 |
|
} |
120
|
|
|
|
121
|
1 |
|
public function onPostPackageUpdate(PackageEvent $event) |
122
|
|
|
{ |
123
|
1 |
|
$package = $event->getOperation()->getTargetPackage(); |
|
|
|
|
124
|
1 |
|
$this->scanModules($package, static::SCAN_TYPE_INSTALL); |
125
|
1 |
|
} |
126
|
|
|
|
127
|
1 |
|
public function onPrePackageUninstall(PackageEvent $event) |
128
|
|
|
{ |
129
|
1 |
|
$package = $event->getOperation()->getPackage(); |
130
|
1 |
|
$this->scanModules($package, static::SCAN_TYPE_REMOVE); |
131
|
1 |
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* @return AssetsInstaller |
135
|
|
|
*/ |
136
|
5 |
|
public function getAssetsInstaller() |
137
|
|
|
{ |
138
|
5 |
|
if (!is_object($this->assetsInstaller)) { |
139
|
1 |
|
$assetInstaller = new AssetsInstaller(); |
140
|
1 |
|
if (php_sapi_name()==='cli') { |
141
|
1 |
|
$assetInstaller->setInput(new ArgvInput())->setOutput(new ConsoleOutput()); |
142
|
1 |
|
$assetInstaller->getOutput()->setDecorated(true); |
143
|
|
|
} |
144
|
1 |
|
$this->assetsInstaller = $assetInstaller; |
145
|
|
|
} |
146
|
5 |
|
return $this->assetsInstaller; |
147
|
|
|
} |
148
|
|
|
|
149
|
4 |
|
private function scanModules(PackageInterface $package, $scanType='install') |
150
|
|
|
{ |
151
|
4 |
|
$installer = $this->composer->getInstallationManager(); |
152
|
4 |
|
$packagePath = $installer->getInstallPath($package); |
153
|
4 |
|
$type = $package->getType(); |
154
|
4 |
|
$publicDir = $packagePath.'/public'; |
155
|
4 |
|
$extras = $package->getExtra(); |
156
|
|
|
|
157
|
4 |
|
if (file_exists($publicDir) && $type === static::YAWIK_MODULE_TYPE) { |
158
|
|
|
// we skip undefined zf module definition |
159
|
3 |
|
if (isset($extras['zf']['module'])) { |
160
|
|
|
// we register module class name |
161
|
3 |
|
$moduleName = $extras['zf']['module']; |
162
|
3 |
|
if (self::SCAN_TYPE_INSTALL == $scanType) { |
163
|
2 |
|
$this->installed[$moduleName] = realpath($publicDir); |
164
|
|
|
} else { |
165
|
3 |
|
$this->uninstalled[] = $moduleName; |
166
|
|
|
} |
167
|
|
|
} else { |
168
|
|
|
$this->output->write('[warning] No module definition for: ' . $package->getName()); |
169
|
|
|
} |
170
|
|
|
} |
171
|
4 |
|
} |
172
|
|
|
} |
173
|
|
|
|