-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathComposerPlugin.php
More file actions
149 lines (132 loc) · 3.56 KB
/
Copy pathComposerPlugin.php
File metadata and controls
149 lines (132 loc) · 3.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
<?php
namespace Cleantalk\Common\ContactsEncoder;
use Composer\Composer;
use Composer\DependencyResolver\Operation\InstallOperation;
use Composer\DependencyResolver\Operation\UpdateOperation;
use Composer\EventDispatcher\EventSubscriberInterface;
use Composer\Installer\PackageEvent;
use Composer\Installer\PackageEvents;
use Composer\IO\IOInterface;
use Composer\Plugin\PluginInterface;
use Composer\Script\Event;
use Composer\Script\ScriptEvents;
/**
* Composer Plugin to display a thank you message after installation.
* @psalm-suppress UnusedClass
*/
class ComposerPlugin implements PluginInterface, EventSubscriberInterface
{
/**
* @var Composer
*/
protected $composer;
/**
* @var IOInterface
*/
protected $io;
/**
* @var bool Flag to prevent showing message multiple times
*/
private static $messageShown = false;
/**
* Package name to check
*/
const PACKAGE_NAME = 'cleantalk/contacts-encoder';
/**
* GitHub repository URL
*/
const GITHUB_URL = 'https://github.com/CleanTalk/contacts-encoder';
/**
* {@inheritdoc}
*/
public function activate(Composer $composer, IOInterface $io)
{
$this->composer = $composer;
$this->io = $io;
}
/**
* {@inheritdoc}
*/
public function deactivate(Composer $composer, IOInterface $io)
{
}
/**
* {@inheritdoc}
*/
public function uninstall(Composer $composer, IOInterface $io)
{
}
/**
* {@inheritdoc}
*/
public static function getSubscribedEvents()
{
return [
PackageEvents::POST_PACKAGE_INSTALL => 'onPackageInstall',
PackageEvents::POST_PACKAGE_UPDATE => 'onPackageUpdate',
ScriptEvents::POST_INSTALL_CMD => 'showThankYouMessage',
ScriptEvents::POST_UPDATE_CMD => 'showThankYouMessage',
];
}
/**
* Handle package install event.
*
* @param PackageEvent $event
* @return void
*/
public function onPackageInstall(PackageEvent $event)
{
$operation = $event->getOperation();
if (!$operation instanceof InstallOperation) {
return;
}
$package = $operation->getPackage();
if ($package->getName() === self::PACKAGE_NAME) {
$this->displayMessage($event->getIO());
}
}
/**
* Handle package update event.
*
* @param PackageEvent $event
* @return void
*/
public function onPackageUpdate(PackageEvent $event)
{
$operation = $event->getOperation();
if (!$operation instanceof UpdateOperation) {
return;
}
$package = $operation->getTargetPackage();
if ($package->getName() === self::PACKAGE_NAME) {
$this->displayMessage($event->getIO());
}
}
/**
* Display the thank you message on script events (for root package).
*
* @param Event $event
* @return void
*/
public function showThankYouMessage(Event $event)
{
$this->displayMessage($event->getIO());
}
/**
* Display the thank you message.
*
* @param IOInterface $io
* @return void
*/
private function displayMessage(IOInterface $io)
{
if (self::$messageShown) {
return;
}
self::$messageShown = true;
$io->write('');
$io->write('<info>Thank you for installing CleanTalk Contacts Encoder SDK!</info>');
$io->write('<comment>★ Star us on GitHub:</comment> ' . self::GITHUB_URL);
$io->write('');
}
}