Passed
Pull Request — 2.x (#925)
by Antonio Carlos
08:34
created

HasCapsules::capsule()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 5
c 1
b 0
f 0
nc 3
nop 1
dl 0
loc 11
rs 10
1
<?php
2
3
namespace A17\Twill\Services\Capsules;
4
5
use Illuminate\Support\Str;
6
7
trait HasCapsules
8
{
9
    protected function getAutoloader()
10
    {
11
        return app()->bound('autoloader')
12
            ? app('autoloader')
13
            : require base_path('vendor/autoload.php');
14
    }
15
16
    public function getCapsuleList()
17
    {
18
        $path = $this->getCapsulesPath();
19
20
        $list = collect(config('twill.capsules.list'));
21
22
        if (config('twill.capsules.loaded')) {
23
            return $list;
24
        }
25
26
        return $list
27
            ->where('enabled', true)
28
            ->map(function ($capsule) use ($path) {
29
                return $this->makeCapsule($capsule, $path);
30
            });
31
    }
32
33
    public function getCapsuleByModel($model)
34
    {
35
        return $this->getCapsuleList()
36
            ->where('singular', Str::studly($model))
37
            ->first();
38
    }
39
40
    public function getCapsuleByModule($module)
41
    {
42
        return $this->getCapsuleList()
43
            ->filter(function ($capsule) use ($module) {
44
                return Str::lower($capsule['plural']) == Str::lower($module);
45
            })
46
            ->first();
47
    }
48
49
    /**
50
     * @return \Illuminate\Config\Repository|\Illuminate\Contracts\Foundation\Application|mixed
51
     */
52
    public function getCapsulesPath()
53
    {
54
        return config('twill.capsules.path');
55
    }
56
57
    /**
58
     * @return \Illuminate\Config\Repository|\Illuminate\Contracts\Foundation\Application|mixed
59
     */
60
    public function getCapsulesSubdir()
61
    {
62
        $subdir = config('twill.capsules.namespaces.subdir');
63
64
        return $subdir;
65
    }
66
67
    public function makeCapsule($capsule, $basePath = null)
68
    {
69
        $basePath = $basePath ?? $this->getCapsulesPath();
70
71
        $capsule['name'] = Str::studly($capsule['name']);
72
73
        $capsule['module'] = Str::camel($capsule['name']);
74
75
        $capsule['plural'] = $name = $capsule['name'];
76
77
        $capsule['singular'] = $singular =
78
            $capsule['singular'] ?? Str::singular($name);
79
80
        $twillNamespace = config('twill.namespace');
0 ignored issues
show
Unused Code introduced by
The assignment to $twillNamespace is dead and can be removed.
Loading history...
81
82
        $capsule['base_namespace'] = config('twill.capsules.namespaces.base');
83
84
        $capsule[
85
            'namespace'
86
        ] = $capsuleNamespace = $this->getManager()->capsuleNamespace(
0 ignored issues
show
Bug introduced by
The method capsuleNamespace() 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

86
        ] = $capsuleNamespace = $this->getManager()->/** @scrutinizer ignore-call */ capsuleNamespace(

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...
87
            $capsule['name']
88
        );
89
90
        $capsule[
91
            'database_namespace'
92
        ] = "$capsuleNamespace\Database";
93
94
        $capsule[
95
            'seeds_namespace'
96
        ] = "{$capsule['database_namespace']}\Seeds";
97
98
        $capsule['model'] = $capsule['models'] = $models =
99
            "{$capsuleNamespace}\\" .
100
            config('twill.capsules.namespaces.models');
101
        $capsule['repositories'] = $repositories =
102
            "{$capsuleNamespace}\\" .
103
            config('twill.capsules.namespaces.repositories');
104
        $capsule['controllers'] = $controllers =
105
            "{$capsuleNamespace}\\" .
106
            config('twill.capsules.namespaces.controllers');
107
        $capsule['requests'] = $requests =
108
            "{$capsuleNamespace}\\" .
109
            config('twill.capsules.namespaces.requests');
110
111
        $capsule['psr4_path'] = "$basePath/{$name}" . (filled($this->getCapsulesSubdir()) ? $this->getCapsulesSubdir().'/' : '');
0 ignored issues
show
Bug introduced by
Are you sure $this->getCapsulesSubdir() of type Illuminate\Config\Reposi...ation\Application|mixed can be used in concatenation? ( Ignorable by Annotation )

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

111
        $capsule['psr4_path'] = "$basePath/{$name}" . (filled($this->getCapsulesSubdir()) ? /** @scrutinizer ignore-type */ $this->getCapsulesSubdir().'/' : '');
Loading history...
112
113
        $capsule['base_path'] = $basePath;
114
115
        $capsule['database_psr4_path'] = "$basePath/{$name}/database";
116
117
        $capsule['seeds_psr4_path'] = "{$capsule['database_psr4_path']}/seeds";
118
119
        $capsule['root_path'] = $root = $this->capsuleRootPath($capsule);
0 ignored issues
show
Unused Code introduced by
The assignment to $root is dead and can be removed.
Loading history...
120
121
        $capsule[
122
            'migrations_dir'
123
        ] = "{$capsule['root_path']}/database/migrations";
124
125
        $capsule['views_dir'] = "{$capsule['root_path']}/resources/views";
126
127
        $capsule['view_prefix'] = "{$name}.resources.views.admin";
128
129
        $capsule['routes_file'] = "{$capsule['root_path']}/routes/admin.php";
130
131
        $capsule['model'] = "{$models}\\{$singular}";
132
133
        $capsule['models_dir'] = $this->namespaceToPath($capsule, $models);
134
135
        $capsule['translation'] = "{$models}\\{$singular}Translation";
136
137
        $capsule['slug'] = "{$models}\\{$singular}Slug";
138
139
        $capsule['revision'] = "{$models}\\{$singular}Revision";
140
141
        $capsule['repository'] = "{$repositories}\\{$singular}Repository";
142
143
        $capsule['repositories_dir'] = $this->namespaceToPath(
144
            $capsule,
145
            $repositories
146
        );
147
148
        $capsule['controller'] = "{$controllers}\\{$singular}Controller";
149
150
        $capsule['controllers_dir'] = $this->namespaceToPath(
151
            $capsule,
152
            $controllers
153
        );
154
155
        $capsule['formRequest'] = "{$requests}\\{$singular}Request";
156
157
        $capsule['requests_dir'] = $this->namespaceToPath($capsule, $requests);
158
159
        $this->registerPsr4Autoloader($capsule);
160
161
        return $capsule;
162
    }
163
164
    public function registerPsr4Autoloader($capsule)
165
    {
166
        $this->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

166
        $this->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...
167
            $capsule['namespace'] . '\\',
168
            $capsule['psr4_path']
169
        );
170
171
        $this->getAutoloader()->setPsr4(
172
            $capsule['database_namespace'] . '\\',
173
            $capsule['database_psr4_path']
174
        );
175
176
        $this->getAutoloader()->setPsr4(
177
            $capsule['database_namespace'] . '\\Seeds\\',
178
            $capsule['database_psr4_path'] . '/seeds'
179
        );
180
    }
181
182
    public function capsuleRootPath($capsule)
183
    {
184
        return config('twill.capsules.path') . '/' . $capsule['name'] ?? null;
185
    }
186
187
    public function getCapsuleRepositoryClass($model)
188
    {
189
        return $this->getCapsuleByModel($model)['repository'] ?? null;
190
    }
191
192
    public function getCapsuleTranslationClass($model)
193
    {
194
        return $this->getCapsuleByModel($model)['translation'] ?? null;
195
    }
196
197
    public function getCapsuleSlugClass($model)
198
    {
199
        return $this->getCapsuleByModel($model)['slug'] ?? null;
200
    }
201
202
    public function getCapsuleRevisionClass($model)
203
    {
204
        return $this->getCapsuleByModel($model)['revision'] ?? null;
205
    }
206
207
    public function getCapsuleFormRequestClass($model)
208
    {
209
        return $this->getCapsuleByModel($model)['formRequest'] ?? null;
210
    }
211
212
    public function getCapsuleViewPrefix($capsule)
213
    {
214
        return $this->getCapsuleByModule(Str::studly($capsule))[
215
            'view_prefix'
216
        ] ?? null;
217
    }
218
219
    public function namespaceToPath($capsule, $namespace)
220
    {
221
        return $this->capsuleNamespaceToPath(
222
            $namespace,
223
            $capsule['namespace'],
224
            $capsule['root_path']
225
        );
226
    }
227
228
    public function capsuleNamespaceToPath(
229
        $namespace,
230
        $capsuleNamespace,
231
        $rootPath
232
    ) {
233
        $namespace = Str::after($namespace, $capsuleNamespace . '\\');
234
235
        $subdir = $this->getCapsulesSubdir();
236
237
        $subdir = filled($subdir) ? "{$subdir}/" : '';
238
239
        return "{$rootPath}/{$subdir}" . str_replace('\\', '/', $namespace);
240
    }
241
242
    public function getManager()
243
    {
244
        return $this->manager = $this->manager ?? app('twill.capsules.manager');
0 ignored issues
show
Bug Best Practice introduced by
The property manager does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
245
    }
246
247
    public function seedCapsules($illuminateSeeder)
248
    {
249
        $twillSeeder = app(CapsuleSeeder::class);
250
251
        $this->getCapsuleList()->each(function ($capsule) use ($twillSeeder, $illuminateSeeder) {
252
            if (filled($capsuleSeeder = $this->makeCapsuleSeeder($capsule))) {
253
                $twillSeeder->setCommand($illuminateSeeder->command);
254
255
                $twillSeeder->call($capsuleSeeder);
256
            }
257
        });
258
    }
259
260
    public function makeCapsuleSeeder($capsule)
261
    {
262
        $seeder = "{$capsule['database_namespace']}\\Seeds\DatabaseSeeder";
263
264
        if (class_exists($seeder)) {
265
            return $seeder;
266
        }
267
268
        return null;
269
    }
270
271
    public function capsuleExists($module)
272
    {
273
        return filled($this->getCapsuleByModule($module));
274
    }
275
276
    public function capsule($string)
277
    {
278
        if (file_exists($string)) {
279
            return $this->getCapsuleByPath($string);
280
        }
281
282
        if (class_exists($string)) {
283
            return $this->getCapsuleByClass($string);
284
        }
285
286
        return $this->getCapsuleByModule($string);
287
    }
288
289
    public function getCapsuleByPath($path)
290
    {
291
        $capsule = $this->getCapsuleList()->first();
292
293
        if (!Str::startsWith($path, $capsule['base_path'])) {
294
            return null;
295
        }
296
297
        $name = Str::before(
298
            Str::after(Str::after($path, $capsule['base_path']), '/'),
299
            '/'
300
        );
301
302
        $name = "{$capsule['base_path']}/$name";
303
304
        return $this->getCapsuleList()
305
            ->where('root_path', $name)
306
            ->first();
307
    }
308
309
    public function getCapsuleByClass($class)
310
    {
311
        $capsule = $this->getCapsuleList()->first();
312
313
        $namespace = Str::beforeLast($class, '\\');
0 ignored issues
show
Unused Code introduced by
The assignment to $namespace is dead and can be removed.
Loading history...
314
315
        if (!Str::startsWith($class, $capsule['base_namespace'])) {
316
            return null;
317
        }
318
319
        $name = Str::before(
320
            Str::after(Str::after($class, $capsule['base_namespace']), '\\'),
321
            '\\'
322
        );
323
324
        $name = "{$capsule['base_namespace']}\\$name";
325
326
        return $this->getCapsuleList()
327
                    ->where('namespace', $name)
328
                    ->first();
329
    }
330
}
331