1
|
|
|
<?php namespace Anomaly\Streams\Platform\Addon\Extension; |
2
|
|
|
|
3
|
|
|
use Anomaly\Streams\Platform\Addon\Extension\Contract\ExtensionInterface; |
4
|
|
|
use Anomaly\Streams\Platform\Model\EloquentCollection; |
5
|
|
|
use Anomaly\Streams\Platform\Model\EloquentModel; |
6
|
|
|
use Anomaly\Streams\Platform\Model\EloquentObserver; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Class ExtensionModel |
10
|
|
|
* |
11
|
|
|
* @link http://pyrocms.com/ |
12
|
|
|
* @author PyroCMS, Inc. <[email protected]> |
13
|
|
|
* @author Ryan Thompson <[email protected]> |
14
|
|
|
*/ |
15
|
|
View Code Duplication |
class ExtensionModel extends EloquentModel implements ExtensionInterface |
|
|
|
|
16
|
|
|
{ |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Define the table name. |
20
|
|
|
* |
21
|
|
|
* @var string |
22
|
|
|
*/ |
23
|
|
|
protected $table = 'addons_extensions'; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Disable timestamps for extensions. |
27
|
|
|
* |
28
|
|
|
* @var bool |
29
|
|
|
*/ |
30
|
|
|
public $timestamps = false; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Boot the model |
34
|
|
|
*/ |
35
|
|
|
protected static function boot() |
36
|
|
|
{ |
37
|
|
|
self::observe(app(EloquentObserver::class)); |
38
|
|
|
|
39
|
|
|
parent::boot(); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Find a extension by it's namespace or return a new |
44
|
|
|
* extension with the given namespace. |
45
|
|
|
* |
46
|
|
|
* @param $namespace |
47
|
|
|
* @return ExtensionModel |
48
|
|
|
*/ |
49
|
|
|
public function findByNamespaceOrNew($namespace) |
50
|
|
|
{ |
51
|
|
|
$extension = $this->findByNamespace($namespace); |
52
|
|
|
|
53
|
|
|
if ($extension instanceof ExtensionModel) { |
54
|
|
|
return $extension; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
$extension = $this->newInstance(); |
58
|
|
|
|
59
|
|
|
$extension->namespace = $namespace; |
|
|
|
|
60
|
|
|
|
61
|
|
|
$extension->save(); |
62
|
|
|
|
63
|
|
|
return $extension; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Find a extension by it's namespace. |
68
|
|
|
* |
69
|
|
|
* @param $namespace |
70
|
|
|
* @return mixed |
71
|
|
|
*/ |
72
|
|
|
public function findByNamespace($namespace) |
73
|
|
|
{ |
74
|
|
|
return $this->where('namespace', $namespace)->first(); |
|
|
|
|
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Get all enabled extension namespaces. |
79
|
|
|
* |
80
|
|
|
* @return EloquentCollection |
81
|
|
|
*/ |
82
|
|
|
public function getEnabledNamespaces() |
83
|
|
|
{ |
84
|
|
|
return $this->where('enabled', true)->get()->pluck('namespace'); |
|
|
|
|
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Get all installed extension namespaces. |
89
|
|
|
* |
90
|
|
|
* @return EloquentCollection |
91
|
|
|
*/ |
92
|
|
|
public function getInstalledNamespaces() |
93
|
|
|
{ |
94
|
|
|
return $this->where('installed', true)->get()->pluck('namespace'); |
|
|
|
|
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Return a new collection. |
99
|
|
|
* |
100
|
|
|
* @param array $items |
101
|
|
|
* @return EloquentCollection |
102
|
|
|
*/ |
103
|
|
|
public function newCollection(array $items = []) |
104
|
|
|
{ |
105
|
|
|
return new EloquentCollection($items); |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.