Passed
Push — main ( a7f090...08efad )
by Thierry
02:01
created

DialogPlugin::getJsCode()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 8
nc 2
nop 0
dl 0
loc 14
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * DialogPlugin.php - modal, alert and confirm dialogs for Jaxon.
5
 *
6
 * Show modal, alert and confirm dialogs with various javascript libraries.
7
 * This class generates js ans css code for dialog libraries.
8
 *
9
 * @package jaxon-core
10
 * @author Thierry Feuzeu <[email protected]>
11
 * @copyright 2016 Thierry Feuzeu <[email protected]>
12
 * @license https://opensource.org/licenses/BSD-3-Clause BSD 3-Clause License
13
 * @link https://github.com/jaxon-php/jaxon-core
14
 */
15
16
namespace Jaxon\Dialogs;
17
18
use Jaxon\App\Config\ConfigManager;
19
use Jaxon\App\I18n\Translator;
20
use Jaxon\Dialogs\Dialog\AbstractLibrary;
21
use Jaxon\Dialogs\Dialog\LibraryHelper;
22
use Jaxon\Exception\SetupException;
23
use Jaxon\Plugin\AbstractPlugin;
24
use Jaxon\Plugin\Code\JsCode;
25
use Closure;
26
27
use function array_filter;
28
use function array_map;
29
use function count;
30
use function implode;
31
use function json_encode;
32
use function trim;
33
34
class DialogPlugin extends AbstractPlugin
35
{
36
    /**
37
     * @const The plugin name
38
     */
39
    const NAME = 'dialog_code';
40
41
    /**
42
     * @var array
43
     */
44
    protected $aLibraries = null;
45
46
    /**
47
     * The constructor
48
     *
49
     * @param ConfigManager $xConfigManager
50
     * @param Translator $xTranslator
51
     * @param DialogManager $xDialogManager
52
     */
53
    public function __construct(private ConfigManager $xConfigManager,
54
        private Translator $xTranslator, private DialogManager $xDialogManager)
55
    {}
56
57
    /**
58
     * @inheritDoc
59
     */
60
    public function getName(): string
61
    {
62
        return self::NAME;
63
    }
64
65
    /**
66
     * @inheritDoc
67
     */
68
    public function getHash(): string
69
    {
70
        // The version number is used as hash
71
        return '5.0.0';
72
    }
73
74
    /**
75
     * @return AbstractLibrary[]
76
     */
77
    private function getLibraries(): array
78
    {
79
        return $this->aLibraries ?: $this->aLibraries = $this->xDialogManager->getLibraries();
80
    }
81
82
    /**
83
     * @return LibraryHelper[]
84
     */
85
    private function getHelpers(): array
86
    {
87
        return array_map(fn($xLibrary) => $xLibrary->helper(), $this->getLibraries());
88
    }
89
90
    /**
91
     * @param array $aCodes
92
     *
93
     * @return string
94
     */
95
    private function getCode(array $aCodes): string
96
    {
97
        $aCodes = array_filter($aCodes, fn($sScript) => $sScript !== '');
98
        return implode("\n", $aCodes);
99
    }
100
101
    /**
102
     * @param Closure $fGetCode
103
     *
104
     * @return string
105
     */
106
    private function getLibCodes(Closure $fGetCode): string
107
    {
108
        return $this->getCode(array_map($fGetCode, $this->getLibraries()));
109
    }
110
111
    /**
112
     * @inheritDoc
113
     */
114
    public function getJs(): string
115
    {
116
        return $this->getLibCodes(fn($xLibrary) => trim($xLibrary->getJs()));
117
    }
118
119
    /**
120
     * @inheritDoc
121
     */
122
    public function getCss(): string
123
    {
124
        return $this->getLibCodes(fn($xLibrary) => trim($xLibrary->getCss()));
125
    }
126
127
    /**
128
     * @inheritDoc
129
     */
130
    public function getScript(): string
131
    {
132
        return $this->getLibCodes(fn($xLibrary) => trim($xLibrary->getScript()));
133
    }
134
135
    /**
136
     * @inheritDoc
137
     */
138
    private function getConfigScript(): string
139
    {
140
        $aOptions = [
141
            'labels' => $this->xTranslator->translations('labels'),
142
            'defaults' => $this->xConfigManager->getAppOption('dialogs.default', []),
143
        ];
144
        $aLibrariesOptions = [];
145
        foreach($this->getLibraries() as $xLibrary)
146
        {
147
            $aLibOptions = $xLibrary->helper()->getJsOptions();
148
            if(count($aLibOptions) > 0)
149
            {
150
                $aLibrariesOptions[$xLibrary->getName()] = $aLibOptions;
151
            }
152
        }
153
        if(count($aLibrariesOptions) > 0)
154
        {
155
            $aOptions['options'] = $aLibrariesOptions;
156
        }
157
        return "jaxon.dialog.config(" . json_encode($aOptions) . ");\n\n";
158
    }
159
160
    /**
161
     * @inheritDoc
162
     * @throws SetupException
163
     */
164
    public function getJsCode(): JsCode
165
    {
166
        $xJsCode = new JsCode();
167
        $xJsCode->sJsBefore = $this->getConfigScript();
168
169
        $aCodes = [];
170
        foreach($this->getHelpers() as $xHelper)
171
        {
172
            $aCodes[] = $xHelper->getScript();
173
            $xJsCode->aFiles = array_merge($xJsCode->aFiles, $xHelper->getFiles());
174
        }
175
        $xJsCode->sJs = $this->getCode($aCodes);
176
177
        return $xJsCode;
178
    }
179
}
180