Completed
Push — master ( bbe544...1bc078 )
by ReliQ
03:23 queued 11s
created

Laravel::provides()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 0
dl 0
loc 5
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace ReliqArts\DirectTranslator\ServiceProvider;
6
7
use Illuminate\Contracts\Foundation\Application;
8
use Illuminate\Support\ServiceProvider as IlluminateServiceProvider;
9
use Psr\Container\ContainerExceptionInterface;
10
use ReliqArts\DirectTranslator\ConfigProvider as ConfigProviderContract;
11
use ReliqArts\DirectTranslator\ConfigProvider\Laravel as LaravelConfigProvider;
12
use ReliqArts\DirectTranslator\ServiceProvider;
13
use ReliqArts\DirectTranslator\Translation\Executor;
14
use ReliqArts\DirectTranslator\Translation\Formatter\SentenceCase;
15
use ReliqArts\DirectTranslator\Translation\Replacer\PatternReplacer;
16
use ReliqArts\DirectTranslator\Translator;
17
use ReliqArts\DirectTranslator\Vocabulary\Builder;
18
use ReliqArts\DirectTranslator\Vocabulary\Builder\StandardVocabularyBuilder;
19
use ReliqArts\DirectTranslator\Vocabulary\Loader;
20
use ReliqArts\DirectTranslator\Vocabulary\Reader;
21
use ReliqArts\DirectTranslator\Vocabulary\Reader\JsonReader;
22
use ReliqArts\DirectTranslator\VocabularyLoader;
23
24
class Laravel extends IlluminateServiceProvider implements ServiceProvider
25
{
26
    /**
27
     * Perform post-registration booting of services.
28
     */
29
    public function boot(): void
30
    {
31
        $this->handleConfig();
32
    }
33
34
    /**
35
     * {@inheritdoc}
36
     */
37
    public function register(): void
38
    {
39
        $this->app->singleton(ConfigProviderContract::class, LaravelConfigProvider::class);
40
        $this->app->singleton(VocabularyLoader::class, Loader::class);
41
        $this->app->singleton(Builder::class, StandardVocabularyBuilder::class);
42
        $this->app->singleton(Reader::class, JsonReader::class);
43
        $this->app->singleton(Translator::class, function (Application $app): Translator {
44
            $executor = new Executor($app->get(VocabularyLoader::class));
45
46
            $executor->addFormatter($app->get(SentenceCase::class));
47
            $executor->addReplacer($app->get(PatternReplacer::class));
48
49
            return $executor;
50
        });
51
    }
52
53
    /**
54
     * @return array
55
     */
56
    public function provides(): array
57
    {
58
        return [
59
            Translator::class,
60
            VocabularyLoader::class,
61
        ];
62
    }
63
64
    /**
65
     * @param string $name
66
     *
67
     * @throws ContainerExceptionInterface
68
     *
69
     * @return mixed
70
     */
71
    public function resolve(string $name)
72
    {
73
        return $this->app->get($name);
74
    }
75
76
    /**
77
     * {@inheritdoc}
78
     */
79
    public function set(string $name, ...$concrete)
80
    {
81
        $this->app->bind($name, ...$concrete);
0 ignored issues
show
Bug introduced by
$concrete is expanded, but the parameter $concrete of Illuminate\Container\Container::bind() does not expect variable arguments. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

81
        $this->app->bind($name, /** @scrutinizer ignore-type */ ...$concrete);
Loading history...
82
    }
83
84
    /**
85
     * @return self
86
     */
87
    private function handleConfig(): self
88
    {
89
        $configFilePath = ConfigProviderContract::CONFIG_FILE_PATH;
90
        $configKey = ConfigProviderContract::CONFIG_KEY_PACKAGE;
91
92
        // merge config
93
        $this->mergeConfigFrom($configFilePath, $configKey);
94
95
        // allow publishing the config file, with tag: [package config key]:config
96
        $this->publishes(
97
            [
98
                $configFilePath => config_path(sprintf('%s.php', $configKey)),
99
            ],
100
            sprintf('%s:config', $configKey)
101
        );
102
103
        return $this;
104
    }
105
}
106