array_ad_conditions.php
6 years ago
cap_map.php
10 years ago
functions.php
6 years ago
load_modules.php
5 years ago
load_modules.php
76 lines
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * Class Advanced_Ads_ModuleLoader |
| 5 | */ |
| 6 | final class Advanced_Ads_ModuleLoader { |
| 7 | |
| 8 | protected static $loader; |
| 9 | protected static $textdomains = array(); |
| 10 | protected static $modules = array(); |
| 11 | |
| 12 | /** |
| 13 | * Get the Composer autoloader. |
| 14 | * |
| 15 | * @return \AdvancedAds\Autoload\ClassLoader |
| 16 | */ |
| 17 | public static function getLoader() |
| 18 | { |
| 19 | if ( is_null( self::$loader ) ) { |
| 20 | self::$loader = require_once ADVADS_BASE_PATH . 'lib/autoload.php'; |
| 21 | } |
| 22 | |
| 23 | return self::$loader; |
| 24 | } |
| 25 | |
| 26 | /** |
| 27 | * Module loader options: |
| 28 | * - array 'disabled': Pretty name by (module) dirname |
| 29 | * |
| 30 | * @param string $path path to modules |
| 31 | * @param array $options module loader options |
| 32 | */ |
| 33 | public static function loadModules($path, $options = array()) { |
| 34 | $loader = self::getLoader(); |
| 35 | |
| 36 | $disabledModules = isset($options['disabled']) ? (array) $options['disabled'] : array(); |
| 37 | $isAdmin = is_admin(); |
| 38 | |
| 39 | // iterate modules |
| 40 | foreach ( glob( $path . '*/main.php' ) as $module ) { |
| 41 | $modulePath = dirname( $module ); |
| 42 | $moduleName = basename( $modulePath ); |
| 43 | |
| 44 | // configuration is enabled by default (localisation, autoloading and other undemanding stuff) |
| 45 | if ( file_exists( $modulePath . '/config.php' ) ) { |
| 46 | $config = require $modulePath . '/config.php'; |
| 47 | // append autoload classmap |
| 48 | if ( isset($config['classmap']) && is_array( $config['classmap'] ) ) { |
| 49 | $loader->addClassmap( $config['classmap'] ); |
| 50 | } |
| 51 | // append textdomain |
| 52 | /*if ( isset($config['textdomain']) && $config['textdomain'] ) { |
| 53 | self::$textdomains[$config['textdomain']] = "modules/$moduleName/languages"; |
| 54 | }*/ |
| 55 | } |
| 56 | |
| 57 | // admin is enabled by default |
| 58 | if ( $isAdmin && file_exists( $modulePath . '/admin.php' ) ) { |
| 59 | include $modulePath . '/admin.php'; // do not care if this fails |
| 60 | } |
| 61 | |
| 62 | // skip if disabled |
| 63 | if ( isset( $disabledModules[$moduleName] ) ) { |
| 64 | continue ; |
| 65 | } |
| 66 | |
| 67 | self::$modules[$moduleName] = $modulePath; |
| 68 | } |
| 69 | |
| 70 | // load modules |
| 71 | foreach ( self::$modules as $name => $path ) { |
| 72 | require_once $path . '/main.php'; |
| 73 | } |
| 74 | } |
| 75 | } |
| 76 |