-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathMessage.php
116 lines (99 loc) · 2.91 KB
/
Message.php
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
<?php
namespace Drupal\Composer\Plugin\ProjectMessage;
use Composer\Package\RootPackageInterface;
/**
* Determine configuration.
*
* @internal
*/
class Message {
/**
* The root package.
*
* @var \Composer\Package\RootPackageInterface
*/
protected $rootPackage;
/**
* The name of the event.
*
* @var string
*/
protected $eventName;
/**
* The message to display.
*
* @var string[]
*/
protected $messageText = [];
/**
* Construct a Config object.
*
* @param \Composer\Package\RootPackageInterface $root_package
* Composer package object for the root package.
* @param string $event_name
* The event name.
*/
public function __construct(RootPackageInterface $root_package, $event_name) {
$this->rootPackage = $root_package;
$this->eventName = $event_name;
}
public function getText() {
if ($this->messageText) {
return $this->messageText;
}
$package_config = $this->rootPackage->getExtra();
$file = $this->eventName . '-message.txt';
if ($config_file = $package_config['drupal-core-project-message'][$this->eventName . '-file'] ?? FALSE) {
$file = $config_file;
}
$message = $package_config['drupal-core-project-message'][$this->eventName . '-message'] ?? [];
if ($message) {
$this->messageText = $message;
}
else {
$this->messageText = $this->getMessageFromFile($file);
}
// Include structured support info from composer.json.
if ($config_keys = $package_config['drupal-core-project-message']['include-keys'] ?? FALSE) {
foreach ($config_keys as $config_key) {
switch ($config_key) {
case 'name':
if ($homepage = $this->rootPackage->getName()) {
$this->messageText[] = ' * Name: ' . $homepage;
}
break;
case 'description':
if ($homepage = $this->rootPackage->getDescription()) {
$this->messageText[] = ' * Description: ' . $homepage;
}
break;
case 'homepage':
if ($homepage = $this->rootPackage->getHomepage()) {
$this->messageText[] = ' * Homepage: ' . $homepage;
}
break;
case 'support':
if ($support = $this->rootPackage->getSupport()) {
$this->messageText[] = ' * Support:';
foreach ($support as $support_key => $support_value) {
$this->messageText[] = ' * ' . $support_key . ': ' . $support_value;
}
}
break;
}
}
}
return $this->messageText;
}
/**
* Reads the message file as an array.
*
* @param string $file
* The file to read. Relative paths are relative to the project directory.
*
* @return string[]
*/
protected function getMessageFromFile($file) {
return file_exists($file) ? file($file, FILE_IGNORE_NEW_LINES) : [];
}
}