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

luttje / filament-user-attributes / 6907253484

17 Nov 2023 05:27PM UTC coverage: 51.704% (+0.3%) from 51.437%
6907253484

push

github

luttje
wizard make menus consistent

0 of 14 new or added lines in 2 files covered. (0.0%)

43 existing lines in 2 files now uncovered.

698 of 1350 relevant lines covered (51.7%)

12.84 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\CodeTraverser;
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

20

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

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

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

NEW
38
    public function finalizeModelSetup()
×
39
    {
40
        $models = self::scanForModels();
×
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
    {
NEW
56
        $choices = array_values($models);
×
57

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

NEW
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 scanForModels(): array
×
83
    {
84
        $models = [];
×
85
        $modelFiles = glob(app_path('Models/*.php'));
×
86

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

92
        return $models;
×
93
    }
94

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

102
        $code = file_get_contents($filePath);
×
103
        return CodeTraverser::usesTrait($code, HasUserAttributes::class) &&
×
104
               CodeTraverser::implementsInterface($code, HasUserAttributesContract::class);
×
105
    }
106

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

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

116
    protected function setupModel(string $model): void
×
117
    {
118
        $file = self::getModelFilePath($model);
×
119
        $contents = file_get_contents($file);
×
120

121
        $contents = CodeTraverser::addTrait($contents, HasUserAttributes::class);
×
122
        $contents = CodeTraverser::addInterface($contents, HasUserAttributesContract::class);
×
123

124
        file_put_contents($file, $contents);
×
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