Completed
Push — master ( 91746d...c63af8 )
by ReliQ
09:16
created

ServiceProvider   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 239
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

Changes 0
Metric Value
wmc 14
lcom 1
cbo 7
dl 0
loc 239
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace ReliqArts\Docweaver;
6
7
use Illuminate\Contracts\Config\Repository as Config;
8
use Illuminate\Contracts\View\Factory as ViewFactory;
9
use Illuminate\Foundation\AliasLoader;
10
use Illuminate\Support\ServiceProvider as BaseServiceProvider;
11
use Illuminate\View\View;
12
use Monolog\Handler\StreamHandler;
13
14
/**
15
 *  ServiceProvider.
16
 */
17
final class ServiceProvider extends BaseServiceProvider
18
{
19
    private const LOGGER_NAME = 'Docweaver';
20
    private const LOG_FILENAME = 'docweaver';
21
22
    /**
23
     * Assets location.
24
     */
25
    protected string $assetsDir = __DIR__ . '/..';
0 ignored issues
show
Bug introduced by
This code did not parse for me. Apparently, there is an error somewhere around this line:

Syntax error, unexpected T_STRING, expecting T_FUNCTION or T_CONST
Loading history...
26
27
    /**
28
     * List of commands.
29
     *
30
     * @var array
31
     */
32
    protected array $commands = [
33
        Console\Command\Publish::class,
34
        Console\Command\Update::class,
35
        Console\Command\UpdateAll::class,
36
    ];
37
38
    /**
39
     * Perform post-registration booting of services.
40
     */
41
    public function boot(): void
42
    {
43
        $this->handleConfig()
44
            ->handleMigrations()
45
            ->handleViews()
46
            ->handleRoutes()
47
            ->handleAssets()
48
            ->handleCommands()
49
            ->addViewComposers();
50
    }
51
52
    /**
53
     * Register bindings in the container.
54
     */
55
    public function register(): void
56
    {
57
        $this->registerAliases()
58
            ->registerBindings();
59
    }
60
61
    /**
62
     * Get the services provided by the provider.
63
     *
64
     * @return array
65
     */
66
    public function provides(): array
67
    {
68
        return $this->commands;
69
    }
70
71
    /**
72
     * @return self
73
     */
74
    protected function handleAssets(): self
75
    {
76
        $this->publishes([
77
            "{$this->assetsDir}/public" => public_path('vendor/docweaver'),
78
        ], 'docweaver-public');
79
80
        return $this;
81
    }
82
83
    /**
84
     * @return self
85
     */
86
    protected function handleConfig(): self
87
    {
88
        $docWeaverConfig = "{$this->assetsDir}/config/config.php";
89
90
        // merge config
91
        $this->mergeConfigFrom($docWeaverConfig, 'docweaver');
92
93
        // allow publishing the config file, with tag: docweaver:config
94
        $this->publishes([$docWeaverConfig => config_path('docweaver.php')], 'docweaver-config');
95
96
        return $this;
97
    }
98
99
    /**
100
     * @return self
101
     */
102
    private function handleCommands(): self
103
    {
104
        if ($this->app->runningInConsole() && !empty($this->commands)) {
105
            $this->commands($this->commands);
106
        }
107
108
        return $this;
109
    }
110
111
    /**
112
     * @return self
113
     */
114
    private function handleMigrations(): self
115
    {
116
        $this->loadMigrationsFrom(sprintf('%s/database/migrations', $this->assetsDir));
117
118
        return $this;
119
    }
120
121
    /**
122
     * @return self
123
     */
124
    private function handleRoutes(): self
125
    {
126
        require realpath(sprintf('%s/routes/web.php', $this->assetsDir));
127
128
        return $this;
129
    }
130
131
    /**
132
     * @return self
133
     */
134
    private function handleViews(): self
135
    {
136
        $viewsDirectory = sprintf('%s/resources/views', $this->assetsDir);
137
138
        // Load the views...
139
        $this->loadViewsFrom($viewsDirectory, 'docweaver');
140
141
        // Allow publishing view files, with tag: views
142
        $this->publishes([$viewsDirectory => base_path('resources/views/vendor/docweaver')], 'docweaver-views');
143
144
        return $this;
145
    }
146
147
    /**
148
     * @return self
149
     */
150
    private function registerAliases(): self
151
    {
152
        $loader = AliasLoader::getInstance();
153
154
        // Register aliases...
155
        $loader->alias('DocweaverProduct', Model\Product::class);
156
        $loader->alias('DocweaverMarkdown', Service\MarkdownParser::class);
157
        $loader->alias('DocweaverDocumentation', Service\Documentation\Provider::class);
158
        $loader->alias('DocweaverPublisher', Service\Documentation\Publisher::class);
159
160
        return $this;
161
    }
162
163
    /**
164
     * @return self
165
     */
166
    private function registerBindings(): self
167
    {
168
        $this->app->bind(
169
            Contract\Filesystem::class,
170
            Service\Filesystem::class
171
        );
172
173
        $this->app->bind(
174
            Contract\Documentation\Publisher::class,
175
            Service\Documentation\Publisher::class
176
        );
177
178
        $this->app->bind(
179
            Contract\Documentation\Provider::class,
180
            Service\Documentation\Provider::class
181
        );
182
183
        $this->app->bind(
184
            Contract\MarkdownParser::class,
185
            Service\MarkdownParser::class
186
        );
187
188
        $this->app->bind(
189
            Contract\VCSCommandRunner::class,
190
            Service\GitCommandRunner::class
191
        );
192
193
        $this->app->bind(
194
            Contract\Product\Maker::class,
195
            Factory\ProductMaker::class
196
        );
197
198
        $this->app->bind(
199
            Contract\Product\Finder::class,
200
            Service\Product\Finder::class
201
        );
202
203
        $this->app->bind(
204
            Contract\Product\Publisher::class,
205
            Service\Product\Publisher::class
206
        );
207
208
        $this->app->singleton(
209
            Contract\ConfigProvider::class,
210
            function (): Contract\ConfigProvider {
211
                return new Service\ConfigProvider(
212
                    resolve(Config::class)
213
                );
214
            }
215
        );
216
217
        $this->app->singleton(
218
            Contract\Logger::class,
219
            function (): Contract\Logger {
220
                $logger = new Service\Logger(self::LOGGER_NAME);
221
                $logFile = storage_path(sprintf('logs/%s.log', self::LOG_FILENAME));
222
                $logger->pushHandler(new StreamHandler($logFile, Service\Logger::DEBUG));
223
224
                return $logger;
225
            }
226
        );
227
228
        return $this;
229
    }
230
231
    /**
232
     * @return self
233
     */
234
    private function addViewComposers(): self
235
    {
236
        $configProvider = resolve(Contract\ConfigProvider::class);
237
        $viewFactory = resolve(ViewFactory::class);
238
239
        $viewFactory->composer(
240
            '*',
241
            function (View $view) use ($configProvider): void {
242
                $view->with('docweaverConfigProvider', $configProvider);
243
            }
244
        );
245
246
        return $this;
247
    }
248
}
249