| 1 |
<?php |
| 2 |
/** |
| 3 |
* Modules Init class. |
| 4 |
* |
| 5 |
* @package SureCookie\Inc\Modules |
| 6 |
* @since 0.0.1 |
| 7 |
*/ |
| 8 |
|
| 9 |
namespace SureCookie\Inc\Modules; |
| 10 |
|
| 11 |
use SureCookie\Inc\Traits\GetInstance; |
| 12 |
|
| 13 |
if ( ! defined( 'ABSPATH' ) ) { |
| 14 |
exit; // Exit if accessed directly. |
| 15 |
} |
| 16 |
|
| 17 |
/** |
| 18 |
* Init class. |
| 19 |
* |
| 20 |
* @since 0.0.1 |
| 21 |
*/ |
| 22 |
class Init { |
| 23 |
use GetInstance; |
| 24 |
|
| 25 |
/** |
| 26 |
* Constructor. |
| 27 |
* |
| 28 |
* @since 0.0.1 |
| 29 |
*/ |
| 30 |
public function __construct() { |
| 31 |
$modules_dir = __DIR__; |
| 32 |
$init_files = glob( "{$modules_dir}/*/init.php" ); |
| 33 |
|
| 34 |
if ( ! $init_files ) { |
| 35 |
return; |
| 36 |
} |
| 37 |
|
| 38 |
foreach ( $init_files as $file ) { |
| 39 |
$module_name = basename( dirname( $file ) ); |
| 40 |
$namespace_name = $this->convert_to_namespace( $module_name ); |
| 41 |
$class_name = "\\SureCookie\\Inc\\Modules\\{$namespace_name}\\Init"; |
| 42 |
|
| 43 |
if ( class_exists( $class_name ) && method_exists( $class_name, 'get_instance' ) ) { |
| 44 |
$class_name::get_instance(); |
| 45 |
} |
| 46 |
} |
| 47 |
} |
| 48 |
|
| 49 |
/** |
| 50 |
* Convert folder name to proper namespace format. |
| 51 |
* |
| 52 |
* @param string $folder_name Module folder name. |
| 53 |
* @return string |
| 54 |
* @since 0.0.1 |
| 55 |
*/ |
| 56 |
private function convert_to_namespace( string $folder_name ): string { |
| 57 |
return str_replace( ' ', '', ucwords( str_replace( '-', ' ', $folder_name ) ) ); |
| 58 |
} |
| 59 |
} |
| 60 |
|