1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Initialise les fichiers .config.json |
4
|
|
|
* |
5
|
|
|
* @package Evarisk\Plugin |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace eoxia; |
9
|
|
|
|
10
|
|
|
if ( ! defined( 'ABSPATH' ) ) { |
11
|
|
|
exit; |
12
|
|
|
} |
13
|
|
|
|
14
|
|
|
if ( ! class_exists( '\eoxia\Config_Util' ) ) { |
15
|
|
|
/** |
16
|
|
|
* Initialise les fichiers .config.json |
17
|
|
|
* |
18
|
|
|
* @author Jimmy Latour <[email protected]> |
19
|
|
|
* @version 1.1.0.0 |
20
|
|
|
*/ |
21
|
|
|
class Config_Util extends \eoxia\Singleton_Util { |
22
|
|
|
/** |
23
|
|
|
* Un tableau contenant toutes les configurations des fichies config.json |
24
|
|
|
* |
25
|
|
|
* @var array |
26
|
|
|
*/ |
27
|
|
|
public static $init = array(); |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Le constructeur obligatoirement pour utiliser la classe \eoxia\Singleton_Util |
31
|
|
|
* |
32
|
|
|
* @return void nothing |
33
|
|
|
*/ |
34
|
|
|
protected function construct() {} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Initialise les fichiers de configuration |
38
|
|
|
* |
39
|
|
|
* @param string $path_to_config_file Le chemin vers le fichier config.json. |
40
|
|
|
* |
41
|
|
|
* @return mixed WP_Error si il ne trouve pas le fichier config du module |
42
|
|
|
*/ |
43
|
|
|
public function init_config( $path_to_config_file, $plugin_slug = '' ) { |
44
|
|
|
if ( empty( $path_to_config_file ) ) { |
45
|
|
|
return new \WP_Error( 'broke', __( 'Impossible de charger le fichier', 'digirisk' ) ); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
$tmp_config = \eoxia\JSON_Util::g()->open_and_decode( $path_to_config_file ); |
|
|
|
|
49
|
|
|
|
50
|
|
|
if ( empty( $tmp_config->slug ) ) { |
51
|
|
|
return new \WP_Error( 'broke', __( 'Le module nécessite un slug', 'digirisk' ) ); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
if ( ! empty( $plugin_slug ) ) { |
55
|
|
|
$slug = $tmp_config->slug; |
56
|
|
|
$tmp_config->path = self::$init[ $plugin_slug ]->path . $tmp_config->path; |
57
|
|
|
if ( isset( $tmp_config->external ) && ! empty( $tmp_config->external ) ) { |
58
|
|
|
self::$init['external']->$slug = $tmp_config; |
59
|
|
|
} else { |
60
|
|
|
self::$init[ $plugin_slug ]->$slug = $tmp_config; |
61
|
|
|
} |
62
|
|
|
} else { |
63
|
|
|
self::$init[ $tmp_config->slug ] = $tmp_config; |
64
|
|
|
} |
65
|
|
|
} |
66
|
|
|
} |
67
|
|
|
} // End if(). |
|
|
|
|
68
|
|
|
|
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: