ViewTrait::getHtmlAttrHelper()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Jaxon\Di\Traits;
4
5
use Jaxon\App\Config\ConfigManager;
6
use Jaxon\Plugin\Response\Dialog\DialogCommand;
7
use Jaxon\Plugin\Response\Dialog\Library\AlertLibrary;
8
use Jaxon\Plugin\Response\Dialog\Library\DialogLibraryHelper;
9
use Jaxon\Plugin\Response\Dialog\DialogManager;
10
use Jaxon\Plugin\Response\Dialog\Library\LibraryInterface;
11
use Jaxon\Plugin\Response\Dialog\Library\MessageInterface;
12
use Jaxon\Plugin\Response\Dialog\Library\ModalInterface;
13
use Jaxon\Plugin\Response\Dialog\Library\QuestionInterface;
14
use Jaxon\App\I18n\Translator;
15
use Jaxon\App\View\HtmlAttrHelper;
16
use Jaxon\App\View\TemplateView;
17
use Jaxon\App\View\ViewRenderer;
18
use Jaxon\Di\ClassContainer;
19
use Jaxon\Di\Container;
20
use Jaxon\Utils\Template\TemplateEngine;
21
22
use function call_user_func;
23
use function rtrim;
24
use function trim;
25
26
trait ViewTrait
27
{
28
    /**
29
     * Register the values into the container
30
     *
31
     * @return void
32
     */
33
    private function registerViews()
34
    {
35
        // Jaxon template view
36
        $this->set(TemplateView::class, function($di) {
0 ignored issues
show
Bug introduced by
It seems like set() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

36
        $this->/** @scrutinizer ignore-call */ 
37
               set(TemplateView::class, function($di) {
Loading history...
37
            return new TemplateView($di->g(TemplateEngine::class));
38
        });
39
        // View Renderer
40
        $this->set(ViewRenderer::class, function($di) {
41
            $xViewRenderer = new ViewRenderer($di->g(Container::class));
42
            // Add the default view renderer
43
            $xViewRenderer->addRenderer('jaxon', function($di) {
44
                return $di->g(TemplateView::class);
45
            });
46
            $sTemplateDir = rtrim(trim($di->g('jaxon.core.dir.template')), '/\\');
47
            $sPaginationDir = $sTemplateDir . DIRECTORY_SEPARATOR . 'pagination';
48
            // By default, render pagination templates with Jaxon.
49
            $xViewRenderer->addNamespace('jaxon', $sTemplateDir, '.php', 'jaxon');
50
            $xViewRenderer->addNamespace('pagination', $sPaginationDir, '.php', 'jaxon');
51
            return $xViewRenderer;
52
        });
53
54
        // Dialog library manager
55
        $this->set(DialogManager::class, function($di) {
56
            return new DialogManager($di->g(Container::class), $di->g(ConfigManager::class), $di->g(Translator::class));
57
        });
58
        $this->set(DialogCommand::class, function($di) {
59
            return new DialogCommand($di->g(DialogManager::class));
60
        });
61
        $this->val(AlertLibrary::class, new AlertLibrary());
0 ignored issues
show
Bug introduced by
It seems like val() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

61
        $this->/** @scrutinizer ignore-call */ 
62
               val(AlertLibrary::class, new AlertLibrary());
Loading history...
62
63
        // Helpers for HTML custom attributes formatting
64
        $this->set(HtmlAttrHelper::class, function($di) {
65
            return new HtmlAttrHelper($di->g(ClassContainer::class));
66
        });
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,
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...
Bug introduced by
$this of type Jaxon\Di\Traits\ViewTrait is incompatible with the type Jaxon\Plugin\Response\Di...ibrary\LibraryInterface expected by parameter $xDialogLibrary of Jaxon\Plugin\Response\Di...ryHelper::__construct(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

82
                $this->xHelper = new DialogLibraryHelper(/** @scrutinizer ignore-type */ $this,
Loading history...
83
                    $di->g(ConfigManager::class), $di->g(TemplateEngine::class));
84
            };
85
            // Can now access protected attributes
86
            $xLibrary = $di->make($sClass);
87
            call_user_func($cSetter->bindTo($xLibrary, $xLibrary));
88
            return $xLibrary;
89
        });
90
        // Set the alias, so the libraries can be found by their names.
91
        $this->alias("dialog_library_$sLibraryName", $sClass);
0 ignored issues
show
Bug introduced by
It seems like alias() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

91
        $this->/** @scrutinizer ignore-call */ 
92
               alias("dialog_library_$sLibraryName", $sClass);
Loading history...
92
    }
93
94
    /**
95
     * Get a dialog library
96
     *
97
     * @param string $sLibraryName
98
     *
99
     * @return LibraryInterface
100
     */
101
    public function getDialogLibrary(string $sLibraryName): LibraryInterface
102
    {
103
        return $this->g("dialog_library_$sLibraryName");
0 ignored issues
show
Bug introduced by
It seems like g() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

103
        return $this->/** @scrutinizer ignore-call */ g("dialog_library_$sLibraryName");
Loading history...
104
    }
105
106
    /**
107
     * Get the QuestionInterface library
108
     *
109
     * @param string $sLibraryName
110
     *
111
     * @return QuestionInterface
112
     */
113
    public function getQuestionLibrary(string $sLibraryName): QuestionInterface
114
    {
115
        $sKey = "dialog_library_$sLibraryName";
116
        return $this->h($sKey) ? $this->g($sKey) : $this->g(AlertLibrary::class);
0 ignored issues
show
Bug introduced by
It seems like h() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

116
        return $this->/** @scrutinizer ignore-call */ h($sKey) ? $this->g($sKey) : $this->g(AlertLibrary::class);
Loading history...
117
    }
118
119
    /**
120
     * Get the MessageInterface library
121
     *
122
     * @param string $sLibraryName
123
     *
124
     * @return MessageInterface
125
     */
126
    public function getMessageLibrary(string $sLibraryName): MessageInterface
127
    {
128
        $sKey = "dialog_library_$sLibraryName";
129
        return $this->h($sKey) ? $this->g($sKey) : $this->g(AlertLibrary::class);
130
    }
131
132
    /**
133
     * Get the ModalInterface library
134
     *
135
     * @param string $sLibraryName
136
     *
137
     * @return ModalInterface|null
138
     */
139
    public function getModalLibrary(string $sLibraryName): ?ModalInterface
140
    {
141
        $sKey = "dialog_library_$sLibraryName";
142
        return $this->h($sKey) ? $this->g($sKey) : null;
143
    }
144
145
    /**
146
     * Get the dialog library manager
147
     *
148
     * @return DialogManager
149
     */
150
    public function getDialogManager(): DialogManager
151
    {
152
        return $this->g(DialogManager::class);
153
    }
154
155
    /**
156
     * Get the dialog manager
157
     *
158
     * @return DialogCommand
159
     */
160
    public function getDialogCommand(): DialogCommand
161
    {
162
        return $this->g(DialogCommand::class);
163
    }
164
165
    /**
166
     * Get the view renderer
167
     *
168
     * @return ViewRenderer
169
     */
170
    public function getViewRenderer(): ViewRenderer
171
    {
172
        return $this->g(ViewRenderer::class);
173
    }
174
175
    /**
176
     * Get the custom attributes helper
177
     *
178
     * @return HtmlAttrHelper
179
     */
180
    public function getHtmlAttrHelper(): HtmlAttrHelper
181
    {
182
        return $this->g(HtmlAttrHelper::class);
183
    }
184
}
185