MediaWiki 1.42.1
FauxHookHandlerArray.php
Go to the documentation of this file.
1<?php
2
4
5use InvalidArgumentException;
6use LogicException;
7use OutOfBoundsException;
8
12class FauxHookHandlerArray implements \ArrayAccess, \IteratorAggregate {
13
14 private HookContainer $hookContainer;
15
16 private string $name;
17
18 private ?array $handlers = null;
19
24 public function __construct( HookContainer $hookContainer, string $name ) {
25 $this->hookContainer = $hookContainer;
26 $this->name = $name;
27 }
28
32 #[\ReturnTypeWillChange]
33 public function offsetExists( $offset ) {
34 return $this->getHandler( $offset ) !== null;
35 }
36
40 #[\ReturnTypeWillChange]
41 public function offsetGet( $offset ) {
42 $handler = $this->getHandler( $offset );
43
44 if ( !$handler ) {
45 throw new OutOfBoundsException( "No such index in the handler list: $offset" );
46 }
47
48 return $handler;
49 }
50
54 #[\ReturnTypeWillChange]
55 public function offsetSet( $offset, $value ) {
56 if ( $offset !== null ) {
57 throw new InvalidArgumentException( '$offset must be null, this array is append only' );
58 }
59
60 $this->hookContainer->register( $this->name, $value );
61 $this->handlers = null;
62 }
63
68 #[\ReturnTypeWillChange]
69 public function offsetUnset( $offset ) {
70 throw new LogicException( 'unset is not supported for hook handler arrays' );
71 }
72
73 private function getHandler( $offset ) {
74 if ( $this->handlers === null ) {
75 // NOTE: getHandlerCallbacks() only exists to support this.
76 // It should be deleted when we no longer need it here.
77 $this->handlers = $this->hookContainer->getHandlerCallbacks( $this->name );
78 }
79
80 return $this->handlers[$offset] ?? null;
81 }
82
83 #[\ReturnTypeWillChange]
84 public function getIterator() {
85 if ( $this->handlers === null ) {
86 // NOTE: getHandlerCallbacks() only exists to support this.
87 // It should be deleted when we no longer need it here.
88 $this->handlers = $this->hookContainer->getHandlerCallbacks( $this->name );
89 }
90
91 return new \ArrayIterator( $this->handlers );
92 }
93}
__construct(HookContainer $hookContainer, string $name)