forked from hhvm/hack-codegen
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCodegenShape.php
More file actions
59 lines (49 loc) · 1.45 KB
/
CodegenShape.php
File metadata and controls
59 lines (49 loc) · 1.45 KB
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
<?hh // strict
/**
* Copyright (c) 2015-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*/
/**
* Generate code for a shape. Please don't use this class directly; instead use
* the function codegen_shape. E.g.:
*
* codegen_shape(array('x' => 'int', 'y' => 'int'))
*/
final class CodegenShape
implements ICodeBuilderRenderer {
use HackBuilderRenderer;
private ?string $manualAttrsID = null;
public function __construct(
private array<string, string> $attrs = array(),
) {}
public function setManualAttrsID(?string $id = null): this {
$this->manualAttrsID = $id;
return $this;
}
public function appendToBuilder(HackBuilder $builder): HackBuilder {
$builder
->addLine('shape(')
->indent();
foreach ($this->attrs as $name => $type) {
$builder->addLine("'%s' => %s,", $name, $type);
}
$manual_id = $this->manualAttrsID;
if ($manual_id !== null) {
$builder
->ensureNewLine()
->beginManualSection($manual_id)
->ensureEmptyLine()
->endManualSection();
}
return $builder
->unindent()
->add(')');
}
}
function codegen_shape(array<string, string> $attrs = array()): CodegenShape {
return new CodegenShape($attrs);
}