Skip to content

Commit 0dc1fd8

Browse files
committed
media install command
1 parent af20073 commit 0dc1fd8

File tree

3 files changed

+219
-66
lines changed

3 files changed

+219
-66
lines changed

packages/media/database/migrations/create_media_table.php.stub

Lines changed: 0 additions & 53 deletions
This file was deleted.
Lines changed: 205 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,205 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Moox\Media\Console\Commands;
6+
7+
use Illuminate\Console\Command;
8+
use Illuminate\Support\Facades\File;
9+
use Illuminate\Support\Facades\Schema;
10+
use function Laravel\Prompts\alert;
11+
use function Laravel\Prompts\confirm;
12+
use function Laravel\Prompts\info;
13+
use function Laravel\Prompts\multiselect;
14+
use function Laravel\Prompts\note;
15+
use function Laravel\Prompts\warning;
16+
17+
class InstallCommand extends Command
18+
{
19+
/**
20+
* The name and signature of the console command.
21+
*
22+
* @var string
23+
*/
24+
protected $signature = 'media:install';
25+
26+
/**
27+
* The console command description.
28+
*
29+
* @var string
30+
*/
31+
protected $description = 'Installs Moox Media, publishes configuration, migrations and registers plugins.';
32+
33+
/**
34+
* Execute the console command.
35+
*/
36+
public function handle(): void
37+
{
38+
$this->art();
39+
$this->welcome();
40+
$this->publishConfiguration();
41+
$this->publishMigrations();
42+
$this->runMigrations();
43+
$this->registerPluginInPanelProvider();
44+
$this->sayGoodbye();
45+
}
46+
47+
public function art(): void
48+
{
49+
info('
50+
51+
▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓ ▓▓▓▓▓▓▓▓▓▓▓ ▓▓▓▓▓▓▓▓▓▓▓▓ ▓▓▓▓▓▓▓▓▓▓▓▓ ▓▓▓▓▓▓▓ ▓▓▓▓▓▓▓
52+
▓▓▒░░▒▓▓▒▒░░░░░░▒▒▓▓▓▒░░░░░░░▒▓▓ ▓▓▓▓▒░░░░░░░▒▓▓▓▓ ▓▓▓▓▓▒░░░░░░░▒▒▓▓▓▓▓▒▒▒▒▓▓ ▓▓▓▒▒▒▒▓▓
53+
▓▒░░░░░░░░░░░░░░░░░░░░░░░░░░░░░▓▓▓▓▓▒░░░░░░░░░░░░░▒▓▓▓ ▓▓▓▓▒░░░░░░░░░░░░░▒▓▓▓░░░░░▒▓▓ ▓▓▒░░░░░▓▓
54+
▓▒░░░░░░▒▓▓▓▓▒░░░░░░░▒▓▓▓▓░░░░░▒▓▓▓░░░░░▒▓▓▓▓▒░░░░░░░▓▓▓▓░░░░░░▒▓▓▓▓▓░░░░░░▒▓▓░░░░░▒▓▓▓▓▓░░░░░▒▓▓
55+
▓▒░░░░▓▓▓▓ ▓▓░░░░░▓▓▓ ▓▓▓░░░░▒▓▓░░░░▒▓▓▓ ▓▓▓▓░░░░░▓░░░░░░▓▓▓▓ ▓▓▓▒░░░░▓▓▓▒░░░░░▓▓▓░░░░░▓▓▓
56+
▓▒░░░░▒▓ ▓▓░░░░░▓▓ ▓▓░░░░▒▓░░░░▒▓▓ ▓▓▓░░▒░░░░░▓▓▓ ▓▓░░░░▒▓▓▓▓░░░░░░░░░░░▓▓
57+
▓▒░░░░▒▓ ▓▓░░░░░▓▓ ▓▓░░░░▒▓░░░░▒▓ ▓▓▓░░░░░▒▓▓ ▓▓▒░░░░▓ ▓▓▓░░░░░░░░░▓▓
58+
▓▒░░░░▒▓ ▓▓░░░░░▓▓ ▓▓░░░░▒▓░░░░▒▓▓ ▓▓▒░░░░░▒░░▒▓▓ ▓▓░░░░▒▓▓▓▒░░░░░▒░░░░░▒▓
59+
▓▒░░░░▒▓ ▓▓░░░░░▓▓ ▓▓░░░░▒▓▓░░░░▒▓▓▓ ▓▓▓▒░░░░░▒▒░░░░░▒▓▓▓ ▓▓▓░░░░░▓▓▓░░░░░▒▓▓▓░░░░░▒▓▓
60+
▓▒░░░░▒▓ ▓▓░░░░░▓▓ ▓▓░░░░▒▓▓▓░░░░░░▒▒▓▓▒░░░░░░▒▓▓▓▓░░░░░░░▒▒▓▓▒░░░░░░▓▓▓░░░░░▒▓▓▓▓▓▒░░░░░▓▓
61+
▓▒░░░░▒▓ ▓▓░░░░░▓▓ ▓▓░░░░▒▓▓▓▓▒░░░░░░░░░░░░░▒▓▓▓ ▓▓▓▓▒░░░░░░░░░░░░░▒▓▓▒░░░░░▓▓▓ ▓▓▒░░░░░▒▓
62+
▓▓░░░▒▓▓ ▓▓▒░░░▒▓▓ ▓▓░░░░▓▓ ▓▓▓▓▒░░░░░░▒▒▓▓▓▓ ▓▓▓▓▓▒▒░░░░░▒▒▓▓▓▓▓░░░░▒▓▓ ▓▓▓░░░░▒▓
63+
▓▓▓▓▓▓▓ ▓▓▓▓▓▓▓ ▓▓▓▓▓▓▓▓ ▓▓▓▓▓▓▓▓▓▓▓▓ ▓▓▓▓▓▓▓▓▓▓▓▓ ▓▓▓▓▓▓▓▓ ▓▓▓▓▓▓▓▓
64+
65+
');
66+
}
67+
68+
public function welcome(): void
69+
{
70+
info('Welcome to Moox Media Installer');
71+
}
72+
73+
public function publishConfiguration(): void
74+
{
75+
if (confirm('Do you wish to publish the configuration?', true)) {
76+
if (!File::exists('config/media.php')) {
77+
info('Publishing Media Configuration...');
78+
$this->callSilent('vendor:publish', ['--tag' => 'media-config']);
79+
80+
return;
81+
}
82+
83+
warning('The Media config already exists. The config will not be published.');
84+
}
85+
}
86+
87+
public function publishMigrations(): void
88+
{
89+
if (confirm('Do you wish to publish the migrations?', true)) {
90+
if (Schema::hasTable('media')) {
91+
warning('The media table already exists. The migrations will not be published.');
92+
93+
return;
94+
}
95+
96+
info('Publishing Media Migrations...');
97+
$this->callSilent('vendor:publish', ['--tag' => 'media-migrations']);
98+
}
99+
}
100+
101+
public function runMigrations(): void
102+
{
103+
if (confirm('Do you wish to run the migrations?', true)) {
104+
info('Running Media Migrations...');
105+
$this->callSilent('migrate');
106+
}
107+
}
108+
109+
public function registerPlugins(string $providerPath): void
110+
{
111+
if (File::exists($providerPath)) {
112+
$content = File::get($providerPath);
113+
114+
$intend = ' ';
115+
116+
$namespace = "\Moox\Media";
117+
118+
$pluginsToAdd = multiselect(
119+
label: 'These plugins will be installed:',
120+
options: ['MediaPlugin', 'MediaCollectionPlugin'],
121+
default: ['MediaPlugin', 'MediaCollectionPlugin'],
122+
);
123+
124+
$function = '::make(),';
125+
126+
$pattern = '/->plugins\(\[([\s\S]*?)\]\);/';
127+
$newPlugins = '';
128+
129+
foreach ($pluginsToAdd as $plugin) {
130+
$searchPlugin = '/' . $plugin . '/';
131+
if (preg_match($searchPlugin, $content)) {
132+
warning($plugin . ' already registered.');
133+
} else {
134+
$newPlugins .= $intend . $namespace . '\\' . $plugin . $function . "\n";
135+
}
136+
}
137+
138+
if ($newPlugins !== '' && $newPlugins !== '0') {
139+
if (preg_match($pattern, $content)) {
140+
info('Plugins section found. Adding new plugins...');
141+
142+
$replacement = "->plugins([$1\n{$newPlugins}\n ]);";
143+
$newContent = preg_replace($pattern, $replacement, $content);
144+
} else {
145+
info('Plugins section created. Adding new plugins...');
146+
147+
$pluginsSection = " ->plugins([\n{$newPlugins}\n ]);";
148+
$placeholderPattern = '/(\->authMiddleware\(\[.*?\]\))\s*\;/s';
149+
$replacement = "$1\n" . $pluginsSection;
150+
$newContent = preg_replace($placeholderPattern, $replacement, $content, 1);
151+
}
152+
153+
File::put($providerPath, $newContent);
154+
}
155+
} else {
156+
alert('There are no new plugins detected.');
157+
}
158+
}
159+
160+
public function registerPluginInPanelProvider(): void
161+
{
162+
$providerPath = app_path('Providers/Filament');
163+
$panelsToregister = $this->getPanelProviderPath();
164+
if ($panelsToregister != null) {
165+
if (is_array($panelsToregister)) {
166+
foreach ($panelsToregister as $panelprovider) {
167+
$this->registerPlugins($providerPath . '/' . $panelprovider);
168+
}
169+
} else {
170+
$this->registerPlugins($panelsToregister);
171+
}
172+
} else {
173+
alert('No PanelProvider Detected please register Plugins manually.');
174+
}
175+
}
176+
177+
public function getPanelProviderPath(): string|array
178+
{
179+
$providerPath = app_path('Providers/Filament');
180+
$providers = File::allFiles($providerPath);
181+
if (count($providers) > 1) {
182+
$providerNames = [];
183+
foreach ($providers as $provider) {
184+
$providerNames[] = $provider->getBasename();
185+
}
186+
187+
$providerPath = multiselect(
188+
label: 'Which Panel should it be registered',
189+
options: [...$providerNames],
190+
default: [$providerNames[0]],
191+
);
192+
}
193+
194+
if (count($providers) == 1) {
195+
$providerPath .= '/' . $providers[0]->getBasename();
196+
}
197+
198+
return $providerPath;
199+
}
200+
201+
public function sayGoodbye(): void
202+
{
203+
note('Moox Media installed successfully. Enjoy!');
204+
}
205+
}

packages/media/src/MediaServiceProvider.php

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,21 @@
44

55
namespace Moox\Media;
66

7-
use Filament\Support\Assets\Css;
8-
use Filament\Support\Assets\Js;
9-
use Filament\Support\Facades\FilamentAsset;
10-
use Filament\Support\Facades\FilamentView;
11-
use Filament\Tables\View\TablesRenderHook;
12-
use Illuminate\Support\Facades\Blade;
13-
use Illuminate\Support\Facades\Gate;
147
use Livewire\Livewire;
15-
use Moox\Media\Http\Livewire\MediaPickerModal;
168
use Moox\Media\Models\Media;
9+
use Filament\Support\Assets\Js;
10+
use Filament\Support\Assets\Css;
11+
use Illuminate\Support\Facades\Gate;
1712
use Moox\Media\Policies\MediaPolicy;
18-
use Moox\Media\Resources\MediaResource\Pages\ListMedia;
13+
use Illuminate\Support\Facades\Blade;
14+
use Moox\Media\Console\Commands\InstallCommand;
1915
use Spatie\LaravelPackageTools\Package;
16+
use Filament\Support\Facades\FilamentView;
17+
use Filament\Tables\View\TablesRenderHook;
18+
use Filament\Support\Facades\FilamentAsset;
19+
use Moox\Media\Http\Livewire\MediaPickerModal;
2020
use Spatie\LaravelPackageTools\PackageServiceProvider;
21+
use Moox\Media\Resources\MediaResource\Pages\ListMedia;
2122

2223
class MediaServiceProvider extends PackageServiceProvider
2324
{
@@ -29,7 +30,7 @@ public function configurePackage(Package $package): void
2930
->hasViews('media-picker')
3031
->hasTranslations()
3132
->discoversMigrations()
32-
->hasCommands()
33+
->hasCommands(InstallCommand::class)
3334
->hasAssets();
3435
}
3536

@@ -39,11 +40,11 @@ public function boot()
3940

4041
Gate::policy(Media::class, MediaPolicy::class);
4142

42-
$this->loadViewsFrom(__DIR__.'/../resources/views', 'media');
43+
$this->loadViewsFrom(__DIR__ . '/../resources/views', 'media');
4344
Livewire::component('media-picker-modal', MediaPickerModal::class);
4445

4546
$this->publishes([
46-
__DIR__.'/../resources/dist/icons' => public_path('vendor/media/icons'),
47+
__DIR__ . '/../resources/dist/icons' => public_path('vendor/media/icons'),
4748
], 'media-icons');
4849

4950
FilamentAsset::register([
@@ -53,7 +54,7 @@ public function boot()
5354

5455
FilamentView::registerRenderHook(
5556
TablesRenderHook::TOOLBAR_TOGGLE_COLUMN_TRIGGER_BEFORE,
56-
fn (): string => Blade::render('@include("localization::lang-selector")'),
57+
fn(): string => Blade::render('@include("localization::lang-selector")'),
5758
scopes: ListMedia::class
5859
);
5960
}

0 commit comments

Comments
 (0)