| 1 |
<?php |
| 2 |
/** |
| 3 |
* Layout Component Registry |
| 4 |
* |
| 5 |
* @package Blockspare |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace Blockspare\Layouts; |
| 9 |
|
| 10 |
use \Exception; |
| 11 |
use \InvalidArgumentException; |
| 12 |
|
| 13 |
/** |
| 14 |
* Class Component_Registry |
| 15 |
* |
| 16 |
* @access private Not intended as a public API. |
| 17 |
* Use the helper functions to manage layouts. |
| 18 |
* This class will likely go away in the future. |
| 19 |
*/ |
| 20 |
final class Component_Registry { |
| 21 |
|
| 22 |
/** |
| 23 |
* Supported component types. |
| 24 |
* |
| 25 |
* @var array |
| 26 |
*/ |
| 27 |
private static $supported_component_types = [ 'layout', 'section','block','page' ]; |
| 28 |
|
| 29 |
/** |
| 30 |
* Required keys for components. |
| 31 |
* |
| 32 |
* @var array |
| 33 |
*/ |
| 34 |
private static $required_data_keys = [ 'type', 'key', 'name', 'content', 'item' ]; |
| 35 |
|
| 36 |
/** |
| 37 |
* Holds the registered layouts. |
| 38 |
* |
| 39 |
* @var array |
| 40 |
*/ |
| 41 |
private static $layouts = []; |
| 42 |
|
| 43 |
/** |
| 44 |
* Holds the registered sections. |
| 45 |
* |
| 46 |
* @var array |
| 47 |
*/ |
| 48 |
private static $sections = []; |
| 49 |
|
| 50 |
/** |
| 51 |
* Holds the registered blocks. |
| 52 |
* |
| 53 |
* @var array |
| 54 |
*/ |
| 55 |
private static $blocks = []; |
| 56 |
|
| 57 |
|
| 58 |
/** |
| 59 |
* Holds the registered pages. |
| 60 |
* |
| 61 |
* @var array |
| 62 |
*/ |
| 63 |
private static $pages = []; |
| 64 |
|
| 65 |
/** |
| 66 |
* The Component_Registry object. |
| 67 |
* |
| 68 |
* @var object Component_Registry |
| 69 |
*/ |
| 70 |
private static $instance; |
| 71 |
|
| 72 |
/** |
| 73 |
* Creates the Component_Registry instance. |
| 74 |
* |
| 75 |
* @return Component_Registry |
| 76 |
*/ |
| 77 |
public static function instance() { |
| 78 |
if ( null === self::$instance ) { |
| 79 |
self::$instance = new self(); |
| 80 |
} |
| 81 |
return self::$instance; |
| 82 |
} |
| 83 |
|
| 84 |
/** |
| 85 |
* Adds a component to the registry. |
| 86 |
* |
| 87 |
* @param array $data Component data. |
| 88 |
* @throws InvalidArgumentException If invalid data is provided or the component is already registered. |
| 89 |
*/ |
| 90 |
public static function add( array $data ) { |
| 91 |
|
| 92 |
if ( empty( $data['type'] ) || ! in_array( $data['type'], self::$supported_component_types, true ) ) { |
| 93 |
throw new InvalidArgumentException( esc_html__( 'You must supply a valid component type.', 'blockspare' ) ); |
| 94 |
} |
| 95 |
|
| 96 |
if ( empty( $data ) ) { |
| 97 |
throw new InvalidArgumentException( __( 'You must supply valid layout data to register a layout.', 'blockspare' ) ); |
| 98 |
} |
| 99 |
|
| 100 |
foreach ( self::$required_data_keys as $required_key ) { |
| 101 |
if ( ! array_key_exists( $required_key, $data ) || empty( $data[ $required_key ] ) ) { |
| 102 |
/* translators: %s: The missing key that is required to register a component. */ |
| 103 |
throw new InvalidArgumentException( sprintf( esc_html__( 'You must supply a %s to register a layout.', 'blockspare' ), $required_key ) ); |
| 104 |
} |
| 105 |
} |
| 106 |
|
| 107 |
switch ( $data['type'] ) { |
| 108 |
case 'layout': |
| 109 |
if ( ! empty( self::$layouts[ $data['key'] ] ) ) { |
| 110 |
/* translators: %s: The component's unique key. */ |
| 111 |
throw new InvalidArgumentException( sprintf( esc_html__( 'The %s layout is already registered.', 'blockspare' ), $data['key'] ) ); |
| 112 |
} |
| 113 |
self::$layouts[ $data['key'] ] = $data; |
| 114 |
break; |
| 115 |
|
| 116 |
case 'section': |
| 117 |
if ( ! empty( self::$sections[ $data['key'] ] ) ) { |
| 118 |
/* translators: %s: The component's unique key. */ |
| 119 |
throw new InvalidArgumentException( sprintf( esc_html__( 'The %s section is already registered.', 'blockspare' ), $data['key'] ) ); |
| 120 |
} |
| 121 |
self::$sections[ $data['key'] ] = $data; |
| 122 |
break; |
| 123 |
|
| 124 |
|
| 125 |
case 'block': |
| 126 |
if ( ! empty( self::$blocks[ $data['key'] ] ) ) { |
| 127 |
/* translators: %s: The component's unique key. */ |
| 128 |
throw new InvalidArgumentException( sprintf( esc_html__( 'The %s section is already registered.', 'blockspare' ), $data['key'] ) ); |
| 129 |
} |
| 130 |
self::$blocks[ $data['key'] ] = $data; |
| 131 |
break; |
| 132 |
|
| 133 |
case 'page': |
| 134 |
if ( ! empty( self::$pages[ $data['key'] ] ) ) { |
| 135 |
/* translators: %s: The component's unique key. */ |
| 136 |
throw new InvalidArgumentException( sprintf( esc_html__( 'The %s section is already registered.', 'blockspare' ), $data['key'] ) ); |
| 137 |
} |
| 138 |
self::$pages[ $data['key'] ] = $data; |
| 139 |
break; |
| 140 |
|
| 141 |
default: |
| 142 |
/* translators: %s: This functions name. Will always be Blockspare\Layouts\Component_Registry::add(). */ |
| 143 |
throw new InvalidArgumentException( sprintf( esc_html__( 'You must supply a valid component type in %s.', 'blockspare' ), __METHOD__ ) ); |
| 144 |
} |
| 145 |
} |
| 146 |
|
| 147 |
/** |
| 148 |
* Removes an existing component from the registry. |
| 149 |
* |
| 150 |
* @param string $type The component type to unregister. |
| 151 |
* @param string $key The unique layout key to be removed. |
| 152 |
* |
| 153 |
* @throws Exception If the required data is not provided or the specified component is not registered. |
| 154 |
* @throws InvalidArgumentException If an unsupported component type is provided. |
| 155 |
*/ |
| 156 |
public static function remove( $type, $key ) { |
| 157 |
|
| 158 |
if ( empty( $type ) || ! in_array( $type, self::$supported_component_types, true ) ) { |
| 159 |
/* translators: %s: This functions name. Will always be Blockspare\Layouts\Component_Registry::remove(). */ |
| 160 |
throw new InvalidArgumentException( sprintf( esc_html__( 'You must supply a valid component type in %s.', 'blockspare' ), __METHOD__ ) ); |
| 161 |
} |
| 162 |
|
| 163 |
if ( empty( $key ) ) { |
| 164 |
/* translators: %s: This functions name. Will always be Blockspare\Layouts\Component_Registry::remove(). */ |
| 165 |
throw new InvalidArgumentException( sprintf( esc_html__( 'You must supply a valid component key in %s.', 'blockspare' ), __METHOD__ ) ); |
| 166 |
} |
| 167 |
|
| 168 |
$key = sanitize_key( $key ); |
| 169 |
|
| 170 |
switch ( $type ) { |
| 171 |
case 'layout': |
| 172 |
if ( empty( self::$layouts[ $key ] ) ) { |
| 173 |
/* translators: The requested components unique key. */ |
| 174 |
throw new Exception( sprintf( esc_html__( 'The %s layout is not registered.', 'blockspare' ), $key ) ); |
| 175 |
} |
| 176 |
unset( self::$layouts[ $key ] ); |
| 177 |
break; |
| 178 |
|
| 179 |
case 'section': |
| 180 |
if ( empty( self::$sections[ $key ] ) ) { |
| 181 |
/* translators: The requested components unique key. */ |
| 182 |
throw new Exception( sprintf( esc_html__( 'The %s section is not registered.', 'blockspare' ), $key ) ); |
| 183 |
} |
| 184 |
unset( self::$sections[ $key ] ); |
| 185 |
break; |
| 186 |
|
| 187 |
case 'block': |
| 188 |
if ( empty( self::$blocks[ $key ] ) ) { |
| 189 |
/* translators: The requested components unique key. */ |
| 190 |
throw new Exception( sprintf( esc_html__( 'The %s section is not registered.', 'blockspare' ), $key ) ); |
| 191 |
} |
| 192 |
unset( self::$blocks[ $key ] ); |
| 193 |
break; |
| 194 |
|
| 195 |
case 'page': |
| 196 |
if ( empty( self::$pages[ $key ] ) ) { |
| 197 |
/* translators: The requested components unique key. */ |
| 198 |
throw new Exception( sprintf( esc_html__( 'The %s section is not registered.', 'blockspare' ), $key ) ); |
| 199 |
} |
| 200 |
unset( self::$pages[ $key ] ); |
| 201 |
break; |
| 202 |
} |
| 203 |
|
| 204 |
} |
| 205 |
|
| 206 |
/** |
| 207 |
* Gets a component from the registry. |
| 208 |
* |
| 209 |
* @param string $type The component type. |
| 210 |
* @param string $key The component's unique key. |
| 211 |
* |
| 212 |
* @return mixed |
| 213 |
* @throws Exception If the required data is not provided or the specified component is not registered. |
| 214 |
* @throws InvalidArgumentException If an unsupported component type is provided. |
| 215 |
*/ |
| 216 |
public static function get( $type, $key ) { |
| 217 |
|
| 218 |
if ( empty( $type ) || ! in_array( $type, self::$supported_component_types, true ) ) { |
| 219 |
/* translators: This function name. Will always be Blockspare\Layouts\Component_Registry::get(). */ |
| 220 |
throw new Exception( sprintf( esc_html__( 'You must supply a component type in %s.', 'blockspare' ), __METHOD__ ) ); |
| 221 |
} |
| 222 |
|
| 223 |
switch ( $type ) { |
| 224 |
case 'layout': |
| 225 |
if ( empty( self::$layouts[ $key ] ) ) { |
| 226 |
/* translators: The requested components unique key. */ |
| 227 |
throw new Exception( sprintf( esc_html__( 'The %s layout is not registered.', 'blockspare' ), $key ) ); |
| 228 |
} |
| 229 |
return self::$layouts[ $key ]; |
| 230 |
|
| 231 |
case 'section': |
| 232 |
if ( empty( self::$sections[ $key ] ) ) { |
| 233 |
/* translators: The requested components unique key. */ |
| 234 |
throw new Exception( sprintf( esc_html__( 'The %s section is not registered.', 'blockspare' ), $key ) ); |
| 235 |
} |
| 236 |
return self::$sections[ $key ]; |
| 237 |
|
| 238 |
case 'block': |
| 239 |
if ( empty( self::$blocks[ $key ] ) ) { |
| 240 |
/* translators: The requested components unique key. */ |
| 241 |
throw new Exception( sprintf( esc_html__( 'The %s section is not registered.', 'blockspare' ), $key ) ); |
| 242 |
} |
| 243 |
return self::$blocks[ $key ]; |
| 244 |
|
| 245 |
|
| 246 |
case 'page': |
| 247 |
if ( empty( self::$pages[ $key ] ) ) { |
| 248 |
/* translators: The requested components unique key. */ |
| 249 |
throw new Exception( sprintf( esc_html__( 'The %s section is not registered.', 'blockspare' ), $key ) ); |
| 250 |
} |
| 251 |
return self::$pages[ $key ]; |
| 252 |
|
| 253 |
default: |
| 254 |
/* translators: This function name. Will always be Blockspare\Layouts\Component_Registry::get(). */ |
| 255 |
throw new InvalidArgumentException( sprintf( esc_html__( 'You must supply a valid component type in %s.', 'blockspare' ), __METHOD__ ) ); |
| 256 |
} |
| 257 |
} |
| 258 |
|
| 259 |
/** |
| 260 |
* Returns the registered layouts. |
| 261 |
* |
| 262 |
* @return array |
| 263 |
*/ |
| 264 |
public static function layouts() { |
| 265 |
return self::$layouts; |
| 266 |
} |
| 267 |
|
| 268 |
/** |
| 269 |
* Returns the registered sections. |
| 270 |
* |
| 271 |
* @return array |
| 272 |
*/ |
| 273 |
public static function sections() { |
| 274 |
return self::$sections; |
| 275 |
} |
| 276 |
|
| 277 |
|
| 278 |
/** |
| 279 |
* Returns the registered sections. |
| 280 |
* |
| 281 |
* @return array |
| 282 |
*/ |
| 283 |
public static function blocks() { |
| 284 |
return self::$blocks; |
| 285 |
} |
| 286 |
|
| 287 |
/** |
| 288 |
* Returns the registered pages. |
| 289 |
* |
| 290 |
* @return array |
| 291 |
*/ |
| 292 |
public static function pages() { |
| 293 |
return self::$pages; |
| 294 |
} |
| 295 |
} |
| 296 |
|