1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
/** |
3
|
|
|
* Copyright MediaCT. All rights reserved. |
4
|
|
|
* https://www.mediact.nl |
5
|
|
|
* @deprecated |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace Mediact\CodingStandard\PhpStorm; |
9
|
|
|
|
10
|
|
|
use Composer\Composer; |
11
|
|
|
use Composer\EventDispatcher\EventSubscriberInterface; |
12
|
|
|
use Composer\IO\IOInterface; |
13
|
|
|
use Composer\Plugin\PluginInterface; |
14
|
|
|
use Composer\Script\Event; |
15
|
|
|
use Composer\Script\ScriptEvents; |
16
|
|
|
use Mediact\CodingStandard\PhpStorm\Patcher\ConfigPatcher; |
17
|
|
|
use Mediact\CodingStandard\PhpStorm\Patcher\ConfigPatcherInterface; |
18
|
|
|
|
19
|
|
|
class InstallerPlugin implements PluginInterface, EventSubscriberInterface |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* @var ConfigPatcherInterface |
23
|
|
|
*/ |
24
|
|
|
private $patcher; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Constructor. |
28
|
|
|
* |
29
|
|
|
* @param ConfigPatcherInterface $patcher |
30
|
|
|
*/ |
31
|
1 |
|
public function __construct(ConfigPatcherInterface $patcher = null) |
32
|
|
|
{ |
33
|
1 |
|
$this->patcher = $patcher !== null |
34
|
1 |
|
? $patcher |
35
|
|
|
: new ConfigPatcher(); |
36
|
1 |
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Apply plugin modifications to Composer |
40
|
|
|
* |
41
|
|
|
* @param Composer $composer |
42
|
|
|
* @param IOInterface $inputOutput |
43
|
|
|
* |
44
|
|
|
* @return void |
45
|
|
|
*/ |
46
|
|
|
public function activate(Composer $composer, IOInterface $inputOutput): void |
47
|
|
|
{ |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Get the subscribed events. |
52
|
|
|
* |
53
|
|
|
* @return array |
54
|
|
|
*/ |
55
|
1 |
|
public static function getSubscribedEvents(): array |
56
|
|
|
{ |
57
|
|
|
return [ |
58
|
1 |
|
ScriptEvents::POST_INSTALL_CMD => 'onNewCodeEvent', |
59
|
1 |
|
ScriptEvents::POST_UPDATE_CMD => 'onNewCodeEvent' |
60
|
|
|
]; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* On new code. |
65
|
|
|
* |
66
|
|
|
* @param Event $event |
67
|
|
|
* |
68
|
|
|
* @return void |
69
|
|
|
*/ |
70
|
1 |
|
public function onNewCodeEvent(Event $event): void |
71
|
|
|
{ |
72
|
1 |
|
$vendorDir = $event->getComposer()->getConfig()->get('vendor-dir'); |
73
|
1 |
|
$projectDir = dirname($vendorDir); |
74
|
1 |
|
$phpStormDir = $projectDir . DIRECTORY_SEPARATOR . '.idea'; |
75
|
1 |
|
$filesDir = dirname(__DIR__) . DIRECTORY_SEPARATOR . 'files'; |
76
|
|
|
|
77
|
1 |
|
$phpStormDefaultPath = $this->getPhpStormDefaultPath(); |
78
|
|
|
|
79
|
1 |
|
if (is_dir($phpStormDir) && is_dir($filesDir)) { |
80
|
1 |
|
$this->patcher->patch( |
81
|
1 |
|
new Environment( |
82
|
1 |
|
new Filesystem($phpStormDir), |
83
|
1 |
|
new Filesystem($phpStormDefaultPath), |
84
|
1 |
|
new Filesystem($filesDir), |
85
|
1 |
|
new Filesystem($projectDir), |
86
|
1 |
|
$event->getIO(), |
87
|
1 |
|
$event->getComposer() |
88
|
|
|
) |
89
|
|
|
); |
90
|
|
|
|
91
|
1 |
|
$output = $event->getIO(); |
92
|
1 |
|
$output->write('Patched the PhpStorm config'); |
93
|
|
|
} |
94
|
1 |
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Get the latest version of clients phpstorm directory |
98
|
|
|
* |
99
|
|
|
* @return string |
100
|
|
|
*/ |
101
|
|
|
public function getPhpStormDefaultPath(): string |
102
|
|
|
{ |
103
|
|
|
$phpStormDefaultPath = ''; |
104
|
|
|
|
105
|
|
|
if (isset($_SERVER['HOME'])) { |
106
|
|
|
$home = $_SERVER['HOME']; |
107
|
|
|
} else { |
108
|
|
|
$home = getenv("HOME"); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
$phpStormDefaultPaths = array_reverse(glob("$home/.[pP]hp[sS]torm201*/config/")); |
|
|
|
|
112
|
|
|
if (! empty($phpStormDefaultPaths)) { |
113
|
|
|
$phpStormDefaultPath = reset($phpStormDefaultPaths); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
return $phpStormDefaultPath; |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
|