Skip to content

Commit

Permalink
add BaseServiceProvider
Browse files Browse the repository at this point in the history
  • Loading branch information
uyab committed Jan 8, 2020
1 parent d43412a commit 3a633ce
Showing 1 changed file with 76 additions and 0 deletions.
76 changes: 76 additions & 0 deletions src/Base/BaseServiceProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<?php

declare(strict_types=1);

namespace Laravolt\Support\Base;

use Illuminate\Support\Arr;
use Illuminate\Support\Collection;
use Illuminate\Support\ServiceProvider;

class BaseServiceProvider extends ServiceProvider
{
protected $name;

/** @var Collection */
protected $config;

public function register()
{
$file = $this->packagePath("config/{$this->name}.php");
$this->mergeConfigFrom($file, "laravolt.{$this->name}");
$this->publishes([$file => config_path("laravolt/{$this->name}.php")], 'config');

$this->config = collect(config("laravolt.{$this->name}"));
}

public function boot()
{
$this->bootRoutes()
->bootMigrations()
->bootViews();
}

protected function bootRoutes()
{
if (Arr::get($this->config, 'route.enabled')) {
$router = $this->app['router'];
require $this->packagePath('routes/web.php');
}

return $this;
}

protected function bootViews()
{
$viewFolder = $this->packagePath('resources/views');
$this->loadViewsFrom($viewFolder, $this->name);
$this->publishes(
[$viewFolder => base_path("resources/views/vendor/{$this->name}}")],
'views'
);

return $this;
}

protected function bootMigrations()
{
$databaseFolder = $this->packagePath('database/migrations');
if ($this->app->runningInConsole()) {
$this->loadMigrationsFrom($databaseFolder);
}
$this->publishes([
$databaseFolder => database_path('migrations'),
], 'migrations');

return $this;
}

protected function packagePath($path)
{
$rc = new \ReflectionClass(get_class($this));
$dir = dirname($rc->getFileName());

return sprintf('%s/../%s', $dir, $path);
}
}

0 comments on commit 3a633ce

Please sign in to comment.