Completed
Push — master ( 587a04...d3f1b1 )
by Kenji
05:12
created

Seeder::callDependency()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 8
nc 3
nop 1
dl 0
loc 14
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
	 * @param bool $callDependencies
33
	 */
34
	public function call($seeder, $callDependencies = true)
35
	{
36
		if ($this->seedPath === null)
37
		{
38
			$this->seedPath = APPPATH . 'database/seeds/';
39
		}
40
41
		$obj = $this->loadSeeder($seeder);
42
		if ($callDependencies === true && $obj instanceof Seeder) {
43
			$obj->callDependencies($this->seedPath);
44
		}
45
		$obj->run();
46
	}
47
48
	/**
49
	 * Get Seeder instance
50
	 *
51
	 * @param string $seeder
52
	 * @return Seeder
53
	 */
54
	protected function loadSeeder($seeder)
55
	{
56
		$file = $this->seedPath . $seeder . '.php';
57
		require_once $file;
58
59
		return new $seeder;
60
	}
61
62
	/**
63
	 * Call dependency seeders
64
	 *
65
	 * @param string $seedPath
66
	 */
67
	public function callDependencies($seedPath)
68
	{
69
		foreach ($this->depends as $path => $seeders) {
70
			$this->seedPath = $seedPath;
71
			if (is_string($path)) {
72
				$this->setPath($path);
73
			}
74
75
			$this->callDependency($seeders);
76
		}
77
		$this->setPath($seedPath);
78
	}
79
80
	/**
81
	 * Call dependency seeder
82
	 *
83
	 * @param string|array $seederName
84
	 */
85
	protected function callDependency($seederName)
86
	{
87
		if (is_array($seederName)) {
88
			array_map([$this, 'callDependency'], $seederName);
89
			return;
90
		}
91
92
		$seeder = $this->loadSeeder($seederName);
93
		if (is_string($this->seedPath)) {
94
			$seeder->setPath($this->seedPath);
95
		}
96
97
		$seeder->call($seederName, true);
98
	}
99
100
	/**
101
	 * Set path for seeder files
102
	 *
103
	 * @param string $path
104
	 */
105
	public function setPath($path)
106
	{
107
		$this->seedPath = rtrim($path, '/').'/';
108
	}
109
110
	public function __get($property)
111
	{
112
		return $this->CI->$property;
113
	}
114
}
115