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