rawilk /
laravel-modules
| 1 | <?php |
||
| 2 | |||
| 3 | namespace Rawilk\LaravelModules; |
||
| 4 | |||
| 5 | use Illuminate\Contracts\Container\Container; |
||
| 6 | use Illuminate\Database\Eloquent\Collection as EloquentCollection; |
||
| 7 | use Illuminate\Filesystem\Filesystem; |
||
| 8 | use Illuminate\Support\Traits\Macroable; |
||
| 9 | use Rawilk\LaravelModules\Contracts\ModuleModel; |
||
| 10 | use Rawilk\LaravelModules\Contracts\Repository; |
||
| 11 | use Rawilk\LaravelModules\Exceptions\ModuleNotFound; |
||
| 12 | |||
| 13 | abstract class EloquentRepository implements Repository |
||
| 14 | { |
||
| 15 | use Macroable; |
||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
| 16 | |||
| 17 | /** @var \Illuminate\Contracts\Container\Container */ |
||
| 18 | protected $app; |
||
| 19 | |||
| 20 | /** @var \Illuminate\Contracts\Cache\Repository */ |
||
| 21 | private $cache; |
||
| 22 | |||
| 23 | /** @var \Illuminate\Contracts\Config\Repository */ |
||
| 24 | private $config; |
||
| 25 | |||
| 26 | /** @var \Illuminate\Filesystem\Filesystem */ |
||
| 27 | private $files; |
||
| 28 | |||
| 29 | /** @var \Rawilk\LaravelModules\Contracts\ModuleModel */ |
||
| 30 | protected $model; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * @param \Illuminate\Contracts\Container\Container $app |
||
| 34 | * @param \Rawilk\LaravelModules\Contracts\ModuleModel $model |
||
| 35 | */ |
||
| 36 | public function __construct(Container $app, ModuleModel $model) |
||
| 37 | { |
||
| 38 | $this->app = $app; |
||
| 39 | $this->model = $model; |
||
| 40 | $this->cache = $app['cache']; |
||
| 41 | $this->config = $app['config']; |
||
| 42 | $this->files = $app['files']; |
||
| 43 | } |
||
| 44 | |||
| 45 | abstract protected function createModule(...$args): Module; |
||
| 46 | |||
| 47 | public function all(array $columns = ['*']): array |
||
| 48 | { |
||
| 49 | return $this->convertToCollection($this->model::allModules($columns))->toArray(); |
||
| 50 | } |
||
| 51 | |||
| 52 | public function allDisabled(array $columns = ['*']): array |
||
| 53 | { |
||
| 54 | return $this->convertToCollection($this->model::allDisabled($columns))->toArray(); |
||
| 55 | } |
||
| 56 | |||
| 57 | public function allEnabled(array $columns = ['*']): array |
||
| 58 | { |
||
| 59 | return $this->convertToCollection($this->model::allEnabled($columns))->toArray(); |
||
| 60 | } |
||
| 61 | |||
| 62 | public function assetPath(string $name): string |
||
| 63 | { |
||
| 64 | return $this->config('paths.assets') . '/' . $name; |
||
| 65 | } |
||
| 66 | |||
| 67 | public function boot(): void |
||
| 68 | { |
||
| 69 | /** @var \Rawilk\LaravelModules\Module $module */ |
||
| 70 | foreach ($this->getOrdered() as $module) { |
||
| 71 | $module->boot(); |
||
| 72 | } |
||
| 73 | } |
||
| 74 | |||
| 75 | public function config(string $key, $default = null) |
||
| 76 | { |
||
| 77 | return $this->config->get("modules.{$key}", $default); |
||
| 78 | } |
||
| 79 | |||
| 80 | public function count(): int |
||
| 81 | { |
||
| 82 | return $this->model::getCount(); |
||
| 83 | } |
||
| 84 | |||
| 85 | public function delete(string $name): bool |
||
| 86 | { |
||
| 87 | return $this->findOrFail($name)->delete(); |
||
| 88 | } |
||
| 89 | |||
| 90 | public function exists(string $name): bool |
||
| 91 | { |
||
| 92 | return $this->model::moduleExists($name); |
||
| 93 | } |
||
| 94 | |||
| 95 | public function find(string $name): ?Module |
||
| 96 | { |
||
| 97 | /** @var \Rawilk\LaravelModules\Models\Module $module */ |
||
| 98 | $module = $this->model::findModule($name); |
||
| 99 | |||
| 100 | if ($module === null) { |
||
| 101 | return null; |
||
| 102 | } |
||
| 103 | |||
| 104 | return $this->createModule($this->app, $module->name, $module->path); |
||
| 105 | } |
||
| 106 | |||
| 107 | public function findByAlias(string $alias): ?Module |
||
| 108 | { |
||
| 109 | /** @var \Rawilk\LaravelModules\Models\Module $module */ |
||
| 110 | $module = $this->model::findByAlias($alias); |
||
| 111 | |||
| 112 | if ($module === null) { |
||
| 113 | return null; |
||
| 114 | } |
||
| 115 | |||
| 116 | return $this->createModule($this->app, $module->name, $module->path); |
||
| 117 | } |
||
| 118 | |||
| 119 | public function findOrFail(string $name): Module |
||
| 120 | { |
||
| 121 | $module = $this->find($name); |
||
| 122 | |||
| 123 | if ($module === null) { |
||
| 124 | throw ModuleNotFound::named($name); |
||
| 125 | } |
||
| 126 | |||
| 127 | return $module; |
||
| 128 | } |
||
| 129 | |||
| 130 | public function findRequirements(string $name): array |
||
| 131 | { |
||
| 132 | $requirements = []; |
||
| 133 | |||
| 134 | $module = $this->findOrFail($name); |
||
| 135 | |||
| 136 | foreach ($module->getRequires() as $requirementName) { |
||
| 137 | $requirements[] = $this->findByAlias($requirementName); |
||
| 138 | } |
||
| 139 | |||
| 140 | return $requirements; |
||
| 141 | } |
||
| 142 | |||
| 143 | public function getByStatus(bool $active, array $columns = ['*']): array |
||
| 144 | { |
||
| 145 | return $this->convertToCollection($this->model::getByStatus($active, $columns))->toArray(); |
||
| 146 | } |
||
| 147 | |||
| 148 | public function getCached(): array |
||
| 149 | { |
||
| 150 | return $this->cache->remember($this->config('cache.key'), $this->config('cache.lifetime'), function () { |
||
| 151 | return $this->toCollection()->toArray(); |
||
| 152 | }); |
||
| 153 | } |
||
| 154 | |||
| 155 | public function getFiles(): Filesystem |
||
| 156 | { |
||
| 157 | return $this->files; |
||
| 158 | } |
||
| 159 | |||
| 160 | public function getModulePath(string $name): string |
||
| 161 | { |
||
| 162 | $module = $this->findOrFail($name); |
||
| 163 | |||
| 164 | return $module->getPath(); |
||
| 165 | } |
||
| 166 | |||
| 167 | public function getOrdered(string $direction = 'asc', array $columns = ['*']): array |
||
| 168 | { |
||
| 169 | return $this->convertToCollection($this->model::getOrdered($direction, $columns))->toArray(); |
||
| 170 | } |
||
| 171 | |||
| 172 | public function getPath(): string |
||
| 173 | { |
||
| 174 | |||
| 175 | } |
||
|
0 ignored issues
–
show
In this branch, the function will implicitly return
null which is incompatible with the type-hinted return string. Consider adding a return statement or allowing null as return value.
For hinted functions/methods where all return statements with the correct type are only reachable via conditions, ?null? gets implicitly returned which may be incompatible with the hinted type. Let?s take a look at an example: interface ReturnsInt {
public function returnsIntHinted(): int;
}
class MyClass implements ReturnsInt {
public function returnsIntHinted(): int
{
if (foo()) {
return 123;
}
// here: null is implicitly returned
}
}
Loading history...
|
|||
| 176 | |||
| 177 | public function getScanPaths(): array |
||
| 178 | { |
||
| 179 | return []; |
||
| 180 | } |
||
| 181 | |||
| 182 | public function isDisabled(string $name): bool |
||
| 183 | { |
||
| 184 | return $this->findOrFail($name)->isDisabled(); |
||
| 185 | } |
||
| 186 | |||
| 187 | public function isEnabled(string $name): bool |
||
| 188 | { |
||
| 189 | return $this->findOrFail($name)->isEnabled(); |
||
| 190 | } |
||
| 191 | |||
| 192 | public function register(): void |
||
| 193 | { |
||
| 194 | /** @var \Rawilk\LaravelModules\Module $module */ |
||
| 195 | foreach ($this->getOrdered() as $module) { |
||
| 196 | $module->register(); |
||
| 197 | } |
||
| 198 | } |
||
| 199 | |||
| 200 | public function scan(): array |
||
| 201 | { |
||
| 202 | return $this->toCollection()->toArray(); |
||
| 203 | } |
||
| 204 | |||
| 205 | public function toCollection(array $columns = ['*']): Collection |
||
| 206 | { |
||
| 207 | return $this->convertToCollection($this->model::allModules($columns)); |
||
| 208 | } |
||
| 209 | |||
| 210 | private function convertToCollection(EloquentCollection $eloquentCollection): Collection |
||
| 211 | { |
||
| 212 | $collection = new Collection; |
||
| 213 | |||
| 214 | $eloquentCollection->map(function ($module) use ($collection) { |
||
| 215 | /** @var \Rawilk\LaravelModules\Models\Module $module */ |
||
| 216 | $collection->push($this->createModule($this->app, $module->name, $module->path)); |
||
| 217 | }); |
||
| 218 | |||
| 219 | return $collection; |
||
| 220 | } |
||
| 221 | } |
||
| 222 |