/
Package.php
160 lines (142 loc) · 4.38 KB
/
Package.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
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
150
151
152
153
154
155
156
157
158
159
160
<?php
namespace BringYourOwnIdeas\Maintenance\Model;
use BringYourOwnIdeas\Maintenance\Jobs\CheckForUpdatesJob;
use SilverStripe\Core\Injector\Injector;
use SilverStripe\ORM\ArrayList;
use SilverStripe\ORM\DataObject;
use SilverStripe\View\ArrayData;
use Symbiote\QueuedJobs\DataObjects\QueuedJobDescriptor;
use Symbiote\QueuedJobs\Services\QueuedJob;
use Symbiote\QueuedJobs\Services\QueuedJobService;
/**
* Describes an installed composer package version.
*/
class Package extends DataObject
{
private static $table_name = 'Package';
private static $db = [
'Name' => 'Varchar(255)',
'Description' => 'Varchar(255)',
'Version' => 'Varchar(255)',
'Type' => 'Varchar(255)',
'Supported' => 'Boolean',
'Rating' => 'Int'
];
private static $summary_fields = [
'Title' => 'Title',
'Description' => 'Description',
'Version' => 'Version',
];
/**
* @var array badge definitions - a keyed array in the format of [Title => Type] {@see getBadges()}
*/
protected $badges = [];
/**
* Strips vendor and 'silverstripe-' prefix from Name property
* @return string More easily digestable module name for human consumers
*/
public function getTitle()
{
return preg_replace('#^[^/]+/(silverstripe-)?#', '', $this->Name ?? '');
}
/**
* Returns HTML formatted summary of this object, uses a template to do this.
* @return string
*/
public function getSummary()
{
$summary = $this->renderWith(__CLASS__ . '/Summary');
$this->extend('updateSummary', $summary);
return $summary;
}
/**
* Gives the summary template {@see getSummary()} a list of badges to show against a package
*
* badgeDefinitions are in the format [$title => $type] where:
* title is the unique string to display
* type is an optional class attribute (applied as a BEM modifier, by default)
*
* @param array $extraBadges allow a user to include extra badges at call time
*
* @return ArrayList
*/
public function getBadges($extraBadges = [])
{
$badgeDefinitions = array_merge($this->badges, $extraBadges);
$badges = ArrayList::create();
foreach ($badgeDefinitions as $title => $type) {
$badges->push(ArrayData::create([
'Title' => $title,
'Type' => $type,
]));
}
$this->extend('updateBadges', $badges);
return $badges;
}
/**
* Adds a badge to the list of badges {@see $badges}
*
* @param string $title
* @param string $type
*
* @return $this
*/
public function addBadge($title, $type)
{
$this->badges[$title] = $type;
return $this;
}
/**
* Replaces the list of badges
*
* @param array $badges {@see $badges}
*
* @return $this
*/
public function setBadges($badges)
{
$this->badges = $badges;
return $this;
}
/**
* Returns a JSON data schema for the frontend React components to use
*
* @return array
*/
public function getDataSchema()
{
$schema = [
'description' => $this->Description,
'link' => 'https://addons.silverstripe.org/add-ons/' . $this->Name,
'linkTitle' => _t(
__CLASS__ . '.ADDONS_LINK_TITLE',
'View {package} on addons.silverstripe.org',
['package' => $this->Title]
),
'rating'=> (int) $this->Rating
];
$this->extend('updateDataSchema', $schema);
return $schema;
}
/**
* Queue up a job to check for updates to packages if there isn't a pending job in the queue already
*/
public function requireDefaultRecords()
{
parent::requireDefaultRecords();
$pendingJobs = QueuedJobDescriptor::get()->filter([
'Implementation' => CheckForUpdatesJob::class,
'JobStatus' => [
QueuedJob::STATUS_NEW,
QueuedJob::STATUS_INIT,
QueuedJob::STATUS_RUN,
],
]);
if ($pendingJobs->count()) {
return;
}
/** @var QueuedJobService $jobService */
$jobService = QueuedJobService::singleton();
$jobService->queueJob(Injector::inst()->create(CheckForUpdatesJob::class));
}
}