1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace BringYourOwnIdeas\Maintenance\Model; |
4
|
|
|
|
5
|
|
|
use BringYourOwnIdeas\Maintenance\Jobs\CheckForUpdatesJob; |
6
|
|
|
use SilverStripe\Core\Injector\Injector; |
7
|
|
|
use SilverStripe\ORM\ArrayList; |
8
|
|
|
use SilverStripe\ORM\DataObject; |
9
|
|
|
use SilverStripe\View\ArrayData; |
10
|
|
|
use Symbiote\QueuedJobs\DataObjects\QueuedJobDescriptor; |
11
|
|
|
use Symbiote\QueuedJobs\Services\QueuedJob; |
12
|
|
|
use Symbiote\QueuedJobs\Services\QueuedJobService; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Describes an installed composer package version. |
16
|
|
|
*/ |
17
|
|
|
class Package extends DataObject |
18
|
|
|
{ |
19
|
|
|
private static $table_name = 'Package'; |
|
|
|
|
20
|
|
|
|
21
|
|
|
private static $db = [ |
|
|
|
|
22
|
|
|
'Name' => 'Varchar(255)', |
23
|
|
|
'Description' => 'Varchar(255)', |
24
|
|
|
'Version' => 'Varchar(255)', |
25
|
|
|
'Type' => 'Varchar(255)', |
26
|
|
|
'Supported' => 'Boolean', |
27
|
|
|
'Rating' => 'Int' |
28
|
|
|
]; |
29
|
|
|
|
30
|
|
|
private static $summary_fields = [ |
|
|
|
|
31
|
|
|
'Title' => 'Title', |
32
|
|
|
'Description' => 'Description', |
33
|
|
|
'Version' => 'Version', |
34
|
|
|
]; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var array badge definitions - a keyed array in the format of [Title => Type] {@see getBadges()} |
38
|
|
|
*/ |
39
|
|
|
protected $badges = []; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Strips vendor and 'silverstripe-' prefix from Name property |
43
|
|
|
* @return string More easily digestable module name for human consumers |
44
|
|
|
*/ |
45
|
|
|
public function getTitle() |
46
|
|
|
{ |
47
|
|
|
return preg_replace('#^[^/]+/(silverstripe-)?#', '', $this->Name); |
|
|
|
|
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Returns HTML formatted summary of this object, uses a template to do this. |
52
|
|
|
* @return string |
53
|
|
|
*/ |
54
|
|
|
public function getSummary() |
55
|
|
|
{ |
56
|
|
|
$summary = $this->renderWith(__CLASS__ . '/Summary'); |
57
|
|
|
$this->extend('updateSummary', $summary); |
58
|
|
|
return $summary; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Gives the summary template {@see getSummary()} a list of badges to show against a package |
63
|
|
|
* |
64
|
|
|
* badgeDefinitions are in the format [$title => $type] where: |
65
|
|
|
* title is the unique string to display |
66
|
|
|
* type is an optional class attribute (applied as a BEM modifier, by default) |
67
|
|
|
* |
68
|
|
|
* @param array $extraBadges allow a user to include extra badges at call time |
69
|
|
|
* |
70
|
|
|
* @return ArrayList |
71
|
|
|
*/ |
72
|
|
|
public function getBadges($extraBadges = []) |
73
|
|
|
{ |
74
|
|
|
$badgeDefinitions = array_merge($this->badges, $extraBadges); |
75
|
|
|
$badges = ArrayList::create(); |
76
|
|
|
foreach ($badgeDefinitions as $title => $type) { |
77
|
|
|
$badges->push(ArrayData::create([ |
78
|
|
|
'Title' => $title, |
79
|
|
|
'Type' => $type, |
80
|
|
|
])); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
$this->extend('updateBadges', $badges); |
84
|
|
|
return $badges; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Adds a badge to the list of badges {@see $badges} |
89
|
|
|
* |
90
|
|
|
* @param string $title |
91
|
|
|
* @param string $type |
92
|
|
|
* |
93
|
|
|
* @return $this |
94
|
|
|
*/ |
95
|
|
|
public function addBadge($title, $type) |
96
|
|
|
{ |
97
|
|
|
$this->badges[$title] = $type; |
98
|
|
|
return $this; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Replaces the list of badges |
103
|
|
|
* |
104
|
|
|
* @param array $badges {@see $badges} |
105
|
|
|
* |
106
|
|
|
* @return $this |
107
|
|
|
*/ |
108
|
|
|
public function setBadges($badges) |
109
|
|
|
{ |
110
|
|
|
$this->badges = $badges; |
111
|
|
|
return $this; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Returns a JSON data schema for the frontend React components to use |
116
|
|
|
* |
117
|
|
|
* @return array |
118
|
|
|
*/ |
119
|
|
|
public function getDataSchema() |
120
|
|
|
{ |
121
|
|
|
$schema = [ |
122
|
|
|
'description' => $this->Description, |
|
|
|
|
123
|
|
|
'link' => 'https://addons.silverstripe.org/add-ons/' . $this->Name, |
|
|
|
|
124
|
|
|
'linkTitle' => _t( |
125
|
|
|
__CLASS__ . '.ADDONS_LINK_TITLE', |
126
|
|
|
'View {package} on addons.silverstripe.org', |
127
|
|
|
['package' => $this->Title] |
128
|
|
|
), |
129
|
|
|
'rating'=> (int) $this->Rating |
|
|
|
|
130
|
|
|
]; |
131
|
|
|
|
132
|
|
|
$this->extend('updateDataSchema', $schema); |
133
|
|
|
|
134
|
|
|
return $schema; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* Queue up a job to check for updates to packages if there isn't a pending job in the queue already |
139
|
|
|
*/ |
140
|
|
|
public function requireDefaultRecords() |
141
|
|
|
{ |
142
|
|
|
parent::requireDefaultRecords(); |
143
|
|
|
|
144
|
|
|
$pendingJobs = QueuedJobDescriptor::get()->filter([ |
145
|
|
|
'Implementation' => CheckForUpdatesJob::class, |
146
|
|
|
'JobStatus' => [ |
147
|
|
|
QueuedJob::STATUS_NEW, |
148
|
|
|
QueuedJob::STATUS_INIT, |
149
|
|
|
QueuedJob::STATUS_RUN, |
150
|
|
|
], |
151
|
|
|
]); |
152
|
|
|
if ($pendingJobs->count()) { |
153
|
|
|
return; |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** @var QueuedJobService $jobService */ |
157
|
|
|
$jobService = QueuedJobService::singleton(); |
158
|
|
|
$jobService->queueJob(Injector::inst()->create(CheckForUpdatesJob::class)); |
159
|
|
|
} |
160
|
|
|
} |
161
|
|
|
|