-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathseeder.js
90 lines (66 loc) · 2.17 KB
/
seeder.js
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
const InvalidArgumentException = require('@ostro/support/exceptions/invalidArgumentException')
const kCommand = Symbol('command')
class Seeder {
get $command() {
return this[kCommand] = this[kCommand] || null
};
set $command(value) {
return this[kCommand] = value
};
constructor($resolver) {
this.$resolver = $resolver
}
async call($classes, $silent = false, $parameters = []) {
$classes = Array.isArray($classes) ? $classes : [$classes];
for (let $class of $classes) {
let $seeder = this.resolve($class);
let $name = get_class_name($seeder);
if ($silent === false && this.$command) {
this.$command.getOutput().writeln(`<comment>Seeding:</comment> ${$name}`);
}
let $startTime = Date.now();
await $seeder.__invoke($parameters);
let $runTime = Number((Date.now() - $startTime) * 1000, 2);
if ($silent === false && this.$command) {
this.$command.getOutput().writeln(`<info>Seeded:</info> ${$name} (${$runTime}ms)`);
}
}
return this;
}
callWith($class, $parameters = []) {
return this.call($class, false, $parameters);
}
callSilent($class, $parameters = []) {
return this.call($class, true, $parameters);
}
resolve($class) {
let $instance
if (this.$app) {
$instance = this.$app.make($class);
$instance.setContainer(this.$app);
} else {
$instance = new $class;
}
if (this.$command) {
$instance.setCommand(this.$command);
}
return $instance;
}
setContainer(app) {
this.$app = app;
return this;
}
setCommand($command) {
this.$command = $command;
return this;
}
__invoke($parameters = []) {
if (!this['run']) {
throw new InvalidArgumentException('Method [run] missing from ' + get_class_name(this));
}
return this.$app ?
this.$app.call([this, 'run'], $parameters) :
this.run(...$parameters);
}
}
module.exports = Seeder