ExtensionHelper.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. <?php
  2. namespace App\Helpers;
  3. /**
  4. * Summary of ExtensionHelper
  5. */
  6. class ExtensionHelper
  7. {
  8. /**
  9. * Get all extensions
  10. * @return array array of all extensions e.g. ["App\Extensions\PayPal", "App\Extensions\Stripe"]
  11. */
  12. public static function getAllExtensions()
  13. {
  14. $extensionNamespaces = glob(app_path() . '/Extensions/*', GLOB_ONLYDIR);
  15. $extensions = [];
  16. foreach ($extensionNamespaces as $extensionNamespace) {
  17. $extensions = array_merge($extensions, glob($extensionNamespace . '/*', GLOB_ONLYDIR));
  18. }
  19. // remove base path from every extension but keep app/Extensions/...
  20. $extensions = array_map(fn ($item) => str_replace(app_path() . '/', 'App/', $item), $extensions);
  21. return $extensions;
  22. }
  23. /**
  24. * Get all extensions by namespace
  25. * @param string $namespace case sensitive namespace of the extension e.g. PaymentGateways
  26. * @return array array of all extensions e.g. ["App\Extensions\PayPal", "App\Extensions\Stripe"]
  27. */
  28. public static function getAllExtensionsByNamespace(string $namespace)
  29. {
  30. $extensions = glob(app_path() . '/Extensions/' . $namespace . '/*', GLOB_ONLYDIR);
  31. // remove base path from every extension but keep app/Extensions/...
  32. $extensions = array_map(fn ($item) => str_replace(app_path() . '/', 'App/', $item), $extensions);
  33. return $extensions;
  34. }
  35. /**
  36. * Get an extension by its name
  37. * @param string $extensionName case sensitive name of the extension e.g. PayPal
  38. * @return string|null the path of the extension e.g. App\Extensions\PayPal
  39. */
  40. public static function getExtension(string $extensionName)
  41. {
  42. $extensions = self::getAllExtensions();
  43. // filter the extensions by the extension name
  44. $extensions = array_filter($extensions, fn ($item) => basename($item) == $extensionName);
  45. // return the only extension
  46. return array_shift($extensions);
  47. }
  48. /**
  49. * Get all extension classes
  50. * @return array array of all extension classes e.g. ["App\Extensions\PayPal\PayPalExtension", "App\Extensions\Stripe\StripeExtension"]
  51. */
  52. public static function getAllExtensionClasses()
  53. {
  54. $extensions = self::getAllExtensions();
  55. // replace all slashes with backslashes
  56. $extensions = array_map(fn ($item) => str_replace('/', '\\', $item), $extensions);
  57. // add the ExtensionClass to the end of the namespace
  58. $extensions = array_map(fn ($item) => $item . '\\' . basename($item) . 'Extension', $extensions);
  59. // filter out non existing extension classes
  60. $extensions = array_filter($extensions, fn ($item) => class_exists($item));
  61. return $extensions;
  62. }
  63. /**
  64. * Get all extension classes by namespace
  65. * @param string $namespace case sensitive namespace of the extension e.g. PaymentGateways
  66. * @return array array of all extension classes e.g. ["App\Extensions\PayPal\PayPalExtension", "App\Extensions\Stripe\StripeExtension"]
  67. */
  68. public static function getAllExtensionClassesByNamespace(string $namespace)
  69. {
  70. $extensions = self::getAllExtensionsByNamespace($namespace);
  71. // replace all slashes with backslashes
  72. $extensions = array_map(fn ($item) => str_replace('/', '\\', $item), $extensions);
  73. // add the ExtensionClass to the end of the namespace
  74. $extensions = array_map(fn ($item) => $item . '\\' . basename($item) . 'Extension', $extensions);
  75. // filter out non existing extension classes
  76. $extensions = array_filter($extensions, fn ($item) => class_exists($item));
  77. return $extensions;
  78. }
  79. /**
  80. * Get the class of an extension by its name
  81. * @param string $extensionName case sensitive name of the extension e.g. PayPal
  82. * @return string|null the class name of the extension e.g. App\Extensions\PayPal\PayPalExtension
  83. */
  84. public static function getExtensionClass(string $extensionName)
  85. {
  86. $extensions = self::getAllExtensions();
  87. foreach ($extensions as $extension) {
  88. if (!(basename($extension) == $extensionName)) {
  89. continue;
  90. }
  91. $extension = str_replace('/', '\\', $extension);
  92. $extensionClass = $extension . '\\' . $extensionName . 'Extension';
  93. return $extensionClass;
  94. }
  95. }
  96. /**
  97. * Get a config of an extension by its name
  98. * @param string $extensionName
  99. * @param string $configname
  100. */
  101. public static function getExtensionConfig(string $extensionName, string $configname)
  102. {
  103. $extension = self::getExtensionClass($extensionName);
  104. $config = $extension::getConfig();
  105. if (isset($config[$configname])) {
  106. return $config[$configname];
  107. }
  108. return null;
  109. }
  110. public static function getAllCsrfIgnoredRoutes()
  111. {
  112. $extensions = self::getAllExtensionClasses();
  113. $routes = [];
  114. foreach ($extensions as $extension) {
  115. $config = $extension::getConfig();
  116. if (isset($config['RoutesIgnoreCsrf'])) {
  117. $routes = array_merge($routes, $config['RoutesIgnoreCsrf']);
  118. }
  119. }
  120. // map over the routes and add the extension name as prefix
  121. $result = array_map(fn ($item) => "extensions/{$item}", $routes);
  122. return $result;
  123. }
  124. /**
  125. * Summary of getAllExtensionMigrations
  126. * @return array of all migration paths look like: app/Extensions/ExtensionNamespace/ExtensionName/migrations/
  127. */
  128. public static function getAllExtensionMigrations()
  129. {
  130. $extensions = self::getAllExtensions();
  131. // Transform the extensions to a path
  132. $extensions = array_map(fn ($item) => self::extensionNameToPath($item), $extensions);
  133. // get all migration directories of the extensions and return them as array
  134. $migrations = [];
  135. foreach ($extensions as $extension) {
  136. $migrationDir = $extension . '/migrations';
  137. if (file_exists($migrationDir)) {
  138. $migrations[] = $migrationDir;
  139. }
  140. }
  141. return $migrations;
  142. }
  143. /**
  144. * Summary of getAllExtensionSettings
  145. * @return array of all setting classes look like: App\Extensions\PaymentGateways\PayPal\PayPalSettings
  146. */
  147. public static function getAllExtensionSettingsClasses()
  148. {
  149. $extensions = self::getAllExtensions();
  150. $settings = [];
  151. foreach ($extensions as $extension) {
  152. $extensionName = basename($extension);
  153. // replace all slashes with backslashes
  154. $extension = str_replace('/', '\\', $extension);
  155. $settingsClass = $extension . '\\' . $extensionName . 'Settings';
  156. if (class_exists($settingsClass)) {
  157. $settings[] = $settingsClass;
  158. }
  159. }
  160. return $settings;
  161. }
  162. public static function getExtensionSettings(string $extensionName)
  163. {
  164. $extension = self::getExtension($extensionName);
  165. // replace all slashes with backslashes
  166. $extension = str_replace('/', '\\', $extension);
  167. $settingClass = $extension . '\\' . $extensionName . 'Settings';
  168. if (class_exists($settingClass)) {
  169. return new $settingClass();
  170. }
  171. }
  172. /**
  173. * Transforms a extension name to a path
  174. * @param string $extensionName e.g. App\Extensions\PaymentGateways\PayPal
  175. * @return string e.g. C:\xampp\htdocs\laravel\app/Extensions/PaymentGateways/PayPal
  176. */
  177. private static function extensionNameToPath(string $extensionName)
  178. {
  179. return app_path() . '/' . str_replace('App/', '', $extensionName);
  180. }
  181. }