PluginProbe
Force Refresh / trunk
Force Refresh vtrunk
3.2.0 3.1.2 3.1.1 3.1.0 trunk 1.0.0 1.1.0 1.1.1 1.1.2 1.2.0 2.0.0 2.1.0 2.1.1 2.1.2 2.1.3 2.1.4 2.1.5 2.1.6 2.10.0 2.10.1 2.10.2 2.11.0 2.11.1 2.12.0 2.12.1 All 53 releases
force-refresh / includes / autoload.php

autoload.php in Force Refresh trunk, at includes/autoload.php

53 lines 1.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * The file responsible for autoloading our classes and interfaces.
4 *
5 * @package ForceRefresh
6 */
7
8 namespace JordanLeven\Plugins\ForceRefresh;
9
10 /**
11 * Function to handle the autoloading of our classes and interfaces.
12 *
13 * @param string $class_name The class attempting to load.
14 *
15 * @return void
16 * @throws \Exception If the file isn't found.
17 */
18 function autoload_classes_and_interfaces( string $class_name ) {
19
20 // If the autoloaded class is not of our namespace, do an early exit.
21 if ( strpos( $class_name, __NAMESPACE__ ) === false ) {
22 return;
23 }
24
25 // Format the string based on our agreed-upon file structure.
26 $file_path = str_replace(
27 array( __NAMESPACE__, '\\', '_' ),
28 array( '', '/', '-' ),
29 $class_name
30 );
31
32 $file_path_absolute = strtolower( sprintf( '%s%s.php', __DIR__, $file_path ) );
33 $file_name = basename( $file_path_absolute );
34 $file_name_adjusted = $file_name;
35
36 // If the file is an interface, then we need to adjust the naming convention.
37 if ( strpos( $file_name, 'interface' ) ) {
38 $file_name_adjusted = 'interface-' . str_replace( '-interface', '', $file_name );
39 } else {
40 $file_name_adjusted = 'classes/class-' . $file_name;
41 }
42
43 $file_path_absolute_adjusted = str_replace( $file_name, $file_name_adjusted, $file_path_absolute );
44
45 if ( ! file_exists( $file_path_absolute_adjusted ) ) {
46 throw new \Exception( sprintf( 'Unable to locate file %s', esc_html( $file_path_absolute_adjusted ) ) );
47 }
48
49 require_once $file_path_absolute_adjusted;
50 }
51
52 spl_autoload_register( __NAMESPACE__ . '\\autoload_classes_and_interfaces' );
53