Completed
Push — master ( 3fdc21...2574b5 )
by ReliQ
01:51
created

ServiceProvider::handleAssets()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

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