ServiceProvider::boot()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 3
c 1
b 0
f 0
dl 0
loc 6
rs 10
cc 1
nc 1
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace ReliqArts\StyleImporter;
6
7
use Illuminate\Config\Repository as ConfigRepository;
8
use Illuminate\Contracts\View\Factory as ViewFactory;
9
use Illuminate\Contracts\View\View;
10
use Monolog\Handler\StreamHandler;
11
use ReliqArts\Contracts\Logger as ReliqArtsLoggerContract;
12
use ReliqArts\ServiceProvider as ReliqArtsServiceProvider;
13
use ReliqArts\Services\ConfigProvider as ReliqArtsConfigProvider;
14
use ReliqArts\Services\Logger as ReliqArtsLogger;
15
use ReliqArts\StyleImporter\CSS\Extractor\FontFaceExtractor;
16
use ReliqArts\StyleImporter\CSS\Extractor\ImportExtractor;
17
use ReliqArts\StyleImporter\CSS\Extractor\MediaBlockExtractor;
18
use ReliqArts\StyleImporter\CSS\Generator as GeneratorContract;
19
use ReliqArts\StyleImporter\CSS\Generator\RuleSetGenerator as RuleSetGenerator;
20
use ReliqArts\StyleImporter\CSS\Processor as ProcessorContract;
21
use ReliqArts\StyleImporter\CSS\Processor\Processor;
22
use ReliqArts\StyleImporter\HTML\ClassExtractor;
23
use ReliqArts\StyleImporter\HTML\Extractor;
24
use ReliqArts\StyleImporter\HTML\Extractor\ElementExtractor as HTMLExtractor;
25
use ReliqArts\StyleImporter\HTML\IDExtractor;
26
use ReliqArts\StyleImporter\HTML\TagExtractor;
27
use ReliqArts\StyleImporter\Importer\Agent;
28
use ReliqArts\StyleImporter\Util\Config;
29
use ReliqArts\StyleImporter\Util\Logger as LoggerContract;
30
31
/**
32
 * Guided Image Service Provider.
33
 *
34
 * @codeCoverageIgnore
35
 */
36
final class ServiceProvider extends ReliqArtsServiceProvider
37
{
38
    protected const CONFIG_KEY = 'styleimporter';
39
    protected const ASSET_DIRECTORY = __DIR__ . '/..';
40
    protected const LOGGER_NAME = self::CONFIG_KEY . '-logger';
41
    protected const LOG_FILENAME = self::CONFIG_KEY;
42
43
    /**
44
     * @var ConfigProvider
45
     */
46
    private $configProvider;
47
48
    public function boot(): void
49
    {
50
        parent::boot();
51
52
        $this->addViewComposers();
53
        $this->handleViews();
54
    }
55
56
    public function register(): void
57
    {
58
        $this->configProvider = new Config(
59
            new ReliqArtsConfigProvider(
60
                resolve(ConfigRepository::class),
61
                $this->getConfigKey()
62
            )
63
        );
64
65
        $this->app->singleton(ClassExtractor::class, HTMLExtractor::class);
66
        $this->app->singleton(IDExtractor::class, HTMLExtractor::class);
67
        $this->app->singleton(TagExtractor::class, HTMLExtractor::class);
68
        $this->app->singleton(Extractor::class, HTMLExtractor::class);
69
        $this->app->singleton(GeneratorContract::class, RuleSetGenerator::class);
70
        $this->app->singleton(Importer::class, Agent::class);
71
        $this->app->singleton(
72
            ConfigProvider::class,
73
            function (): ConfigProvider {
74
                return $this->configProvider;
75
            }
76
        );
77
        $this->app->singleton(
78
            ProcessorContract::class,
79
            function (): ProcessorContract {
80
                return new Processor(
81
                    resolve(ImportExtractor::class),
82
                    resolve(FontFaceExtractor::class),
83
                    resolve(MediaBlockExtractor::class),
84
                    resolve(GeneratorContract::class),
85
                    resolve(LoggerContract::class)
86
                );
87
            }
88
        );
89
        $this->app->singleton(
90
            LoggerContract::class,
91
            function (): ReliqArtsLoggerContract {
92
                $logger = new ReliqArtsLogger($this->getLoggerName());
93
                $logFile = storage_path(sprintf('logs/%s.log', self::LOG_FILENAME));
94
                $logger->pushHandler(new StreamHandler($logFile, ReliqArtsLogger::DEBUG));
95
96
                return $logger;
97
            }
98
        );
99
    }
100
101
    private function addViewComposers(): void
102
    {
103
        $viewFactory = resolve(ViewFactory::class);
104
        $viewFactory->composer(
105
            '*',
106
            function (View $view): void {
107
                $view->with('styleImporter', resolve(Importer::class));
108
                $view->with('styleImporterConfigProvider', resolve(ConfigProvider::class));
109
                $view->with($this->configProvider->getCurrentViewNameVariableName(), $view->getName());
110
            }
111
        );
112
    }
113
114
    private function handleViews(): void
115
    {
116
        $configKey = $this->getConfigKey();
117
        $viewsDirectory = sprintf('%s/resources/views', $this->getAssetDirectory());
118
119
        // Load the views...
120
        $this->loadViewsFrom($viewsDirectory, $configKey);
121
122
        // Allow publishing view files, with tag: views
123
        $this->publishes([
124
            $viewsDirectory => base_path(sprintf('resources/views/vendor/%s', $configKey)),
125
        ], sprintf('%s-views', $configKey));
126
    }
127
}
128