Issues (2884)

src/Di/Traits/ViewTrait.php (1 issue)

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
        // Jaxon template view
35
        $this->set(TemplateView::class, function($di) {
36
            return new TemplateView($di->g(TemplateEngine::class));
37
        });
38
        // View Renderer
39
        $this->set(ViewRenderer::class, function($di) {
40
            $xViewRenderer = new ViewRenderer($di->g(Container::class));
41
            // Add the default view renderer
42
            $xViewRenderer->addRenderer('jaxon', function($di) {
43
                return $di->g(TemplateView::class);
44
            });
45
            $sTemplateDir = rtrim(trim($di->g('jaxon.core.dir.template')), '/\\');
46
            $sPaginationDir = $sTemplateDir . DIRECTORY_SEPARATOR . 'pagination';
47
            // By default, render pagination templates with Jaxon.
48
            $xViewRenderer->addNamespace('jaxon', $sTemplateDir, '.php', 'jaxon');
49
            $xViewRenderer->addNamespace('pagination', $sPaginationDir, '.php', 'jaxon');
50
            return $xViewRenderer;
51
        });
52
53
        // Pagination Paginator
54
        $this->set(Paginator::class, function($di) {
55
            return new Paginator($di->g(PaginationRenderer::class));
56
        });
57
        // Pagination Renderer
58
        $this->set(PaginationRenderer::class, function($di) {
59
            return new PaginationRenderer($di->g(ViewRenderer::class));
60
        });
61
62
        // Dialog library manager
63
        $this->set(DialogLibraryManager::class, function($di) {
64
            return new DialogLibraryManager($di->g(Container::class), $di->g(ConfigManager::class), $di->g(Translator::class));
65
        });
66
        $this->val(AlertLibrary::class, new AlertLibrary());
67
    }
68
69
    /**
70
     * Register a javascript dialog library adapter.
71
     *
72
     * @param string $sClass
73
     * @param string $sLibraryName
74
     *
75
     * @return void
76
     */
77
    public function registerDialogLibrary(string $sClass, string $sLibraryName)
78
    {
79
        $this->set($sClass, function($di) use($sClass) {
80
            // Set the protected attributes of the library
81
            $cSetter = function() use($di) {
82
                $this->xHelper = new DialogLibraryHelper($this, $di->g(ConfigManager::class), $di->g(TemplateEngine::class));
0 ignored issues
show
Bug Best Practice introduced by
The property xHelper does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
83
            };
84
            // Can now access protected attributes
85
            $xLibrary = $di->make($sClass);
86
            call_user_func($cSetter->bindTo($xLibrary, $xLibrary));
87
            return $xLibrary;
88
        });
89
        // Set the alias, so the libraries can be found by their names.
90
        $this->alias("dialog_library_$sLibraryName", $sClass);
91
    }
92
93
    /**
94
     * Get a dialog library
95
     *
96
     * @param string $sLibraryName
97
     *
98
     * @return LibraryInterface
99
     */
100
    public function getDialogLibrary(string $sLibraryName): LibraryInterface
101
    {
102
        return $this->g("dialog_library_$sLibraryName");
103
    }
104
105
    /**
106
     * Get the QuestionInterface library
107
     *
108
     * @param string $sLibraryName
109
     *
110
     * @return QuestionInterface
111
     */
112
    public function getQuestionLibrary(string $sLibraryName): QuestionInterface
113
    {
114
        $sKey = "dialog_library_$sLibraryName";
115
        return $this->h($sKey) ? $this->g($sKey) : $this->g(AlertLibrary::class);
116
    }
117
118
    /**
119
     * Get the MessageInterface library
120
     *
121
     * @param string $sLibraryName
122
     *
123
     * @return MessageInterface
124
     */
125
    public function getMessageLibrary(string $sLibraryName): MessageInterface
126
    {
127
        $sKey = "dialog_library_$sLibraryName";
128
        return $this->h($sKey) ? $this->g($sKey) : $this->g(AlertLibrary::class);
129
    }
130
131
    /**
132
     * Get the ModalInterface library
133
     *
134
     * @param string $sLibraryName
135
     *
136
     * @return ModalInterface|null
137
     */
138
    public function getModalLibrary(string $sLibraryName): ?ModalInterface
139
    {
140
        $sKey = "dialog_library_$sLibraryName";
141
        return $this->h($sKey) ? $this->g($sKey) : null;
142
    }
143
144
    /**
145
     * Get the dialog library manager
146
     *
147
     * @return DialogLibraryManager
148
     */
149
    public function getDialogLibraryManager(): DialogLibraryManager
150
    {
151
        return $this->g(DialogLibraryManager::class);
152
    }
153
154
    /**
155
     * Get the view renderer
156
     *
157
     * @return ViewRenderer
158
     */
159
    public function getViewRenderer(): ViewRenderer
160
    {
161
        return $this->g(ViewRenderer::class);
162
    }
163
164
    /**
165
     * Get the paginator
166
     *
167
     * @return Paginator
168
     */
169
    public function getPaginator(): Paginator
170
    {
171
        return $this->g(Paginator::class);
172
    }
173
}
174