Repository navigation
Expand file tree
/
Copy pathAbstractView.php
More file actions
114 lines (95 loc) · 2.83 KB
/
Copy pathAbstractView.php
File metadata and controls
114 lines (95 loc) · 2.83 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
<?php
declare(strict_types=1);
namespace Atk4\Ui;
use Atk4\Core\AppScopeTrait;
use Atk4\Core\ContainerTrait;
use Atk4\Core\InitializerTrait;
use Atk4\Core\NameTrait;
use Atk4\Core\StaticAddToTrait;
use Atk4\Core\TrackableTrait;
/**
* Abstract view tree item (used only for View and Callback, you want probably to extend one of these).
*
* @property View[] $elements
*
* @method View getOwner()
*/
abstract class AbstractView
{
use AppScopeTrait;
use ContainerTrait {
add as private _add;
}
use InitializerTrait {
init as private _init;
}
use NameTrait;
use StaticAddToTrait;
use TrackableTrait;
/** @var string Default name of the element. */
public $defaultName = 'atk';
/**
* If add() method is called, but current view is not part of render tree yet,
* then arguments to add() are simply stored in this array. When the view is
* initialized by calling init() or adding into App or another initialized View,
* then add() will be re-invoked with the contents of this array.
*
* @var array
*/
protected $_addLater = [];
/** @var bool will be set to true after rendered. This is so that we don't render view twice. */
protected $_rendered = false;
// }}}
// {{{ Default init() method and add() logic
/**
* For the absence of the application, we would add a very
* simple one.
*/
protected function initDefaultApp()
{
$this->setApp(new App([
'catchExceptions' => false,
'alwaysRun' => false,
'catchRunawayCallbacks' => false,
]));
$this->getApp()->invokeInit();
}
/**
* Called when view becomes part of render tree. You can override it but avoid
* placing any "heavy processing" here.
*/
protected function init(): void
{
if (!$this->issetApp()) {
$this->initDefaultApp();
}
if ($this->name === null) {
$this->name = $this->defaultName;
}
$this->_init();
// add default objects
foreach ($this->_addLater as [$object, $args]) {
$this->add($object, $args);
}
$this->_addLater = [];
}
/**
* @param AbstractView $object
*/
public function add($object, $args = null): self
{
(self::class)::assertInstanceOf($object);
if (func_num_args() > 2) { // prevent bad usage
throw new \Error('Too many method arguments');
} elseif ($this->_rendered) {
throw new Exception('You cannot add anything into the view after it was rendered');
}
if (!$this->issetApp()) {
$this->_addLater[] = [$object, $args];
return $object;
}
// will call init() of the object
$this->_add($object, $args);
return $object;
}
}