Passed
Pull Request — 2.x (#1446)
by Harings
13:09
created

TwillCapsules::getCapsuleForModule()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 5
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 11
rs 10
1
<?php
2
3
namespace A17\Twill;
4
5
use A17\Twill\Exceptions\NoCapsuleFoundException;
6
use A17\Twill\Helpers\Capsule;
7
use Illuminate\Support\Collection;
8
use Illuminate\Support\Str;
9
10
class TwillCapsules
11
{
12
    /**
13
     * @var \A17\Twill\Helpers\Capsule[]
14
     */
15
    public $registeredCapsules = [];
16
17
    public function registerPackageCapsule(
18
        string $name,
19
        string $namespace,
20
        string $path,
21
        string $singular = null,
22
        bool $enabled = true
23
    ): Capsule {
24
        $capsule = new Capsule($name, $namespace, $path, $singular, $enabled, true);
25
26
        $this->registerCapsule($capsule);
27
28
        return $this->registeredCapsules[$name];
29
    }
30
31
    public function registerCapsule(Capsule $capsule): void
32
    {
33
        $this->registeredCapsules[$capsule->name] = $capsule;
34
    }
35
36
    /**
37
     * Generates a non package capsule object.
38
     */
39
    public function makeProjectCapsule(string $name): Capsule
40
    {
41
        return new Capsule($name, $this->capsuleNamespace($name), config("twill.capsules.path") . '/' . $name);
42
    }
43
44
    /**
45
     * @throws \A17\Twill\Exceptions\NoCapsuleFoundException
46
     */
47
    public function getCapsuleForModule(string $module): Capsule
48
    {
49
        $capsule = $this->getRegisteredCapsules()->first(function (Capsule $capsule) use ($module) {
50
            return $capsule->getModule() === $module;
51
        });
52
53
        if (!$capsule) {
54
            throw new NoCapsuleFoundException($module);
55
        }
56
57
        return $capsule;
58
    }
59
60
    /**
61
     * @throws \A17\Twill\Exceptions\NoCapsuleFoundException
62
     */
63
    public function getCapsuleForModel(string $model): Capsule
64
    {
65
        $capsule = $this->getRegisteredCapsules()->first(function (Capsule $capsule) use ($model) {
66
            return $capsule->getSingular() === $model;
67
        });
68
69
        if (!$capsule) {
70
            throw new NoCapsuleFoundException($model);
71
        }
72
73
        return $capsule;
74
    }
75
76
    /**
77
     * @return Collection<Capsule>
78
     */
79
    public function getRegisteredCapsules(): Collection
80
    {
81
        $this->loadProjectCapsules();
82
83
        return collect($this->registeredCapsules);
84
    }
85
86
    public function loadProjectCapsules(): void
87
    {
88
        $path = config('twill.capsules.path');
89
90
        $list = collect(config('twill.capsules.list'));
91
92
        $list
93
            ->where('enabled', true)
94
            ->map(function ($capsule) use ($path) {
95
                $this->registerCapsule(
96
                    new Capsule(
97
                        $capsule['name'],
98
                        $this->capsuleNamespace($capsule['name']),
99
                        $path . '/' . $capsule['name'],
100
                        $capsule['singular'] ?? null,
101
                        $capsule['enabled'] ?? true
102
                    )
103
                );
104
            });
105
    }
106
107
    public function capsuleNamespace($capsuleName, $type = null): string
108
    {
109
        // @todo: Read from capsules to get this data.
110
        $base = config('twill.capsules.namespaces.base');
111
112
        $type = config("twill.capsules.namespaces.$type");
113
114
        return "$base\\$capsuleName" . (filled($type) ? "\\$type" : '');
115
    }
116
117
    public function capsuleNamespaceToPath(
118
        $namespace,
119
        $capsuleNamespace,
120
        $rootPath
121
    ): string {
122
        $namespace = Str::after($namespace, $capsuleNamespace . '\\');
123
124
        return "$rootPath/{$this->getProjectCapsulesSubdirectory()}" . str_replace('\\', '/', $namespace);
125
    }
126
127
    public function getProjectCapsulesPath(): string
128
    {
129
        return config("twill.capsules.path") . $this->getProjectCapsulesSubdirectory();
130
    }
131
132
    private function getProjectCapsulesSubdirectory(): string {
133
        $subdirectory = config('twill.capsules.namespaces.subdir');
134
        return filled($subdirectory) ? "$subdirectory/" : '';
135
    }
136
137
    public function getAutoloader()
138
    {
139
        return app()->bound('autoloader')
140
            ? app('autoloader')
141
            : require base_path('vendor/autoload.php');
142
    }
143
}
144