| 1 |
<?php |
| 2 |
/** |
| 3 |
* OpenStation App Framework — class autoloader. |
| 4 |
* |
| 5 |
* Maps the `OpenStation\` namespace onto this directory using the |
| 6 |
* WordPress file-naming convention, so a class, an interface and a |
| 7 |
* trait each live where PHPCS expects them: |
| 8 |
* |
| 9 |
* OpenStation\App → class-app.php |
| 10 |
* OpenStation\App\State → app/class-state.php |
| 11 |
* OpenStation\App\Contracts\Auth → app/contracts/interface-auth.php |
| 12 |
* OpenStation\App\WordPress\Auth → app/wordpress/class-auth.php |
| 13 |
* |
| 14 |
* The framework is deliberately host-agnostic: nothing under this |
| 15 |
* directory calls a WordPress function except the adapters in |
| 16 |
* `app/wordpress/` and the procedural glue in `wordpress.php`. A |
| 17 |
* plain PHP host defines `OPENSTATION_STANDALONE` before requiring |
| 18 |
* this file and gets the same framework with the standalone |
| 19 |
* adapters (see `docs/app-framework.md`). |
| 20 |
* |
| 21 |
* @package OpenStation |
| 22 |
*/ |
| 23 |
|
| 24 |
// Direct access, unless a standalone host is booting on bare PHP. |
| 25 |
if ( ! defined( 'ABSPATH' ) ) { |
| 26 |
defined( 'OPENSTATION_STANDALONE' ) || exit; |
| 27 |
} |
| 28 |
|
| 29 |
if ( ! defined( 'OPENSTATION_FRAMEWORK_DIR' ) ) { |
| 30 |
define( 'OPENSTATION_FRAMEWORK_DIR', __DIR__ ); |
| 31 |
} |
| 32 |
|
| 33 |
spl_autoload_register( |
| 34 |
static function ( $class_name ) { |
| 35 |
$prefix = 'OpenStation\\'; |
| 36 |
if ( 0 !== strpos( $class_name, $prefix ) ) { |
| 37 |
return; |
| 38 |
} |
| 39 |
|
| 40 |
$parts = explode( '\\', substr( $class_name, strlen( $prefix ) ) ); |
| 41 |
$name = array_pop( $parts ); |
| 42 |
$dir = OPENSTATION_FRAMEWORK_DIR; |
| 43 |
if ( ! empty( $parts ) ) { |
| 44 |
$dir .= '/' . strtolower( implode( '/', $parts ) ); |
| 45 |
} |
| 46 |
|
| 47 |
// `LogReader` → `log-reader`, matching `class-log-reader.php`. |
| 48 |
$file = strtolower( (string) preg_replace( '/(?<!^)[A-Z]/', '-$0', $name ) ); |
| 49 |
|
| 50 |
foreach ( array( 'class-', 'interface-', 'trait-' ) as $kind ) { |
| 51 |
$path = $dir . '/' . $kind . $file . '.php'; |
| 52 |
if ( is_file( $path ) ) { |
| 53 |
require_once $path; |
| 54 |
return; |
| 55 |
} |
| 56 |
} |
| 57 |
} |
| 58 |
); |
| 59 |
|
| 60 |
// Template helpers are functions, which no autoloader can find. |
| 61 |
require_once OPENSTATION_FRAMEWORK_DIR . '/app/html.php'; |
| 62 |
|