ExtensionHelper.php 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. <?php
  2. namespace App\Helpers;
  3. class ExtensionHelper
  4. {
  5. /**
  6. * Get a config of an extension by its name
  7. * @param string $extensionName
  8. * @param string $configname
  9. */
  10. public static function getExtensionConfig(string $extensionName, string $configname)
  11. {
  12. $extensions = ExtensionHelper::getAllExtensions();
  13. // call the getConfig function of the config file of the extension like that
  14. // call_user_func("App\\Extensions\\PaymentGateways\\Stripe" . "\\getConfig");
  15. foreach ($extensions as $extension) {
  16. if (!(basename($extension) == $extensionName)) {
  17. continue;
  18. }
  19. $configFile = $extension . '/config.php';
  20. if (file_exists($configFile)) {
  21. include_once $configFile;
  22. $config = call_user_func('App\\Extensions\\' . basename(dirname($extension)) . '\\' . basename($extension) . "\\getConfig");
  23. }
  24. if (isset($config[$configname])) {
  25. return $config[$configname];
  26. }
  27. }
  28. return null;
  29. }
  30. public static function getAllCsrfIgnoredRoutes()
  31. {
  32. $extensions = ExtensionHelper::getAllExtensions();
  33. $routes = [];
  34. foreach ($extensions as $extension) {
  35. $configFile = $extension . '/config.php';
  36. if (file_exists($configFile)) {
  37. include_once $configFile;
  38. $config = call_user_func('App\\Extensions\\' . basename(dirname($extension)) . '\\' . basename($extension) . "\\getConfig");
  39. }
  40. if (isset($config['RoutesIgnoreCsrf'])) {
  41. $routes = array_merge($routes, $config['RoutesIgnoreCsrf']);
  42. }
  43. // map over the routes and add the extension name as prefix
  44. $result = array_map(fn ($item) => "extensions/{$item}", $routes);
  45. }
  46. return $result;
  47. }
  48. /**
  49. * Get all extensions
  50. * @return array
  51. */
  52. public static function getAllExtensions()
  53. {
  54. $extensionNamespaces = glob(app_path() . '/Extensions/*', GLOB_ONLYDIR);
  55. $extensions = [];
  56. foreach ($extensionNamespaces as $extensionNamespace) {
  57. $extensions = array_merge($extensions, glob($extensionNamespace . '/*', GLOB_ONLYDIR));
  58. }
  59. return $extensions;
  60. }
  61. public static function getAllExtensionsByNamespace(string $namespace)
  62. {
  63. $extensions = glob(app_path() . '/Extensions/' . $namespace . '/*', GLOB_ONLYDIR);
  64. return $extensions;
  65. }
  66. }