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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
|
#!/usr/bin/php
<?php
/**
* Prado command line developer tools.
*/
if(!isset($_SERVER['argv']))
die('Must be run from the command line');
require_once(dirname(__FILE__).'/prado.php');
class PradoShellApplication extends TApplication
{
public function run()
{
$this->initApplication();
}
}
class DummyHttpRequest extends THttpRequest
{
protected function resolveRequest()
{
}
}
function __shell_print_var($shell,$var)
{
if(!$shell->has_semicolon)
echo Prado::varDump($var);
}
restore_exception_handler();
//run it;
PradoCommandLineInterpreter::run($_SERVER['argv']);
if(strtolower($_SERVER['argv'][1])==='shell')
include_once(dirname(__FILE__).'/3rdParty/PhpShell/php-shell-cmd.php');
class PradoCommandLineInterpreter
{
protected $_actions=array(
'PradoCommandLineCreateProject' => null,
'PradoCommandLineCreateTests' => null,
'PradoCommandLinePhpShell' => null
);
public function __construct()
{
foreach(array_keys($this->_actions) as $class)
$this->_actions[$class] = new $class;
}
public static function run($args)
{
echo "Command line tools for Prado ".Prado::getVersion().".\n";
if(count($args) > 1)
array_shift($args);
static $int;
if(is_null($int))
$int = new self;
$valid = false;
foreach($int->_actions as $class => $action)
{
if($action->isValidAction($args))
{
$valid |= $action->performAction($args);
break;
}
else
{
$valid = false;
}
}
if(!$valid)
$int->printHelp();
}
public function printHelp()
{
echo "usage: php prado-cli.php action <parameter> [optional]\n";
echo "example: php prado-cli.php -c mysite\n\n";
echo "actions:\n";
foreach($this->_actions as $action)
echo $action->renderHelp();
}
}
abstract class PradoCommandLineAction
{
abstract public function performAction($args);
protected function createDirectory($dir, $mask)
{
if(!is_dir($dir))
{
mkdir($dir);
echo "creating $dir\n";
}
if(is_dir($dir))
chmod($dir, $mask);
}
protected function createFile($filename, $content)
{
if(!is_file($filename))
{
file_put_contents($filename, $content);
echo "creating $filename\n";
}
}
public function isValidAction($args)
{
return strtolower($args[0]) === $this->action &&
count($args)-1 >= count($this->parameters);
}
public function renderHelp()
{
$params = array();
foreach($this->parameters as $v)
$params[] = '<'.$v.'>';
$parameters = join($params, ' ');
$options = array();
foreach($this->optional as $v)
$options[] = '['.$v.']';
$optional = (strlen($parameters) ? ' ' : ''). join($options, ' ');
$description='';
foreach(explode("\n", wordwrap($this->description,65)) as $line)
$description .= ' '.$line."\n";
return <<<EOD
{$this->action} {$parameters}{$optional}
{$description}
EOD;
}
}
class PradoCommandLineCreateProject extends PradoCommandLineAction
{
protected $action = '-c';
protected $parameters = array('directory');
protected $optional = array();
protected $description = 'Creates a Prado project skeleton for the given <directory>.';
public function performAction($args)
{
$this->createNewPradoProject($args[1]);
return true;
}
/**
* Functions to create new prado project.
*/
protected function createNewPradoProject($dir)
{
if(strlen(trim($dir)) == 0)
return;
$rootPath = realpath(dirname(trim($dir)));
$basePath = $rootPath.'/'.basename($dir);
$assetPath = $basePath.'/assets';
$protectedPath = $basePath.'/protected';
$runtimePath = $basePath.'/protected/runtime';
$pagesPath = $protectedPath.'/pages';
$indexFile = $basePath.'/index.php';
$htaccessFile = $protectedPath.'/.htaccess';
$defaultPageFile = $pagesPath.'/Home.page';
$this->createDirectory($basePath, 0755);
$this->createDirectory($assetPath,0777);
$this->createDirectory($protectedPath,0755);
$this->createDirectory($runtimePath,0777);
$this->createDirectory($pagesPath,0755);
$this->createFile($indexFile, $this->renderIndexFile());
$this->createFile($htaccessFile, $this->renderHtaccessFile());
$this->createFile($defaultPageFile, $this->renderDefaultPage());
}
protected function renderIndexFile()
{
$framework = realpath(dirname(__FILE__)).'/prado.php';
return '<?php
$frameworkPath=\''.$framework.'\';
/** The directory checks may be removed if performance is required **/
$basePath=dirname(__FILE__);
$assetsPath=$basePath."/assets";
$runtimePath=$basePath."/protected/runtime";
if(!is_file($frameworkPath))
die("Unable to find prado framework path $frameworkPath.");
if(!is_writable($assetsPath))
die("Please make sure that the directory $assetsPath is writable by Web server process.");
if(!is_writable($runtimePath))
die("Please make sure that the directory $runtimePath is writable by Web server process.");
require_once($frameworkPath);
$application=new TApplication;
$application->run();
?>';
}
protected function renderHtaccessFile()
{
return 'deny from all';
}
protected function renderDefaultPage()
{
return <<<EOD
<h1>Welcome to Prado!</h1>
EOD;
}
}
class PradoCommandLineCreateTests extends PradoCommandLineAction
{
protected $action = '-t';
protected $parameters = array('directory');
protected $optional = array();
protected $description = 'Create test fixtures in the given <directory>.';
public function performAction($args)
{
$this->createTestFixtures($args[1]);
return true;
}
protected function createTestFixtures($dir)
{
if(strlen(trim($dir)) == 0)
return;
$rootPath = realpath(dirname(trim($dir)));
$basePath = $rootPath.'/'.basename($dir);
$tests = $basePath.'/tests';
$unit_tests = $tests.'/unit';
$functional_tests = $tests.'/functional';
$this->createDirectory($tests,0755);
$this->createDirectory($unit_tests,0755);
$this->createDirectory($functional_tests,0755);
$unit_test_index = $tests.'/unit.php';
$functional_test_index = $tests.'/functional.php';
$this->createFile($unit_test_index, $this->renderUnitTestFixture());
$this->createFile($functional_test_index, $this->renderFunctionalTestFixture());
}
protected function renderUnitTestFixture()
{
$tester = realpath(dirname(__FILE__).'/../tests/test_tools/unit_tests.php');
return '<?php
include_once \''.$tester.'\';
$app_directory = "../protected";
$test_cases = dirname(__FILE__)."/unit";
$tester = new PradoUnitTester($test_cases, $app_directory);
$tester->run(new HtmlReporter());
?>';
}
protected function renderFunctionalTestFixture()
{
$tester = realpath(dirname(__FILE__).'/../tests/test_tools/functional_tests.php');
return '<?php
include_once \''.$tester.'\';
$test_cases = dirname(__FILE__)."/functional";
$tester=new PradoFunctionalTester($test_cases);
$tester->run(new SimpleReporter());
?>';
}
}
class PradoCommandLinePhpShell extends PradoCommandLineAction
{
protected $action = 'shell';
protected $parameters = array();
protected $optional = array('directory');
protected $description = 'Runs a PHP interactive interpreter. Initializes the Prado application in the given [directory].';
public function performAction($args)
{
if(count($args) > 1)
$this->createAndRunPradoApp($args);
return true;
}
protected function createAndRunPradoApp($args)
{
$app_dir = realpath($args[1].'/protected/');
if($app_dir !== false)
{
$app = new PradoShellApplication($app_dir);
$app->setRequest(new DummyHttpRequest());
$app->run();
$dir = substr(str_replace(realpath('./'),'',$app_dir),1);
echo '** Loaded Prado appplication in directory "'.$dir."\".\n";
}
else
echo '** Unable to load Prado application in directory "'.$args[1]."\".\n";
}
}
?>
|