1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Part of Cli for CodeIgniter |
4
|
|
|
* |
5
|
|
|
* @author Kenji Suzuki <https://github.com/kenjis> |
6
|
|
|
* @license MIT License |
7
|
|
|
* @copyright 2015 Kenji Suzuki |
8
|
|
|
* @link https://github.com/kenjis/codeigniter-cli |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace Kenjis\CodeIgniter_Cli\Command; |
12
|
|
|
|
13
|
|
|
use Aura\Cli\Stdio; |
14
|
|
|
use Aura\Cli\Context; |
15
|
|
|
use Aura\Cli\Status; |
16
|
|
|
use CI_Controller; |
17
|
|
|
|
18
|
|
|
class Seed extends Command |
19
|
|
|
{ |
20
|
|
|
private $seeder_path; |
21
|
|
|
|
22
|
|
|
public function __construct(Context $context, Stdio $stdio, CI_Controller $ci) |
23
|
|
|
{ |
24
|
|
|
parent::__construct($context, $stdio, $ci); |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @param string $seeder_path directory of seeder files |
29
|
|
|
*/ |
30
|
|
|
public function setSeederPath($seeder_path) |
31
|
|
|
{ |
32
|
|
|
$this->seeder_path = $seeder_path; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @param string $class class name |
37
|
|
|
*/ |
38
|
|
|
public function __invoke($class = null) |
39
|
|
|
{ |
40
|
|
|
$options =[ |
41
|
|
|
'l', // short flag -l, parameter is not allowed |
42
|
|
|
'list', // long option --list, parameter is not allowed |
43
|
|
|
]; |
44
|
|
|
$getopt = $this->context->getopt($options); |
45
|
|
|
$list = $getopt->get('-l', false) || $getopt->get('--list', false); |
|
|
|
|
46
|
|
|
|
47
|
|
|
if ($list) { |
48
|
|
|
$this->listSeederFiles(); |
49
|
|
|
return; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
if ($class === null) { |
53
|
|
|
$seeder_list = $this->findSeeder(); |
54
|
|
|
} else { |
55
|
|
|
$seeder_list = [$this->seeder_path . $class . '.php']; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
$this->runSeederList($seeder_list); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* run another seeder |
63
|
|
|
* |
64
|
|
|
* @param string $class class name |
65
|
|
|
*/ |
66
|
|
|
public function call($class) |
67
|
|
|
{ |
68
|
|
|
$seeder_list = [$this->seeder_path . $class . '.php']; |
69
|
|
|
$this->runSeederList($seeder_list); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
private function runSeederList($seeder_list) |
73
|
|
|
{ |
74
|
|
|
foreach ($seeder_list as $file) { |
75
|
|
|
if (! is_readable($file)) { |
76
|
|
|
$this->stdio->errln('<<red>>Can\'t read: ' . $file . '<<reset>>'); |
77
|
|
|
break; |
78
|
|
|
} |
79
|
|
|
require_once $file; |
80
|
|
|
$classname = basename($file, '.php'); |
81
|
|
|
if (! class_exists($classname)) { |
82
|
|
|
$this->stdio->errln( |
83
|
|
|
'<<red>>No such class: ' . $classname . ' in ' . $file . '<<reset>>' |
84
|
|
|
. ' [' . __METHOD__ . ': line ' . __LINE__ . ']' |
85
|
|
|
); |
86
|
|
|
break; |
87
|
|
|
} |
88
|
|
|
$seeder = new $classname($this->context, $this->stdio, $this->ci); |
89
|
|
|
$seeder->setSeederPath($this->seeder_path); |
90
|
|
|
$this->runSeed($seeder); |
91
|
|
|
$this->stdio->outln('<<green>>Seeded: ' . $classname . '<<reset>>'); |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
private function listSeederFiles() |
96
|
|
|
{ |
97
|
|
|
$seeder_list = $this->findSeeder(); |
98
|
|
|
foreach ($seeder_list as $file) { |
99
|
|
|
if (is_readable($file)) { |
100
|
|
|
$this->stdio->outln(' <<green>>' . $file . '<<reset>>'); |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
private function runSeed($seeder) |
106
|
|
|
{ |
107
|
|
|
$seeder->run(); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
private function findSeeder() |
111
|
|
|
{ |
112
|
|
|
$seeders = []; |
113
|
|
|
foreach (glob($this->seeder_path . '*.php') as $file) { |
114
|
|
|
$seeders[] = $file; |
115
|
|
|
} |
116
|
|
|
return $seeders; |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: