| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* A redirect action - what happens after a URL is matched. |
| 5 |
* |
| 6 |
* @phpstan-type UrlActionData array{ |
| 7 |
* code: int, |
| 8 |
* target: string, |
| 9 |
* type: string |
| 10 |
* } |
| 11 |
* @phpstan-type ErrorActionData array{ |
| 12 |
* code: int, |
| 13 |
* type: string |
| 14 |
* } |
| 15 |
* @phpstan-type NothingActionData array{ |
| 16 |
* code: int, |
| 17 |
* type: string |
| 18 |
* } |
| 19 |
* @phpstan-type RandomActionData array{ |
| 20 |
* code: int, |
| 21 |
* type: string |
| 22 |
* } |
| 23 |
* @phpstan-type PassActionData array{ |
| 24 |
* code: int, |
| 25 |
* target: string, |
| 26 |
* type: string |
| 27 |
* } |
| 28 |
* @phpstan-type RedActionData UrlActionData|ErrorActionData|NothingActionData|RandomActionData|PassActionData |
| 29 |
*/ |
| 30 |
abstract class Red_Action { |
| 31 |
/** |
| 32 |
* The action code (i.e. HTTP code) |
| 33 |
* |
| 34 |
* @var integer |
| 35 |
*/ |
| 36 |
protected $code = 0; |
| 37 |
|
| 38 |
/** |
| 39 |
* The action type |
| 40 |
* |
| 41 |
* @var string |
| 42 |
*/ |
| 43 |
protected $type = ''; |
| 44 |
|
| 45 |
/** |
| 46 |
* Target URL, if any |
| 47 |
* |
| 48 |
* @var string|null |
| 49 |
*/ |
| 50 |
protected $target = null; |
| 51 |
|
| 52 |
/** |
| 53 |
* Constructor |
| 54 |
* |
| 55 |
* @param RedActionData|array{} $values Values. |
| 56 |
*/ |
| 57 |
public function __construct( $values = [] ) { |
| 58 |
if ( isset( $values['code'] ) ) { |
| 59 |
$this->code = $values['code']; |
| 60 |
} |
| 61 |
|
| 62 |
if ( isset( $values['target'] ) ) { |
| 63 |
$this->target = $values['target']; |
| 64 |
} |
| 65 |
|
| 66 |
if ( isset( $values['type'] ) ) { |
| 67 |
$this->type = $values['type']; |
| 68 |
} |
| 69 |
} |
| 70 |
|
| 71 |
/** |
| 72 |
* @return string |
| 73 |
*/ |
| 74 |
abstract public function name(); |
| 75 |
|
| 76 |
/** |
| 77 |
* Create an action object |
| 78 |
* |
| 79 |
* @param string $name Action type. |
| 80 |
* @param integer $code Action code. |
| 81 |
* @return Red_Action|null |
| 82 |
*/ |
| 83 |
public static function create( $name, $code ) { |
| 84 |
$avail = self::available(); |
| 85 |
|
| 86 |
if ( isset( $avail[ $name ] ) ) { |
| 87 |
if ( ! class_exists( strtolower( $avail[ $name ][1] ) ) ) { |
| 88 |
include_once __DIR__ . '/../actions/' . $avail[ $name ][0]; |
| 89 |
} |
| 90 |
|
| 91 |
/** |
| 92 |
* @var Red_Action |
| 93 |
*/ |
| 94 |
$obj = new $avail[ $name ][1]( [ 'code' => $code ] ); |
| 95 |
$obj->type = $name; |
| 96 |
return $obj; |
| 97 |
} |
| 98 |
|
| 99 |
return null; |
| 100 |
} |
| 101 |
|
| 102 |
/** |
| 103 |
* Get list of available actions |
| 104 |
* |
| 105 |
* @return array<string, array{string, string}> |
| 106 |
*/ |
| 107 |
public static function available() { |
| 108 |
return [ |
| 109 |
'url' => [ 'url.php', 'Url_Action' ], |
| 110 |
'error' => [ 'error.php', 'Error_Action' ], |
| 111 |
'nothing' => [ 'nothing.php', 'Nothing_Action' ], |
| 112 |
'random' => [ 'random.php', 'Random_Action' ], |
| 113 |
'pass' => [ 'pass.php', 'Pass_Action' ], |
| 114 |
]; |
| 115 |
} |
| 116 |
|
| 117 |
/** |
| 118 |
* Get the action code |
| 119 |
* |
| 120 |
* @return integer |
| 121 |
*/ |
| 122 |
public function get_code() { |
| 123 |
return $this->code; |
| 124 |
} |
| 125 |
|
| 126 |
/** |
| 127 |
* Get action type |
| 128 |
* |
| 129 |
* @return string |
| 130 |
*/ |
| 131 |
public function get_type() { |
| 132 |
return $this->type; |
| 133 |
} |
| 134 |
|
| 135 |
/** |
| 136 |
* Set the target for this action |
| 137 |
* |
| 138 |
* @param string $target_url The original URL from the client. |
| 139 |
* @return void |
| 140 |
*/ |
| 141 |
public function set_target( $target_url ) { |
| 142 |
$this->target = $target_url; |
| 143 |
} |
| 144 |
|
| 145 |
/** |
| 146 |
* Get the target for this action |
| 147 |
* |
| 148 |
* @return string|null |
| 149 |
*/ |
| 150 |
public function get_target() { |
| 151 |
return $this->target; |
| 152 |
} |
| 153 |
|
| 154 |
/** |
| 155 |
* Does this action need a target? |
| 156 |
* |
| 157 |
* @return boolean |
| 158 |
*/ |
| 159 |
public function needs_target() { |
| 160 |
return false; |
| 161 |
} |
| 162 |
|
| 163 |
/** |
| 164 |
* Run this action. May not return from this function. |
| 165 |
* |
| 166 |
* @return void |
| 167 |
*/ |
| 168 |
abstract public function run(); |
| 169 |
} |
| 170 |
|