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

luttje / filament-user-attributes / 6917010354

18 Nov 2023 11:58PM UTC coverage: 76.475% (+13.3%) from 63.165%
6917010354

push

github

luttje
Merge branch 'main' of https://github.com/luttje/filament-user-attributes

1141 of 1492 relevant lines covered (76.47%)

28.91 hits per line

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

31.58
/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\Facades\FilamentUserAttributes;
9
use Luttje\FilamentUserAttributes\Traits\HasUserAttributes;
10

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

18
        parent::__construct();
4✔
19
    }
20

21
    public function handle()
4✔
22
    {
23
        if (!$this->promptForModelSetup()) {
4✔
24
            return;
4✔
25
        }
26

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

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

38
    public function finalizeModelSetup()
×
39
    {
40
        $models = FilamentUserAttributes::getConfigurableModels(configuredOnly: false);
×
41
        $chosenModels = $this->getChosenModels($models);
×
42

43
        $this->displaySelectedModels($chosenModels);
×
44

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

49
        $this->setupModels($chosenModels);
×
50

51
        return;
×
52
    }
53

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

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

66
        return $chosenModels;
×
67
    }
68

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

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

82
    public static function isModelSetup(string $model): bool
4✔
83
    {
84
        $file = FilamentUserAttributes::findModelFilePath($model);
4✔
85
        if (!file_exists($file)) {
4✔
86
            return false;
×
87
        }
88

89
        $code = file_get_contents($file);
4✔
90
        return CodeEditor::usesTrait($code, HasUserAttributes::class) &&
4✔
91
               CodeEditor::implementsInterface($code, HasUserAttributesContract::class);
4✔
92
    }
93

94
    protected function setupModels(array $models): void
×
95
    {
96
        $this->warn("\nWe will now implement the HasUserAttributesContract interface with the HasUserAttributes trait for you...");
×
97

98
        foreach ($models as $model) {
×
99
            $this->setupModel($model);
×
100
        }
101
    }
102

103
    protected function setupModel(string $model): void
×
104
    {
105
        $file = FilamentUserAttributes::findModelFilePath($model);
×
106

107
        $editor = CodeEditor::make();
×
108
        $editor->editFileWithBackup($file, function ($code) use ($editor) {
×
109
            $code = $editor->addTrait($code, HasUserAttributes::class);
×
110
            $code = $editor->addInterface($code, HasUserAttributesContract::class);
×
111
            return $code;
×
112
        });
×
113
    }
114
}
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