-
Notifications
You must be signed in to change notification settings - Fork 194
/
Copy pathSeeder.php
133 lines (116 loc) · 2.24 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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
<?php
/**
* Part of ci-phpunit-test
*
* @author Kenji Suzuki <https://github.com/kenjis>
* @license MIT License
* @copyright 2015 Kenji Suzuki
* @link https://github.com/kenjis/ci-phpunit-test
*/
class Seeder
{
/**
* @var CI_Controller
*/
private $CI;
/**
* @var CI_DB_query_builder
*/
protected $db;
/**
* @var CI_DB_forge
*/
protected $dbforge;
/**
* @var string
*/
protected $seedPath;
/**
* @var array
*/
protected $depends = [];
public function __construct()
{
$this->CI =& get_instance();
$this->CI->load->database();
$this->CI->load->dbforge();
$this->db = $this->CI->db;
$this->dbforge = $this->CI->dbforge;
}
/**
* Run another seeder
*
* @param string $seeder Seeder classname
* @param bool $callDependencies
*/
public function call($seeder, $callDependencies = true)
{
if ($this->seedPath === null)
{
$this->seedPath = APPPATH . 'database/seeds/';
}
$obj = $this->loadSeeder($seeder);
if ($callDependencies === true && $obj instanceof Seeder) {
$obj->callDependencies($this->seedPath);
}
$obj->run();
}
/**
* Get Seeder instance
*
* @param string $seeder
* @return Seeder
*/
protected function loadSeeder($seeder)
{
$file = $this->seedPath . $seeder . '.php';
require_once $file;
return new $seeder;
}
/**
* Call dependency seeders
*
* @param string $seedPath
*/
public function callDependencies($seedPath)
{
foreach ($this->depends as $path => $seeders) {
$this->seedPath = $seedPath;
if (is_string($path)) {
$this->setPath($path);
}
$this->callDependency($seeders);
}
$this->setPath($seedPath);
}
/**
* Call dependency seeder
*
* @param string|array $seederName
*/
protected function callDependency($seederName)
{
if (is_array($seederName)) {
array_map([$this, 'callDependency'], $seederName);
return;
}
$seeder = $this->loadSeeder($seederName);
if (is_string($this->seedPath)) {
$seeder->setPath($this->seedPath);
}
$seeder->call($seederName, true);
}
/**
* Set path for seeder files
*
* @param string $path
*/
public function setPath($path)
{
$this->seedPath = rtrim($path, '/').'/';
}
public function __get($property)
{
return $this->CI->$property;
}
}