|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Part of ci-phpunit-test |
|
4
|
|
|
* |
|
5
|
|
|
* @author Kenji Suzuki <https://github.com/kenjis> |
|
6
|
|
|
* @license MIT License |
|
7
|
|
|
* @copyright 2015 Kenji Suzuki |
|
8
|
|
|
* @link https://github.com/kenjis/ci-phpunit-test |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
class Seeder |
|
|
|
|
|
|
12
|
|
|
{ |
|
13
|
|
|
private $CI; |
|
14
|
|
|
protected $db; |
|
15
|
|
|
protected $dbforge; |
|
16
|
|
|
protected $seedPath; |
|
17
|
|
|
protected $depends = []; |
|
18
|
|
|
|
|
19
|
|
|
public function __construct() |
|
20
|
|
|
{ |
|
21
|
|
|
$this->CI =& get_instance(); |
|
22
|
|
|
$this->CI->load->database(); |
|
23
|
|
|
$this->CI->load->dbforge(); |
|
24
|
|
|
$this->db = $this->CI->db; |
|
25
|
|
|
$this->dbforge = $this->CI->dbforge; |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* Run another seeder |
|
30
|
|
|
* |
|
31
|
|
|
* @param string $seeder Seeder classname |
|
32
|
|
|
*/ |
|
33
|
|
|
public function call($seeder, $call_depends = true) |
|
34
|
|
|
{ |
|
35
|
|
|
if ($this->seedPath === null) |
|
36
|
|
|
{ |
|
37
|
|
|
$this->seedPath = APPPATH . 'database/seeds/'; |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
$obj = $this->loadSeeder($seeder); |
|
41
|
|
|
if ($call_depends === true && $obj instanceof Seeder) { |
|
42
|
|
|
$obj->callDepends($this->seedPath); |
|
43
|
|
|
} |
|
44
|
|
|
$obj->run(); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* Get Seeder instance |
|
49
|
|
|
* |
|
50
|
|
|
* @param string $seeder |
|
51
|
|
|
* @return Seeder |
|
52
|
|
|
*/ |
|
53
|
|
|
protected function loadSeeder($seeder) |
|
54
|
|
|
{ |
|
55
|
|
|
$file = $this->seedPath . $seeder . '.php'; |
|
56
|
|
|
require_once $file; |
|
57
|
|
|
|
|
58
|
|
|
return new $seeder; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* Call depend seeder list |
|
63
|
|
|
* |
|
64
|
|
|
* @param string $seedPath |
|
65
|
|
|
*/ |
|
66
|
|
|
public function callDepends($seedPath) |
|
67
|
|
|
{ |
|
68
|
|
|
foreach ($this->depends as $path => $seeders) { |
|
69
|
|
|
$this->seedPath = $seedPath; |
|
70
|
|
|
if (is_string($path)) { |
|
71
|
|
|
$this->setPath($path); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
$this->callDepend($seeders); |
|
75
|
|
|
} |
|
76
|
|
|
$this->setPath($seedPath); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* Call depend seeder |
|
81
|
|
|
* |
|
82
|
|
|
* @param string|array $seederName |
|
83
|
|
|
*/ |
|
84
|
|
|
protected function callDepend($seederName) |
|
85
|
|
|
{ |
|
86
|
|
|
if (is_array($seederName)) { |
|
87
|
|
|
array_map([$this, 'callDepend'], $seederName); |
|
88
|
|
|
return; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
$seeder = $this->loadSeeder($seederName); |
|
92
|
|
|
if (is_string($this->seedPath)) { |
|
93
|
|
|
$seeder->setPath($this->seedPath); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
$seeder->call($seederName, true); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* Set path for seeder files |
|
101
|
|
|
* |
|
102
|
|
|
* @param string $path |
|
103
|
|
|
*/ |
|
104
|
|
|
public function setPath($path) |
|
105
|
|
|
{ |
|
106
|
|
|
$this->seedPath = rtrim($path, '/').'/'; |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
public function __get($property) |
|
110
|
|
|
{ |
|
111
|
|
|
return $this->CI->$property; |
|
112
|
|
|
} |
|
113
|
|
|
} |
|
114
|
|
|
|
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.