Skip to content

Commit 09e5e3a

Browse files
committed
fix: improve Laravel version compatibility and simplify middleware configuration
1 parent d383bab commit 09e5e3a

File tree

4 files changed

+27
-19
lines changed

4 files changed

+27
-19
lines changed

config/artisan-command-palette-ui.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
'route_prefix' => 'artisan-command-palette',
1515

1616
// Middleware for the command palette routes
17-
'middleware' => ['web', 'auth'],
17+
'middleware' => [],
1818

1919
// Commands to exclude from the palette
2020
'excluded_commands' => [

routes/web.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
Route::group([
77
'prefix' => config('artisan-command-palette-ui.route_prefix', 'artisan-command-palette'),
8-
'middleware' => config('artisan-command-palette-ui.middleware', ['web', 'auth']),
8+
'middleware' => config('artisan-command-palette-ui.middleware', ['auth']),
99
], function () {
1010
Route::get('/', [ArtisanCommandController::class, 'index'])->name('artisan-command-palette.index');
1111
Route::get('/commands', [ArtisanCommandController::class, 'listCommands'])->name('artisan-command-palette.commands');

src/ArtisanCommandPaletteUIServiceProvider.php

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,17 @@ public function boot()
1616
// Load views
1717
$this->loadViewsFrom(__DIR__ . '/../resources/views', 'artisan-command-palette-ui');
1818

19-
// Load routes
20-
$this->loadRoutesFrom(__DIR__ . '/../routes/web.php');
19+
// Register routes in a way compatible with all Laravel versions
20+
if (version_compare($this->app->version(), '12.0.0', '>=')) {
21+
// Laravel 12+ route registration
22+
$this->app->booted(function () {
23+
$router = $this->app['router'];
24+
require __DIR__ . '/../routes/web.php';
25+
});
26+
} else {
27+
// Legacy route registration
28+
$this->loadRoutesFrom(__DIR__ . '/../routes/web.php');
29+
}
2130

2231
// Load assets
2332
$this->publishes([
@@ -44,7 +53,8 @@ public function register()
4453
{
4554
// Merge config
4655
$this->mergeConfigFrom(
47-
__DIR__ . '/../config/artisan-command-palette-ui.php', 'artisan-command-palette-ui'
56+
__DIR__ . '/../config/artisan-command-palette-ui.php',
57+
'artisan-command-palette-ui'
4858
);
4959
}
5060
}

src/Http/Controllers/ArtisanCommandController.php

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
namespace LaravelReady\ArtisanCommandPaletteUI\Http\Controllers;
44

55
use Illuminate\Http\Request;
6-
use Illuminate\Http\JsonResponse;
76
use Illuminate\Routing\Controller;
87
use Illuminate\Support\Facades\App;
98
use Illuminate\Support\Facades\Artisan;
@@ -34,14 +33,14 @@ protected function getCommandGroups()
3433
$commandGroups = Config::get('artisan-command-palette-ui.command_groups', []);
3534
$envRestrictions = Config::get('artisan-command-palette-ui.environment_restricted_groups', []);
3635
$currentEnv = App::environment();
37-
36+
3837
// Apply environment restrictions
3938
foreach ($envRestrictions as $group => $allowedEnvs) {
4039
if (!in_array($currentEnv, $allowedEnvs) && isset($commandGroups[$group])) {
4140
$commandGroups[$group] = [];
4241
}
4342
}
44-
43+
4544
return $commandGroups;
4645
}
4746

@@ -60,13 +59,13 @@ public function listCommands()
6059
// Return both predefined command groups and all available commands
6160
$commandGroups = $this->getCommandGroups();
6261
$allCommands = $this->getAllCommands();
63-
62+
6463
return Response::json([
6564
'groups' => $commandGroups,
6665
'all' => $allCommands,
6766
]);
6867
}
69-
68+
7069
/**
7170
* Get all available commands excluding the ones in the excluded list.
7271
*
@@ -112,7 +111,7 @@ protected function getAllCommands()
112111
public function executeCommand(Request $request)
113112
{
114113
$command = $request->input('command');
115-
114+
116115
if (empty($command)) {
117116
return Response::json([
118117
'success' => false,
@@ -126,8 +125,9 @@ public function executeCommand(Request $request)
126125
$commandName = array_shift($parts);
127126
$arguments = $parts;
128127

129-
// Check if command exists
130-
if (!Artisan::has($commandName)) {
128+
// Check if command exists - compatible with Laravel 8-12
129+
$commands = Artisan::all();
130+
if (!array_key_exists($commandName, $commands)) {
131131
return Response::json([
132132
'success' => false,
133133
'message' => 'Error executing command',
@@ -147,15 +147,13 @@ public function executeCommand(Request $request)
147147

148148
// Execute the command
149149
try {
150-
ob_start();
151-
$exitCode = Artisan::call($commandName, $this->parseArguments($arguments));
152-
$output = ob_get_clean();
150+
Artisan::call($command);
151+
$output = Artisan::output();
153152

154153
return Response::json([
155154
'success' => true,
156155
'message' => 'Command executed successfully',
157156
'output' => $output,
158-
'exit_code' => $exitCode
159157
]);
160158
} catch (\Exception $e) {
161159
return Response::json([
@@ -175,7 +173,7 @@ public function executeCommand(Request $request)
175173
protected function parseArguments(array $arguments)
176174
{
177175
$result = [];
178-
176+
179177
foreach ($arguments as $argument) {
180178
if (strpos($argument, '=') !== false) {
181179
list($key, $value) = explode('=', $argument, 2);
@@ -186,7 +184,7 @@ protected function parseArguments(array $arguments)
186184
$result[] = $argument;
187185
}
188186
}
189-
187+
190188
return $result;
191189
}
192190
}

0 commit comments

Comments
 (0)