2023-01-03 17:31:21 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Helpers;
|
|
|
|
|
2023-03-04 14:20:49 +00:00
|
|
|
/**
|
|
|
|
* Summary of ExtensionHelper
|
|
|
|
*/
|
2023-01-03 17:31:21 +00:00
|
|
|
class ExtensionHelper
|
|
|
|
{
|
2023-01-21 00:03:33 +00:00
|
|
|
/**
|
|
|
|
* Get a config of an extension by its name
|
|
|
|
* @param string $extensionName
|
|
|
|
* @param string $configname
|
|
|
|
*/
|
|
|
|
public static function getExtensionConfig(string $extensionName, string $configname)
|
2023-01-03 17:31:21 +00:00
|
|
|
{
|
2023-01-21 00:03:33 +00:00
|
|
|
$extensions = ExtensionHelper::getAllExtensions();
|
2023-01-03 17:31:21 +00:00
|
|
|
|
2023-01-21 00:03:33 +00:00
|
|
|
// 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;
|
|
|
|
}
|
2023-01-03 17:31:21 +00:00
|
|
|
|
2023-01-21 00:03:33 +00:00
|
|
|
$configFile = $extension . '/config.php';
|
|
|
|
if (file_exists($configFile)) {
|
|
|
|
include_once $configFile;
|
|
|
|
$config = call_user_func('App\\Extensions\\' . basename(dirname($extension)) . '\\' . basename($extension) . "\\getConfig");
|
|
|
|
}
|
2023-01-03 17:31:21 +00:00
|
|
|
|
|
|
|
|
2023-01-21 00:03:33 +00:00
|
|
|
if (isset($config[$configname])) {
|
|
|
|
return $config[$configname];
|
|
|
|
}
|
2023-01-03 17:31:21 +00:00
|
|
|
}
|
|
|
|
|
2023-01-21 00:03:33 +00:00
|
|
|
return null;
|
2023-01-03 17:31:21 +00:00
|
|
|
}
|
2023-01-14 22:33:30 +00:00
|
|
|
|
2023-01-21 00:03:33 +00:00
|
|
|
public static function getAllCsrfIgnoredRoutes()
|
2023-01-14 22:33:30 +00:00
|
|
|
{
|
2023-01-21 00:03:33 +00:00
|
|
|
$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']);
|
|
|
|
}
|
|
|
|
|
2023-02-06 10:32:19 +00:00
|
|
|
// map over the routes and add the extension name as prefix
|
|
|
|
$result = array_map(fn ($item) => "extensions/{$item}", $routes);
|
2023-01-21 00:03:33 +00:00
|
|
|
}
|
2023-01-14 22:33:30 +00:00
|
|
|
|
2023-02-06 10:32:19 +00:00
|
|
|
return $result;
|
2023-01-21 00:03:33 +00:00
|
|
|
}
|
2023-01-14 22:33:30 +00:00
|
|
|
|
2023-01-21 00:03:33 +00:00
|
|
|
/**
|
|
|
|
* Get all extensions
|
2023-03-04 14:20:49 +00:00
|
|
|
* @return array of all extension paths look like: app/Extensions/ExtensionNamespace/ExtensionName
|
2023-01-21 00:03:33 +00:00
|
|
|
*/
|
|
|
|
public static function getAllExtensions()
|
|
|
|
{
|
|
|
|
$extensionNamespaces = glob(app_path() . '/Extensions/*', GLOB_ONLYDIR);
|
|
|
|
$extensions = [];
|
|
|
|
foreach ($extensionNamespaces as $extensionNamespace) {
|
|
|
|
$extensions = array_merge($extensions, glob($extensionNamespace . '/*', GLOB_ONLYDIR));
|
2023-01-14 22:33:30 +00:00
|
|
|
}
|
|
|
|
|
2023-01-21 00:03:33 +00:00
|
|
|
return $extensions;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function getAllExtensionsByNamespace(string $namespace)
|
|
|
|
{
|
|
|
|
$extensions = glob(app_path() . '/Extensions/' . $namespace . '/*', GLOB_ONLYDIR);
|
|
|
|
|
|
|
|
return $extensions;
|
2023-01-14 22:33:30 +00:00
|
|
|
}
|
2023-03-04 14:20:49 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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;
|
|
|
|
}
|
2023-03-04 18:59:59 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Summary of getAllExtensionSettings
|
|
|
|
* @return array of all setting classes look like: App\Extensions\PaymentGateways\PayPal\PayPalSettings
|
|
|
|
*/
|
2023-03-04 19:28:27 +00:00
|
|
|
public static function getAllExtensionSettingsClasses()
|
2023-03-04 18:59:59 +00:00
|
|
|
{
|
|
|
|
$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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
error_log(print_r($settings, true));
|
|
|
|
|
|
|
|
|
|
|
|
return $settings;
|
|
|
|
}
|
2023-03-04 19:28:27 +00:00
|
|
|
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-01-15 00:39:38 +00:00
|
|
|
}
|