|
@@ -8,88 +8,154 @@ namespace App\Helpers;
|
|
|
class ExtensionHelper
|
|
|
{
|
|
|
/**
|
|
|
- * Get a config of an extension by its name
|
|
|
- * @param string $extensionName
|
|
|
- * @param string $configname
|
|
|
+ * Get all extensions
|
|
|
+ * @return array array of all extensions e.g. ["App\Extensions\PayPal", "App\Extensions\Stripe"]
|
|
|
*/
|
|
|
- public static function getExtensionConfig(string $extensionName, string $configname)
|
|
|
+ public static function getAllExtensions()
|
|
|
+ {
|
|
|
+ $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);
|
|
|
+
|
|
|
+ return $extensions;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 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 = ExtensionHelper::getAllExtensions();
|
|
|
+ $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);
|
|
|
+
|
|
|
+ return $extensions;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 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);
|
|
|
+
|
|
|
+ // return the only extension
|
|
|
+ return array_shift($extensions);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Get all extension classes
|
|
|
+ * @return array array of all extension classes e.g. ["App\Extensions\PayPal\PayPalExtension", "App\Extensions\Stripe\StripeExtension"]
|
|
|
+ */
|
|
|
+ public static function getAllExtensionClasses()
|
|
|
+ {
|
|
|
+ $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));
|
|
|
+
|
|
|
+ return $extensions;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 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"]
|
|
|
+ */
|
|
|
+ public static function getAllExtensionClassesByNamespace(string $namespace)
|
|
|
+ {
|
|
|
+ $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));
|
|
|
+
|
|
|
+ return $extensions;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 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
|
|
|
+ */
|
|
|
+ public static function getExtensionClass(string $extensionName)
|
|
|
+ {
|
|
|
+ $extensions = self::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");
|
|
|
- }
|
|
|
+ $extensionClass = $extension . '\\' . $extensionName . 'Extension';
|
|
|
+ return $extensionClass;
|
|
|
+ }
|
|
|
+ }
|
|
|
|
|
|
|
|
|
- if (isset($config[$configname])) {
|
|
|
- return $config[$configname];
|
|
|
- }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Get a config of an extension by its name
|
|
|
+ * @param string $extensionName
|
|
|
+ * @param string $configname
|
|
|
+ */
|
|
|
+ public static function getExtensionConfig(string $extensionName, string $configname)
|
|
|
+ {
|
|
|
+
|
|
|
+ $extension = self::getExtensionClass($extensionName);
|
|
|
+
|
|
|
+ $config = $extension::getConfig();
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ if (isset($config[$configname])) {
|
|
|
+ return $config[$configname];
|
|
|
}
|
|
|
|
|
|
+
|
|
|
return null;
|
|
|
}
|
|
|
|
|
|
public static function getAllCsrfIgnoredRoutes()
|
|
|
{
|
|
|
- $extensions = ExtensionHelper::getAllExtensions();
|
|
|
+ $extensions = self::getAllExtensionClasses();
|
|
|
|
|
|
$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");
|
|
|
- }
|
|
|
+ $config = $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);
|
|
|
}
|
|
|
+ // 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();
|
|
|
+ $extensions = self::getAllExtensions();
|
|
|
+ // Transform the extensions to a path
|
|
|
+ $extensions = array_map(fn ($item) => self::extensionNameToPath($item), $extensions);
|
|
|
|
|
|
// get all migration directories of the extensions and return them as array
|
|
|
$migrations = [];
|
|
@@ -109,21 +175,15 @@ class ExtensionHelper
|
|
|
*/
|
|
|
public static function getAllExtensionSettingsClasses()
|
|
|
{
|
|
|
- $extensions = ExtensionHelper::getAllExtensions();
|
|
|
+ $extensions = self::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;
|
|
|
+ $settingsClass = $extension . '\\' . $extensionName . 'Settings';
|
|
|
+ if (class_exists($settingsClass)) {
|
|
|
+ $settings[] = $settingsClass;
|
|
|
}
|
|
|
}
|
|
|
|
|
@@ -132,25 +192,21 @@ class ExtensionHelper
|
|
|
|
|
|
public static function getExtensionSettings(string $extensionName)
|
|
|
{
|
|
|
- $extensions = ExtensionHelper::getAllExtensions();
|
|
|
+ $extension = self::getExtension($extensionName);
|
|
|
+ $settingClass = $extension . '\\' . $extensionName . 'Settings';
|
|
|
|
|
|
- // 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();
|
|
|
- }
|
|
|
+ if (class_exists($settingClass)) {
|
|
|
+ return new $settingClass();
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 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));
|
|
|
+ }
|
|
|
}
|