1
|
|
|
<?php |
2
|
|
|
namespace Caffeinated\Modules\Repositories; |
3
|
|
|
|
4
|
|
|
use Caffeinated\Modules\Contracts\RepositoryInterface; |
5
|
|
|
use Illuminate\Config\Repository as Config; |
6
|
|
|
use Illuminate\Filesystem\Filesystem; |
7
|
|
|
|
8
|
|
|
abstract class Repository implements RepositoryInterface |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* @var \Illuminate\Config\Repository |
12
|
|
|
*/ |
13
|
|
|
protected $config; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @var \Illuminate\Filesystem\Filesystem |
17
|
|
|
*/ |
18
|
|
|
protected $files; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var string $path Path to the defined modules directory |
22
|
|
|
*/ |
23
|
|
|
protected $path; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Constructor method. |
27
|
|
|
* |
28
|
|
|
* @param \Illuminate\Config\Repository $config |
29
|
|
|
* @param \Illuminate\Filesystem\Filesystem $files |
30
|
|
|
*/ |
31
|
|
|
public function __construct(Config $config, Filesystem $files) |
32
|
|
|
{ |
33
|
|
|
$this->config = $config; |
34
|
|
|
$this->files = $files; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Get all module basenames |
39
|
|
|
* |
40
|
|
|
* @return array |
41
|
|
|
*/ |
42
|
|
|
protected function getAllBasenames() |
43
|
|
|
{ |
44
|
|
|
$path = $this->getPath(); |
45
|
|
|
|
46
|
|
|
try { |
47
|
|
|
$collection = collect($this->files->directories($path)); |
48
|
|
|
|
49
|
|
|
$basenames = $collection->map(function($item, $key) { |
|
|
|
|
50
|
|
|
return basename($item); |
51
|
|
|
}); |
52
|
|
|
|
53
|
|
|
return $basenames; |
54
|
|
|
} catch (\InvalidArgumentException $e) { |
55
|
|
|
return collect(array()); |
56
|
|
|
} |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Get a module's manifest contents. |
61
|
|
|
* |
62
|
|
|
* @param string $slug |
63
|
|
|
* @return Collection|null |
64
|
|
|
*/ |
65
|
|
|
public function getManifest($slug) |
66
|
|
|
{ |
67
|
|
|
if (! is_null($slug)) { |
68
|
|
|
$module = studly_case($slug); |
69
|
|
|
$path = $this->getManifestPath($module); |
70
|
|
|
$contents = $this->files->get($path); |
71
|
|
|
$collection = collect(json_decode($contents, true)); |
72
|
|
|
|
73
|
|
|
return $collection; |
|
|
|
|
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
return null; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Get modules path. |
81
|
|
|
* |
82
|
|
|
* @return string |
83
|
|
|
*/ |
84
|
|
|
public function getPath() |
85
|
|
|
{ |
86
|
|
|
return $this->path ?: $this->config->get('modules.path'); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Set modules path in "RunTime" mode. |
91
|
|
|
* |
92
|
|
|
* @param string $path |
93
|
|
|
* @return object $this |
94
|
|
|
*/ |
95
|
|
|
public function setPath($path) |
96
|
|
|
{ |
97
|
|
|
$this->path = $path; |
98
|
|
|
|
99
|
|
|
return $this; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Get path for the specified module. |
104
|
|
|
* |
105
|
|
|
* @param string $slug |
106
|
|
|
* @return string |
107
|
|
|
*/ |
108
|
|
|
public function getModulePath($slug) |
109
|
|
|
{ |
110
|
|
|
$module = studly_case($slug); |
111
|
|
|
|
112
|
|
|
return $this->getPath()."/{$module}/"; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Get path of module manifest file. |
117
|
|
|
* |
118
|
|
|
* @param string $module |
|
|
|
|
119
|
|
|
* @return string |
120
|
|
|
*/ |
121
|
|
|
protected function getManifestPath($slug) |
122
|
|
|
{ |
123
|
|
|
return $this->getModulePath($slug).'module.json'; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* Get modules namespace. |
128
|
|
|
* |
129
|
|
|
* @return string |
130
|
|
|
*/ |
131
|
|
|
public function getNamespace() |
132
|
|
|
{ |
133
|
|
|
return rtrim($this->config->get('modules.namespace'), '/\\'); |
134
|
|
|
} |
135
|
|
|
} |
136
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.