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;
4
 
5
use Illuminate\Console\Command;
6
use Illuminate\Console\View\Components\TwoColumnDetail;
7
use Illuminate\Contracts\Container\Container;
8
use Illuminate\Database\Console\Seeds\WithoutModelEvents;
9
use Illuminate\Support\Arr;
10
use InvalidArgumentException;
11
 
12
abstract class Seeder
13
{
14
    /**
15
     * The container instance.
16
     *
17
     * @var \Illuminate\Contracts\Container\Container
18
     */
19
    protected $container;
20
 
21
    /**
22
     * The console command instance.
23
     *
24
     * @var \Illuminate\Console\Command
25
     */
26
    protected $command;
27
 
28
    /**
29
     * Seeders that have been called at least one time.
30
     *
31
     * @var array
32
     */
33
    protected static $called = [];
34
 
35
    /**
36
     * Run the given seeder class.
37
     *
38
     * @param  array|string  $class
39
     * @param  bool  $silent
40
     * @param  array  $parameters
41
     * @return $this
42
     */
43
    public function call($class, $silent = false, array $parameters = [])
44
    {
45
        $classes = Arr::wrap($class);
46
 
47
        foreach ($classes as $class) {
48
            $seeder = $this->resolve($class);
49
 
50
            $name = get_class($seeder);
51
 
52
            if ($silent === false && isset($this->command)) {
53
                with(new TwoColumnDetail($this->command->getOutput()))->render(
54
                    $name,
55
                    '<fg=yellow;options=bold>RUNNING</>'
56
                );
57
            }
58
 
59
            $startTime = microtime(true);
60
 
61
            $seeder->__invoke($parameters);
62
 
63
            if ($silent === false && isset($this->command)) {
64
                $runTime = number_format((microtime(true) - $startTime) * 1000, 2);
65
 
66
                with(new TwoColumnDetail($this->command->getOutput()))->render(
67
                    $name,
68
                    "<fg=gray>$runTime ms</> <fg=green;options=bold>DONE</>"
69
                );
70
 
71
                $this->command->getOutput()->writeln('');
72
            }
73
 
74
            static::$called[] = $class;
75
        }
76
 
77
        return $this;
78
    }
79
 
80
    /**
81
     * Run the given seeder class.
82
     *
83
     * @param  array|string  $class
84
     * @param  array  $parameters
85
     * @return void
86
     */
87
    public function callWith($class, array $parameters = [])
88
    {
89
        $this->call($class, false, $parameters);
90
    }
91
 
92
    /**
93
     * Silently run the given seeder class.
94
     *
95
     * @param  array|string  $class
96
     * @param  array  $parameters
97
     * @return void
98
     */
99
    public function callSilent($class, array $parameters = [])
100
    {
101
        $this->call($class, true, $parameters);
102
    }
103
 
104
    /**
105
     * Run the given seeder class once.
106
     *
107
     * @param  array|string  $class
108
     * @param  bool  $silent
109
     * @return void
110
     */
111
    public function callOnce($class, $silent = false, array $parameters = [])
112
    {
113
        if (in_array($class, static::$called)) {
114
            return;
115
        }
116
 
117
        $this->call($class, $silent, $parameters);
118
    }
119
 
120
    /**
121
     * Resolve an instance of the given seeder class.
122
     *
123
     * @param  string  $class
124
     * @return \Illuminate\Database\Seeder
125
     */
126
    protected function resolve($class)
127
    {
128
        if (isset($this->container)) {
129
            $instance = $this->container->make($class);
130
 
131
            $instance->setContainer($this->container);
132
        } else {
133
            $instance = new $class;
134
        }
135
 
136
        if (isset($this->command)) {
137
            $instance->setCommand($this->command);
138
        }
139
 
140
        return $instance;
141
    }
142
 
143
    /**
144
     * Set the IoC container instance.
145
     *
146
     * @param  \Illuminate\Contracts\Container\Container  $container
147
     * @return $this
148
     */
149
    public function setContainer(Container $container)
150
    {
151
        $this->container = $container;
152
 
153
        return $this;
154
    }
155
 
156
    /**
157
     * Set the console command instance.
158
     *
159
     * @param  \Illuminate\Console\Command  $command
160
     * @return $this
161
     */
162
    public function setCommand(Command $command)
163
    {
164
        $this->command = $command;
165
 
166
        return $this;
167
    }
168
 
169
    /**
170
     * Run the database seeds.
171
     *
172
     * @param  array  $parameters
173
     * @return mixed
174
     *
175
     * @throws \InvalidArgumentException
176
     */
177
    public function __invoke(array $parameters = [])
178
    {
179
        if (! method_exists($this, 'run')) {
180
            throw new InvalidArgumentException('Method [run] missing from '.get_class($this));
181
        }
182
 
183
        $callback = fn () => isset($this->container)
184
            ? $this->container->call([$this, 'run'], $parameters)
185
            : $this->run(...$parameters);
186
 
187
        $uses = array_flip(class_uses_recursive(static::class));
188
 
189
        if (isset($uses[WithoutModelEvents::class])) {
190
            $callback = $this->withoutModelEvents($callback);
191
        }
192
 
193
        return $callback();
194
    }
195
}