ExtensionHelper.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. <?php
  2. namespace App\Helpers;
  3. /**
  4. * Summary of ExtensionHelper
  5. */
  6. class ExtensionHelper
  7. {
  8. /**
  9. * Get a config of an extension by its name
  10. * @param string $extensionName
  11. * @param string $configname
  12. */
  13. public static function getExtensionConfig(string $extensionName, string $configname)
  14. {
  15. $extensions = ExtensionHelper::getAllExtensions();
  16. // call the getConfig function of the config file of the extension like that
  17. // call_user_func("App\\Extensions\\PaymentGateways\\Stripe" . "\\getConfig");
  18. foreach ($extensions as $extension) {
  19. if (!(basename($extension) == $extensionName)) {
  20. continue;
  21. }
  22. $configFile = $extension . '/config.php';
  23. if (file_exists($configFile)) {
  24. include_once $configFile;
  25. $config = call_user_func('App\\Extensions\\' . basename(dirname($extension)) . '\\' . basename($extension) . "\\getConfig");
  26. }
  27. if (isset($config[$configname])) {
  28. return $config[$configname];
  29. }
  30. }
  31. return null;
  32. }
  33. public static function getAllCsrfIgnoredRoutes()
  34. {
  35. $extensions = ExtensionHelper::getAllExtensions();
  36. $routes = [];
  37. foreach ($extensions as $extension) {
  38. $configFile = $extension . '/config.php';
  39. if (file_exists($configFile)) {
  40. include_once $configFile;
  41. $config = call_user_func('App\\Extensions\\' . basename(dirname($extension)) . '\\' . basename($extension) . "\\getConfig");
  42. }
  43. if (isset($config['RoutesIgnoreCsrf'])) {
  44. $routes = array_merge($routes, $config['RoutesIgnoreCsrf']);
  45. }
  46. // map over the routes and add the extension name as prefix
  47. $result = array_map(fn ($item) => "extensions/{$item}", $routes);
  48. }
  49. return $result;
  50. }
  51. /**
  52. * Get all extensions
  53. * @return array of all extension paths look like: app/Extensions/ExtensionNamespace/ExtensionName
  54. */
  55. public static function getAllExtensions()
  56. {
  57. $extensionNamespaces = glob(app_path() . '/Extensions/*', GLOB_ONLYDIR);
  58. $extensions = [];
  59. foreach ($extensionNamespaces as $extensionNamespace) {
  60. $extensions = array_merge($extensions, glob($extensionNamespace . '/*', GLOB_ONLYDIR));
  61. }
  62. return $extensions;
  63. }
  64. public static function getAllExtensionsByNamespace(string $namespace)
  65. {
  66. $extensions = glob(app_path() . '/Extensions/' . $namespace . '/*', GLOB_ONLYDIR);
  67. return $extensions;
  68. }
  69. /**
  70. * Summary of getAllExtensionMigrations
  71. * @return array of all migration paths look like: app/Extensions/ExtensionNamespace/ExtensionName/migrations/
  72. */
  73. public static function getAllExtensionMigrations()
  74. {
  75. $extensions = ExtensionHelper::getAllExtensions();
  76. // get all migration directories of the extensions and return them as array
  77. $migrations = [];
  78. foreach ($extensions as $extension) {
  79. $migrationDir = $extension . '/migrations';
  80. if (file_exists($migrationDir)) {
  81. $migrations[] = $migrationDir;
  82. }
  83. }
  84. return $migrations;
  85. }
  86. /**
  87. * Summary of getAllExtensionSettings
  88. * @return array of all setting classes look like: App\Extensions\PaymentGateways\PayPal\PayPalSettings
  89. */
  90. public static function getAllExtensionSettingsClasses()
  91. {
  92. $extensions = ExtensionHelper::getAllExtensions();
  93. $settings = [];
  94. foreach ($extensions as $extension) {
  95. $extensionName = basename($extension);
  96. $settingFile = $extension . '/' . $extensionName . 'Settings.php';
  97. if (file_exists($settingFile)) {
  98. // remove the base path from the setting file path to get the namespace
  99. $settingFile = str_replace(app_path() . '/', '', $settingFile);
  100. $settingFile = str_replace('.php', '', $settingFile);
  101. $settingFile = str_replace('/', '\\', $settingFile);
  102. $settingFile = 'App\\' . $settingFile;
  103. $settings[] = $settingFile;
  104. }
  105. }
  106. return $settings;
  107. }
  108. public static function getExtensionSettings(string $extensionName)
  109. {
  110. $extensions = ExtensionHelper::getAllExtensions();
  111. // find the setting file of the extension and return an instance of it
  112. foreach ($extensions as $extension) {
  113. if (!(basename($extension) == $extensionName)) {
  114. continue;
  115. }
  116. $extensionName = basename($extension);
  117. $settingFile = $extension . '/' . $extensionName . 'Settings.php';
  118. if (file_exists($settingFile)) {
  119. // remove the base path from the setting file path to get the namespace
  120. $settingFile = str_replace(app_path() . '/', '', $settingFile);
  121. $settingFile = str_replace('.php', '', $settingFile);
  122. $settingFile = str_replace('/', '\\', $settingFile);
  123. $settingFile = 'App\\' . $settingFile;
  124. return new $settingFile();
  125. }
  126. }
  127. }
  128. }