-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathmockdata
executable file
·156 lines (123 loc) · 4.45 KB
/
mockdata
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
#!/usr/bin/php
<?php
function parse_parameters($noopt = array()) {
$result = array();
$params = $GLOBALS['argv'];
// could use getopt() here (since PHP 5.3.0), but it doesn't work relyingly
reset($params);
while (list($tmp, $p) = each($params)) {
if ($p{0} == '-') {
$pname = substr($p, 1);
$value = true;
if ($pname{0} == '-') {
// long-opt (--<param>)
$pname = substr($pname, 1);
if (strpos($p, '=') !== false) {
// value specified inline (--<param>=<value>)
list($pname, $value) = explode('=', substr($p, 2), 2);
}
}
// check if next parameter is a descriptor or a value
$nextparm = current($params);
if (!in_array($pname, $noopt) && $value === true && $nextparm !== false && $nextparm{0} != '-') list($tmp, $value) = each($params);
$result[$pname] = $value;
} else {
// param doesn't belong to any option
$result[] = $p;
}
}
return $result;
}
function switch_to_project_root() {
while (dirname(getcwd()) != "/") {
foreach (scandir(".") as $file) {
if ($file == ".." || $file == ".")
continue;
if (is_dir($file)) {
if (file_exists($file . "/cli-script.php")) {
return $file;
}
}
}
chdir(dirname(getcwd()));
}
return false;
}
function run_process($cmd)
{
$descriptorspec = array(
0 => array("pipe", "r"), // stdin is a pipe that the child will read from
1 => array("pipe", "w"), // stdout is a pipe that the child will write to
2 => array("pipe", "w") // stderr is a file to write to
);
$pipes= array();
$process = proc_open($cmd, $descriptorspec, $pipes);
$output= "";
if (!is_resource($process)) return false;
#close child's input immediately
fclose($pipes[0]);
stream_set_blocking($pipes[1],false);
stream_set_blocking($pipes[2],false);
$todo= array($pipes[1],$pipes[2]);
while( true ) {
$read= array();
if( !feof($pipes[1]) ) $read[]= $pipes[1];
if( !feof($pipes[2]) ) $read[]= $pipes[2];
if (!$read) break;
$ready= stream_select($read, $write=NULL, $ex= NULL, 2);
if ($ready === false) {
break; #should never happen - something died
}
foreach ($read as $r) {
$s= fread($r,1024);
echo $s;
$output.= $s;
}
}
fclose($pipes[1]);
fclose($pipes[2]);
return $output;
}
$help = "
Usage: mockdata <generate|populate|cleanup>
Options:
\t-count\t\t\t\tThe number of records to create
\t-relation-limit\t\t\tThe number of related records to create when relations are enabled
\t-parent\t\t\t\tAn identifying ID, URLSegment of the parent object
\t-parent-field\t\t\tThe parent field for the object being created. Defaults to ParentID. Only used when -parent is specified.
\t
\t--no-relations\t\t\tSuppress creation of related has_many/many_many records
\t--no-downloads\t\t\tSuppress downloading of images. Use local stock images instead.
\t--overwrite\t\t\tOverwrite fields that already have value.
";
if(!$framework_dir = switch_to_project_root()) {
echo "Could not find the root of a SilverStripe project in this directory!";
die();
}
$PARAMS = parse_parameters();
if(!isset($PARAMS[1]) || !in_array($PARAMS[1], array('generate','populate','cleanup','help'))) {
die($help);
}
if(!isset($PARAMS[2])) {
if($PARAMS[1] == "cleanup") {
$PARAMS[2] = "__all__";
}
else {
die($help);
}
}
$operation = $PARAMS[1];
$className = $PARAMS[2];
if($operation == "help") die($help);
$args = array();
$args[] = isset($PARAMS['parent']) ? "--parent=".$PARAMS['parent'] : "";
$args[] = isset($PARAMS['parent-field']) ? "--parentField=".$PARAMS['parent-field'] : "";
$args[] = isset($PARAMS['count']) ? "--count=" . $PARAMS['count'] : "";
$args[] = isset($PARAMS['no-releations']) ? "--includeRelations=false" : "";
$args[] = isset($PARAMS['no-downloads']) ? "--downloadImages=false" : "";
$args[] = isset($PARAMS['relation-limit']) ? "--relationCreateLimit=".$PARAMS['relation-limit'] : "";
$args[] = isset($PARAMS['overwrite']) ? "--onlyEmpty=false" : "";
$arguments = implode(" ", $args);
$cmd = $framework_dir . "/sake dev/tasks/MockDataTask $operation $className $arguments --flush=all";
run_process($cmd);
echo "\n\n";