Smutty
MVC Framework
View Code: Smutty_Smut
Browse: All · Classes · Plugins
1
<?php
2
3
/**
4
* this class is the command line "smut" utility. it implements
5
* all the actions like creating models, controllers, etc...
6
*
7
*/
8
9
class Smutty_Smut extends Smutty_Object {
10
11
/**
12
* prints out a usage message for smut
13
*
14
* @return nothing
15
*
16
*/
17
19
$tpl = new Smutty_Template_Smutty();
20
$tpl->display( 'smut/usage.tpl' );
21
exit();
22
}
23
24
/**
25
* creates a controller
26
*
27
* @param String $name the name of the controller
28
* @return nothing
29
*
30
*/
31
33
// write the controller file
34
$tpl = new Smutty_Template_Smutty();
35
$tpl->assign( 'name', $name );
36
$this->writeFile(
37
'application/controllers/' . $name . 'Controller.php',
38
$tpl->fetch('smut/controller.tpl')
39
);
40
// make sure views folder exists
41
$viewFolder = 'application/views/' . strtolower($name);
42
if ( !file_exists($viewFolder) )
43
mkdir( $viewFolder );
44
}
45
46
/**
47
* creates a model
48
*
49
* @param String $name the name of the model to create
50
* @return nothing
51
*
52
*/
53
55
$tpl = new Smutty_Template_Smutty();
56
$tpl->assign( 'name', $name );
57
$this->writeFile(
58
'application/models/' . $name . '.php',
59
$tpl->fetch('smut/model.tpl')
60
);
61
}
62
63
/**
64
* writes the given data to the specified file
65
*
66
* @param String $path the path of the file
67
* @param String $data the data to write
68
* @return nothing
69
*
70
*/
71
73
$f = fopen( $path, 'w' );
74
fputs( $f, $data );
75
fclose( $f );
76
}
77
78
/**
79
* runs a single test case
80
*
81
* @param String $name the name of the test
82
* @return nothing
83
*
84
*/
85
87
88
$suite = new Smutty_Test_Suite( 'application/tests' );
89
$suite->addTest( $name );
90
$suite->run();
91
92
}
93
94
/**
95
* this action makes sure everything is set up for smutty
96
*
97
* @return nothing
98
*
99
*/
100
102
103
//
104
// app.cfg
105
//
106
$appFile = 'application/app.cfg';
107
echo 'Checking app.cfg... ';
108
if ( !file_exists($appFile) ) {
109
echo 'not found, creating... ';
110
copy( "$appFile-sample", $appFile );
111
}
112
echo "Ok!\n";
113
114
//
115
// smarty cache
116
//
117
echo 'Checking cache permissions... ';
118
$cacheDir = 'library/smarty/cache';
119
if ( !is_writable($cacheDir) ) {
120
echo 'not writeable, chmoding... ';
121
system( 'chmod -R 777 "' . escapeshellarg($cacheDir) . '" ' );
122
}
123
echo "Ok!\n";
124
125
//
126
// htaccess
127
//
128
echo 'Checking .htaccess file... ';
129
$htaccessFile = 'htdocs/.htaccess';
130
if ( !file_exists($htaccessFile) ) {
131
echo 'not found, creating... ';
132
copy( 'htdocs/htaccess', $htaccessFile );
133
}
134
echo "Ok!\n";
135
136
}
137
138
/**
139
* performs an action on the database
140
*
141
* @param String $cmd action to perform
142
* @return nothing
143
*
144
*/
145
147
148
switch ( $cmd ) {
149
case 'build':
150
echo "Rebuilding database:\n";
151
$db = Smutty_Database::getInstance();
152
$cfg = Smutty_Config::getInstance();
153
$type = $cfg->get( 'db.type' );
154
foreach ( array('drop','create','data') as $file ) {
155
echo "Creating $file...";
156
$path = "./application/schemas/$type/$file.sql";
157
if ( file_exists($path) ) {
158
$db->script( $path );
159
echo "Done!\n";
160
}
161
else echo "$path not found...\n";
162
}
163
break;
164
default:
165
$this->usage();
166
}
167
168
}
169
170
/**
171
* this is the main entry point for running this
172
* class from the command line
173
*
174
* @param array $argv the command line arguments
175
* @return nothing
176
*
177
*/
178
180
181
if ( file_exists('application/app.cfg') ) {
182
$cfg =& Smutty_Config::getInstance();
183
$cfg->loadDefaults();
184
}
185
186
$action = $argv[ 1 ];
187
$name = $argv[ 2 ];
188
189
switch ( $action ) {
190
case 'controller':
191
if ( !$name ) $this->usage();
192
$this->createController( $name );
193
break;
194
case 'model':
195
if ( !$name ) $this->usage();
196
$this->createModel( $name );
197
break;
198
case 'test':
199
if ( !$name ) $this->usage();
200
$this->runTest( $name );
201
break;
202
case 'db':
203
if ( !$name ) $this->usage();
204
$this->db( $name );
205
break;
206
case 'setup':
207
$this->setup();
208
break;
209
default:
210
$this->usage();
211
}
212
213
}
214
215
}
216
217
?>
The code shown here is the code that is currently running this site. If you want to view the latest SVN version of the code then go to the Subversion repository.