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
|
|
|
class Plugin implements PluginInterface, EventSubscriberInterface |
28
|
|
|
{ |
29
|
|
|
const YAWIK_MODULE_TYPE = 'yawik-module'; |
30
|
|
|
|
31
|
|
|
const SCAN_TYPE_INSTALL = 'install'; |
32
|
|
|
const SCAN_TYPE_REMOVE = 'remove'; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var Composer |
36
|
|
|
*/ |
37
|
|
|
private $composer; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @var IOInterface |
41
|
|
|
*/ |
42
|
|
|
private $output; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* An array list of available modules |
46
|
|
|
* |
47
|
|
|
* @var array |
48
|
|
|
*/ |
49
|
|
|
private $installed = []; |
50
|
|
|
|
51
|
|
|
private $uninstalled = []; |
52
|
|
|
|
53
|
|
|
private $projectPath; |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @var AssetsInstaller |
57
|
|
|
*/ |
58
|
|
|
private $assetsInstaller; |
59
|
|
|
|
60
|
5 |
|
public function __construct($projectPath=null) |
61
|
|
|
{ |
62
|
5 |
|
$this->projectPath = is_null($projectPath) ? getcwd():$projectPath; |
63
|
5 |
|
} |
64
|
|
|
|
65
|
4 |
|
public function activate(Composer $composer, IOInterface $io) |
66
|
|
|
{ |
67
|
4 |
|
$this->composer = $composer; |
68
|
4 |
|
$this->output = $io; |
69
|
4 |
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Define AssetsInstaller to use |
73
|
|
|
* This very usefull during testing process |
74
|
|
|
* @param $installer |
75
|
|
|
*/ |
76
|
5 |
|
public function setAssetsInstaller($installer) |
77
|
|
|
{ |
78
|
5 |
|
$this->assetsInstaller = $installer; |
79
|
5 |
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Provide composer event listeners. |
83
|
|
|
* |
84
|
|
|
* @return array |
85
|
|
|
*/ |
86
|
1 |
|
public static function getSubscribedEvents() |
87
|
|
|
{ |
88
|
|
|
return [ |
89
|
1 |
|
'post-autoload-dump' => 'onPostAutoloadDump', |
90
|
|
|
'post-package-install' => 'onPostPackageInstall', |
91
|
|
|
'post-package-update' => 'onPostPackageUpdate', |
92
|
|
|
'pre-package-uninstall' => 'onPrePackageUninstall' |
93
|
|
|
]; |
94
|
|
|
} |
95
|
|
|
|
96
|
4 |
|
public function onPostAutoloadDump() |
97
|
|
|
{ |
98
|
4 |
|
if (count($this->installed) > 0) { |
99
|
2 |
|
$this->getAssetsInstaller()->install($this->installed); |
100
|
|
|
} |
101
|
4 |
|
if (count($this->uninstalled) > 0) { |
102
|
1 |
|
$this->getAssetsInstaller()->uninstall($this->uninstalled); |
103
|
|
|
} |
104
|
4 |
|
} |
105
|
|
|
|
106
|
2 |
|
public function onPostPackageInstall(PackageEvent $event) |
107
|
|
|
{ |
108
|
2 |
|
$package = $event->getOperation()->getPackage(); |
|
|
|
|
109
|
2 |
|
$this->scanModules($package, static::SCAN_TYPE_INSTALL); |
110
|
2 |
|
} |
111
|
|
|
|
112
|
1 |
|
public function onPostPackageUpdate(PackageEvent $event) |
113
|
|
|
{ |
114
|
1 |
|
$package = $event->getOperation()->getTargetPackage(); |
|
|
|
|
115
|
1 |
|
$this->scanModules($package, static::SCAN_TYPE_INSTALL); |
116
|
1 |
|
} |
117
|
|
|
|
118
|
1 |
|
public function onPrePackageUninstall(PackageEvent $event) |
119
|
|
|
{ |
120
|
1 |
|
$package = $event->getOperation()->getPackage(); |
121
|
1 |
|
$this->scanModules($package, static::SCAN_TYPE_REMOVE); |
122
|
1 |
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* @return AssetsInstaller |
126
|
|
|
*/ |
127
|
4 |
|
public function getAssetsInstaller() |
128
|
|
|
{ |
129
|
4 |
|
if (!is_object($this->assetsInstaller)) { |
130
|
1 |
|
$assetInstaller = new AssetsInstaller(); |
131
|
1 |
|
if (php_sapi_name()==='cli') { |
132
|
1 |
|
$assetInstaller->setInput(new ArgvInput())->setOutput(new ConsoleOutput()); |
133
|
1 |
|
$assetInstaller->getOutput()->setDecorated(true); |
134
|
|
|
} |
135
|
1 |
|
$this->assetsInstaller = $assetInstaller; |
136
|
|
|
} |
137
|
4 |
|
return $this->assetsInstaller; |
138
|
|
|
} |
139
|
|
|
|
140
|
4 |
|
private function scanModules(PackageInterface $package, $scanType='install') |
141
|
|
|
{ |
142
|
4 |
|
$installer = $this->composer->getInstallationManager(); |
143
|
4 |
|
$packagePath = $installer->getInstallPath($package); |
144
|
4 |
|
$type = $package->getType(); |
145
|
4 |
|
$publicDir = $packagePath.'/public'; |
146
|
4 |
|
$extras = $package->getExtra(); |
147
|
|
|
|
148
|
4 |
|
if (file_exists($publicDir) && $type === static::YAWIK_MODULE_TYPE) { |
149
|
|
|
// we skip undefined zf module definition |
150
|
3 |
|
if (isset($extras['zf']['module'])) { |
151
|
|
|
// we register module class name |
152
|
3 |
|
$moduleName = $extras['zf']['module']; |
153
|
3 |
|
if (self::SCAN_TYPE_INSTALL == $scanType) { |
154
|
2 |
|
$this->installed[$moduleName] = realpath($publicDir); |
155
|
|
|
} else { |
156
|
3 |
|
$this->uninstalled[] = $moduleName; |
157
|
|
|
} |
158
|
|
|
} else { |
159
|
|
|
$this->output->write('[warning] No module definition for: ' . $package->getName()); |
160
|
|
|
} |
161
|
|
|
} |
162
|
4 |
|
} |
163
|
|
|
} |
164
|
|
|
|