TemplateModule   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 89
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 10
eloc 43
c 2
b 0
f 0
dl 0
loc 89
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A settings() 0 16 2
A services() 0 4 1
A onDisable() 0 9 3
A onEnable() 0 13 3
A description() 0 3 1
1
<?php
2
3
/**
4
 * This file is part of template
5
 *
6
 * For the full copyright and license information, please view the LICENSE
7
 * file that was distributed with this source code.
8
 */
9
10
declare(strict_types=1);
11
12
namespace Slick\Template;
13
14
use Dotenv\Dotenv;
15
use Slick\ModuleApi\Infrastructure\AbstractModule;
16
use Slick\ModuleApi\Infrastructure\FrontController\WebModuleInterface;
17
use function Slick\ModuleApi\importSettingsFile;
18
use function Slick\ModuleApi\mergeArrays;
19
20
/**
21
 * TemplateModule
22
 *
23
 * @package Slick\Template
24
 */
25
final class TemplateModule extends AbstractModule implements WebModuleInterface
26
{
27
    const CONFIG_MODULES_TEMPLATE_PHP = '/config/modules/template.php';
28
    private static string $defaultSettings = <<<EOS
29
<?php
30
31
/**
32
 * This file is part of template module
33
 */
34
35
return [
36
    'paths' => [dirname(__DIR__, 2) . '/templates'],
37
    'options' => [
38
        'debug' => isset(\$_ENV["APP_ENV"]) ? \$_ENV["APP_ENV"] == 'develop' : false,
39
    ],
40
    'framework' => 'bulma',
41
    'theme' => 'sandstone'
42
];
43
 
44
EOS;
45
46
    public function services(): array
47
    {
48
        $servicesFile = dirname(__DIR__) . '/config/services.php';
49
        return importSettingsFile($servicesFile);
50
    }
51
52
    /**
53
     * Get the merged settings from default and user configurations.
54
     *
55
     * @param Dotenv $dotenv The Dotenv instance.
56
     * @return array<string, mixed> The merged settings array.
57
     * @SuppressWarnings(PHPMD.UnusedFormalParameter)
58
     */
59
    public function settings(Dotenv $dotenv): array
60
    {
61
        $defaultSettings = importSettingsFile(dirname(__DIR__) . '/config/settings.php');
62
        $templateConfigPath = APP_ROOT . self::CONFIG_MODULES_TEMPLATE_PHP;
63
64
        if (!file_exists($templateConfigPath)) {
65
            return $defaultSettings;
66
        }
67
68
        $userSettings = ['template' => importSettingsFile($templateConfigPath)];
69
        $userSettings['template']['paths'] = array_merge(
70
            $userSettings['template']['paths'],
71
            $defaultSettings['template']['paths']
72
        );
73
74
        return mergeArrays($defaultSettings, $userSettings);
75
    }
76
77
    public function description(): ?string
78
    {
79
        return "Allows integration and usage of a template engine of your choice.";
80
    }
81
82
    /**
83
     * Handles the "onEnable" event.
84
     *
85
     * @param array<string, mixed> $context An optional array of context data.
86
     *
87
     * @return void
88
     * @SuppressWarnings(PHPMD.UnusedFormalParameter)
89
     */
90
    public function onEnable(array $context = []): void
91
    {
92
        $path = APP_ROOT . '/templates';
93
        if (!file_exists($path)) {
94
            mkdir($path, 0755, true);
95
        }
96
97
        $settingsFile = APP_ROOT . self::CONFIG_MODULES_TEMPLATE_PHP;
98
        if (file_exists($settingsFile)) {
99
            return;
100
        }
101
102
        file_put_contents($settingsFile, self::$defaultSettings);
103
    }
104
105
    public function onDisable(array $context = []): void
106
    {
107
        if (!$context['purge']) {
108
            return;
109
        }
110
111
        $settingsFile = APP_ROOT . self::CONFIG_MODULES_TEMPLATE_PHP;
112
        if (file_exists($settingsFile)) {
113
            unlink($settingsFile);
114
        }
115
    }
116
}
117