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
|
|
|
/**
|
2023-03-27 12:36:04 +00:00
|
|
|
* Get all extensions
|
|
|
|
* @return array array of all extensions e.g. ["App\Extensions\PayPal", "App\Extensions\Stripe"]
|
2023-01-21 00:03:33 +00:00
|
|
|
*/
|
2023-03-27 12:36:04 +00:00
|
|
|
public static function getAllExtensions()
|
2023-01-03 17:31:21 +00:00
|
|
|
{
|
2023-03-27 12:36:04 +00:00
|
|
|
$extensionNamespaces = glob(app_path() . '/Extensions/*', GLOB_ONLYDIR);
|
|
|
|
$extensions = [];
|
|
|
|
foreach ($extensionNamespaces as $extensionNamespace) {
|
|
|
|
$extensions = array_merge($extensions, glob($extensionNamespace . '/*', GLOB_ONLYDIR));
|
|
|
|
}
|
|
|
|
// remove base path from every extension but keep app/Extensions/...
|
|
|
|
$extensions = array_map(fn ($item) => str_replace('/', '\\', str_replace(app_path() . '/', 'App/', $item)), $extensions);
|
2023-01-03 17:31:21 +00:00
|
|
|
|
2023-03-27 12:36:04 +00:00
|
|
|
return $extensions;
|
|
|
|
}
|
2023-01-03 17:31:21 +00:00
|
|
|
|
2023-03-27 12:36:04 +00:00
|
|
|
/**
|
|
|
|
* Get all extensions by namespace
|
|
|
|
* @param string $namespace case sensitive namespace of the extension e.g. PaymentGateways
|
|
|
|
* @return array array of all extensions e.g. ["App\Extensions\PayPal", "App\Extensions\Stripe"]
|
|
|
|
*/
|
|
|
|
public static function getAllExtensionsByNamespace(string $namespace)
|
|
|
|
{
|
|
|
|
$extensions = glob(app_path() . '/Extensions/' . $namespace . '/*', GLOB_ONLYDIR);
|
|
|
|
// remove base path from every extension but keep app/Extensions/...
|
|
|
|
$extensions = array_map(fn ($item) => str_replace('/', '\\', str_replace(app_path() . '/', 'App/', $item)), $extensions);
|
2023-03-22 14:57:10 +00:00
|
|
|
|
2023-03-27 12:36:04 +00:00
|
|
|
return $extensions;
|
|
|
|
}
|
2023-01-03 17:31:21 +00:00
|
|
|
|
2023-03-27 12:36:04 +00:00
|
|
|
/**
|
|
|
|
* Get an extension by its name
|
|
|
|
* @param string $extensionName case sensitive name of the extension e.g. PayPal
|
|
|
|
* @return string|null the path of the extension e.g. App\Extensions\PayPal
|
|
|
|
*/
|
|
|
|
public static function getExtension(string $extensionName)
|
|
|
|
{
|
|
|
|
$extensions = self::getAllExtensions();
|
|
|
|
// filter the extensions by the extension name
|
|
|
|
$extensions = array_filter($extensions, fn ($item) => basename($item) == $extensionName);
|
2023-03-22 14:57:10 +00:00
|
|
|
|
2023-03-27 12:36:04 +00:00
|
|
|
// return the only extension
|
|
|
|
return array_shift($extensions);
|
2023-01-03 17:31:21 +00:00
|
|
|
}
|
2023-01-14 22:33:30 +00:00
|
|
|
|
2023-03-27 12:36:04 +00:00
|
|
|
/**
|
|
|
|
* Get all extension classes
|
|
|
|
* @return array array of all extension classes e.g. ["App\Extensions\PayPal\PayPalExtension", "App\Extensions\Stripe\StripeExtension"]
|
|
|
|
*/
|
2023-03-22 14:57:10 +00:00
|
|
|
public static function getAllExtensionClasses()
|
|
|
|
{
|
2023-03-27 12:36:04 +00:00
|
|
|
$extensions = self::getAllExtensions();
|
|
|
|
// add the ExtensionClass to the end of the namespace
|
|
|
|
$extensions = array_map(fn ($item) => $item . '\\' . basename($item) . 'Extension', $extensions);
|
|
|
|
// filter out non existing extension classes
|
|
|
|
$extensions = array_filter($extensions, fn ($item) => class_exists($item));
|
2023-03-22 14:57:10 +00:00
|
|
|
|
|
|
|
return $extensions;
|
|
|
|
}
|
|
|
|
|
2023-03-27 12:36:04 +00:00
|
|
|
/**
|
|
|
|
* Get all extension classes by namespace
|
|
|
|
* @param string $namespace case sensitive namespace of the extension e.g. PaymentGateways
|
|
|
|
* @return array array of all extension classes e.g. ["App\Extensions\PayPal\PayPalExtension", "App\Extensions\Stripe\StripeExtension"]
|
|
|
|
*/
|
2023-03-22 14:57:10 +00:00
|
|
|
public static function getAllExtensionClassesByNamespace(string $namespace)
|
|
|
|
{
|
2023-03-27 12:36:04 +00:00
|
|
|
$extensions = self::getAllExtensionsByNamespace($namespace);
|
|
|
|
// add the ExtensionClass to the end of the namespace
|
|
|
|
$extensions = array_map(fn ($item) => $item . '\\' . basename($item) . 'Extension', $extensions);
|
|
|
|
// filter out non existing extension classes
|
|
|
|
$extensions = array_filter($extensions, fn ($item) => class_exists($item));
|
2023-03-22 14:57:10 +00:00
|
|
|
|
|
|
|
return $extensions;
|
|
|
|
}
|
|
|
|
|
2023-03-27 12:36:04 +00:00
|
|
|
/**
|
|
|
|
* Get the class of an extension by its name
|
|
|
|
* @param string $extensionName case sensitive name of the extension e.g. PayPal
|
|
|
|
* @return string|null the class name of the extension e.g. App\Extensions\PayPal\PayPalExtension
|
|
|
|
*/
|
2023-03-22 14:57:10 +00:00
|
|
|
public static function getExtensionClass(string $extensionName)
|
|
|
|
{
|
|
|
|
$extensions = self::getAllExtensions();
|
|
|
|
|
|
|
|
foreach ($extensions as $extension) {
|
|
|
|
if (!(basename($extension) == $extensionName)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
$extensionClass = $extension . '\\' . $extensionName . 'Extension';
|
|
|
|
return $extensionClass;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-27 12:36:04 +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-03-22 15:11:53 +00:00
|
|
|
{
|
|
|
|
|
2023-03-27 12:36:04 +00:00
|
|
|
$extension = self::getExtensionClass($extensionName);
|
|
|
|
|
|
|
|
$config = $extension::getConfig();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (isset($config[$configname])) {
|
|
|
|
return $config[$configname];
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return null;
|
2023-03-22 15:11:53 +00:00
|
|
|
}
|
|
|
|
|
2023-01-21 00:03:33 +00:00
|
|
|
public static function getAllCsrfIgnoredRoutes()
|
2023-01-14 22:33:30 +00:00
|
|
|
{
|
2023-03-22 14:57:10 +00:00
|
|
|
$extensions = self::getAllExtensionClasses();
|
2023-01-21 00:03:33 +00:00
|
|
|
|
|
|
|
$routes = [];
|
2023-03-22 14:57:10 +00:00
|
|
|
|
2023-01-21 00:03:33 +00:00
|
|
|
foreach ($extensions as $extension) {
|
2023-03-22 14:57:10 +00:00
|
|
|
$config = $extension::getConfig();
|
2023-01-21 00:03:33 +00:00
|
|
|
|
|
|
|
if (isset($config['RoutesIgnoreCsrf'])) {
|
|
|
|
$routes = array_merge($routes, $config['RoutesIgnoreCsrf']);
|
|
|
|
}
|
|
|
|
}
|
2023-03-22 14:57:10 +00:00
|
|
|
// map over the routes and add the extension name as prefix
|
|
|
|
$result = array_map(fn ($item) => "extensions/{$item}", $routes);
|
|
|
|
|
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-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()
|
|
|
|
{
|
2023-03-27 12:36:04 +00:00
|
|
|
|
|
|
|
error_log(print_r(self::getAllCsrfIgnoredRoutes(), true));
|
|
|
|
|
2023-03-22 14:57:10 +00:00
|
|
|
$extensions = self::getAllExtensions();
|
2023-03-27 12:36:04 +00:00
|
|
|
// Transform the extensions to a path
|
|
|
|
$extensions = array_map(fn ($item) => self::extensionNameToPath($item), $extensions);
|
2023-03-04 14:20:49 +00:00
|
|
|
|
|
|
|
// 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
|
|
|
{
|
2023-03-22 14:57:10 +00:00
|
|
|
$extensions = self::getAllExtensions();
|
2023-03-04 18:59:59 +00:00
|
|
|
|
|
|
|
$settings = [];
|
|
|
|
foreach ($extensions as $extension) {
|
|
|
|
|
|
|
|
$extensionName = basename($extension);
|
2023-03-22 15:11:53 +00:00
|
|
|
$settingsClass = $extension . '\\' . $extensionName . 'Settings';
|
|
|
|
if (class_exists($settingsClass)) {
|
|
|
|
$settings[] = $settingsClass;
|
2023-03-04 18:59:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $settings;
|
|
|
|
}
|
2023-03-04 19:28:27 +00:00
|
|
|
|
|
|
|
public static function getExtensionSettings(string $extensionName)
|
|
|
|
{
|
2023-03-22 15:11:53 +00:00
|
|
|
$extension = self::getExtension($extensionName);
|
|
|
|
$settingClass = $extension . '/' . $extensionName . 'Settings';
|
2023-03-04 19:28:27 +00:00
|
|
|
|
2023-03-22 15:11:53 +00:00
|
|
|
if (class_exists($settingClass)) {
|
|
|
|
return new $settingClass();
|
2023-03-04 19:28:27 +00:00
|
|
|
}
|
|
|
|
}
|
2023-03-27 12:36:04 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Transforms a extension name to a path
|
|
|
|
* @param string $extensionName e.g. App\Extensions\PaymentGateways\PayPal
|
|
|
|
* @return string e.g. C:\xampp\htdocs\laravel\app/Extensions/PaymentGateways/PayPal
|
|
|
|
*/
|
|
|
|
private static function extensionNameToPath(string $extensionName)
|
|
|
|
{
|
|
|
|
return app_path() . '/' . str_replace('\\', '/', str_replace('App\\', '', $extensionName));
|
|
|
|
}
|
2023-01-15 00:39:38 +00:00
|
|
|
}
|