-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathInstallCommand.php
84 lines (68 loc) · 2.48 KB
/
InstallCommand.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
<?php
declare(strict_types=1);
namespace Sandulat\LaravelDashboardTemplate\Console;
use Illuminate\Support\Str;
use Illuminate\Console\Command;
use Illuminate\Console\DetectsApplicationNamespace;
/**
* Credits: laravel/telescope.
*/
class InstallCommand extends Command
{
use DetectsApplicationNamespace;
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'laravel-dashboard-template:install';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Install all of the dashboard resources';
/**
* Execute the console command.
*
* @return void
*/
public function handle(): void
{
$this->comment('Publishing Dashboard Service Provider...');
$this->callSilent('vendor:publish', ['--tag' => 'laravel-dashboard-template-provider']);
$this->comment('Publishing Dashboard Assets...');
$this->callSilent('vendor:publish', ['--tag' => 'laravel-dashboard-template-assets']);
$this->registerDashboardServiceProvider();
$this->info('Dashboard scaffolding installed successfully.');
}
/**
* Register the dashboard service provider in the application configuration file.
*
* @return void
*/
protected function registerDashboardServiceProvider(): void
{
$namespace = str_replace_last('\\', '', $this->getAppNamespace());
$appConfig = file_get_contents(config_path('app.php'));
if (Str::contains($appConfig, $namespace.'\\Providers\\DashboardServiceProvider::class')) {
return;
}
$lineEndingCount = [
"\r\n" => substr_count($appConfig, "\r\n"),
"\r" => substr_count($appConfig, "\r"),
"\n" => substr_count($appConfig, "\n"),
];
$eol = array_keys($lineEndingCount, max($lineEndingCount))[0];
file_put_contents(config_path('app.php'), str_replace(
"{$namespace}\\Providers\EventServiceProvider::class,".$eol,
"{$namespace}\\Providers\EventServiceProvider::class,".$eol." {$namespace}\Providers\DashboardServiceProvider::class,".$eol,
$appConfig
));
file_put_contents(app_path('Providers/DashboardServiceProvider.php'), str_replace(
"namespace App\Providers;",
"namespace {$namespace}\Providers;",
file_get_contents(app_path('Providers/DashboardServiceProvider.php'))
));
}
}