| 1 |
<?php |
| 2 |
|
| 3 |
namespace MediaWiki\HookContainer; |
| 4 |
|
| 5 |
use InvalidArgumentException; |
| 6 |
use LogicException; |
| 7 |
use OutOfBoundsException; |
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
class FauxHookHandlerArray implements \ArrayAccess, \IteratorAggregate { |
| 13 |
|
| 14 |
private HookContainer $hookContainer; |
| 15 |
|
| 16 |
private string $name; |
| 17 |
|
| 18 |
private ?array $handlers = null; |
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
public function __construct( HookContainer $hookContainer, string $name ) { |
| 25 |
$this->hookContainer = $hookContainer; |
| 26 |
$this->name = $name; |
| 27 |
} |
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
#[\ReturnTypeWillChange] |
| 33 |
public function offsetExists( $offset ) { |
| 34 |
return $this->getHandler( $offset ) !== null; |
| 35 |
} |
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 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 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 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 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 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 |
|
| 76 |
|
| 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 |
|
| 87 |
|
| 88 |
$this->handlers = $this->hookContainer->getHandlerCallbacks( $this->name ); |
| 89 |
} |
| 90 |
|
| 91 |
return new \ArrayIterator( $this->handlers ); |
| 92 |
} |
| 93 |
} |