>|null */ private static ?array $map = null; /** * Absolute path to the capabilities file. * * @since 0.1.0 */ public static function path(): string { return dirname( __DIR__, 3 ) . '/config/capabilities.php'; } /** * Full map keyed by route identifier. * * @return array> * * @throws RuntimeException When the file is missing or invalid. * * @since 0.1.0 */ public static function all(): array { if ( null !== self::$map ) { return self::$map; } $path = self::path(); if ( ! is_readable( $path ) ) { throw new RuntimeException( 'Bookline capabilities map is missing: config/capabilities.php' ); } $loaded = require $path; if ( ! is_array( $loaded ) ) { throw new RuntimeException( 'Bookline capabilities map must return an array.' ); } $map = []; foreach ( $loaded as $key => $entry ) { if ( is_string( $key ) && is_array( $entry ) ) { $map[ $key ] = $entry; } } self::$map = $map; return self::$map; } /** * Look up a single route entry. * * @param string $route_id Route identifier. * @return array * * @throws RuntimeException When the identifier is absent. * * @since 0.1.0 */ public static function require( string $route_id ): array { $map = self::all(); if ( ! isset( $map[ $route_id ] ) ) { throw new RuntimeException( sprintf( 'Bookline capabilities map has no entry for route "%s".', esc_html( $route_id ) ) ); } return $map[ $route_id ]; } /** * Whether a route id is present. * * @param string $route_id Route identifier. * * @since 0.1.0 */ public static function has( string $route_id ): bool { return isset( self::all()[ $route_id ] ); } /** * Reset the in-memory cache (tests only). * * @since 0.1.0 */ public static function reset_cache(): void { self::$map = null; } /** * Public path prefixes from map entries marked public. * * @return list * * @since 0.1.0 */ public static function public_path_prefixes(): array { $out = []; foreach ( self::all() as $entry ) { if ( empty( $entry['public'] ) || ! isset( $entry['path'] ) || ! is_string( $entry['path'] ) ) { continue; } $path = $entry['path']; // Strip regex capture groups for prefix matching against the allow-list. $prefix = (string) preg_replace( '#/\(.*$#', '', $path ); if ( '' !== $prefix ) { $out[] = $prefix; } } return array_values( array_unique( $out ) ); } }