Skip to content

Commit

Permalink
Merge pull request #168 from Laravel-Lang/11.1-split
Browse files Browse the repository at this point in the history
Added abstract service provider for the templates
  • Loading branch information
Andrey Helldar authored Dec 2, 2021
2 parents 61706ca + e7dfd7f commit b15fbcc
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 b15fbcc

Please sign in to comment.