Subversion-Projekte lars-tiefland.laravel_shop

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
148 lars 1
<?php
2
 
3
namespace Illuminate\Database\Console\Factories;
4
 
5
use Illuminate\Console\GeneratorCommand;
6
use Illuminate\Support\Str;
7
use Symfony\Component\Console\Attribute\AsCommand;
8
use Symfony\Component\Console\Input\InputOption;
9
 
10
#[AsCommand(name: 'make:factory')]
11
class FactoryMakeCommand extends GeneratorCommand
12
{
13
    /**
14
     * The console command name.
15
     *
16
     * @var string
17
     */
18
    protected $name = 'make:factory';
19
 
20
    /**
21
     * The name of the console command.
22
     *
23
     * This name is used to identify the command during lazy loading.
24
     *
25
     * @var string|null
26
     *
27
     * @deprecated
28
     */
29
    protected static $defaultName = 'make:factory';
30
 
31
    /**
32
     * The console command description.
33
     *
34
     * @var string
35
     */
36
    protected $description = 'Create a new model factory';
37
 
38
    /**
39
     * The type of class being generated.
40
     *
41
     * @var string
42
     */
43
    protected $type = 'Factory';
44
 
45
    /**
46
     * Get the stub file for the generator.
47
     *
48
     * @return string
49
     */
50
    protected function getStub()
51
    {
52
        return $this->resolveStubPath('/stubs/factory.stub');
53
    }
54
 
55
    /**
56
     * Resolve the fully-qualified path to the stub.
57
     *
58
     * @param  string  $stub
59
     * @return string
60
     */
61
    protected function resolveStubPath($stub)
62
    {
63
        return file_exists($customPath = $this->laravel->basePath(trim($stub, '/')))
64
            ? $customPath
65
            : __DIR__.$stub;
66
    }
67
 
68
    /**
69
     * Build the class with the given name.
70
     *
71
     * @param  string  $name
72
     * @return string
73
     */
74
    protected function buildClass($name)
75
    {
76
        $factory = class_basename(Str::ucfirst(str_replace('Factory', '', $name)));
77
 
78
        $namespaceModel = $this->option('model')
79
                        ? $this->qualifyModel($this->option('model'))
80
                        : $this->qualifyModel($this->guessModelName($name));
81
 
82
        $model = class_basename($namespaceModel);
83
 
84
        $namespace = $this->getNamespace(
85
            Str::replaceFirst($this->rootNamespace(), 'Database\\Factories\\', $this->qualifyClass($this->getNameInput()))
86
        );
87
 
88
        $replace = [
89
            '{{ factoryNamespace }}' => $namespace,
90
            'NamespacedDummyModel' => $namespaceModel,
91
            '{{ namespacedModel }}' => $namespaceModel,
92
            '{{namespacedModel}}' => $namespaceModel,
93
            'DummyModel' => $model,
94
            '{{ model }}' => $model,
95
            '{{model}}' => $model,
96
            '{{ factory }}' => $factory,
97
            '{{factory}}' => $factory,
98
        ];
99
 
100
        return str_replace(
101
            array_keys($replace), array_values($replace), parent::buildClass($name)
102
        );
103
    }
104
 
105
    /**
106
     * Get the destination class path.
107
     *
108
     * @param  string  $name
109
     * @return string
110
     */
111
    protected function getPath($name)
112
    {
113
        $name = (string) Str::of($name)->replaceFirst($this->rootNamespace(), '')->finish('Factory');
114
 
115
        return $this->laravel->databasePath().'/factories/'.str_replace('\\', '/', $name).'.php';
116
    }
117
 
118
    /**
119
     * Guess the model name from the Factory name or return a default model name.
120
     *
121
     * @param  string  $name
122
     * @return string
123
     */
124
    protected function guessModelName($name)
125
    {
126
        if (str_ends_with($name, 'Factory')) {
127
            $name = substr($name, 0, -7);
128
        }
129
 
130
        $modelName = $this->qualifyModel(Str::after($name, $this->rootNamespace()));
131
 
132
        if (class_exists($modelName)) {
133
            return $modelName;
134
        }
135
 
136
        if (is_dir(app_path('Models/'))) {
137
            return $this->rootNamespace().'Models\Model';
138
        }
139
 
140
        return $this->rootNamespace().'Model';
141
    }
142
 
143
    /**
144
     * Get the console command options.
145
     *
146
     * @return array
147
     */
148
    protected function getOptions()
149
    {
150
        return [
151
            ['model', 'm', InputOption::VALUE_OPTIONAL, 'The name of the model'],
152
        ];
153
    }
154
}