Skip to content

Commit a8d281c

Browse files
committed
fixed classwriting for method and GParameter
changed interface for gparameter hint for better refactoring fixed (a little) writing for simple expressions in gfunction body added installer InitConfigurationPart, for very simple configuratino creating
1 parent 2300759 commit a8d281c

File tree

17 files changed

+313
-56
lines changed

17 files changed

+313
-56
lines changed

lib/Webforge/Code/Generator/ClassWriter.php

Lines changed: 51 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,16 @@ class ClassWriter {
3131
*/
3232
protected $codeWriter;
3333

34+
/**
35+
* @var string
36+
*/
37+
protected $namespaceContext;
38+
39+
/**
40+
* @var Webforge\Code\Generator\Imports
41+
*/
42+
protected $classImports;
43+
3444
public function __construct() {
3545
$this->imports = new Imports();
3646
}
@@ -56,10 +66,10 @@ public function writeGClassFile(GClass $gClass, $eol = "\n") {
5666
$php .= $eol;
5767
}
5868

59-
$imports = clone $this->imports;
60-
$imports->mergeFromClass($gClass);
69+
$this->classImports = clone $this->imports;
70+
$this->classImports->mergeFromClass($gClass);
6171

62-
if ($use = $imports->php($namespace)) {
72+
if ($use = $this->classImports->php($namespace)) {
6373
$php .= $use;
6474
$php .= $eol;
6575
}
@@ -79,6 +89,7 @@ public function writeGClassFile(GClass $gClass, $eol = "\n") {
7989
*/
8090
public function writeGClass(GClass $gClass, $namespace, $eol = "\n") {
8191
$that = $this;
92+
$this->namespaceContext = $namespace;
8293

8394
$php = NULL;
8495

@@ -162,7 +173,6 @@ public function writeMethod(GMethod $method, $baseIndent = 0, $eol = "\n") {
162173
$php .= str_repeat(' ', $baseIndent);
163174

164175
$php .= $this->writeModifiers($method->getModifiers());
165-
166176
$php .= $this->writeGFunction($method, $baseIndent, $eol);
167177

168178
return $php;
@@ -196,43 +206,42 @@ public function writeGFunction($function, $baseIndent = 0, $eol = "\n") {
196206
public function writeFunctionBody(GFunctionBody $body = NULL, $baseIndent = 0, $eol = "\n") {
197207
$php = NULL;
198208

199-
$phpBody = $body ? $body->php($baseIndent+2, $eol) : '';
209+
$phpBody = $body ? $body->php($baseIndent+2, $eol).$eol : '';
200210

201211
$php .= ' {';
202212
//if ($this->cbraceComment != NULL) { // inline comment wie dieser
203213
//$php .= ' '.$this->cbraceComment;
204214
//}
205215
$php .= $eol;
206-
$php .= $phpBody;
216+
$php .= $phpBody;
207217
$php .= S::indent('}', $baseIndent, $eol);
208218

209219
return $php;
210220
}
211221

212222

213223
protected function writeFunctionSignature($function, $baseIndent = 0, $eol = "\n") {
214-
$php = 'function '.$function->getName().$this->writeParameters($function->getParameters(), $baseIndent, $eol);
224+
$php = 'function '.$function->getName().$this->writeParameters($function->getParameters(), $this->namespaceContext, $baseIndent, $eol);
215225

216-
return S::indent($php, $baseIndent, $eol);
226+
return $php;
217227
}
218228

219-
public function writeParameters(Array $parameters) {
229+
public function writeParameters(Array $parameters, $namespace) {
220230
$that = $this;
221231

222232
$php = '(';
223-
$php .= A::implode($parameters, ', ', function ($parameter) use ($that) {
224-
return $that->writeParameter($parameter);
233+
$php .= A::implode($parameters, ', ', function ($parameter) use ($that, $namespace) {
234+
return $that->writeParameter($parameter, $namespace);
225235
});
226236
$php .= ')';
227237

228238
return $php;
229239
}
230240

231-
public function writeParameter(GParameter $parameter) {
232-
$php = NULL;
241+
public function writeParameter(GParameter $parameter, $namespace) {
242+
$php = '';
233243

234-
// Type Hint
235-
$php .= $parameter->getHint();
244+
$php .= $this->writeParameterTypeHint($parameter, $namespace);
236245

237246
// name
238247
$php .= ($parameter->isReference() ? '&' : '').'$'.$parameter->getName();
@@ -251,6 +260,33 @@ public function writeParameter(GParameter $parameter) {
251260

252261
return $php;
253262
}
263+
264+
/**
265+
* @return string with whitespace at the end if hint is set
266+
*/
267+
protected function writeParameterTypeHint(GParameter $parameter, $namespace) {
268+
if ($parameter->hasHint()) {
269+
270+
if (($import = $parameter->getHintImport()) instanceof GClass) {
271+
272+
if (isset($this->classImports) && $this->classImports->have($import)) {
273+
$useFQN = FALSE;
274+
} elseif ($this->imports->have($import)) {
275+
$useFQN = FALSE;
276+
} elseif ($import->getNamespace() === $namespace) {
277+
$useFQN = FALSE;
278+
} else {
279+
$useFQN = TRUE;
280+
}
281+
282+
return $parameter->getHint($useFQN).' ';
283+
} else {
284+
return $parameter->getHint().' ';
285+
}
286+
}
287+
288+
return '';
289+
}
254290

255291
public function writeArgumentValue($value) {
256292
if (is_array($value) && A::getType($value) === 'numeric') {

lib/Webforge/Code/Generator/GFunctionBody.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public static function create($body) {
3737
}
3838

3939
public function php($baseIndent = 0, $eol = "\n") {
40-
$printer = new PrettyPrinter();
40+
$printer = new PrettyPrinter($baseIndent, $eol);
4141

4242
return $printer->prettyPrint($this->stmts);
4343
}

lib/Webforge/Code/Generator/GParameter.php

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use Psc\Data\Type\MixedType;
77
use Psc\Data\Type\ObjectType;
88
use Psc\Data\Type\ArrayType;
9+
use Psc\Data\Type\ParameterHintedType;
910

1011
/**
1112
* Models a GParameter for a GFunction / GMethod
@@ -86,12 +87,29 @@ public function setType(Type $type = NULL) {
8687
}
8788

8889
/**
89-
* Returns the Hint of the Parameter as a String
90+
* Returns the Hint of the Parameter (if any)
9091
*
91-
* @return string
92+
* @return Webforge\Code\Generator\GClass|string|NULL
93+
*/
94+
public function getHint($useFQN = TRUE) {
95+
if ($this->hasHint()) {
96+
return $this->type->getParameterHint($useFQN);
97+
}
98+
}
99+
100+
/**
101+
* @return Webforge\Code\Generator\GClass|NULL
92102
*/
93-
public function getHint($namespaceContext = NULL) {
94-
return ltrim($this->type->getPHPHint($namespaceContext), '\\') ?: NULL;
103+
public function getHintImport() {
104+
if ($this->hasHint() && ($import = $this->type->getParameterHintImport()) instanceof \Psc\Code\Generate\GClass) {
105+
return new GClass($import->getFQN());
106+
}
107+
108+
return NULL;
109+
}
110+
111+
public function hasHint() {
112+
return $this->type instanceof ParameterHintedType;
95113
}
96114

97115
/**

lib/Webforge/Code/Generator/PrettyPrinter.php

Lines changed: 33 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6,28 +6,43 @@
66
use PHPParser_Node_Stmt_Case;
77
use PHPParser_Node_Expr_ClosureUse;
88
use PHPParser_Node_Expr_Closure;
9+
use Webforge\Common\String AS S;
910

1011
class PrettyPrinter extends \PHPParser_PrettyPrinter_Zend {
12+
13+
protected $baseIndent;
14+
15+
public function __construct($baseIndent = 0) {
16+
$this->baseIndent = $baseIndent;
17+
parent::__construct();
18+
}
19+
20+
public function prettyPrint(array $nodes) {
21+
$php = parent::prettyPrint($nodes);
22+
23+
return S::indent($php, $this->baseIndent, "\n");
24+
}
1125

12-
public function pStmt_Switch(PHPParser_Node_Stmt_Switch $node) {
13-
return 'switch (' . $this->p($node->cond) . ') {'
14-
. "\n" . $this->pStmts($node->cases) . "\n" .'}';
15-
}
26+
public function pStmt_Switch(PHPParser_Node_Stmt_Switch $node) {
27+
return 'switch (' . $this->p($node->cond) . ') {'
28+
. "\n" . $this->pStmts($node->cases) . "\n" .'}';
29+
}
1630

17-
public function pStmt_Case(PHPParser_Node_Stmt_Case $node) {
18-
return (null !== $node->cond ? 'case ' . $this->p($node->cond) : 'default') . ':'
19-
. (count($node->stmts) > 0
20-
? "\n" . $this->pStmts($node->stmts)
21-
: ''
22-
);
23-
}
31+
public function pStmt_Case(PHPParser_Node_Stmt_Case $node) {
32+
return (null !== $node->cond ? 'case ' . $this->p($node->cond) : 'default') . ':'
33+
. (count($node->stmts) > 0
34+
? "\n" . $this->pStmts($node->stmts)
35+
: ''
36+
);
37+
}
2438

25-
public function pExpr_Closure(PHPParser_Node_Expr_Closure $node) {
26-
return ($node->static ? 'static ' : '')
27-
. 'function ' . ($node->byRef ? '&' : '')
28-
. '(' . $this->pCommaSeparated($node->params) . ')'
29-
. (!empty($node->uses) ? ' use (' . $this->pCommaSeparated($node->uses) . ')': '')
30-
. ' {' . "\n" . $this->pStmts($node->stmts) . "\n" . '}';
31-
}
39+
public function pExpr_Closure(PHPParser_Node_Expr_Closure $node) {
40+
return ($node->static ? 'static ' : '')
41+
. 'function ' . ($node->byRef ? '&' : '')
42+
. '(' . $this->pCommaSeparated($node->params) . ')'
43+
. (!empty($node->uses) ? ' use (' . $this->pCommaSeparated($node->uses) . ')': '')
44+
. ' {' . "\n" . $this->pStmts($node->stmts) . "\n" . '}';
45+
}
3246

3347
}
48+
?>

lib/Webforge/Framework/Container.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,8 @@ public function getPartsInstaller(OutputInterface $output = NULL) {
198198
new \Webforge\Setup\Installer\CreateBootstrapPart(),
199199
new \Webforge\Setup\Installer\InstallTestSuitePart(),
200200
new \Webforge\Setup\Installer\WriteHtaccessPart(),
201-
new \Webforge\Setup\Installer\PscJSBoilerplatePart()
201+
new \Webforge\Setup\Installer\PscJSBoilerplatePart(),
202+
new \Webforge\Setup\Installer\InitConfigurationPart()
202203
),
203204
$this,
204205
$output

lib/Webforge/Framework/PscCMSBridge.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Webforge\Setup\Package\Package;
66
use Psc\CMS\Project;
77
use Psc\PSC;
8+
use Psc\CMS\Configuration;
89

910
class PscCMSBridge {
1011

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
namespace Webforge\Setup\Installer;
4+
5+
use Psc\System\Dir;
6+
use Webforge\Common\String AS S;
7+
use Webforge\Setup\Package\PackageAware;
8+
9+
class InitConfigurationPart extends Part implements PackageAware {
10+
11+
public function __construct() {
12+
parent::__construct('InitConfiguration');
13+
}
14+
15+
public function installTo(Dir $target, Installer $installer) {
16+
$etc = $target->sub('etc')->create();
17+
18+
$installer->writeTemplate(
19+
$installer->getInstallTemplates()->getFile('changelog.template.php'),
20+
$etc->getFile('changelog.php'),
21+
array(
22+
'time'=>date('d.m.Y H:i'),
23+
'msg'=>'init configuration'
24+
)
25+
);
26+
27+
$installer->writeTemplate(
28+
$installer->getInstallTemplates()->getFile('config.template.php'),
29+
$etc->getFile('config.php'),
30+
array(
31+
'db.password'=>S::random(14),
32+
'defaultLanguage'=>'de',
33+
'package.title'=>$this->package->getTitle()
34+
)
35+
);
36+
}
37+
}
38+
?>

lib/Webforge/Setup/Installer/Installer.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ public function install(Part $part, Dir $destination);
2222
public function copy($source, $destination, $flags = 0x000000);
2323

2424
public function write($contents, File $destination, $flags = 0x000000);
25+
26+
public function writeTemplate(File $template, File $destination, Array $vars = array(), $flags = 0x000000);
2527

2628
/**
2729
* @return Psc\System\Dir

lib/Webforge/Setup/Installer/Part.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,23 @@
44

55
use Psc\System\File;
66
use Psc\System\Dir;
7+
use Webforge\Setup\Package\Package;
78

9+
/**
10+
* "implement" PackageAware to have the $package set to the local package
11+
*/
812
abstract class Part {
913

1014
/**
1115
* @var string
1216
*/
1317
protected $name;
1418

19+
/**
20+
* @var Webforge\Setup\Package\Package
21+
*/
22+
protected $package;
23+
1524
public function __construct($name) {
1625
$this->name = $name;
1726
}
@@ -36,5 +45,21 @@ public function setName($name) {
3645
public function getName() {
3746
return $this->name;
3847
}
48+
49+
/**
50+
* @param Webforge\Setup\Package\Package $package
51+
* @chainable
52+
*/
53+
public function setPackage(Package $package) {
54+
$this->package = $package;
55+
return $this;
56+
}
57+
58+
/**
59+
* @return Webforge\Setup\Package\Package
60+
*/
61+
public function getPackage() {
62+
return $this->package;
63+
}
3964
}
4065
?>

lib/Webforge/Setup/Installer/PartsInstaller.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use Webforge\Setup\Package\PackageAware;
1010
use Symfony\Component\Console\Output\OutputInterface;
1111
use Symfony\Component\Console\Output\NullOutput;
12+
use Psc\TPL\TPL;
1213

1314
/**
1415
* @todo an output interface to communicate and warn
@@ -75,6 +76,19 @@ public function write($contents, File $destination, $flags = 0x000000) {
7576

7677
return $this;
7778
}
79+
80+
public function writeTemplate(File $template, File $destination, Array $vars = array(), $flags = 0x000000) {
81+
if (($flags & self::IF_NOT_EXISTS) && $destination->exists()) {
82+
$this->warn('will not overwrite (per request): '.$destination);
83+
return $this;
84+
}
85+
86+
$contents = TPL::miniTemplate($template->getContents(), $vars);
87+
88+
$destination->writeContents($contents);
89+
90+
return $this;
91+
}
7892

7993
/**
8094
* @return Psc\System\Dir

0 commit comments

Comments
 (0)