AppTrait::setLogger()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 3
rs 10
1
<?php
2
3
/**
4
 * AppTrait.php
5
 *
6
 * Jaxon application
7
 *
8
 * @package jaxon-core
9
 * @author Thierry Feuzeu <[email protected]>
10
 * @copyright 2022 Thierry Feuzeu <[email protected]>
11
 * @license https://opensource.org/licenses/BSD-3-Clause BSD 3-Clause License
12
 * @link https://github.com/jaxon-php/jaxon-core
13
 */
14
15
namespace Jaxon\App\Ajax;
16
17
use Jaxon\App\Ajax\Bootstrap;
18
use Jaxon\App\Ajax\Lib;
19
use Jaxon\App\Config\ConfigManager;
20
use Jaxon\App\I18n\Translator;
21
use Jaxon\Exception\RequestException;
22
use Jaxon\Plugin\Manager\PluginManager;
23
use Jaxon\Plugin\Request\CallableClass\CallableRegistry;
24
use Jaxon\Response\AjaxResponse;
25
use Jaxon\Response\ResponseManager;
26
use Psr\Container\ContainerInterface;
27
use Psr\Log\LoggerInterface;
28
use Closure;
29
30
trait AppTrait
31
{
32
    use LibTrait;
33
34
    /**
35
     * @return void
36
     */
37
    private function initApp()
38
    {
39
        $this->xContainer = Lib::getInstance()->di();
40
        $this->xClassContainer = Lib::getInstance()->cls();
41
        // Set the attributes from the container
42
        $this->xConfigManager = $this->xContainer->g(ConfigManager::class);
43
        $this->xResponseManager = $this->xContainer->g(ResponseManager::class);
44
        $this->xPluginManager = $this->xContainer->g(PluginManager::class);
45
        $this->xCallableRegistry = $this->xContainer->g(CallableRegistry::class);
46
        $this->xTranslator = $this->xContainer->g(Translator::class);
47
    }
48
49
    /**
50
     * Get the Jaxon application bootstrapper.
51
     *
52
     * @return Bootstrap
53
     */
54
    protected function bootstrap(): Bootstrap
55
    {
56
        return $this->di()->getBootstrap();
57
    }
58
59
    /**
60
     * Set the ajax endpoint URI
61
     *
62
     * @param string $sUri    The ajax endpoint URI
63
     *
64
     * @return void
65
     */
66
    public function uri(string $sUri)
67
    {
68
        $this->setOption('core.request.uri', $sUri);
69
    }
70
71
    /**
72
     * Set the javascript asset
73
     *
74
     * @param bool $bExport    Whether to export the js code in a file
75
     * @param bool $bMinify    Whether to minify the exported js file
76
     * @param string $sUri    The URI to access the js file
77
     * @param string $sDir    The directory where to create the js file
78
     *
79
     * @return void
80
     */
81
    public function asset(bool $bExport, bool $bMinify, string $sUri = '', string $sDir = '')
82
    {
83
        $this->bootstrap()->asset($bExport, $bMinify, $sUri, $sDir);
84
    }
85
86
    /**
87
     * Read the options from the file, if provided, and return the config
88
     *
89
     * @param string $sConfigFile The full path to the config file
90
     * @param string $sConfigSection The section of the config file to be loaded
91
     *
92
     * @return ConfigManager
93
     */
94
    public function config(string $sConfigFile = '', string $sConfigSection = ''): ConfigManager
0 ignored issues
show
Unused Code introduced by
The parameter $sConfigFile is not used and could be removed. ( Ignorable by Annotation )

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

94
    public function config(/** @scrutinizer ignore-unused */ string $sConfigFile = '', string $sConfigSection = ''): ConfigManager

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $sConfigSection is not used and could be removed. ( Ignorable by Annotation )

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

94
    public function config(string $sConfigFile = '', /** @scrutinizer ignore-unused */ string $sConfigSection = ''): ConfigManager

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
95
    {
96
        return $this->xConfigManager;
97
    }
98
99
    /**
100
     * Get the value of an application config option
101
     *
102
     * @param string $sName The option name
103
     * @param mixed $xDefault The default value, to be returned if the option is not defined
104
     *
105
     * @return mixed
106
     */
107
    public function getAppOption(string $sName, $xDefault = null)
108
    {
109
        return $this->xConfigManager->getAppOption($sName, $xDefault);
110
    }
111
112
    /**
113
     * Check the presence of an application config option
114
     *
115
     * @param string $sName The option name
116
     *
117
     * @return bool
118
     */
119
    public function hasAppOption(string $sName): bool
120
    {
121
        return $this->xConfigManager->hasAppOption($sName);
122
    }
123
124
    /**
125
     * Get the HTTP response
126
     *
127
     * @param string $sCode    The HTTP response code
128
     *
129
     * @return mixed
130
     */
131
    abstract public function httpResponse(string $sCode = '200');
132
133
    /**
134
     * Get the Jaxon ajax response
135
     *
136
     * @return AjaxResponse
137
     */
138
    public function ajaxResponse(): AjaxResponse
139
    {
140
        return $this->xResponseManager->getResponse();
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->xResponseManager->getResponse() returns the type Jaxon\Response\AbstractResponse which includes types incompatible with the type-hinted return Jaxon\Response\AjaxResponse.
Loading history...
141
    }
142
143
    /**
144
     * Process an incoming Jaxon request, and return the response.
145
     *
146
     * @return void
147
     * @throws RequestException
148
     */
149
    public function processRequest()
150
    {
151
        $this->di()->getRequestHandler()->processRequest();
152
    }
153
154
    /**
155
     * Set the container provided by the integrated framework
156
     *
157
     * @param ContainerInterface $xContainer    The container implementation
158
     *
159
     * @return void
160
     */
161
    public function setContainer(ContainerInterface $xContainer)
162
    {
163
        $this->di()->setContainer($xContainer);
164
    }
165
166
    /**
167
     * Add a view renderer with an id
168
     *
169
     * @param string $sRenderer    The renderer name
170
     * @param string $sExtension    The extension to append to template names
171
     * @param Closure $xClosure    A closure to create the view instance
172
     *
173
     * @return void
174
     */
175
    public function addViewRenderer(string $sRenderer, string $sExtension, Closure $xClosure)
176
    {
177
        $this->di()->getViewRenderer()->setDefaultRenderer($sRenderer, $sExtension, $xClosure);
178
    }
179
180
    /**
181
     * Set the logger.
182
     *
183
     * @param LoggerInterface $logger
184
     *
185
     * @return void
186
     */
187
    public function setLogger(LoggerInterface $logger)
188
    {
189
        $this->di()->setLogger($logger);
190
    }
191
192
    /**
193
     * Set the session manager
194
     *
195
     * @param Closure $xClosure    A closure to create the session manager instance
196
     *
197
     * @return void
198
     */
199
    public function setSessionManager(Closure $xClosure)
200
    {
201
        $this->di()->setSessionManager($xClosure);
202
    }
203
204
    /**
205
     * @inheritDoc
206
     */
207
    public function setup(string $sConfigFile = '')
0 ignored issues
show
Unused Code introduced by
The parameter $sConfigFile is not used and could be removed. ( Ignorable by Annotation )

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

207
    public function setup(/** @scrutinizer ignore-unused */ string $sConfigFile = '')

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
208
    {}
209
}
210