Skip to content

Commit

Permalink
Fix styling
Browse files Browse the repository at this point in the history
  • Loading branch information
bezhanSalleh authored and github-actions[bot] committed Nov 6, 2024
1 parent 7e0e342 commit 5469188
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 39 deletions.
2 changes: 2 additions & 0 deletions src/Commands/Concerns/CanManipulateFiles.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,12 @@ protected function copy(string $source, string $destination): bool
if (! $this->fileExists($destination)) {
$filesystem->copy($source, $destination);
$this->components->info("$destination file published!");

return true;
}

$this->components->warn("$destination already exists, skipping ...");

return false;
}
}
2 changes: 1 addition & 1 deletion src/Commands/InstallCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@

use Filament\Facades\Filament;
use Illuminate\Console\Command;
use Symfony\Component\Console\Attribute\AsCommand;
use Illuminate\Contracts\Console\PromptsForMissingInput;
use Symfony\Component\Console\Attribute\AsCommand;

#[AsCommand(name: 'shield:install')]
class InstallCommand extends Command implements PromptsForMissingInput
Expand Down
3 changes: 2 additions & 1 deletion src/Commands/MakeShieldSuperAdminCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class MakeShieldSuperAdminCommand extends Command

protected Authenticatable $superAdmin;

protected null | string $superAdminRole = null;
protected ?string $superAdminRole = null;

protected function getAuthGuard(): Guard
{
Expand Down Expand Up @@ -59,6 +59,7 @@ public function handle(): int
if (Utils::isTeamFeatureEnabled()) {
if (blank($teamId)) {
$this->components->error('Please provide the team id via `--team` option to assign the super admin to a team.');

return self::FAILURE;
}
if (Utils::getRoleModel()::whereName(Utils::getSuperAdminName())->whereTeamId($teamId)->doesntExist()) {
Expand Down
41 changes: 21 additions & 20 deletions src/Commands/SetupCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,23 @@

namespace BezhanSalleh\FilamentShield\Commands;

use Throwable;
use BezhanSalleh\FilamentShield\Stringer;
use BezhanSalleh\FilamentShield\Support\Utils;
use Illuminate\Console\Command;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Eloquent\Model;
use BezhanSalleh\FilamentShield\Stringer;
use BezhanSalleh\FilamentShield\Support\Utils;
use BezhanSalleh\FilamentShield\Commands\Concerns;
use Symfony\Component\Console\Attribute\AsCommand;
use Throwable;

use function Laravel\Prompts\confirm;

#[AsCommand(name: 'shield:setup', description: 'Setup and install core requirements for Shield')]
class SetupCommand extends Command
{
use Concerns\CanManipulateFiles;

public $signature = 'shield:setup
{--F|fresh : re-run the migrations}
{--minimal : Output minimal amount of info to console}
Expand Down Expand Up @@ -113,14 +113,14 @@ protected function install(bool $fresh = false): void

$shieldConfig = Stringer::for(config_path('filament-shield.php'));

if (is_null(config()->get("filament-shield.tenant_model", null))) {
if (is_null(config()->get('filament-shield.tenant_model', null))) {
$shieldConfig->prepend('auth_provider_model', "'tenant_model' => null,")
->newLine();
}

$shieldConfig
->append('tenant_model', "'tenant_model' => '" . get_class($tenantModel) . "',")
->deleteLine("tenant_model")
->deleteLine('tenant_model')
->save();

if (! $this->fileExists(config_path('permission.php'))) {
Expand All @@ -142,27 +142,27 @@ protected function install(bool $fresh = false): void
$this->copy($source . '/Permission.php', $destination . '/Permission.php');

$appServiceProvider = Stringer::for(app_path('Providers/AppServiceProvider.php'));
if (
! $appServiceProvider->containsChainedBlock('app(\Spatie\Permission\PermissionRegistrar::class)
if (
! $appServiceProvider->containsChainedBlock('app(\Spatie\Permission\PermissionRegistrar::class)
->setPermissionClass(Permission::class)
->setRoleClass(Role::class)')
) {
if (! $appServiceProvider->contains('use App\Models\Role;')) {
$appServiceProvider->append('use', 'use App\Models\Role;');
}
) {
if (! $appServiceProvider->contains('use App\Models\Role;')) {
$appServiceProvider->append('use', 'use App\Models\Role;');
}

if (! $appServiceProvider->contains('use App\Models\Permission;')) {
$appServiceProvider->append('use', 'use App\Models\Permission;');
}
if (! $appServiceProvider->contains('use App\Models\Permission;')) {
$appServiceProvider->append('use', 'use App\Models\Permission;');
}

$appServiceProvider
->appendBlock("public function boot()", "
$appServiceProvider
->appendBlock('public function boot()', "
app(\Spatie\Permission\PermissionRegistrar::class)
->setPermissionClass(Permission::class)
->setRoleClass(Role::class);
", true)
->save();
}
->save();
}
}

$this->{$this->option('minimal') ? 'callSilent' : 'call'}('vendor:publish', [
Expand Down Expand Up @@ -213,6 +213,7 @@ protected function getModel(string $model): ?Model
if (! class_exists($model) || ! (app($model) instanceof Model)) {
$this->components->error("Model not found: {$model}");
exit();

return null;
}

Expand Down
16 changes: 8 additions & 8 deletions src/FilamentShield.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,19 @@

namespace BezhanSalleh\FilamentShield;

use BezhanSalleh\FilamentShield\Support\Utils;
use Closure;
use Illuminate\Support\Str;
use Filament\Widgets\Widget;
use Filament\Facades\Filament;
use Filament\Support\Concerns\EvaluatesClosures;
use Filament\Widgets\TableWidget;
use Filament\Widgets\Widget;
use Filament\Widgets\WidgetConfiguration;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\DB;
use Spatie\Permission\Models\Role;
use Illuminate\Support\Facades\Lang;
use Filament\Widgets\WidgetConfiguration;
use Illuminate\Support\Str;
use Spatie\Permission\Models\Role;
use Spatie\Permission\PermissionRegistrar;
use BezhanSalleh\FilamentShield\Support\Utils;
use Filament\Support\Concerns\EvaluatesClosures;

class FilamentShield
{
Expand Down Expand Up @@ -106,13 +106,13 @@ protected static function giveSuperAdminPermission(string | array | Collection $
}
}

public static function createRole(?string $name = null, ?int $team_id= null): Role
public static function createRole(?string $name = null, ?int $team_id = null): Role
{
if (Utils::isTeamFeatureEnabled()) {
return Utils::getRoleModel()::firstOrCreate(
[
'name' => $name ?? Utils::getSuperAdminName(),
'team_id' => $team_id
'team_id' => $team_id,
],
['guard_name' => Utils::getFilamentAuthGuard()]
);
Expand Down
26 changes: 17 additions & 9 deletions src/Stringer.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
<?php /** @noinspection PhpSuspiciousNameCombinationInspection */
<?php

/** @noinspection PhpSuspiciousNameCombinationInspection */

declare(strict_types=1);

Expand Down Expand Up @@ -311,11 +313,14 @@ protected function findMethodDeclaration(string $needle): ?array
$braceLevel = 0;
$methodEndLine = $i + 1;
for ($j = $i + 1; $j < count($lines); $j++) {
if (str_contains($lines[$j], '{')) $braceLevel++;
if (str_contains($lines[$j], '{')) {
$braceLevel++;
}
if (str_contains($lines[$j], '}')) {
$braceLevel--;
if ($braceLevel === 0) {
$methodEndLine = $j;

break;
}
}
Expand All @@ -333,7 +338,7 @@ protected function findMethodDeclaration(string $needle): ?array
'indentation' => $indentation,
'is_method' => true,
'opening_brace_line' => $i + 1,
'closing_brace_line' => $methodEndLine
'closing_brace_line' => $methodEndLine,
];
}
}
Expand Down Expand Up @@ -368,9 +373,10 @@ public function findChainedBlock(string $block): ?array
if (str_contains($normalizedLine, trim($methodCalls[$currentMethodIndex]))) {
$currentMethodIndex++;
$endLine++;
} elseif (!empty($currentLine)) {
} elseif (! empty($currentLine)) {
// If we find a non-empty line that doesn't match, break
$matchFound = false;

break;
} else {
// Skip empty lines
Expand All @@ -396,7 +402,7 @@ public function findChainedBlock(string $block): ?array
'start' => $startPos,
'end' => $endPos,
'indentation' => $indentation,
'is_block' => true
'is_block' => true,
];
}
}
Expand All @@ -411,14 +417,14 @@ public function containsChainedBlock(string $block): bool

public function appendBlock(string $needle, string $contentToAppend, bool $afterBlock = false): static
{
if (!$this->contains($needle)) {
if (! $this->contains($needle)) {
return $this;
}

// Use findMethodDeclaration for better method handling
$lineInfo = $this->findMethodDeclaration($needle);

if (!$lineInfo) {
if (! $lineInfo) {
return $this;
}

Expand All @@ -435,9 +441,11 @@ public function appendBlock(string $needle, string $contentToAppend, bool $after
$formattedContent = '';
foreach ($contentLines as $index => $line) {
$trimmedLine = trim($line);
if (empty($trimmedLine)) continue;
if (empty($trimmedLine)) {
continue;
}

$formattedContent .= ($index > 0 ? PHP_EOL . $methodIndent : '' ) . $contentIndent . $trimmedLine;
$formattedContent .= ($index > 0 ? PHP_EOL . $methodIndent : '') . $contentIndent . $trimmedLine;
}

// Add new line if flag is set
Expand Down

0 comments on commit 5469188

Please sign in to comment.