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