• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

luttje / filament-user-attributes / 6909724102

17 Nov 2023 10:28PM UTC coverage: 59.278% (+7.6%) from 51.704%
6909724102

push

github

luttje
fix basename not working for linux

1 of 2 new or added lines in 2 files covered. (50.0%)

103 existing lines in 8 files now uncovered.

837 of 1412 relevant lines covered (59.28%)

16.41 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/src/Commands/WizardStepModels.php
1
<?php
2

3
namespace Luttje\FilamentUserAttributes\Commands;
4

5
use Illuminate\Console\Command;
6
use Luttje\FilamentUserAttributes\CodeGeneration\CodeEditor;
7
use Luttje\FilamentUserAttributes\Contracts\HasUserAttributesContract;
8
use Luttje\FilamentUserAttributes\Traits\HasUserAttributes;
9

10
class WizardStepModels extends Command
11
{
12
    public function __construct()
×
13
    {
14
        $this->signature = 'filament-user-attributes:wizard-models';
×
15
        $this->description = 'Wizard to help setup your models with Filament User Attributes';
×
16

17
        parent::__construct();
×
18
    }
19

UNCOV
20
    public function handle()
×
21
    {
UNCOV
22
        if (!$this->promptForModelSetup()) {
×
23
            return;
×
24
        }
25

UNCOV
26
        $this->finalizeModelSetup();
×
27
    }
28

UNCOV
29
    protected function promptForModelSetup(): bool
×
30
    {
UNCOV
31
        return $this->confirm(
×
32
            "Do add support for user attributes to your models?",
×
33
            true
×
34
        );
×
35
    }
36

UNCOV
37
    public function finalizeModelSetup()
×
38
    {
UNCOV
39
        $models = self::scanForModels();
×
40
        $chosenModels = $this->getChosenModels($models);
×
41

UNCOV
42
        $this->displaySelectedModels($chosenModels);
×
43

UNCOV
44
        if (empty($chosenModels)) {
×
45
            return;
×
46
        }
47

UNCOV
48
        $this->setupModels($chosenModels);
×
49

UNCOV
50
        return;
×
51
    }
52

UNCOV
53
    protected function getChosenModels(array $models): array
×
54
    {
UNCOV
55
        $choices = array_values($models);
×
56

UNCOV
57
        $chosenModels = $this->choice(
×
58
            "Which of your models should be able to have user attributes? (comma separated)",
×
59
            $choices,
×
60
            null,
×
61
            null,
×
62
            true
×
63
        );
×
64

UNCOV
65
        return $chosenModels;
×
66
    }
67

UNCOV
68
    protected function displaySelectedModels(array $chosenModels): void
×
69
    {
UNCOV
70
        if (empty($chosenModels)) {
×
71
            return;
×
72
        }
73

UNCOV
74
        $this->info('The following models will be setup to support user attributes:');
×
75
        foreach ($chosenModels as $model) {
×
76
            $setupStatus = self::isModelSetup($model) ? "<fg=green>(already setup)</>" : "";
×
77
            $this->line("- $model $setupStatus");
×
78
        }
79
    }
80

UNCOV
81
    public static function scanForModels(): array
×
82
    {
UNCOV
83
        $models = [];
×
84
        $modelFiles = glob(app_path('Models/*.php'));
×
85

UNCOV
86
        foreach ($modelFiles as $file) {
×
87
            $modelName = basename($file, '.php');
×
88
            $models[] = app()->getNamespace() . 'Models\\' . $modelName;
×
89
        }
90

UNCOV
91
        return $models;
×
92
    }
93

UNCOV
94
    public static function isModelSetup(string $model): bool
×
95
    {
UNCOV
96
        $file = self::getModelFilePath($model);
×
97
        if (!file_exists($file)) {
×
98
            return false;
×
99
        }
100

UNCOV
101
        $code = file_get_contents($file);
×
102
        return CodeEditor::usesTrait($code, HasUserAttributes::class) &&
×
103
               CodeEditor::implementsInterface($code, HasUserAttributesContract::class);
×
104
    }
105

UNCOV
106
    protected function setupModels(array $models): void
×
107
    {
UNCOV
108
        $this->warn("\nWe will now implement the HasUserAttributesContract interface with the HasUserAttributes trait for you...");
×
109

UNCOV
110
        foreach ($models as $model) {
×
111
            $this->setupModel($model);
×
112
        }
113
    }
114

UNCOV
115
    protected function setupModel(string $model): void
×
116
    {
UNCOV
117
        $file = self::getModelFilePath($model);
×
118

119
        $editor = CodeEditor::make();
×
UNCOV
120
        $editor->editFileWithBackup($file, function ($code) use ($editor) {
×
121
            $code = $editor->addTrait($code, HasUserAttributes::class);
×
122
            $code = $editor->addInterface($code, HasUserAttributesContract::class);
×
UNCOV
123
            return $code;
×
124
        });
×
125
    }
126

127
    private static function getModelFilePath(string $model): string
×
128
    {
129
        return app_path(str_replace('\\', '/', substr($model, strlen(app()->getNamespace()))) . '.php');
×
130
    }
131
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc