/
Addon.php
457 lines (392 loc) · 9.17 KB
/
Addon.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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
<?php
namespace Statamic\Extend;
use Facades\Statamic\Licensing\LicenseManager;
use ReflectionClass;
use Statamic\Facades\File;
use Statamic\Facades\Path;
use Statamic\Support\Arr;
use Statamic\Support\Str;
use Statamic\Updater\AddonChangelog;
final class Addon
{
/**
* The identifier.
* Typically a composer package name. eg. statamic/trapper-keeper.
*
* @var string
*/
protected $id;
/**
* The marketplace product ID of the addon.
*
* @var int
*/
protected $marketplaceId;
/**
* The marketplace slug of the addon.
*
* @var int
*/
protected $marketplaceSlug;
/**
* The marketplace slug of the addon's seller.
*
* @var int
*/
protected $marketplaceSellerSlug;
/**
* The addon's namespace. eg. "Statamic\Addons\TrapperKeeper".
*
* @var string
*/
protected $namespace;
/**
* The directory the package is located within. eg. "/path/to/vendor/statamic/trapper-keeper".
*
* @var string
*/
protected $directory;
/**
* The autoloaded directory, relative to the addon root. eg. "src" or "".
*
* @var string
*/
protected $autoload;
/**
* The service provider class.
*
* @var string
*/
protected $provider;
/**
* The name of the addon. eg. "Trapper Keeper".
*
* @var string
*/
protected $name;
/**
* The addon slug, if overridden.
*
* @var string
*/
protected $slug;
/**
* The addon description.
*
* @var string
*/
protected $description;
/**
* The addon's version.
*
* @var string
*/
protected $version;
/**
* The latest version of the addon (via marketplace).
*
* @var string
*/
protected $latestVersion;
/**
* The marketing URL.
*
* @var string
*/
protected $url;
/**
* The marketplace URL.
*
* @var string
*/
protected $marketplaceUrl;
/**
* The name of the developer.
*
* @var string
*/
protected $developer;
/**
* The developer's URL.
*
* @var string
*/
protected $developerUrl;
/**
* Whether the addon is commercial.
*
* @var bool
*/
protected $isCommercial = false;
/**
* Available editions.
*
* @var array|null
*/
protected $editions = [];
/**
* @param string $id
*/
public function __construct($id)
{
$this->id = $id;
}
/**
* Make an addon instance.
*
* @return self
*/
public static function make($name)
{
return new self($name);
}
/**
* Make an addon instance from package details.
*
* @param array $package
* @return self
*/
public static function makeFromPackage(array $package)
{
$instance = self::make($package['id']);
$keys = [
'id', 'slug', 'editions', 'marketplaceId', 'marketplaceSlug', 'marketplaceUrl', 'marketplaceSellerSlug', 'name', 'namespace',
'autoload', 'provider', 'description', 'package', 'version', 'latestVersion', 'url', 'developer', 'developerUrl', 'isCommercial',
];
foreach (Arr::only($package, $keys) as $key => $value) {
$method = Str::camel($key);
$instance->$method($value);
}
return $instance;
}
/**
* The ID (in a vendor/package format)
* eg. statamic/trapper-keeper.
*
* @return string
*/
public function id()
{
return $this->id;
}
/**
* The composer package string
* eg. statamic/trapper-keeper.
*
* @return string
*/
public function package()
{
return $this->id();
}
/**
* The composer package name string
* eg. in statamic/trapper-keeper, it's trapper-keeper.
*
* @return string
*/
public function packageName()
{
return explode('/', $this->package())[1];
}
/**
* The composer vendor name string
* eg. in statamic/trapper-keeper, it's statamic.
*
* @return string
*/
public function vendorName()
{
return explode('/', $this->package())[0];
}
/**
* The marketplace variant ID of the addon.
*
* @param int $id
* @return int
*/
public function marketplaceId($id = null)
{
return $id
? $this->marketplaceId = $id
: $this->marketplaceId;
}
/**
* The marketplace slug of the addon.
*
* @param string $slug
* @return string
*/
public function marketplaceSlug($slug = null)
{
return $slug
? $this->marketplaceSlug = $slug
: $this->marketplaceSlug;
}
/**
* The handle of the addon.
*
* For referencing in YAML, etc.
*
* @return string
*/
public function handle()
{
return str_replace('-', '_', explode('/', $this->id)[1]);
}
/**
* The slug of the addon.
*
* For referencing in URLs.
*
* @param string $slug
* @return string
*/
public function slug($slug = null)
{
return $slug
? $this->slug = $slug
: ($this->slug ?? Str::slug(explode('/', $this->id)[1]));
}
/**
* The name of addon.
*
* @param string $name
* @return string|self
*/
public function name($name = null)
{
if (is_null($name)) {
return $this->name ?? $this->id;
}
$this->name = $name;
return $this;
}
/**
* Whether a given file exists in the addon's directory.
*
* @param string $path
* @return bool
*/
public function hasFile($path)
{
if (! $this->directory()) {
throw new \Exception('Cannot check files without a provider specified.');
}
return File::exists(Path::assemble($this->directory(), $path));
}
/**
* Get the contents of a given file in the addon's directory.
*
* @param string $path
* @return string
*/
public function getFile($path)
{
if (! $this->directory()) {
throw new \Exception('Cannot get files without a provider specified.');
}
return File::get(Path::assemble($this->directory(), $path));
}
/**
* Write the given contents to a file.
*
* @param string $path
* @param string $contents
*/
public function putFile($path, $contents)
{
if (! $this->directory()) {
throw new \Exception('Cannot write files without a provider specified.');
}
File::put(
Path::assemble($this->directory(), $path),
$contents
);
}
/**
* Get the configuration values.
*
* @return array
*/
public function config()
{
return config($this->handle());
}
/**
* Get addon changelog.
*
* @return AddonChangelog|null
*/
public function changelog()
{
return new AddonChangelog($this);
}
public function isLatestVersion()
{
if (! $this->latestVersion) {
return true;
}
return version_compare($this->version, $this->latestVersion, '=');
}
public function license()
{
return LicenseManager::addons()->get($this->package());
}
/**
* Handle method calls.
* Typically will get or set property values.
*
* @param string $method
* @param array $args
* @return mixed
*/
public function __call($method, $args)
{
if (! property_exists($this, $method)) {
throw new \Exception(sprintf('Call to undefined method %s::%s', get_class($this), $method));
}
if (empty($args)) {
return $this->$method;
}
$this->$method = $args[0];
return $this;
}
/**
* The directory the package is located within. eg. "/path/to/vendor/statamic/trapper-keeper/".
*
* @return string
*/
public function directory()
{
if (! $this->provider) {
return null;
}
if ($this->directory) {
return $this->directory;
}
$reflector = new ReflectionClass($this->provider);
$dir = Str::removeRight(dirname($reflector->getFileName()), rtrim($this->autoload, '/'));
return $this->directory = Path::tidy(Str::ensureRight($dir, '/'));
}
public function existsOnMarketplace()
{
return $this->marketplaceSlug() !== null;
}
public function edition()
{
$configured = config('statamic.editions.addons.'.$this->package());
if ($configured && ! $this->editions()->contains($configured)) {
throw new \Exception("Invalid edition [$configured] for addon ".$this->package());
}
return $configured ?? $this->editions()->first();
}
public function editions($editions = null)
{
if (func_num_args() === 0) {
return collect($this->editions);
}
$this->editions = $editions;
return $this;
}
}