ExtensionHelper.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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('/', '\\', 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('/', '\\', 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. // add the ExtensionClass to the end of the namespace
  56. $extensions = array_map(fn ($item) => $item . '\\' . basename($item) . 'Extension', $extensions);
  57. // filter out non existing extension classes
  58. $extensions = array_filter($extensions, fn ($item) => class_exists($item));
  59. return $extensions;
  60. }
  61. /**
  62. * Get all extension classes by namespace
  63. * @param string $namespace case sensitive namespace of the extension e.g. PaymentGateways
  64. * @return array array of all extension classes e.g. ["App\Extensions\PayPal\PayPalExtension", "App\Extensions\Stripe\StripeExtension"]
  65. */
  66. public static function getAllExtensionClassesByNamespace(string $namespace)
  67. {
  68. $extensions = self::getAllExtensionsByNamespace($namespace);
  69. // add the ExtensionClass to the end of the namespace
  70. $extensions = array_map(fn ($item) => $item . '\\' . basename($item) . 'Extension', $extensions);
  71. // filter out non existing extension classes
  72. $extensions = array_filter($extensions, fn ($item) => class_exists($item));
  73. return $extensions;
  74. }
  75. /**
  76. * Get the class of an extension by its name
  77. * @param string $extensionName case sensitive name of the extension e.g. PayPal
  78. * @return string|null the class name of the extension e.g. App\Extensions\PayPal\PayPalExtension
  79. */
  80. public static function getExtensionClass(string $extensionName)
  81. {
  82. $extensions = self::getAllExtensions();
  83. foreach ($extensions as $extension) {
  84. if (!(basename($extension) == $extensionName)) {
  85. continue;
  86. }
  87. $extensionClass = $extension . '\\' . $extensionName . 'Extension';
  88. return $extensionClass;
  89. }
  90. }
  91. /**
  92. * Get a config of an extension by its name
  93. * @param string $extensionName
  94. * @param string $configname
  95. */
  96. public static function getExtensionConfig(string $extensionName, string $configname)
  97. {
  98. $extension = self::getExtensionClass($extensionName);
  99. $config = $extension::getConfig();
  100. if (isset($config[$configname])) {
  101. return $config[$configname];
  102. }
  103. return null;
  104. }
  105. public static function getAllCsrfIgnoredRoutes()
  106. {
  107. $extensions = self::getAllExtensionClasses();
  108. $routes = [];
  109. foreach ($extensions as $extension) {
  110. $config = $extension::getConfig();
  111. if (isset($config['RoutesIgnoreCsrf'])) {
  112. $routes = array_merge($routes, $config['RoutesIgnoreCsrf']);
  113. }
  114. }
  115. // map over the routes and add the extension name as prefix
  116. $result = array_map(fn ($item) => "extensions/{$item}", $routes);
  117. return $result;
  118. }
  119. /**
  120. * Summary of getAllExtensionMigrations
  121. * @return array of all migration paths look like: app/Extensions/ExtensionNamespace/ExtensionName/migrations/
  122. */
  123. public static function getAllExtensionMigrations()
  124. {
  125. $extensions = self::getAllExtensions();
  126. // Transform the extensions to a path
  127. $extensions = array_map(fn ($item) => self::extensionNameToPath($item), $extensions);
  128. // get all migration directories of the extensions and return them as array
  129. $migrations = [];
  130. foreach ($extensions as $extension) {
  131. $migrationDir = $extension . '/migrations';
  132. if (file_exists($migrationDir)) {
  133. $migrations[] = $migrationDir;
  134. }
  135. }
  136. return $migrations;
  137. }
  138. /**
  139. * Summary of getAllExtensionSettings
  140. * @return array of all setting classes look like: App\Extensions\PaymentGateways\PayPal\PayPalSettings
  141. */
  142. public static function getAllExtensionSettingsClasses()
  143. {
  144. $extensions = self::getAllExtensions();
  145. $settings = [];
  146. foreach ($extensions as $extension) {
  147. $extensionName = basename($extension);
  148. $settingsClass = $extension . '\\' . $extensionName . 'Settings';
  149. if (class_exists($settingsClass)) {
  150. $settings[] = $settingsClass;
  151. }
  152. }
  153. return $settings;
  154. }
  155. public static function getExtensionSettings(string $extensionName)
  156. {
  157. $extension = self::getExtension($extensionName);
  158. $settingClass = $extension . '\\' . $extensionName . 'Settings';
  159. if (class_exists($settingClass)) {
  160. return new $settingClass();
  161. }
  162. }
  163. /**
  164. * Transforms a extension name to a path
  165. * @param string $extensionName e.g. App\Extensions\PaymentGateways\PayPal
  166. * @return string e.g. C:\xampp\htdocs\laravel\app/Extensions/PaymentGateways/PayPal
  167. */
  168. private static function extensionNameToPath(string $extensionName)
  169. {
  170. return app_path() . '/' . str_replace('\\', '/', str_replace('App\\', '', $extensionName));
  171. }
  172. }