forked from illuminate/database
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSeeder.php
118 lines (95 loc) · 2.62 KB
/
Seeder.php
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
<?php namespace Illuminate\Database;
use Illuminate\Events\Dispatcher;
use Illuminate\Filesystem\Filesystem;
class Seeder {
/**
* The filesystem instance.
*
* @var Illuminate\Filesystem
*/
protected $files;
/**
* The event dispatcher instance.
*
* @var Illuminate\Events\Dispatcher
*/
protected $events;
/**
* The database seed file list.
*
* @var array
*/
protected $seeds;
/**
* Create a new database seeder instance.
*
* @param Illuminate\Filesystem $files
* @param Illuminate\Events\Dispatcher $events
* @return void
*/
public function __construct(Filesystem $files, Dispatcher $events = null)
{
$this->files = $files;
$this->events = $events;
}
/**
* Seed the given connection from the given path.
*
* @param Illuminate\Database\Connection $connection
* @param string $path
* @return int
*/
public function seed(Connection $connection, $path)
{
$total = 0;
foreach ($this->getFiles($path) as $file)
{
$records = $this->files->getRequire($file);
// We'll grab the table name here, which could either come from the array or
// from the filename itself. Then, we will simply insert the records into
// the databases via a connection and fire an event noting the seeding.
$table = $this->getTable($records, $file);
$connection->table($table)->delete();
$connection->table($table)->insert($records);
$total += $count = count($records);
// Once we have seeded the table, we will fire an event to let any listeners
// know the tables have been seeded and how many records were inserted so
// information can be presented to the developer about the seeding run.
if (isset($this->events))
{
$payload = compact('table', 'count');
$this->events->fire('illuminate.seeding', $payload);
}
}
return $total;
}
/**
* Get all of the files at a given path.
*
* @param string $path
* @return array
*/
protected function getFiles($path)
{
if (isset($this->seeds)) return $this->seeds;
// If the seeds haven't been read before, we will glob the directory and sort
// them alphabetically just in case the developer is using numbers to make
// the seed run in a certain order based on their database design needs.
$files = $this->files->glob($path.'/*.php');
sort($files);
return $this->seeds = $files;
}
/**
* Get the table from the given records and file.
*
* @param array $records
* @param string $file
* @return string
*/
protected function getTable( & $records, $file)
{
$table = array_get($records, 'table', basename($file, '.php'));
unset($records['table']);
return $table;
}
}