MediaWiki master
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
20 public function __construct( HookContainer $hookContainer, string $name ) {
21 $this->hookContainer = $hookContainer;
22 $this->name = $name;
23 }
24
28 #[\ReturnTypeWillChange]
29 public function offsetExists( $offset ) {
30 return $this->getHandler( $offset ) !== null;
31 }
32
36 #[\ReturnTypeWillChange]
37 public function offsetGet( $offset ) {
38 $handler = $this->getHandler( $offset );
39
40 if ( !$handler ) {
41 throw new OutOfBoundsException( "No such index in the handler list: $offset" );
42 }
43
44 return $handler;
45 }
46
50 #[\ReturnTypeWillChange]
51 public function offsetSet( $offset, $value ) {
52 if ( $offset !== null ) {
53 throw new InvalidArgumentException( '$offset must be null, this array is append only' );
54 }
55
56 $this->hookContainer->register( $this->name, $value );
57 $this->handlers = null;
58 }
59
64 #[\ReturnTypeWillChange]
65 public function offsetUnset( $offset ) {
66 throw new LogicException( 'unset is not supported for hook handler arrays' );
67 }
68
69 private function getHandler( $offset ) {
70 if ( $this->handlers === null ) {
71 // NOTE: getHandlerCallbacks() only exists to support this.
72 // It should be deleted when we no longer need it here.
73 $this->handlers = $this->hookContainer->getHandlerCallbacks( $this->name );
74 }
75
76 return $this->handlers[$offset] ?? null;
77 }
78
79 #[\ReturnTypeWillChange]
80 public function getIterator() {
81 if ( $this->handlers === null ) {
82 // NOTE: getHandlerCallbacks() only exists to support this.
83 // It should be deleted when we no longer need it here.
84 $this->handlers = $this->hookContainer->getHandlerCallbacks( $this->name );
85 }
86
87 return new \ArrayIterator( $this->handlers );
88 }
89}
__construct(HookContainer $hookContainer, string $name)