|
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
|
|
|
/** |
|
24
|
|
|
* Paths utility class. |
|
25
|
|
|
* |
|
26
|
|
|
* Setup template and locale paths. |
|
27
|
|
|
*/ |
|
28
|
|
|
class Paths |
|
29
|
|
|
{ |
|
30
|
|
|
/** |
|
31
|
|
|
* Setup template and locale paths. |
|
32
|
|
|
* If `plugins` option is true, setup paths for all plugins. |
|
33
|
|
|
* If `plugin` option is set, setup paths for that plugin. |
|
34
|
|
|
* Otherwise setup paths for the app. |
|
35
|
|
|
* |
|
36
|
|
|
* @param array $templatePaths Template paths |
|
37
|
|
|
* @param string $localePath Locale path |
|
38
|
|
|
* @param string $defaultDomain Default domain |
|
39
|
|
|
* @param array $options Options |
|
40
|
|
|
* @return void |
|
41
|
|
|
*/ |
|
42
|
|
|
public static function setup( |
|
43
|
|
|
array &$templatePaths, |
|
44
|
|
|
string &$localePath, |
|
45
|
|
|
string &$defaultDomain, |
|
46
|
|
|
array $options |
|
47
|
|
|
): void { |
|
48
|
|
|
if (Hash::get($options, 'plugins') === true) { |
|
49
|
|
|
self::setupPlugins($templatePaths, $localePath); |
|
50
|
|
|
|
|
51
|
|
|
return; |
|
52
|
|
|
} |
|
53
|
|
|
if (Hash::get($options, 'plugin') !== null) { |
|
54
|
|
|
self::setupPlugin($templatePaths, $localePath, $defaultDomain, (string)Hash::get($options, 'plugin')); |
|
55
|
|
|
|
|
56
|
|
|
return; |
|
57
|
|
|
} |
|
58
|
|
|
$app = Hash::get($options, 'app'); |
|
59
|
|
|
$basePath = $app ?? getcwd(); |
|
60
|
|
|
$templatePaths = [$basePath . DS . 'src', $basePath . DS . 'config']; |
|
61
|
|
|
$templatePaths = array_merge($templatePaths, App::path(View::NAME_TEMPLATE)); |
|
62
|
|
|
$templatePaths = array_filter($templatePaths, function ($path) { |
|
63
|
|
|
return strpos($path, 'plugins') === false; |
|
64
|
|
|
}); |
|
65
|
|
|
$localePath = (string)Hash::get((array)App::path('locales'), 0); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* Setup template paths and locale path for a plugin. |
|
70
|
|
|
* |
|
71
|
|
|
* @param array $templatePaths Template paths |
|
72
|
|
|
* @param string $localePath Locale path |
|
73
|
|
|
* @param string $defaultDomain Default domain |
|
74
|
|
|
* @param string $plugin The plugin name |
|
75
|
|
|
* @return void |
|
76
|
|
|
*/ |
|
77
|
|
|
public static function setupPlugin( |
|
78
|
|
|
array &$templatePaths, |
|
79
|
|
|
string &$localePath, |
|
80
|
|
|
string &$defaultDomain, |
|
81
|
|
|
string $plugin |
|
82
|
|
|
): void { |
|
83
|
|
|
$templatePaths = array_merge( |
|
84
|
|
|
[ |
|
85
|
|
|
Plugin::classPath($plugin), |
|
86
|
|
|
Plugin::configPath($plugin), |
|
87
|
|
|
], |
|
88
|
|
|
App::path(View::NAME_TEMPLATE, $plugin) |
|
89
|
|
|
); |
|
90
|
|
|
$defaultDomain = $plugin; |
|
91
|
|
|
$localePath = (string)Hash::get((array)App::path('locales', $plugin), '0'); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* Setup template paths and locale path for all plugins. |
|
96
|
|
|
* |
|
97
|
|
|
* @param array $templatePaths Template paths |
|
98
|
|
|
* @param string $localePath Locale path |
|
99
|
|
|
* @return void |
|
100
|
|
|
*/ |
|
101
|
|
|
public static function setupPlugins(array &$templatePaths, string &$localePath): void |
|
102
|
|
|
{ |
|
103
|
|
|
$pluginsPaths = App::path('plugins'); |
|
104
|
|
|
$plugins = array_reduce( |
|
105
|
|
|
$pluginsPaths, |
|
106
|
|
|
fn (array $acc, string $path) => array_merge( |
|
107
|
|
|
$acc, |
|
108
|
|
|
array_filter( |
|
109
|
|
|
(array)scandir($path), |
|
110
|
|
|
fn ($file) => is_string($file) && !in_array($file, ['.', '..']) && Plugin::getCollection()->has($file) |
|
111
|
|
|
) |
|
112
|
|
|
), |
|
113
|
|
|
[] |
|
114
|
|
|
); |
|
115
|
|
|
$templatePathsTmp = App::path('templates'); |
|
116
|
|
|
$templatePathsTmp[] = APP; |
|
117
|
|
|
$templatePathsTmp[] = dirname(APP) . DS . 'config'; |
|
118
|
|
|
$templatePathsTmp = array_reduce( |
|
119
|
|
|
$plugins, |
|
120
|
|
|
fn (array $acc, string $plugin) => array_merge( |
|
121
|
|
|
$acc, |
|
122
|
|
|
App::path('templates', $plugin), |
|
123
|
|
|
[ |
|
124
|
|
|
Plugin::classPath($plugin), |
|
125
|
|
|
dirname(Plugin::classPath($plugin)) . DS . 'config', |
|
126
|
|
|
], |
|
127
|
|
|
), |
|
128
|
|
|
$templatePathsTmp |
|
129
|
|
|
); |
|
130
|
|
|
$templatePaths = $templatePathsTmp; |
|
131
|
|
|
$localesPaths = (array)App::path('locales'); |
|
132
|
|
|
$localePath = (string)Hash::get($localesPaths, 0); |
|
133
|
|
|
} |
|
134
|
|
|
} |
|
135
|
|
|
|