Passed
Pull Request — master (#81)
by Dante
01:03
created

Paths::setupPlugins()   A

Complexity

Conditions 3
Paths 1

Size

Total Lines 32
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 22
c 1
b 0
f 0
nc 1
nop 2
dl 0
loc 32
rs 9.568
1
<?php
2
declare(strict_types=1);
3
4
/**
5
 * BEdita, API-first content management framework
6
 * Copyright 2023 Atlas Srl, Chialab Srl
7
 *
8
 * This file is part of BEdita: you can redistribute it and/or modify
9
 * it under the terms of the GNU Lesser General Public License as published
10
 * by the Free Software Foundation, either version 3 of the License, or
11
 * (at your option) any later version.
12
 *
13
 * See LICENSE.LGPL or <http://gnu.org/licenses/lgpl-3.0.html> for more details.
14
 */
15
16
namespace BEdita\I18n\Filesystem;
17
18
use Cake\Core\App;
19
use Cake\Core\Plugin;
20
use Cake\Utility\Hash;
21
use Cake\View\View;
22
23
class Paths
24
{
25
    /**
26
     * Setup template and locale paths
27
     *
28
     * @param array $templatePaths Template paths
29
     * @param string $localePath Locale path
30
     * @param string $defaultDomain Default domain
31
     * @param array $options Options
32
     * @return void
33
     */
34
    public static function setup(
35
        array &$templatePaths,
36
        string &$localePath,
37
        string &$defaultDomain,
38
        array $options
39
    ): void {
40
        if (Hash::get($options, 'plugins') === true) {
41
            self::setupPlugins($templatePaths, $localePath);
42
43
            return;
44
        }
45
        if (Hash::get($options, 'plugin') !== null) {
46
            self::setupPlugin($templatePaths, $localePath, $defaultDomain, (string)Hash::get($options, 'plugin'));
47
48
            return;
49
        }
50
        $app = Hash::get($options, 'app');
51
        $basePath = $app ?? getcwd();
52
        $templatePaths = [$basePath . DS . 'src', $basePath . DS . 'config'];
53
        $templatePaths = array_merge($templatePaths, App::path(View::NAME_TEMPLATE));
54
        $templatePaths = array_filter($templatePaths, function ($path) {
55
            return strpos($path, 'plugins') === false;
56
        });
57
        $localePath = (string)Hash::get((array)App::path('locales'), 0);
58
    }
59
60
    /**
61
     * Setup template paths and locale path for a plugin
62
     *
63
     * @param array $templatePaths Template paths
64
     * @param string $localePath Locale path
65
     * @param string $defaultDomain Default domain
66
     * @param string $plugin The plugin name
67
     * @return void
68
     */
69
    public static function setupPlugin(
70
        array &$templatePaths,
71
        string &$localePath,
72
        string &$defaultDomain,
73
        string $plugin
74
    ): void {
75
        $templatePaths = array_merge(
76
            [
77
                Plugin::classPath($plugin),
78
                Plugin::configPath($plugin),
79
            ],
80
            App::path(View::NAME_TEMPLATE, $plugin)
81
        );
82
        $defaultDomain = $plugin;
83
        $localePath = (string)Hash::get((array)App::path('locales', $plugin), '0');
84
    }
85
86
    /**
87
     * Setup template paths and locale path for all plugins
88
     *
89
     * @param array $templatePaths Template paths
90
     * @param string $localePath Locale path
91
     * @return void
92
     */
93
    public static function setupPlugins(array &$templatePaths, string &$localePath): void
94
    {
95
        $pluginsPaths = App::path('plugins');
96
        $plugins = array_reduce(
97
            $pluginsPaths,
98
            fn (array $acc, string $path) => array_merge(
99
                $acc,
100
                array_filter(
101
                    (array)scandir($path),
102
                    fn ($file) => is_string($file) && !in_array($file, ['.', '..']) && Plugin::getCollection()->has($file)
103
                )
104
            ),
105
            []
106
        );
107
        $templatePathsTmp = App::path('templates');
108
        $templatePathsTmp[] = APP;
109
        $templatePathsTmp[] = dirname(APP) . DS . 'config';
110
        $templatePathsTmp = array_reduce(
111
            $plugins,
112
            fn (array $acc, string $plugin) => array_merge(
113
                $acc,
114
                App::path('templates', $plugin),
115
                [
116
                    Plugin::classPath($plugin),
117
                    dirname(Plugin::classPath($plugin)) . DS . 'config',
118
                ],
119
            ),
120
            $templatePathsTmp
121
        );
122
        $templatePaths = $templatePathsTmp;
123
        $localesPaths = (array)App::path('locales');
124
        $localePath = (string)Hash::get($localesPaths, 0);
125
    }
126
}
127