|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Gestion des modules |
|
4
|
|
|
* |
|
5
|
|
|
* @package Evarisk\Plugin |
|
6
|
|
|
*/ |
|
7
|
|
|
|
|
8
|
|
|
namespace eoxia; |
|
9
|
|
|
|
|
10
|
|
|
if ( ! defined( 'ABSPATH' ) ) { |
|
11
|
|
|
exit; |
|
12
|
|
|
} |
|
13
|
|
|
|
|
14
|
|
|
if ( ! class_exists( '\eoxia\Module_Util' ) ) { |
|
15
|
|
|
/** |
|
16
|
|
|
* Gestion des modules |
|
17
|
|
|
* |
|
18
|
|
|
* @author Jimmy Latour <[email protected]> |
|
19
|
|
|
* @version 1.1.0.0 |
|
20
|
|
|
*/ |
|
21
|
|
|
class Module_Util extends \eoxia\Singleton_Util { |
|
22
|
|
|
/** |
|
23
|
|
|
* Le constructeur obligatoirement pour utiliser la classe \eoxia\Singleton_Util |
|
24
|
|
|
* |
|
25
|
|
|
* @return void nothing |
|
26
|
|
|
*/ |
|
27
|
|
|
protected function construct() {} |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* Parcours le fichier digirisk.config.json pour récupérer les chemins vers tous les modules. |
|
31
|
|
|
* Initialise ensuite un par un, tous ses modules. |
|
32
|
|
|
* |
|
33
|
|
|
* @return mixed WP_Error si les configs de DigiRisk ne sont pas initialisés, ou si aucun module n'est présent. |
|
34
|
|
|
*/ |
|
35
|
|
|
public function exec_module( $path, $plugin_slug ) { |
|
36
|
|
|
if ( empty( \eoxia\Config_Util::$init[ $plugin_slug ] ) ) { |
|
37
|
|
|
return new \WP_Error( 'broke', sptrinf( __( 'Les configurations de base de %s ne sont pas initialisées', $plugin_slug ), $plugin_slug ) ); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
if ( empty( \eoxia\Config_Util::$init[ $plugin_slug ]->modules ) ) { |
|
41
|
|
|
return new \WP_Error( 'broke', __( 'Aucun module a charger', $plugin_slug ) ); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
if ( ! empty( \eoxia\Config_Util::$init[ $plugin_slug ]->modules ) ) { |
|
45
|
|
|
foreach ( \eoxia\Config_Util::$init[ $plugin_slug ]->modules as $module_json_path ) { |
|
46
|
|
|
self::inc_config_module( $plugin_slug, $path . $module_json_path ); |
|
47
|
|
|
self::inc_module( $plugin_slug, $path . $module_json_path ); |
|
48
|
|
|
} |
|
49
|
|
|
} |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* Appelle la méthode init_config de \eoxia\Config_Util pour initialiser les configs du module |
|
54
|
|
|
* |
|
55
|
|
|
* @param string $module_json_path Le chemin vers le dossier du module. |
|
56
|
|
|
* @return void nothing |
|
57
|
|
|
*/ |
|
58
|
|
|
public function inc_config_module( $plugin_slug, $module_json_path ) { |
|
59
|
|
|
\eoxia\Config_Util::g()->init_config( $module_json_path, $plugin_slug ); |
|
|
|
|
|
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* Inclus les dépendences du module (qui sont défini dans le config.json du module en question) |
|
64
|
|
|
* |
|
65
|
|
|
* @param string $module_json_path Le chemin vers le module. |
|
66
|
|
|
* @return void nothing |
|
67
|
|
|
*/ |
|
68
|
|
|
public function inc_module( $plugin_slug, $module_json_path ) { |
|
69
|
|
|
$module_name = basename( $module_json_path, '.config.json' ); |
|
70
|
|
|
$module_path = dirname( $module_json_path ); |
|
71
|
|
|
|
|
72
|
|
|
if ( ! isset( \eoxia\Config_Util::$init[ $plugin_slug ]->$module_name->state ) || ( isset( \eoxia\Config_Util::$init[ $plugin_slug ]->$module_name->state ) && \eoxia\Config_Util::$init[ $plugin_slug ]->$module_name->state ) ) { |
|
73
|
|
View Code Duplication |
if ( ! empty( \eoxia\Config_Util::$init[ $plugin_slug ]->$module_name->dependencies ) ) { |
|
|
|
|
|
|
74
|
|
|
foreach ( \eoxia\Config_Util::$init[ $plugin_slug ]->$module_name->dependencies as $dependence_folder => $list_option ) { |
|
75
|
|
|
$path_to_module_and_dependence_folder = $module_path . '/' . $dependence_folder . '/'; |
|
76
|
|
|
|
|
77
|
|
|
if ( ! empty( $list_option->priority ) ) { |
|
78
|
|
|
$this->inc_priority_file( $path_to_module_and_dependence_folder, $dependence_folder, $list_option->priority ); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
\eoxia\Include_util::g()->in_folder( $path_to_module_and_dependence_folder ); |
|
|
|
|
|
|
82
|
|
|
} |
|
83
|
|
|
} |
|
84
|
|
|
} |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* Inclus les fichiers prioritaires qui se trouvent dans la clé "priority" dans le .config.json du module |
|
89
|
|
|
* |
|
90
|
|
|
* @param string $path_to_module_and_dependence_folder Le chemin vers le module. |
|
91
|
|
|
* @param string $dependence_folder le chemin vers le dossier à inclure. |
|
92
|
|
|
* @param array $list_priority_file La liste des chemins des fichiers à inclure en priorité. |
|
93
|
|
|
* @return void nothing |
|
94
|
|
|
*/ |
|
95
|
|
View Code Duplication |
public function inc_priority_file( $path_to_module_and_dependence_folder, $dependence_folder, $list_priority_file ) { |
|
|
|
|
|
|
96
|
|
|
if ( ! empty( $list_priority_file ) ) { |
|
97
|
|
|
foreach ( $list_priority_file as $file_name ) { |
|
98
|
|
|
$path_file = realpath( $path_to_module_and_dependence_folder . $file_name . '.' . $dependence_folder . '.php' ); |
|
99
|
|
|
|
|
100
|
|
|
require_once( $path_file ); |
|
101
|
|
|
} |
|
102
|
|
|
} |
|
103
|
|
|
} |
|
104
|
|
|
} |
|
105
|
|
|
} // End if(). |
|
|
|
|
|
|
106
|
|
|
|
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the parent class: