123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156 |
- <?php
- namespace App\Helpers;
- /**
- * Summary of ExtensionHelper
- */
- class ExtensionHelper
- {
- /**
- * Get a config of an extension by its name
- * @param string $extensionName
- * @param string $configname
- */
- public static function getExtensionConfig(string $extensionName, string $configname)
- {
- $extensions = ExtensionHelper::getAllExtensions();
- // call the getConfig function of the config file of the extension like that
- // call_user_func("App\\Extensions\\PaymentGateways\\Stripe" . "\\getConfig");
- foreach ($extensions as $extension) {
- if (!(basename($extension) == $extensionName)) {
- continue;
- }
- $configFile = $extension . '/config.php';
- if (file_exists($configFile)) {
- include_once $configFile;
- $config = call_user_func('App\\Extensions\\' . basename(dirname($extension)) . '\\' . basename($extension) . "\\getConfig");
- }
- if (isset($config[$configname])) {
- return $config[$configname];
- }
- }
- return null;
- }
- public static function getAllCsrfIgnoredRoutes()
- {
- $extensions = ExtensionHelper::getAllExtensions();
- $routes = [];
- foreach ($extensions as $extension) {
- $configFile = $extension . '/config.php';
- if (file_exists($configFile)) {
- include_once $configFile;
- $config = call_user_func('App\\Extensions\\' . basename(dirname($extension)) . '\\' . basename($extension) . "\\getConfig");
- }
- if (isset($config['RoutesIgnoreCsrf'])) {
- $routes = array_merge($routes, $config['RoutesIgnoreCsrf']);
- }
- // map over the routes and add the extension name as prefix
- $result = array_map(fn ($item) => "extensions/{$item}", $routes);
- }
- return $result;
- }
- /**
- * Get all extensions
- * @return array of all extension paths look like: app/Extensions/ExtensionNamespace/ExtensionName
- */
- public static function getAllExtensions()
- {
- $extensionNamespaces = glob(app_path() . '/Extensions/*', GLOB_ONLYDIR);
- $extensions = [];
- foreach ($extensionNamespaces as $extensionNamespace) {
- $extensions = array_merge($extensions, glob($extensionNamespace . '/*', GLOB_ONLYDIR));
- }
- return $extensions;
- }
- public static function getAllExtensionsByNamespace(string $namespace)
- {
- $extensions = glob(app_path() . '/Extensions/' . $namespace . '/*', GLOB_ONLYDIR);
- return $extensions;
- }
- /**
- * Summary of getAllExtensionMigrations
- * @return array of all migration paths look like: app/Extensions/ExtensionNamespace/ExtensionName/migrations/
- */
- public static function getAllExtensionMigrations()
- {
- $extensions = ExtensionHelper::getAllExtensions();
- // get all migration directories of the extensions and return them as array
- $migrations = [];
- foreach ($extensions as $extension) {
- $migrationDir = $extension . '/migrations';
- if (file_exists($migrationDir)) {
- $migrations[] = $migrationDir;
- }
- }
- return $migrations;
- }
- /**
- * Summary of getAllExtensionSettings
- * @return array of all setting classes look like: App\Extensions\PaymentGateways\PayPal\PayPalSettings
- */
- public static function getAllExtensionSettingsClasses()
- {
- $extensions = ExtensionHelper::getAllExtensions();
- $settings = [];
- foreach ($extensions as $extension) {
- $extensionName = basename($extension);
- $settingFile = $extension . '/' . $extensionName . 'Settings.php';
- if (file_exists($settingFile)) {
- // remove the base path from the setting file path to get the namespace
- $settingFile = str_replace(app_path() . '/', '', $settingFile);
- $settingFile = str_replace('.php', '', $settingFile);
- $settingFile = str_replace('/', '\\', $settingFile);
- $settingFile = 'App\\' . $settingFile;
- $settings[] = $settingFile;
- }
- }
- return $settings;
- }
- public static function getExtensionSettings(string $extensionName)
- {
- $extensions = ExtensionHelper::getAllExtensions();
- // find the setting file of the extension and return an instance of it
- foreach ($extensions as $extension) {
- if (!(basename($extension) == $extensionName)) {
- continue;
- }
- $extensionName = basename($extension);
- $settingFile = $extension . '/' . $extensionName . 'Settings.php';
- if (file_exists($settingFile)) {
- // remove the base path from the setting file path to get the namespace
- $settingFile = str_replace(app_path() . '/', '', $settingFile);
- $settingFile = str_replace('.php', '', $settingFile);
- $settingFile = str_replace('/', '\\', $settingFile);
- $settingFile = 'App\\' . $settingFile;
- return new $settingFile();
- }
- }
- }
- }
|