1
|
|
|
<?php |
2
|
|
|
|
|
|
|
|
3
|
|
|
namespace Jaxon\Di\Traits; |
4
|
|
|
|
5
|
|
|
use Jaxon\App\Config\ConfigManager; |
6
|
|
|
use Jaxon\App\Dialog\Library\AlertLibrary; |
7
|
|
|
use Jaxon\App\Dialog\Library\DialogLibraryHelper; |
8
|
|
|
use Jaxon\App\Dialog\Library\DialogLibraryManager; |
9
|
|
|
use Jaxon\App\Dialog\LibraryInterface; |
10
|
|
|
use Jaxon\App\Dialog\MessageInterface; |
11
|
|
|
use Jaxon\App\Dialog\ModalInterface; |
12
|
|
|
use Jaxon\App\Dialog\QuestionInterface; |
13
|
|
|
use Jaxon\App\I18n\Translator; |
14
|
|
|
use Jaxon\App\View\PaginationRenderer; |
15
|
|
|
use Jaxon\App\View\TemplateView; |
16
|
|
|
use Jaxon\App\View\ViewRenderer; |
17
|
|
|
use Jaxon\Di\Container; |
18
|
|
|
use Jaxon\Request\Call\Paginator; |
19
|
|
|
use Jaxon\Utils\Template\TemplateEngine; |
20
|
|
|
|
21
|
|
|
use function call_user_func; |
22
|
|
|
use function rtrim; |
23
|
|
|
use function trim; |
24
|
|
|
|
25
|
|
|
trait ViewTrait |
|
|
|
|
26
|
|
|
{ |
27
|
|
|
/** |
28
|
|
|
* Register the values into the container |
29
|
|
|
* |
30
|
|
|
* @return void |
31
|
|
|
*/ |
32
|
|
|
private function registerViews() |
|
|
|
|
33
|
|
|
{ |
34
|
|
|
// View Renderer |
35
|
|
|
$this->set(ViewRenderer::class, function($c) { |
|
|
|
|
36
|
|
|
$xViewRenderer = new ViewRenderer($c->g(Container::class)); |
37
|
|
|
// Add the default view renderer |
38
|
|
|
$xViewRenderer->addRenderer('jaxon', function($di) { |
39
|
|
|
return new TemplateView($di->g(TemplateEngine::class)); |
40
|
|
|
}); |
41
|
|
|
$sTemplateDir = rtrim(trim($c->g('jaxon.core.dir.template')), '/\\'); |
|
|
|
|
42
|
|
|
$sPaginationDir = $sTemplateDir . DIRECTORY_SEPARATOR . 'pagination'; |
43
|
|
|
// By default, render pagination templates with Jaxon. |
44
|
|
|
$xViewRenderer->addNamespace('jaxon', $sTemplateDir, '.php', 'jaxon'); |
45
|
|
|
$xViewRenderer->addNamespace('pagination', $sPaginationDir, '.php', 'jaxon'); |
46
|
|
|
return $xViewRenderer; |
47
|
|
|
}); |
48
|
|
|
|
49
|
|
|
// Pagination Paginator |
50
|
|
|
$this->set(Paginator::class, function($c) { |
51
|
|
|
return new Paginator($c->g(PaginationRenderer::class)); |
52
|
|
|
}); |
53
|
|
|
// Pagination Renderer |
54
|
|
|
$this->set(PaginationRenderer::class, function($c) { |
55
|
|
|
return new PaginationRenderer($c->g(ViewRenderer::class)); |
56
|
|
|
}); |
57
|
|
|
|
58
|
|
|
// Dialog library manager |
59
|
|
|
$this->set(DialogLibraryManager::class, function($c) { |
60
|
|
|
return new DialogLibraryManager($c->g(Container::class), $c->g(ConfigManager::class), $c->g(Translator::class)); |
61
|
|
|
}); |
62
|
|
|
$this->val(AlertLibrary::class, new AlertLibrary()); |
|
|
|
|
63
|
|
|
} |
|
|
|
|
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Register a javascript dialog library adapter. |
67
|
|
|
* |
68
|
|
|
* @param string $sClass |
|
|
|
|
69
|
|
|
* @param string $sLibraryName |
|
|
|
|
70
|
|
|
* |
71
|
|
|
* @return void |
72
|
|
|
*/ |
73
|
|
|
public function registerDialogLibrary(string $sClass, string $sLibraryName) |
74
|
|
|
{ |
75
|
|
|
$this->set($sClass, function($c) use($sClass) { |
76
|
|
|
// Set the protected attributes of the library |
77
|
|
|
$cSetter = function() use($c) { |
78
|
|
|
$this->xHelper = new DialogLibraryHelper($this, $c->g(ConfigManager::class), $c->g(TemplateEngine::class)); |
|
|
|
|
79
|
|
|
}; |
80
|
|
|
// Can now access protected attributes |
81
|
|
|
$xLibrary = $c->make($sClass); |
82
|
|
|
call_user_func($cSetter->bindTo($xLibrary, $xLibrary)); |
83
|
|
|
return $xLibrary; |
84
|
|
|
}); |
85
|
|
|
// Set the alias, so the libraries can be found by their names. |
86
|
|
|
$this->alias("dialog_library_$sLibraryName", $sClass); |
|
|
|
|
87
|
|
|
} |
|
|
|
|
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Get a dialog library |
91
|
|
|
* |
92
|
|
|
* @param string $sLibraryName |
|
|
|
|
93
|
|
|
* |
94
|
|
|
* @return LibraryInterface |
95
|
|
|
*/ |
96
|
|
|
public function getDialogLibrary(string $sLibraryName): LibraryInterface |
97
|
|
|
{ |
98
|
|
|
return $this->g("dialog_library_$sLibraryName"); |
|
|
|
|
99
|
|
|
} |
|
|
|
|
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Get the QuestionInterface library |
103
|
|
|
* |
104
|
|
|
* @param string $sLibraryName |
|
|
|
|
105
|
|
|
* |
106
|
|
|
* @return QuestionInterface |
107
|
|
|
*/ |
108
|
|
|
public function getQuestionLibrary(string $sLibraryName): QuestionInterface |
109
|
|
|
{ |
110
|
|
|
$sKey = "dialog_library_$sLibraryName"; |
111
|
|
|
return $this->h($sKey) ? $this->g($sKey) : $this->g(AlertLibrary::class); |
|
|
|
|
112
|
|
|
} |
|
|
|
|
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Get the MessageInterface library |
116
|
|
|
* |
117
|
|
|
* @param string $sLibraryName |
|
|
|
|
118
|
|
|
* |
119
|
|
|
* @return MessageInterface |
120
|
|
|
*/ |
121
|
|
|
public function getMessageLibrary(string $sLibraryName): MessageInterface |
122
|
|
|
{ |
123
|
|
|
$sKey = "dialog_library_$sLibraryName"; |
124
|
|
|
return $this->h($sKey) ? $this->g($sKey) : $this->g(AlertLibrary::class); |
125
|
|
|
} |
|
|
|
|
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* Get the ModalInterface library |
129
|
|
|
* |
130
|
|
|
* @param string $sLibraryName |
|
|
|
|
131
|
|
|
* |
132
|
|
|
* @return ModalInterface|null |
133
|
|
|
*/ |
134
|
|
|
public function getModalLibrary(string $sLibraryName): ?ModalInterface |
135
|
|
|
{ |
136
|
|
|
$sKey = "dialog_library_$sLibraryName"; |
137
|
|
|
return $this->h($sKey) ? $this->g($sKey) : null; |
138
|
|
|
} |
|
|
|
|
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* Get the dialog library manager |
142
|
|
|
* |
143
|
|
|
* @return DialogLibraryManager |
144
|
|
|
*/ |
145
|
|
|
public function getDialogLibraryManager(): DialogLibraryManager |
146
|
|
|
{ |
147
|
|
|
return $this->g(DialogLibraryManager::class); |
148
|
|
|
} |
|
|
|
|
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* Get the view renderer |
152
|
|
|
* |
153
|
|
|
* @return ViewRenderer |
154
|
|
|
*/ |
155
|
|
|
public function getViewRenderer(): ViewRenderer |
156
|
|
|
{ |
157
|
|
|
return $this->g(ViewRenderer::class); |
158
|
|
|
} |
|
|
|
|
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* Get the paginator |
162
|
|
|
* |
163
|
|
|
* @return Paginator |
164
|
|
|
*/ |
165
|
|
|
public function getPaginator(): Paginator |
166
|
|
|
{ |
167
|
|
|
return $this->g(Paginator::class); |
168
|
|
|
} |
|
|
|
|
169
|
|
|
} |
170
|
|
|
|