Passed
Pull Request — 2.x (#1446)
by Harings
10:23
created

Capsule::getRepositoriesNamespace()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 4
rs 10
1
<?php
2
3
namespace A17\Twill\Helpers;
4
5
use A17\Twill\Facades\TwillCapsules;
6
use Illuminate\Database\Migrations\Migrator;
7
use Illuminate\Support\Facades\App;
8
use Illuminate\Support\Facades\Config;
9
use Illuminate\Support\Facades\File;
10
use Illuminate\Support\Facades\View;
11
use Illuminate\Support\Str;
12
13
class Capsule
14
{
15
    /**
16
     * @var string
17
     */
18
    public $name;
19
20
    /**
21
     * @var string
22
     */
23
    public $path;
24
25
    /**
26
     * @var bool
27
     */
28
    public $enabled;
29
30
    /**
31
     * @var bool
32
     */
33
    public $packageCapsule = false;
34
35
    /**
36
     * @var bool
37
     */
38
    public $loaded = false;
39
40
    /**
41
     * @var string|null
42
     */
43
    private $singular;
44
45
    /**
46
     * @var string
47
     */
48
    private $namespace;
49
50
    public function __construct(
51
        string $name,
52
        string $namespace,
53
        string $path,
54
        string $singular = null,
55
        bool $enabled = true,
56
        bool $packageCapsule = false
57
    ) {
58
        $this->name = $name;
59
        $this->path = $path;
60
        $this->enabled = $enabled;
61
        $this->namespace = $namespace;
62
        $this->singular = $singular;
63
        $this->packageCapsule = $packageCapsule;
64
65
        $this->boot();
66
    }
67
68
    public function boot(): void
69
    {
70
        $this->registerPsr4Autoloader();
71
        $this->autoloadConfigFiles();
72
        $this->registerServiceProvider();
73
        $this->registerViews();
74
        $this->loadMigrations();
75
76
        if ($this->packageCapsule) {
77
            $this->registerConfig();
78
        }
79
    }
80
81
    public function registerPsr4Autoloader(): void
82
    {
83
        //@todo: Inst this working without this.
84
        TwillCapsules::getAutoloader()->setPsr4(
0 ignored issues
show
Bug introduced by
The method setPsr4() does not exist on Illuminate\Contracts\Foundation\Application. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

84
        TwillCapsules::getAutoloader()->/** @scrutinizer ignore-call */ setPsr4(

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
85
            $this->namespace . '\\',
86
            $this->getPsr4Path()
87
        );
88
89
        TwillCapsules::getAutoloader()->setPsr4(
90
            $this->getDatabaseNamespace() . '\\',
91
            $this->getDatabasePsr4Path()
92
        );
93
94
        TwillCapsules::getAutoloader()->setPsr4(
95
            $this->getSeedsNamespace() . '\\',
96
            $this->getSeedsPsr4Path()
97
        );
98
    }
99
100
    public function registerServiceProvider(): void
101
    {
102
        $serviceProviderName = $this->name . 'CapsuleServiceProvider';
103
104
        if (File::exists($this->path . '/' . $serviceProviderName . '.php')) {
105
            // @todo: test.
106
            App::register($this->namespace . '\\' . $serviceProviderName);
107
        }
108
    }
109
110
    public function autoloadConfigFiles(): void
111
    {
112
        $files = $this->getConfig()['autoload']['files'] ?? null;
113
114
        if (blank($files)) {
115
            return;
116
        }
117
118
        collect($files)->each(function ($file) {
119
            if (file_exists($file)) {
120
                require_once $file;
121
            }
122
        });
123
    }
124
125
    public function registerViews(): void
126
    {
127
        View::addLocation(Str::replaceLast('/' . $this->name, '', $this->path));
128
    }
129
130
    public function loadMigrations(): void
131
    {
132
        $callback = function (Migrator $migrator) {
133
            $migrator->path($this->getMigrationsPath());
134
        };
135
136
        App()->afterResolving('migrator', $callback);
137
138
        if (app()->resolved('migrator')) {
139
            $callback(App::make('migrator'));
140
        }
141
    }
142
143
    public function getBasePath(string $path): string
144
    {
145
        $exploded = explode('/', $path);
146
        return implode('/', array_pop($exploded));
147
    }
148
149
    public function getModule(): string
150
    {
151
        return Str::camel($this->name);
152
    }
153
154
    public function getDisplayName(): string
155
    {
156
        return Str::studly($this->name);
157
    }
158
159
    public function getPlural(): string
160
    {
161
        return $this->name;
162
    }
163
164
    public function getSingular(): string
165
    {
166
        return $this->singular ?? Str::singular($this->name);
167
    }
168
169
    public function getBaseNamespace(): string
170
    {
171
        $explodedNamespace = explode('\\', $this->namespace);
172
        return implode('\\', array_pop($explodedNamespace));
173
    }
174
175
    public function getDatabaseNamespace(): string
176
    {
177
        return $this->namespace . '\\Database';
178
    }
179
180
    public function getDatabasePsr4Path(): string
181
    {
182
        return $this->path . '/database';
183
    }
184
185
    public function getSeedsNamespace(): string
186
    {
187
        return $this->namespace . '\\Seeds\\Database';
188
    }
189
190
    public function getSeedsPsr4Path(): string
191
    {
192
        return $this->getDatabasePsr4Path() . '/seeds';
193
    }
194
195
    public function getMigrationsPath(): string
196
    {
197
        return $this->getDatabasePsr4Path() . '/migrations';
198
    }
199
200
    public function getResourcesPath(): string
201
    {
202
        return $this->getPsr4Path() . '/resources';
203
    }
204
205
    public function getLanguagesPath(): string
206
    {
207
        return $this->getResourcesPath() . '/lang';
208
    }
209
210
    public function getViewsPath(): string
211
    {
212
        return $this->getResourcesPath() . '/views';
213
    }
214
215
    public function getModelNamespace(): string
216
    {
217
        // @todo: config('twill.capsules.namespaces.models');
218
        return $this->namespace . '\\Models';
219
    }
220
221
    public function getModelsDir(): string
222
    {
223
        return $this->getPsr4Path() . '/Models';
224
    }
225
226
    public function getRepositoriesNamespace(): string
227
    {
228
        // @todo: config('twill.capsules.namespaces.repositories');
229
        return $this->namespace . '\\Repositories';
230
    }
231
232
    public function getRepositoriesDir(): string
233
    {
234
        return $this->getPsr4Path() . '/Repositories';
235
    }
236
237
    public function getControllersNamespace(): string
238
    {
239
        // @todo: config('twill.capsules.namespaces.controllers');
240
        return $this->namespace . '\\Http\\Controllers';
241
    }
242
243
    public function getControllersDir(): string
244
    {
245
        return $this->getPsr4Path() . '/Http/Controllers';
246
    }
247
248
    public function getRequestsNamespace(): string
249
    {
250
        // @todo: config('twill.capsules.namespaces.requests');
251
        return $this->namespace . '\\Http\\Requests';
252
    }
253
254
    public function getRequestsDir(): string
255
    {
256
        return $this->getPsr4Path() . '/Http/Requests';
257
    }
258
259
    public function getPsr4Path(): string
260
    {
261
        // @todo: config('twill.capsules.namespaces.subdir');
262
        return $this->path;
263
    }
264
265
    public function getViewPrefix(): string
266
    {
267
        return "{$this->getModule()}.resources.views.admin";
268
    }
269
270
    public function getRoutesFile(): string
271
    {
272
        return $this->getPsr4Path() . '/routes/admin.php';
273
    }
274
275
    public function routesFileExists(): bool
276
    {
277
        return file_exists($this->getRoutesFile());
278
    }
279
280
    public function getModel(): string
281
    {
282
        return $this->getModelNamespace() . '\\' . $this->getSingular();
283
    }
284
285
    public function getTranslationModel(): string
286
    {
287
        return $this->getModelNamespace() . '\\' . $this->getSingular() . 'Translation';
288
    }
289
290
    public function getSlugModel(): string
291
    {
292
        return $this->getModelNamespace() . '\\' . $this->getSingular() . 'Slug';
293
    }
294
295
    public function getRevisionModel(): string
296
    {
297
        return $this->getModelNamespace() . '\\' . $this->getSingular() . 'Revision';
298
    }
299
300
    public function getRepositoryClass(): string
301
    {
302
        return $this->getRepositoriesNamespace() . '\\' . $this->getSingular() . 'Repository';
303
    }
304
305
    public function getControllerClass(): string
306
    {
307
        return $this->getControllersNamespace() . '\\' . $this->getSingular() . 'Controller';
308
    }
309
310
    public function getFormRequestClass(): string
311
    {
312
        return $this->getRequestsNamespace() . '\\' . $this->getSingular() . 'Request';
313
    }
314
315
    public function getConfigFile(): string
316
    {
317
        return $this->path . '/config.php';
318
    }
319
320
    public function getConfig(): array
321
    {
322
        if (file_exists($this->getConfigFile())) {
323
            return require $this->getConfigFile();
324
        }
325
        return [];
326
    }
327
328
    public function registerConfig(): void
329
    {
330
        $config = Config::get('twill-navigation', []);
331
332
        $config[$this->name] = [
333
            'title' => $this->name,
334
            'module' => true,
335
        ];
336
337
        Config::set('twill-navigation', $config);
338
    }
339
}
340