|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
/** |
|
5
|
|
|
* Abstract Class for registering single hook subscriber. |
|
6
|
|
|
* |
|
7
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
|
8
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
|
9
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
|
10
|
|
|
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
|
11
|
|
|
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
|
12
|
|
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
|
13
|
|
|
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
|
14
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
|
15
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
|
16
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
|
17
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
|
18
|
|
|
* |
|
19
|
|
|
* @author Glynn Quelch <[email protected]> |
|
20
|
|
|
* @license http://www.opensource.org/licenses/mit-license.html MIT License |
|
21
|
|
|
* @package DIPT\Hook_Subscriber |
|
22
|
|
|
*/ |
|
23
|
|
|
|
|
24
|
|
|
namespace PinkCrab\Hook_Subscriber; |
|
25
|
|
|
|
|
26
|
|
|
use InvalidArgumentException; |
|
27
|
|
|
use PinkCrab\Loader\Hook_Loader; |
|
28
|
|
|
use PinkCrab\Perique\Application\App; |
|
29
|
|
|
use PinkCrab\Hook_Subscriber\Hook_Subscriber; |
|
30
|
|
|
|
|
31
|
|
|
abstract class Abstract_Hook_Subscription implements Hook_Subscriber { |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* The hook to register the subscriber |
|
35
|
|
|
* |
|
36
|
|
|
* @var string|null |
|
37
|
|
|
*/ |
|
38
|
|
|
protected ?string $hook = null; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* Optional hook to use to register the primary hook. |
|
42
|
|
|
* |
|
43
|
|
|
* @var string|null |
|
44
|
|
|
*/ |
|
45
|
|
|
protected ?string $deferred_hook = null; |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* Hook call priority |
|
49
|
|
|
* |
|
50
|
|
|
* @var int |
|
51
|
|
|
*/ |
|
52
|
|
|
protected int $priority = 10; |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* Number of args passed. |
|
56
|
|
|
* Default set to 10 to allow execute to use spread op. |
|
57
|
|
|
* |
|
58
|
|
|
* @var int |
|
59
|
|
|
*/ |
|
60
|
|
|
protected int $args = 20; |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* Registers subscriber to the loader. |
|
64
|
|
|
* |
|
65
|
|
|
* @param Hook_Loader $loader |
|
66
|
|
|
* @return void |
|
67
|
|
|
* @throws InvalidArgumentException |
|
68
|
|
|
*/ |
|
69
|
|
|
public function register( Hook_Loader $loader ): void { |
|
70
|
|
|
|
|
71
|
|
|
if ( ! is_string( $this->hook ) || empty( $this->hook ) ) { |
|
72
|
|
|
throw new InvalidArgumentException( \sprintf( 'Hook not defined in %s subscriber', static::class ) ); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
// If we have a differed event. |
|
76
|
|
|
if ( ! empty( $this->deferred_hook ) ) { |
|
77
|
|
|
$loader->action( |
|
78
|
|
|
$this->deferred_hook, |
|
79
|
|
|
function( ...$args ) { // phpcs:disable Generic.CodeAnalysis.UnusedFunctionParameter.FoundInImplementedInterface |
|
|
|
|
|
|
80
|
|
|
|
|
81
|
|
|
// Create a new isntance using DI and register using the defined hook. |
|
82
|
|
|
$instance = App::make( static::class ); |
|
83
|
|
|
if ( is_callable( array( $instance, 'execute' ) ) ) { // Ensure valid callable |
|
84
|
|
|
\add_action( (string) $this->hook, array( $instance, 'execute' ), $this->priority, $this->args ); |
|
85
|
|
|
} |
|
86
|
|
|
}, |
|
87
|
|
|
$this->priority, |
|
88
|
|
|
$this->args |
|
89
|
|
|
); |
|
90
|
|
|
} else { |
|
91
|
|
|
$loader->action( |
|
92
|
|
|
$this->hook, |
|
93
|
|
|
array( $this, 'execute' ), |
|
94
|
|
|
$this->priority, |
|
95
|
|
|
$this->args |
|
96
|
|
|
); |
|
97
|
|
|
} |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* Callback back method. |
|
102
|
|
|
* |
|
103
|
|
|
* @param mixed ...$args |
|
104
|
|
|
* @return void |
|
105
|
|
|
*/ |
|
106
|
|
|
abstract public function execute( ...$args ): void; |
|
107
|
|
|
} |
|
108
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.