|
| 1 | +/* |
| 2 | + * Copyright (c) 2015-present, Facebook, Inc. |
| 3 | + * All rights reserved. |
| 4 | + * |
| 5 | + * This source code is licensed under the MIT license found in the |
| 6 | + * LICENSE file in the root directory of this source tree. |
| 7 | + * |
| 8 | + */ |
| 9 | + |
| 10 | +namespace Facebook\HackCodegen; |
| 11 | + |
| 12 | +use namespace HH\Lib\Str; |
| 13 | + |
| 14 | +/** |
| 15 | + * Generate code for an xhp attribute. Please don't use this class directly; |
| 16 | + * instead use the function ICodegenFactory->codegenAttribute. E.g.: |
| 17 | + * |
| 18 | + * ICodegenFactory->codegenAttribute('src') |
| 19 | + * ->setType('string') |
| 20 | + * ->setInlineComment('A script src must be a valid URI') |
| 21 | + * ->render(); |
| 22 | + */ |
| 23 | +final class CodegenXHPAttribute implements ICodeBuilderRenderer { |
| 24 | + |
| 25 | + use HackBuilderRenderer; |
| 26 | + |
| 27 | + private ?string $comment; |
| 28 | + private ?string $type; |
| 29 | + private ?string $value; |
| 30 | + private ?XHPAttributeDecorator $decorator; |
| 31 | + |
| 32 | + public function __construct( |
| 33 | + protected IHackCodegenConfig $config, |
| 34 | + private string $name, |
| 35 | + ) {} |
| 36 | + |
| 37 | + public function getName(): string { |
| 38 | + return $this->name; |
| 39 | + } |
| 40 | + |
| 41 | + public function getType(): ?string { |
| 42 | + return $this->type; |
| 43 | + } |
| 44 | + |
| 45 | + public function getValue(): mixed { |
| 46 | + return $this->value; |
| 47 | + } |
| 48 | + |
| 49 | + public function setDecorator(?XHPAttributeDecorator $decorator): this { |
| 50 | + invariant( |
| 51 | + $decorator is null || $this->value is null, |
| 52 | + 'XHP attributes with a default value can not have an %s decorator', |
| 53 | + xhp_attribute_decorator_to_string($decorator), |
| 54 | + ); |
| 55 | + $this->decorator = $decorator; |
| 56 | + return $this; |
| 57 | + } |
| 58 | + |
| 59 | + public function setInlineComment(string $comment): this { |
| 60 | + $this->comment = $comment; |
| 61 | + return $this; |
| 62 | + } |
| 63 | + |
| 64 | + /** |
| 65 | + * Set the type of the member var. In Hack, if it's nullable |
| 66 | + * you should prepend the question mark, e.g. "?string". |
| 67 | + * XHP enums should be avoided, but you can specify "enum { 'foo' }" |
| 68 | + * as a literal string if you need it. |
| 69 | + */ |
| 70 | + public function setType(string $type): this { |
| 71 | + $this->type = $type; |
| 72 | + return $this; |
| 73 | + } |
| 74 | + |
| 75 | + public function setTypef( |
| 76 | + Str\SprintfFormatString $format, |
| 77 | + mixed ...$args |
| 78 | + ): this { |
| 79 | + return $this->setType(\vsprintf($format, $args)); |
| 80 | + } |
| 81 | + |
| 82 | + /** |
| 83 | + * Set the initial value for the variable. You can pass numbers, strings, |
| 84 | + * arrays, etc, and it will generate the code to render those values. |
| 85 | + */ |
| 86 | + public function setValue<T>( |
| 87 | + T $value, |
| 88 | + IHackBuilderValueRenderer<T> $renderer, |
| 89 | + ): this { |
| 90 | + invariant( |
| 91 | + $this->decorator is null, |
| 92 | + 'XHP attributes with an %s decorator can not have a default value', |
| 93 | + xhp_attribute_decorator_to_string($this->decorator), |
| 94 | + ); |
| 95 | + $this->value = $renderer->render($this->config, $value); |
| 96 | + return $this; |
| 97 | + } |
| 98 | + |
| 99 | + public function appendToBuilder(HackBuilder $builder): HackBuilder { |
| 100 | + $value = $this->value; |
| 101 | + |
| 102 | + return $builder |
| 103 | + ->addDocBlock($this->comment) |
| 104 | + ->addIf($this->type is nonnull, $this->type.' ') |
| 105 | + ->add($this->name) |
| 106 | + ->addIf($this->value is nonnull, ' = '.$value) |
| 107 | + ->addIf( |
| 108 | + $this->decorator is nonnull, |
| 109 | + ' '. |
| 110 | + xhp_attribute_decorator_to_string( |
| 111 | + $this->decorator ?? XHPAttributeDecorator::REQUIRED, |
| 112 | + ), |
| 113 | + ); |
| 114 | + } |
| 115 | + |
| 116 | +} |
0 commit comments