class-component-abstract.php
1.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
<?php
/**
* Component abstract.
*
* @package Block_Lab
* @copyright Copyright(c) 2020, Block Lab
* @license http://opensource.org/licenses/GPL-2.0 GNU General Public License, version 2 (GPL-2.0)
*/
namespace Block_Lab;
/**
* Class ComponentAbstract
*/
abstract class Component_Abstract implements Component_Interface {
/**
* Point to the $plugin instance.
*
* @var Plugin_Interface
*/
protected $plugin;
/**
* Set the plugin so that it can be referenced later.
*
* @param Plugin_Interface $plugin The plugin.
*
* @return Component_Interface $this
*/
public function set_plugin( Plugin_Interface $plugin ) {
$this->plugin = $plugin;
return $this;
}
/**
* Handle deprecated component methods.
*
* @param string $name The name of the method called in this class.
* @param array $arguments The arguments passed to the method.
*
* @return mixed The result of calling the deprecated method, if it exists.
*
* @throws \Error Fallback to a standard PHP error.
*/
public function __call( $name, $arguments ) {
$class = get_class( $this );
$class_name = strtolower( str_replace( '\\', '__', $class ) );
$function_name = "${class_name}__${name}";
if ( function_exists( $function_name ) ) {
return call_user_func_array( $function_name, $arguments );
}
// Intentionally untranslated, to match PHP's error message.
throw new \Error( "Call to undefined method $class::$name()" );
}
/**
* Register any hooks that this component needs.
*
* @return void
*/
abstract public function register_hooks();
}