-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSeeder.php
More file actions
155 lines (127 loc) · 4.05 KB
/
Copy pathSeeder.php
File metadata and controls
155 lines (127 loc) · 4.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
<?php
/**
* Seeder class file.
*
* @package Mantle
*/
namespace Mantle\Database;
use InvalidArgumentException;
use Mantle\Console\Command;
use Mantle\Contracts\Container;
use Mantle\Database\Factory\Factory_Container;
use Mantle\Support\Arr;
/**
* Base Database Seeder
*/
abstract class Seeder {
/**
* The container instance.
*/
protected ?Container $container = null;
/**
* The console command instance.
*/
protected ?Command $command = null;
/**
* Factory Instance.
*/
protected static ?Factory_Container $factory = null;
/**
* Seed the given connection from the given path.
*
* @param array<class-string>|class-string $class Seed to run.
* @param bool $silent Flag if the seed should be silent.
* @param array<mixed> $parameters Parameters to pass to the seeder.
*/
public function call( array|string $class, bool $silent = false, array $parameters = [] ): static {
$classes = Arr::wrap( $class );
foreach ( $classes as $class ) {
$seeder = $this->resolve( $class );
$name = $seeder::class;
if ( ! $silent && $this->command instanceof \Mantle\Console\Command ) {
$this->command->line( "Seeding: {$name}" );
}
$start_time = microtime( true );
$seeder( $parameters );
$elapsed = microtime( true ) - $start_time;
$run_time = $elapsed < 1 ? number_format( $elapsed * 1000, 2 ) . ' ms' : number_format( $elapsed, 2 ) . ' seconds';
if ( ! $silent && $this->command instanceof \Mantle\Console\Command ) {
$this->command->line( "Seeded: {$name} ({$run_time})" );
}
}
return $this;
}
/**
* Run the given seeder class with the given arguments.
*
* @param array<class-string>|class-string $class Seed to run.
* @param array<mixed> $parameters Parameters to pass to the seeder.
*/
public function call_with( array|string $class, array $parameters = [] ): static {
return $this->call( $class, false, $parameters );
}
/**
* Resolve an instance of the given seeder class.
*
* @throws InvalidArgumentException If the class is not an instance of Seeder.
*
* @param class-string $class Seeder class to resolve.
*/
protected function resolve( string $class ): Seeder {
$instance = $this->container instanceof \Mantle\Contracts\Container ? $this->container->make( $class ) : new $class();
if ( ! $instance instanceof Seeder ) {
throw new InvalidArgumentException( "Class [{$class}] must be an instance of " . self::class );
}
if ( $this->container instanceof \Mantle\Contracts\Container ) {
$instance->set_container( $this->container );
}
if ( $this->command instanceof \Mantle\Console\Command ) {
$instance->set_command( $this->command );
}
return $instance;
}
/**
* Set the IoC container instance.
*
* @param Container $container IoC container.
*/
public function set_container( Container $container ): static {
$this->container = $container;
return $this;
}
/**
* Set the command instance instance.
*
* @param Command $command
*/
public function set_command( Command $command ): static {
$this->command = $command;
return $this;
}
/**
* Run the database seeds.
*
* @param array<mixed> $parameters Parameters to pass to the seeder.
*
* @throws InvalidArgumentException Thrown on bad seeder class.
*/
public function __invoke( array $parameters = [] ): mixed {
if ( ! method_exists( $this, 'run' ) ) {
throw new InvalidArgumentException( 'Method [run] missing from ' . static::class );
}
return $this->container instanceof \Mantle\Contracts\Container
? $this->container->call( [ $this, 'run' ], $parameters )
: $this->run( $parameters );
}
/**
* Fetches the factory object for generating WordPress fixtures.
*
* @throws InvalidArgumentException If the container is not set.
*/
protected function factory(): Factory_Container {
if ( ! $this->container instanceof \Mantle\Contracts\Container ) {
throw new InvalidArgumentException( 'Container not set to instantiate factory.' );
}
return $this->container->make( Factory_Container::class );
}
}