ServiceProvider   A
last analyzed

Complexity

Total Complexity 14

Size/Duplication

Total Lines 180
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 77
dl 0
loc 180
rs 10
c 1
b 0
f 0
wmc 14

12 Methods

Rating   Name   Duplication   Size   Complexity  
A handleAssets() 0 10 1
A handleCommands() 0 7 3
A handleMigrations() 0 5 1
A handleConfig() 0 11 1
A registerBindings() 0 39 1
A register() 0 4 1
A boot() 0 9 1
A handleViews() 0 11 1
A handleRoutes() 0 5 1
A registerAliases() 0 11 1
A provides() 0 3 1
A addViewComposers() 0 13 1
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 League\CommonMark\CommonMarkConverter;
13
use League\CommonMark\Environment as CommonMarkEnvironment;
14
use League\CommonMark\Extras\CommonMarkExtrasExtension;
15
use Monolog\Handler\StreamHandler;
16
use ReliqArts\Docweaver\Console\Command\Publish;
17
use ReliqArts\Docweaver\Console\Command\Update;
18
use ReliqArts\Docweaver\Console\Command\UpdateAll;
19
use ReliqArts\Docweaver\Contract\ConfigProvider as ConfigProviderContract;
20
use ReliqArts\Docweaver\Contract\Documentation\Provider as DocumentationProviderContract;
21
use ReliqArts\Docweaver\Contract\Documentation\Publisher as DocumentationPublisherContract;
22
use ReliqArts\Docweaver\Contract\Filesystem as FilesystemContract;
23
use ReliqArts\Docweaver\Contract\Logger as LoggerContract;
24
use ReliqArts\Docweaver\Contract\MarkdownParser as MarkdownParserContract;
25
use ReliqArts\Docweaver\Contract\Product\Finder as ProductFinderContract;
26
use ReliqArts\Docweaver\Contract\Product\Maker as ProductMakerContract;
27
use ReliqArts\Docweaver\Contract\Product\Publisher as ProductPublisherContract;
28
use ReliqArts\Docweaver\Contract\VCSCommandRunner;
29
use ReliqArts\Docweaver\Factory\ProductMaker;
30
use ReliqArts\Docweaver\Model\Product;
31
use ReliqArts\Docweaver\Service\ConfigProvider;
32
use ReliqArts\Docweaver\Service\Documentation\Provider as DocumentationProvider;
33
use ReliqArts\Docweaver\Service\Documentation\Publisher as DocumentationPublisher;
34
use ReliqArts\Docweaver\Service\Filesystem;
35
use ReliqArts\Docweaver\Service\GitCommandRunner;
36
use ReliqArts\Docweaver\Service\Logger;
37
use ReliqArts\Docweaver\Service\MarkdownParser;
38
use ReliqArts\Docweaver\Service\Product\Finder as ProductFinder;
39
use ReliqArts\Docweaver\Service\Product\Publisher as ProductPublisher;
40
41
final class ServiceProvider extends BaseServiceProvider
42
{
43
    private const LOGGER_NAME = 'Docweaver';
44
    private const LOG_FILENAME = 'docweaver';
45
46
    /**
47
     * Assets location.
48
     */
49
    protected string $assetsDir = __DIR__ . '/..';
50
51
    /**
52
     * List of commands.
53
     *
54
     * @var array
55
     */
56
    protected array $commands = [
57
        Publish::class,
58
        Update::class,
59
        UpdateAll::class,
60
    ];
61
62
    /**
63
     * Perform post-registration booting of services.
64
     */
65
    public function boot(): void
66
    {
67
        $this->handleConfig()
68
            ->handleMigrations()
69
            ->handleViews()
70
            ->handleRoutes()
71
            ->handleAssets()
72
            ->handleCommands()
73
            ->addViewComposers();
74
    }
75
76
    /**
77
     * Register bindings in the container.
78
     */
79
    public function register(): void
80
    {
81
        $this->registerAliases()
82
            ->registerBindings();
83
    }
84
85
    /**
86
     * Get the services provided by the provider.
87
     */
88
    public function provides(): array
89
    {
90
        return $this->commands;
91
    }
92
93
    protected function handleAssets(): self
94
    {
95
        $this->publishes(
96
            [
97
                sprintf('%s/public', $this->assetsDir) => public_path('vendor/docweaver'),
98
            ],
99
            'docweaver-public'
100
        );
101
102
        return $this;
103
    }
104
105
    protected function handleConfig(): self
106
    {
107
        $docWeaverConfig = "{$this->assetsDir}/config/config.php";
108
109
        // merge config
110
        $this->mergeConfigFrom($docWeaverConfig, 'docweaver');
111
112
        // allow publishing the config file, with tag: docweaver:config
113
        $this->publishes([$docWeaverConfig => config_path('docweaver.php')], 'docweaver-config');
114
115
        return $this;
116
    }
117
118
    private function handleCommands(): self
119
    {
120
        if (!empty($this->commands) && $this->app->runningInConsole()) {
121
            $this->commands($this->commands);
122
        }
123
124
        return $this;
125
    }
126
127
    private function handleMigrations(): self
128
    {
129
        $this->loadMigrationsFrom(sprintf('%s/database/migrations', $this->assetsDir));
130
131
        return $this;
132
    }
133
134
    private function handleRoutes(): self
135
    {
136
        require realpath(sprintf('%s/routes/web.php', $this->assetsDir));
137
138
        return $this;
139
    }
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
    private function registerAliases(): self
155
    {
156
        $loader = AliasLoader::getInstance();
157
158
        // Register aliases...
159
        $loader->alias('DocweaverProduct', Product::class);
160
        $loader->alias('DocweaverMarkdown', MarkdownParser::class);
161
        $loader->alias('DocweaverDocumentation', DocumentationProvider::class);
162
        $loader->alias('DocweaverPublisher', DocumentationPublisher::class);
163
164
        return $this;
165
    }
166
167
    private function registerBindings(): self
168
    {
169
        $this->app->singleton(FilesystemContract::class, Filesystem::class);
170
        $this->app->singleton(DocumentationPublisherContract::class, DocumentationPublisher::class);
171
        $this->app->singleton(DocumentationProviderContract::class, DocumentationProvider::class);
172
        $this->app->singleton(VCSCommandRunner::class, GitCommandRunner::class);
173
        $this->app->singleton(ProductMakerContract::class, ProductMaker::class);
174
        $this->app->singleton(ProductFinderContract::class, ProductFinder::class);
175
        $this->app->singleton(ProductPublisherContract::class, ProductPublisher::class);
176
        $this->app->singleton(
177
            ConfigProviderContract::class,
178
            static function (): ConfigProviderContract {
179
                return new ConfigProvider(
180
                    resolve(Config::class)
181
                );
182
            }
183
        );
184
        $this->app->singleton(
185
            LoggerContract::class,
186
            static function (): LoggerContract {
187
                $logger = new Logger(self::LOGGER_NAME);
188
                $logFile = storage_path(sprintf('logs/%s.log', self::LOG_FILENAME));
189
                $logger->pushHandler(new StreamHandler($logFile, Logger::DEBUG));
190
191
                return $logger;
192
            }
193
        );
194
        $this->app->singleton(
195
            MarkdownParserContract::class,
196
            static function (): MarkdownParserContract {
197
                $config = [];
198
                $environment = CommonMarkEnvironment::createCommonMarkEnvironment();
199
                $environment->addExtension(new CommonMarkExtrasExtension());
200
201
                return new MarkdownParser(new CommonMarkConverter($config, $environment));
202
            }
203
        );
204
205
        return $this;
206
    }
207
208
    private function addViewComposers(): self
209
    {
210
        $configProvider = resolve(ConfigProviderContract::class);
211
        $viewFactory = resolve(ViewFactory::class);
212
213
        $viewFactory->composer(
214
            '*',
215
            static function (View $view) use ($configProvider): void {
216
                $view->with('docweaverConfigProvider', $configProvider);
217
            }
218
        );
219
220
        return $this;
221
    }
222
}
223