diff --git a/src/Concerns/BaseServiceProvider.php b/src/Concerns/BaseServiceProvider.php new file mode 100644 index 00000000..41185e76 --- /dev/null +++ b/src/Concerns/BaseServiceProvider.php @@ -0,0 +1,75 @@ + + * + * @copyright 2021 Andrey Helldar + * + * @license MIT + * + * @see https://github.com/Laravel-Lang/publisher + */ + +declare(strict_types=1); + +namespace LaravelLang\Publisher\Concerns; + +use DragonCode\Support\Facades\Helpers\Ables\Arrayable; +use Illuminate\Support\ServiceProvider; +use LaravelLang\Publisher\Constants\Config; +use LaravelLang\Publisher\Exceptions\ProviderNotDefinedException; + +class BaseServiceProvider extends ServiceProvider +{ + protected $provider; + + public function register(): void + { + $this->registerProvider(); + } + + protected function registerProvider(): void + { + $config = $this->plugins(); + + $plugins = $this->push($config); + + $this->setConfig($plugins); + } + + protected function getProvider(): string + { + if ($this->provider) { + return $this->provider; + } + + throw new ProviderNotDefinedException(); + } + + protected function push(array $plugins): array + { + return Arrayable::of($plugins) + ->push($this->getProvider()) + ->unique() + ->sort() + ->values() + ->get(); + } + + protected function plugins(): array + { + return config(Config::PUBLIC_KEY); + } + + protected function setConfig(array $plugins): void + { + $key = Config::PUBLIC_KEY . '.plugins'; + + config()->set($key, $plugins); + } +} diff --git a/src/Exceptions/ProviderNotDefinedException.php b/src/Exceptions/ProviderNotDefinedException.php new file mode 100644 index 00000000..f605df40 --- /dev/null +++ b/src/Exceptions/ProviderNotDefinedException.php @@ -0,0 +1,30 @@ + + * + * @copyright 2021 Andrey Helldar + * + * @license MIT + * + * @see https://github.com/Laravel-Lang/publisher + */ + +declare(strict_types=1); + +namespace LaravelLang\Publisher\Exceptions; + +use RuntimeException; + +class ProviderNotDefinedException extends RuntimeException +{ + public function __construct() + { + parent::__construct('Provider class not defined.'); + } +}