Skip to content

Commit 39f3cd4

Browse files
committed
nette/php-generator used to generate files
1 parent e9182d7 commit 39f3cd4

File tree

1 file changed

+37
-16
lines changed

1 file changed

+37
-16
lines changed

app/tasks/MainTask.php

Lines changed: 37 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -19,47 +19,68 @@ public function mainAction($arguments = [])
1919
return 1;
2020
}
2121

22-
$namespace = (isset($arguments[0]) && $arguments[0] != 'none') ? $arguments[0] : null;
23-
$trait = (isset($arguments[1]) && $arguments[1] != 'none') ? $arguments[1] : null;
24-
$extends = '\Phalcon\Forms\Form';
25-
$extends = isset($arguments[2]) ? ($arguments[2] == 'null' ? $extends : ($arguments[2] == 'none' ? null : $arguments[2])) : $extends;
22+
print_r($arguments);
23+
24+
$namespaceName = (isset($arguments[0]) && $arguments[0] != 'none') ? $arguments[0] : null;
25+
$traitName = (isset($arguments[1]) && $arguments[1] != 'none') ? $arguments[1] : null;
26+
$extendsName = '\Phalcon\Forms\Form';
27+
$extendsName = isset($arguments[2]) ? ($arguments[2] == 'null' ? $extendsName : ($arguments[2] == 'none' ? null : $arguments[2])) : $extendsName;
2628

2729
echo "Generation started.", PHP_EOL;
2830

2931
$tables = $this->db->listTables();
3032
foreach ($tables as $table) {
3133
echo "Processing table `{$table}`... ";
3234

33-
$class = \Phalcon\Text::camelize($table);
35+
$className = \Phalcon\Text::camelize($table);
36+
37+
$class = new \Nette\PhpGenerator\ClassType($className . "Form");
38+
if ($extendsName != null) {
39+
$class->setExtends($extendsName);
40+
}
41+
if ($traitName != null) {
42+
$class->addTrait($traitName);
43+
}
3444

35-
$columns = [];
3645
$columnsList = $this->db->describeColumns($table);
3746
foreach ($columnsList as $column) {
3847
if ($column->isPrimary()) {
3948
continue;
4049
}
41-
$columns[] = [
50+
$columnInfo = [
4251
"name" => lcfirst(\Phalcon\Text::camelize($column->getName())),
4352
"type" => $this->getBestPhalconType($column),
4453
"size" => $column->getSize()
4554
];
55+
$method = $class->addMethod($columnInfo['name'] . "Field");
56+
$method->setVisibility('private');
57+
$method->addBody('$element = new \Phalcon\Forms\Element\\' . $columnInfo['type'] . '("' . $columnInfo['name'] . '");');
58+
$method->addBody('$element->setLabel("' . $columnInfo['name'] . '");');
59+
if ($columnInfo['type'] == 'Select') {
60+
$method->addBody('$element->setOptions([]);');
61+
}
62+
63+
if ($columnInfo['type'] == 'Text' && $columnInfo['size']) {
64+
$method->addBody('$element->addValidator(new \Phalcon\Validation\Validator\StringLength([');
65+
$method->addBody(' "max" => ' . $columnInfo['size']);
66+
$method->addBody(']));');
67+
}
68+
$method->addBody('return $element;');
4669
}
4770

48-
$this->view->start();
49-
$this->view->setVar("class", $class);
50-
$this->view->setVar("namespace", $namespace);
51-
$this->view->setVar("trait", $trait);
52-
$this->view->setVar("extends", $extends);
53-
$this->view->setVar("columns", $columns);
54-
$this->view->render('main', 'DefaultForm');
55-
$this->view->finish();
71+
$content = "<?php" . PHP_EOL . PHP_EOL;
72+
if ($namespaceName != null) {
73+
$content .= "namespace " . $namespaceName . ";" . PHP_EOL . PHP_EOL;
74+
}
75+
$content .= (string)$class;
5676

57-
if (file_put_contents($this->outputFolder . "/" . $class . "Form.php", $this->view->getContent()) === false) {
77+
if (file_put_contents($this->outputFolder . "/" . $className . "Form.php", $content) === false) {
5878
echo "Impossible to write the file in the output folder.", PHP_EOL;
5979
echo "Please check the permissions for `{$this->outputFolder}`.", PHP_EOL;
6080
echo "Script aborted.";
6181
return 1;
6282
}
83+
6384
echo "done", PHP_EOL;
6485
}
6586

0 commit comments

Comments
 (0)