Completed
Pull Request — master (#236)
by
unknown
02:57
created

Seeder::loadSeeder()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 1
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
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
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
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