|
1 | 1 | <?php |
2 | 2 |
|
| 3 | +/** |
| 4 | + * @task children Managing Children |
| 5 | + */ |
3 | 6 | abstract class AphrontView extends Phobject |
4 | 7 | implements PhutilSafeHTMLProducerInterface { |
5 | 8 |
|
6 | 9 | protected $user; |
7 | 10 | protected $children = array(); |
8 | 11 |
|
| 12 | + |
| 13 | +/* -( Configuration )------------------------------------------------------ */ |
| 14 | + |
| 15 | + |
| 16 | + /** |
| 17 | + * @task config |
| 18 | + */ |
9 | 19 | public function setUser(PhabricatorUser $user) { |
10 | 20 | $this->user = $user; |
11 | 21 | return $this; |
12 | 22 | } |
13 | 23 |
|
| 24 | + |
| 25 | + /** |
| 26 | + * @task config |
| 27 | + */ |
14 | 28 | protected function getUser() { |
15 | 29 | return $this->user; |
16 | 30 | } |
17 | 31 |
|
| 32 | + |
| 33 | +/* -( Managing Children )-------------------------------------------------- */ |
| 34 | + |
| 35 | + |
| 36 | + /** |
| 37 | + * Test if this View accepts children. |
| 38 | + * |
| 39 | + * By default, views accept children, but subclases may override this method |
| 40 | + * to prevent children from being appended. Doing so will cause |
| 41 | + * @{method:appendChild} to throw exceptions instead of appending children. |
| 42 | + * |
| 43 | + * @return bool True if the View should accept children. |
| 44 | + * @task children |
| 45 | + */ |
18 | 46 | protected function canAppendChild() { |
19 | 47 | return true; |
20 | 48 | } |
21 | 49 |
|
| 50 | + |
| 51 | + /** |
| 52 | + * Append a child to the list of children. |
| 53 | + * |
| 54 | + * This method will only work if the view supports children, which is |
| 55 | + * determined by @{method:canAppendChild}. |
| 56 | + * |
| 57 | + * @param wild Something renderable. |
| 58 | + * @return this |
| 59 | + */ |
22 | 60 | final public function appendChild($child) { |
23 | 61 | if (!$this->canAppendChild()) { |
24 | 62 | $class = get_class($this); |
25 | 63 | throw new Exception( |
26 | 64 | pht("View '%s' does not support children.", $class)); |
27 | 65 | } |
| 66 | + |
28 | 67 | $this->children[] = $child; |
| 68 | + |
29 | 69 | return $this; |
30 | 70 | } |
31 | 71 |
|
| 72 | + |
| 73 | + /** |
| 74 | + * Produce children for rendering. |
| 75 | + * |
| 76 | + * Historically, this method reduced children to a string representation, |
| 77 | + * but it no longer does. |
| 78 | + * |
| 79 | + * @return wild Renderable children. |
| 80 | + * @task |
| 81 | + */ |
32 | 82 | final protected function renderChildren() { |
33 | 83 | return $this->children; |
34 | 84 | } |
35 | 85 |
|
| 86 | + |
36 | 87 | /** |
37 | | - * @deprecated |
| 88 | + * Test if an element has no children. |
| 89 | + * |
| 90 | + * @return bool True if this element has children. |
| 91 | + * @task children |
38 | 92 | */ |
39 | | - final protected function renderSingleView($child) { |
40 | | - phutil_deprecated( |
41 | | - 'AphrontView->renderSingleView()', |
42 | | - "This method no longer does anything; it can be removed and replaced ". |
43 | | - "with its arguments."); |
44 | | - return $child; |
| 93 | + final public function hasChildren() { |
| 94 | + if ($this->children) { |
| 95 | + $this->children = $this->reduceChildren($this->children); |
| 96 | + } |
| 97 | + return (bool)$this->children; |
45 | 98 | } |
46 | 99 |
|
47 | | - final protected function isEmptyContent($content) { |
48 | | - if (is_array($content)) { |
49 | | - foreach ($content as $element) { |
50 | | - if (!$this->isEmptyContent($element)) { |
51 | | - return false; |
| 100 | + |
| 101 | + /** |
| 102 | + * Reduce effectively-empty lists of children to be actually empty. This |
| 103 | + * recursively removes `null`, `''`, and `array()` from the list of children |
| 104 | + * so that @{method:hasChildren} can more effectively align with expectations. |
| 105 | + * |
| 106 | + * NOTE: Because View children are not rendered, a View which renders down |
| 107 | + * to nothing will not be reduced by this method. |
| 108 | + * |
| 109 | + * @param list<wild> Renderable children. |
| 110 | + * @return list<wild> Reduced list of children. |
| 111 | + * @task children |
| 112 | + */ |
| 113 | + private function reduceChildren(array $children) { |
| 114 | + foreach ($children as $key => $child) { |
| 115 | + if ($child === null) { |
| 116 | + unset($children[$key]); |
| 117 | + } else if ($child === '') { |
| 118 | + unset($children[$key]); |
| 119 | + } else if (is_array($child)) { |
| 120 | + $child = $this->reduceChildren($child); |
| 121 | + if ($child) { |
| 122 | + $children[$key] = $child; |
| 123 | + } else { |
| 124 | + unset($children[$key]); |
52 | 125 | } |
53 | 126 | } |
54 | | - return true; |
55 | | - } else { |
56 | | - return !strlen((string)$content); |
57 | 127 | } |
| 128 | + return $children; |
58 | 129 | } |
59 | 130 |
|
| 131 | + |
| 132 | +/* -( Rendering )---------------------------------------------------------- */ |
| 133 | + |
| 134 | + |
60 | 135 | abstract public function render(); |
61 | 136 |
|
| 137 | + |
| 138 | +/* -( PhutilSafeHTMLProducerInterface )------------------------------------ */ |
| 139 | + |
| 140 | + |
62 | 141 | public function producePhutilSafeHTML() { |
63 | 142 | return $this->render(); |
64 | 143 | } |
|
0 commit comments