forked from philpalmieri/ci-startup
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerate
145 lines (119 loc) · 3.73 KB
/
generate
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
#!/usr/bin/env php
<?php
/*
* fooStack, CIUnit for CodeIgniter
* Copyright (c) 2008-2009 Clemens Gruenberger
* Released under the MIT license, see:
* http://www.opensource.org/licenses/mit-license.php
*/
/**
* generate fixtures from test database
* fixtures are named according to convention and are only
* created if they didin't exist before
*/
require_once (dirname(__FILE__) . '/../application/third_party/CIUnit/bootstrap_phpunit.php');
// Set the controller to the default controller. This allows us access to the
// CI framework.
set_controller();
/*
* this function for extraction of command line arguments
* might go back into some foo helper class
*/
function args_arr($arg_arr = false)
{
//extract commandline arguments
if ($arg_arr === false) {
$argv = $_SERVER['argv'];
} else {
$argv = $arg_arr;
}
array_shift($argv);
$flags = array();
$params = array();
$args = array();
foreach ($argv as $arg) {
$param_parts = preg_split('/=/', $arg);
if (count($param_parts) == 2) {
$params[$param_parts[0]] = $param_parts[1];
} else {
if (substr($arg,0,1) == "-") {
$flags[] = substr($arg, 1);
} else {
$args[] = $arg;
}
}
}
return array('params' => $params, 'flags' => $flags, 'args' => $args);
}
class Generate {
function __construct()
{
$this->CI = &get_instance();
$this->CI->spyc = new Spyc();
$this->Ci->fixture = new Fixture();
$this->args_arr = args_arr();
$this->params = $this->args_arr['params'];
$this->args = $this->args_arr['args'];
$this->flags = $this->args_arr['flags'];
if (isset($this->args[0]))
{
$function = $this->args[0];
$this->$function();
}
else
{
echo "
Please specify what task to run:
e.g.: 'php generate fixtures' or
'php generate migration'
flags can by specified by '-flag'
parameters by 'PARAM=VALUE'
e.g.: 'php generate fixtures -verbose'
'php generate fixtures db=different_db_than_default'
To receive more information on a task
type 'php generate help task'
e.g.: 'php generate help migration'
";
}
}
function fixtures()
{
$db = isset($this->params['db'])? $this->params['db']:'db';
$prefix = ($db == 'db2')? "db2_":"";
/**
* Update this controller to your default controller
*/
$this->CI = set_controller('welcome');
echo "generating...\n";
$this->CI->load->database();
$tables = $this->CI->$db->list_tables();
foreach ($tables as $table)
{
echo $table;
$fields = $this->CI->$db->list_fields($table);
$fixt_name= 'fixtures/'.$prefix.$table.'_fixt.yml';
if (file_exists($fixt_name))
{
echo "fixture $fixt_name already exits!\n";
}
else
{
$h = fopen($fixt_name, 'w') or die("can't open file");
//write fixture!
foreach (array('1', '2', '3') as $rowname)
{
fwrite($h, $rowname.":\n");
foreach ($fields as $field)
{
//echo $field;
fwrite($h, ' ' . $field . ": \n");
}
fwrite($h, "\n");
}
echo " *** fixture $fixt_name written.\n";
fclose($h);
}
}
}
}
$gen = new Generate();