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\Seeds;
4
 
5
use Illuminate\Console\Command;
6
use Illuminate\Console\ConfirmableTrait;
7
use Illuminate\Database\ConnectionResolverInterface as Resolver;
8
use Illuminate\Database\Eloquent\Model;
9
use Symfony\Component\Console\Attribute\AsCommand;
10
use Symfony\Component\Console\Input\InputArgument;
11
use Symfony\Component\Console\Input\InputOption;
12
 
13
#[AsCommand(name: 'db:seed')]
14
class SeedCommand extends Command
15
{
16
    use ConfirmableTrait;
17
 
18
    /**
19
     * The console command name.
20
     *
21
     * @var string
22
     */
23
    protected $name = 'db:seed';
24
 
25
    /**
26
     * The name of the console command.
27
     *
28
     * This name is used to identify the command during lazy loading.
29
     *
30
     * @var string|null
31
     *
32
     * @deprecated
33
     */
34
    protected static $defaultName = 'db:seed';
35
 
36
    /**
37
     * The console command description.
38
     *
39
     * @var string
40
     */
41
    protected $description = 'Seed the database with records';
42
 
43
    /**
44
     * The connection resolver instance.
45
     *
46
     * @var \Illuminate\Database\ConnectionResolverInterface
47
     */
48
    protected $resolver;
49
 
50
    /**
51
     * Create a new database seed command instance.
52
     *
53
     * @param  \Illuminate\Database\ConnectionResolverInterface  $resolver
54
     * @return void
55
     */
56
    public function __construct(Resolver $resolver)
57
    {
58
        parent::__construct();
59
 
60
        $this->resolver = $resolver;
61
    }
62
 
63
    /**
64
     * Execute the console command.
65
     *
66
     * @return int
67
     */
68
    public function handle()
69
    {
70
        if (! $this->confirmToProceed()) {
71
            return 1;
72
        }
73
 
74
        $this->components->info('Seeding database.');
75
 
76
        $previousConnection = $this->resolver->getDefaultConnection();
77
 
78
        $this->resolver->setDefaultConnection($this->getDatabase());
79
 
80
        Model::unguarded(function () {
81
            $this->getSeeder()->__invoke();
82
        });
83
 
84
        if ($previousConnection) {
85
            $this->resolver->setDefaultConnection($previousConnection);
86
        }
87
 
88
        return 0;
89
    }
90
 
91
    /**
92
     * Get a seeder instance from the container.
93
     *
94
     * @return \Illuminate\Database\Seeder
95
     */
96
    protected function getSeeder()
97
    {
98
        $class = $this->input->getArgument('class') ?? $this->input->getOption('class');
99
 
100
        if (! str_contains($class, '\\')) {
101
            $class = 'Database\\Seeders\\'.$class;
102
        }
103
 
104
        if ($class === 'Database\\Seeders\\DatabaseSeeder' &&
105
            ! class_exists($class)) {
106
            $class = 'DatabaseSeeder';
107
        }
108
 
109
        return $this->laravel->make($class)
110
                        ->setContainer($this->laravel)
111
                        ->setCommand($this);
112
    }
113
 
114
    /**
115
     * Get the name of the database connection to use.
116
     *
117
     * @return string
118
     */
119
    protected function getDatabase()
120
    {
121
        $database = $this->input->getOption('database');
122
 
123
        return $database ?: $this->laravel['config']['database.default'];
124
    }
125
 
126
    /**
127
     * Get the console command arguments.
128
     *
129
     * @return array
130
     */
131
    protected function getArguments()
132
    {
133
        return [
134
            ['class', InputArgument::OPTIONAL, 'The class name of the root seeder', null],
135
        ];
136
    }
137
 
138
    /**
139
     * Get the console command options.
140
     *
141
     * @return array
142
     */
143
    protected function getOptions()
144
    {
145
        return [
146
            ['class', null, InputOption::VALUE_OPTIONAL, 'The class name of the root seeder', 'Database\\Seeders\\DatabaseSeeder'],
147
            ['database', null, InputOption::VALUE_OPTIONAL, 'The database connection to seed'],
148
            ['force', null, InputOption::VALUE_NONE, 'Force the operation to run when in production'],
149
        ];
150
    }
151
}