Skip to content

Commit

Permalink
Added abstract service provider for the templates
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrey Helldar committed Dec 2, 2021
1 parent 0ef36c0 commit e7dfd7f
Show file tree
Hide file tree
Showing 2 changed files with 105 additions and 0 deletions.
75 changes: 75 additions & 0 deletions src/Concerns/BaseServiceProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<?php

/*
* This file is part of the "laravel-lang/publisher" project.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @author Andrey Helldar <[email protected]>
*
* @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);
}
}
30 changes: 30 additions & 0 deletions src/Exceptions/ProviderNotDefinedException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

/*
* This file is part of the "laravel-lang/publisher" project.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @author Andrey Helldar <[email protected]>
*
* @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.');
}
}

0 comments on commit e7dfd7f

Please sign in to comment.