|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace ProcessMaker; |
| 4 | + |
| 5 | +use Illuminate\Support\Str; |
| 6 | + |
| 7 | +/** |
| 8 | + * A way to check if a package is installed by seeing if its service provider |
| 9 | + * class exists. This should be computationally cheaper than the method used in the |
| 10 | + * about page (but doesn't provide the extra data that the about page has). |
| 11 | + * |
| 12 | + * The constants below are added as a convenience, and are not a complete list of package |
| 13 | + * service providers. They do drive the "magic" method way of using this helper, though. |
| 14 | + * |
| 15 | + * You can use this helper by: |
| 16 | + * - using the magic methods: |
| 17 | + * e.g. PackageHelper::isPmPackageProcessDocumenterInstalled() |
| 18 | + * - passing the constants directly: |
| 19 | + * e.g. PackageHelper::isPackageInstalled(PackageHelper::PM_DOCKER_EXECUTOR_LUA) |
| 20 | + * - passing your own service provider class string: |
| 21 | + * e.g. PackageHelper::isPackageInstalled('ProcessMaker\Package\WebEntry\WebEntryServiceProvider') |
| 22 | + * |
| 23 | + * We don't use the ::class way to get the service provider's classname string, since |
| 24 | + * that would fail if the class is not installed. |
| 25 | + * |
| 26 | + * @package ProcessMaker |
| 27 | + * |
| 28 | + * @method static bool isPmDockerExecutorLuaInstalled() |
| 29 | + * @method static bool isPmDockerExecutorNodeInstalled() |
| 30 | + * @method static bool isPmDockerExecutorPhpInstalled() |
| 31 | + * @method static bool isPmPackageProcessDocumenterInstalled() |
| 32 | + * @method static bool isPmPackageWebentryInstalled() |
| 33 | + */ |
| 34 | +class PackageHelper |
| 35 | +{ |
| 36 | + const PM_DOCKER_EXECUTOR_LUA = 'ProcessMaker\Package\DockerExecutorLua\DockerExecutorLuaServiceProvider'; |
| 37 | + const PM_DOCKER_EXECUTOR_NODE = 'ProcessMaker\Package\DockerExecutorNode\DockerExecutorNodeServiceProvider'; |
| 38 | + const PM_DOCKER_EXECUTOR_PHP = 'ProcessMaker\Package\DockerExecutorPhp\DockerExecutorPhpServiceProvider'; |
| 39 | + const PM_PACKAGE_PROCESS_DOCUMENTER = 'ProcessMaker\Package\PackageProcessDocumenter\PackageServiceProvider'; |
| 40 | + const PM_PACKAGE_WEBENTRY = 'ProcessMaker\Package\WebEntry\WebEntryServiceProvider'; |
| 41 | + |
| 42 | + public static function isPackageInstalled(string $serviceProviderClass): bool |
| 43 | + { |
| 44 | + if (! $serviceProviderClass) { |
| 45 | + return false; |
| 46 | + } |
| 47 | + |
| 48 | + return class_exists($serviceProviderClass); |
| 49 | + } |
| 50 | + |
| 51 | + /** |
| 52 | + * Handle "magic" invocation. |
| 53 | + * |
| 54 | + * @param $methodName |
| 55 | + * @param $parameters |
| 56 | + * @return bool |
| 57 | + * @throws \Exception |
| 58 | + */ |
| 59 | + public static function __callStatic(string $methodName, array $parameters): bool |
| 60 | + { |
| 61 | + $matches = []; |
| 62 | + $matchesMagicMethodSignature = preg_match('/^is(.+)Installed$/', $methodName, $matches); |
| 63 | + if ($matchesMagicMethodSignature) { |
| 64 | + $constantName = self::magicNameToConstantName($matches[1]); |
| 65 | + if (! defined('self::' . $constantName)) { |
| 66 | + throw new \Exception( |
| 67 | + sprintf('%s: No constant named \'%s\' defined.', self::class, $constantName) |
| 68 | + ); |
| 69 | + } |
| 70 | + return self::isPackageInstalled(constant('self::' . $constantName)); |
| 71 | + } |
| 72 | + |
| 73 | + throw new \Exception(sprintf('%s: No function named \'%s\' defined.', self::class, $methodName)); |
| 74 | + } |
| 75 | + |
| 76 | + /** |
| 77 | + * Turns the inside of a magic method invocation into its |
| 78 | + * symbolic constant form (i.e. ALL_UPPER_SNAKE). |
| 79 | + */ |
| 80 | + private static function magicNameToConstantName(string $magicName): string |
| 81 | + { |
| 82 | + return strtoupper(Str::snake($magicName)); |
| 83 | + } |
| 84 | +} |
0 commit comments