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