/
GiicCommand.php
126 lines (99 loc) · 3.36 KB
/
GiicCommand.php
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
<?php
/**
* Class file.
*/
/**
*
*/
Yii::import('system.cli.commands.*');
Yii::import('vendor.schmunk42.giic.ShellColors');
class GiicCommand extends CConsoleCommand
{
/**
* use the ShellColors class
* @var class
*/
private $_shellAlert;
/**
* initialize the colors
*/
public function init()
{
parent::init();
$this->_shellAlert = new ShellColors();
}
public function getHelp()
{
return <<<EOD
USAGE
yiic gii <path-to-giic-config>
DESCRIPTION
This command runs gii on the command line
PARAMETERS
* path-to-giic-config: path alias
EOD;
}
/**
* Execute the action.
*
* @param array $args command line parameters specific for this command
*/
public function actionGenerate($args)
{
//TODO: make available via param --xdebug-trace
//xdebug_start_trace('giic');
if (!$this->confirm("\nAttention! The command may overwrite exisiting files wihtout further notice.\n\nEnable overwrite all existing files?")) {
define('GIIC_ALL_CONFIRMED', false);
} else {
define('GIIC_ALL_CONFIRMED', true);
}
// fake input params
$_SERVER['REQUEST_URI'] = "console://index.php";
$_SERVER['SCRIPT_FILENAME'] = $_SERVER['SCRIPT_NAME'] = "index.php";
$_POST['generate'] = true;
$_POST['answers'] = true;
// create gii module for controller
Yii::import('system.gii.*');
$module = Yii::createComponent('system.gii.GiiModule', 'gii', null);
$module->password = false;
// load config
$path = Yii::getPathOfAlias($args[0]) . "/giic-config.php";
if(!is_file($path)){
echo $this->_shellAlert->getColoredString("File in {$path} not exist!", "white", "red") . "\n";
exit;
}
$config = require($path);
// execute actions (run gii controller action multiple times)
foreach ($config['actions'] AS $action) {
// fake input param
$_POST[$action['codeModel']] = $action['model'];
// create generator
$controller = Yii::createComponent(
$action['generator'],
lcfirst($action['codeModel']),
$module
);
// assign template
$controller->templates = $action['templates'];
// message
echo $action['codeModel']."\n".substr(CJSON::encode($action['model']),0,160);
echo "\n\n";
// assign controller to application
Yii::app()->controller = $controller;
// capture output from controller
ob_start();
$controller->run('index');
$html = ob_get_clean();
// TODO: tidy
// sanitize, XSLT hotfix
$html = str_replace(" ","",$html);
$html = str_replace('png">','png"/>',$html);
// parse for console output
$xslt = new XSLTProcessor();
$xslt->importStylesheet(new SimpleXMLElement(file_get_contents(dirname(__FILE__).'/giic.xsl')));
file_put_contents(dirname(__FILE__).'/giic.html.log', $html);
echo $xslt->transformToXml(new SimpleXMLElement($html));
// TODO: add $html output with --verbose
}
}
}