ExtensionHelper.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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. // add extension/ infront of every route
  44. foreach ($routes as $key => $route) {
  45. $routes[$key] = 'extensions/' . $route;
  46. }
  47. }
  48. return $routes;
  49. }
  50. /**
  51. * Get all extensions
  52. * @return array
  53. */
  54. public static function getAllExtensions()
  55. {
  56. $extensionNamespaces = glob(app_path() . '/Extensions/*', GLOB_ONLYDIR);
  57. $extensions = [];
  58. foreach ($extensionNamespaces as $extensionNamespace) {
  59. $extensions = array_merge($extensions, glob($extensionNamespace . '/*', GLOB_ONLYDIR));
  60. }
  61. return $extensions;
  62. }
  63. public static function getAllExtensionsByNamespace(string $namespace)
  64. {
  65. $extensions = glob(app_path() . '/Extensions/' . $namespace . '/*', GLOB_ONLYDIR);
  66. return $extensions;
  67. }
  68. }