feat: ✨ Added several functions to the ExtensionHelper
This commit is contained in:
parent
e795b65df1
commit
3909202958
|
@ -4,44 +4,81 @@ namespace App\Helpers;
|
||||||
|
|
||||||
class ExtensionHelper
|
class ExtensionHelper
|
||||||
{
|
{
|
||||||
public static function getExtensionConfig($extensionName, $nameSpace)
|
/**
|
||||||
|
* Get a config of an extension by its name
|
||||||
|
* @param string $extensionName
|
||||||
|
* @param string $configname
|
||||||
|
*/
|
||||||
|
public static function getExtensionConfig(string $extensionName, string $configname)
|
||||||
{
|
{
|
||||||
$extension = app_path() . '/Extensions/' . $nameSpace . "/" . $extensionName . "/index.php";
|
$extensions = ExtensionHelper::getAllExtensions();
|
||||||
// Check if extension exists
|
|
||||||
if (!file_exists($extension)) {
|
// 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;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
// call the getConfig function from the index.php file of the extension
|
public static function getAllCsrfIgnoredRoutes()
|
||||||
$config = include_once $extension;
|
|
||||||
|
|
||||||
// Check if the getConfig function exists
|
|
||||||
if (!function_exists('get' . $extensionName . 'Config')) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
$config = call_user_func('get' . $extensionName . 'Config');
|
|
||||||
|
|
||||||
// Check if the getConfig function returned an array
|
|
||||||
if (!is_array($config)) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
return $config;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static function getPayMethod($extensionName, $nameSpace)
|
|
||||||
{
|
{
|
||||||
// return the payment method of the extension to be used elsewhere
|
$extensions = ExtensionHelper::getAllExtensions();
|
||||||
// for example in the payment controller
|
|
||||||
// the function starts with the name of the extension and ends with Pay
|
|
||||||
|
|
||||||
$config = self::getExtensionConfig($extensionName, $nameSpace);
|
$routes = [];
|
||||||
|
foreach ($extensions as $extension) {
|
||||||
if ($config == null) {
|
$configFile = $extension . '/config.php';
|
||||||
return null;
|
if (file_exists($configFile)) {
|
||||||
|
include_once $configFile;
|
||||||
|
$config = call_user_func('App\\Extensions\\' . basename(dirname($extension)) . '\\' . basename($extension) . "\\getConfig");
|
||||||
}
|
}
|
||||||
|
|
||||||
return $config['payMethod'];
|
if (isset($config['RoutesIgnoreCsrf'])) {
|
||||||
|
$routes = array_merge($routes, $config['RoutesIgnoreCsrf']);
|
||||||
|
}
|
||||||
|
|
||||||
|
// add extension/ infront of every route
|
||||||
|
foreach ($routes as $key => $route) {
|
||||||
|
$routes[$key] = 'extensions/' . $route;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return $routes;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get all extensions
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
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;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -38,21 +38,17 @@ class PaymentController extends Controller
|
||||||
*/
|
*/
|
||||||
public function checkOut(ShopProduct $shopProduct)
|
public function checkOut(ShopProduct $shopProduct)
|
||||||
{
|
{
|
||||||
// get all payment gateway extensions
|
$extensions = ExtensionHelper::getAllExtensionsByNamespace('PaymentGateways');
|
||||||
$extensions = glob(app_path() . '/Extensions/PaymentGateways/*', GLOB_ONLYDIR);
|
|
||||||
|
|
||||||
// build a paymentgateways array that contains the routes for the payment gateways and the image path for the payment gateway which lays in public/images/Extensions/PaymentGateways with the extensionname in lowercase
|
// build a paymentgateways array that contains the routes for the payment gateways and the image path for the payment gateway which lays in public/images/Extensions/PaymentGateways with the extensionname in lowercase
|
||||||
$paymentGateways = [];
|
$paymentGateways = [];
|
||||||
foreach ($extensions as $extension) {
|
foreach ($extensions as $extension) {
|
||||||
$extensionName = basename($extension);
|
$extensionName = basename($extension);
|
||||||
$config = ExtensionHelper::getExtensionConfig($extensionName, 'PaymentGateways');
|
|
||||||
if ($config) {
|
|
||||||
$payment = new \stdClass();
|
$payment = new \stdClass();
|
||||||
$payment->name = $config['name'];
|
$payment->name = ExtensionHelper::getExtensionConfig($extensionName, 'name');
|
||||||
$payment->image = asset('images/Extensions/PaymentGateways/' . strtolower($extensionName) . '_logo.png');
|
$payment->image = asset('images/Extensions/PaymentGateways/' . strtolower($extensionName) . '_logo.png');
|
||||||
$paymentGateways[] = $payment;
|
$paymentGateways[] = $payment;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
$discount = PartnerDiscount::getDiscount();
|
$discount = PartnerDiscount::getDiscount();
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue