FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
tapper/src/Console/Component.php at master · tapperphp/tapper · GitHub
Uh oh!
There was an error while loading.
Please reload this page
.
tapperphp
/
tapper
Public
Notifications
You must be signed in to change notification settings
Fork
1
Star
7
Code
Issues
0
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
tapper
/
src
/
Console
/
Component.php
Copy path
More file actions
More file actions
Latest commit
History
History
History
176 lines (141 loc) · 5.11 KB
Breadcrumbs
tapper
/
src
/
Console
/
Component.php
Copy path
File metadata and controls
176 lines (141 loc) · 5.11 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
<?php
declare
(strict_types=
1
);
namespace
Tapper
\
Console
;
use
DI
\
Container
;
use
PhpTui
\
Term
\
MouseEventKind
;
use
PhpTui
\
Tui
\
Display
\
Area
;
use
PhpTui
\
Tui
\
Widget
\
Widget
;
use
React
\
EventLoop
\
LoopInterface
;
use
ReflectionObject
;
use
Tapper
\
Console
\
CommandAttributes
\
FirstRender
;
use
Tapper
\
Console
\
CommandAttributes
\
KeyPressed
;
use
Tapper
\
Console
\
CommandAttributes
\
Mouse
;
use
Tapper
\
Console
\
CommandAttributes
\
OnEvent
;
use
Tapper
\
Console
\
CommandAttributes
\
Periodic
;
use
Tapper
\
Console
\
Commands
\
Command
;
use
Tapper
\
Console
\
State
\
AppState
;
abstract
class
Component
{
const
BEFORE_INIT
=
'
beforeInit
'
;
const
AFTER_INIT
=
'
afterInit
'
;
protected
array
$
components
= [];
protected
array
$
componentInstances
= [];
private
bool
$
isActive
=
false
;
protected
array
$
timers
= [];
protected
?
Area
$
area
=
null
;
private
array
$
firstRenders
= [];
public
function
__construct
(
protected
readonly
LoopInterface
$
loop
,
protected
readonly
EventBus
$
eventBus
,
protected
readonly
CommandInvoker
$
commandInvoker
,
protected
readonly
Container
$
container
,
protected
readonly
AppState
$
appState
,
) {
if
(
method_exists
(
$
this
,
self
::
BEFORE_INIT
)) {
$
this
->
container
->
call
([
$
this
,
self
::
BEFORE_INIT
]);
}
$
reflection
=
new
ReflectionObject
(
$
this
);
$
methods
=
$
reflection
->
getMethods
();
foreach
(
$
methods
as
$
method
) {
$
attributes
= [
...
$
method
->
getAttributes
(KeyPressed::class),
...
$
method
->
getAttributes
(Mouse::class),
...
$
method
->
getAttributes
(OnEvent::class),
];
foreach
(
$
attributes
as
$
attribute
) {
$
methodName
=
$
method
->
getName
();
$
attribute
=
$
attribute
->
newInstance
();
$
eventBus
->
listen
(
$
attribute
->
key
,
function
(
array
$
data
)
use
(
$
attribute
,
$
methodName
) {
if
(
$
attribute
instanceof
KeyPressed
&&
$
attribute
?->keyModifiers !==
null
&&
$
attribute
?->keyModifiers !==
$
data
[
'
modifiers
'
]
) {
return
;
}
$
scrolls
= [
MouseEventKind::ScrollDown,
MouseEventKind::ScrollLeft,
MouseEventKind::ScrollRight,
MouseEventKind::ScrollUp,
];
if
(
$
attribute
instanceof
Mouse
&& !
in_array
(
$
attribute
->
key
,
$
scrolls
)
&&
$
attribute
->
button
!==
$
data
[
'
event
'
]->
button
) {
return
;
}
if
(!
$
this
->
isActive
&& !
$
attribute
->
global
) {
return
;
}
$
this
->
$
methodName
(
$
data
);
}
);
}
$
attributes
=
$
method
->
getAttributes
(Periodic::class);
foreach
(
$
attributes
as
$
attribute
) {
$
methodName
=
$
method
->
getName
();
$
attribute
=
$
attribute
->
newInstance
();
$
this
->
timers
[] =
$
loop
->
addPeriodicTimer
(
$
attribute
->
interval
, [
$
this
,
$
methodName
]);
}
$
attributes
=
$
method
->
getAttributes
(FirstRender::class);
foreach
(
$
attributes
as
$
attribute
) {
$
methodName
=
$
method
->
getName
();
$
attribute
=
$
attribute
->
newInstance
();
$
this
->
firstRenders
[] = [
$
this
,
$
methodName
];
}
}
$
this
->
registerComponents
();
if
(
method_exists
(
$
this
,
self
::
AFTER_INIT
)) {
$
this
->
container
->
call
([
$
this
,
self
::
AFTER_INIT
]);
}
}
private
function
registerComponents
():
void
{
foreach
(
$
this
->
components
as
$
component
) {
$
this
->
componentInstances
[
$
component
] =
$
this
->
container
->
make
(
$
component
);
}
}
protected
function
renderComponent
(
string
$
component
,
Area
$
area
):
Widget
{
return
$
this
->
getComponent
(
$
component
)->
render
(
$
area
);
}
protected
function
getComponent
(
string
$
component
)
{
return
$
this
->
componentInstances
[
$
component
];
}
protected
function
execute
(
Command
$
command
):
mixed
{
return
$
this
->
commandInvoker
->
invoke
(
$
command
);
}
public
function
isActive
():
bool
{
return
$
this
->
isActive
;
}
public
function
activate
():
void
{
$
this
->
isActive
=
true
;
}
public
function
deactivate
():
void
{
$
this
->
isActive
=
false
;
}
public
function
render
(
Area
$
area
):
Widget
{
if
(
$
this
->
area
===
null
) {
$
this
->
area
=
$
area
;
$
this
->
callFirstRenders
();
}
$
this
->
area
=
$
area
;
return
$
this
->
view
(
$
area
);
}
abstract
protected
function
view
(
Area
$
area
):
Widget
;
private
function
callFirstRenders
():
void
{
foreach
(
$
this
->
firstRenders
as
$
firstRender
) {
$
firstRender
();
}
}
}
Back
|
FazBrowse Home
|
New Git URL