reliqarts /
direct-translator
| 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 | /** |
||
| 25 | * Class Laravel. |
||
| 26 | * |
||
| 27 | * @codeCoverageIgnore |
||
| 28 | */ |
||
| 29 | class Laravel extends IlluminateServiceProvider implements ServiceProvider |
||
| 30 | { |
||
| 31 | /** |
||
| 32 | * Perform post-registration booting of services. |
||
| 33 | */ |
||
| 34 | public function boot(): void |
||
| 35 | { |
||
| 36 | $this->handleConfig(); |
||
| 37 | } |
||
| 38 | |||
| 39 | /** |
||
| 40 | * {@inheritdoc} |
||
| 41 | */ |
||
| 42 | public function register(): void |
||
| 43 | { |
||
| 44 | $this->app->singleton(ConfigProviderContract::class, LaravelConfigProvider::class); |
||
| 45 | $this->app->singleton(VocabularyLoader::class, Loader::class); |
||
| 46 | $this->app->singleton(Builder::class, StandardVocabularyBuilder::class); |
||
| 47 | $this->app->singleton(Reader::class, JsonReader::class); |
||
| 48 | $this->app->singleton(Translator::class, function (Application $app): Translator { |
||
| 49 | $executor = new Executor($app->get(VocabularyLoader::class)); |
||
| 50 | |||
| 51 | $executor->addFormatter($app->get(SentenceCase::class)); |
||
| 52 | $executor->addReplacer($app->get(PatternReplacer::class)); |
||
| 53 | |||
| 54 | return $executor; |
||
| 55 | }); |
||
| 56 | } |
||
| 57 | |||
| 58 | /** |
||
| 59 | * @return array |
||
| 60 | */ |
||
| 61 | public function provides(): array |
||
| 62 | { |
||
| 63 | return [ |
||
| 64 | Translator::class, |
||
| 65 | VocabularyLoader::class, |
||
| 66 | ]; |
||
| 67 | } |
||
| 68 | |||
| 69 | /** |
||
| 70 | * @param string $name |
||
| 71 | * |
||
| 72 | * @throws ContainerExceptionInterface |
||
| 73 | * |
||
| 74 | * @return mixed |
||
| 75 | */ |
||
| 76 | public function resolve(string $name) |
||
| 77 | { |
||
| 78 | return $this->app->get($name); |
||
| 79 | } |
||
| 80 | |||
| 81 | /** |
||
| 82 | * {@inheritdoc} |
||
| 83 | */ |
||
| 84 | public function set(string $name, ...$concrete) |
||
| 85 | { |
||
| 86 | $this->app->bind($name, ...$concrete); |
||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
| 87 | } |
||
| 88 | |||
| 89 | /** |
||
| 90 | * @return self |
||
| 91 | */ |
||
| 92 | private function handleConfig(): self |
||
| 93 | { |
||
| 94 | $configFilePath = ConfigProviderContract::CONFIG_FILE_PATH; |
||
| 95 | $configKey = ConfigProviderContract::CONFIG_KEY_PACKAGE; |
||
| 96 | |||
| 97 | // merge config |
||
| 98 | $this->mergeConfigFrom($configFilePath, $configKey); |
||
| 99 | |||
| 100 | // allow publishing the config file, with tag: [package config key]:config |
||
| 101 | $this->publishes( |
||
| 102 | [ |
||
| 103 | $configFilePath => config_path(sprintf('%s.php', $configKey)), |
||
| 104 | ], |
||
| 105 | sprintf('%s:config', $configKey) |
||
| 106 | ); |
||
| 107 | |||
| 108 | return $this; |
||
| 109 | } |
||
| 110 | } |
||
| 111 |