forked from ProcessMaker/processmaker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPackageHelper.php
More file actions
102 lines (82 loc) · 3.88 KB
/
PackageHelper.php
File metadata and controls
102 lines (82 loc) · 3.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
<?php
namespace ProcessMaker;
use Illuminate\Support\Str;
/**
* A way to check if a package is installed by seeing if its service provider
* class exists. This should be computationally cheaper than the method used in the
* about page (but doesn't provide the extra data that the about page has).
*
* The constants below are added as a convenience, and are not a complete list of package
* service providers. They do drive the "magic" method way of using this helper, though.
*
* You can use this helper by:
* - using the magic methods:
* e.g. PackageHelper::isPmPackageProcessDocumenterInstalled()
* - passing the constants directly:
* e.g. PackageHelper::isPackageInstalled(PackageHelper::PM_DOCKER_EXECUTOR_LUA)
* - passing your own service provider class string:
* e.g. PackageHelper::isPackageInstalled('ProcessMaker\Package\WebEntry\WebEntryServiceProvider')
*
* We don't use the ::class way to get the service provider's classname string, since
* that would fail if the class is not installed.
*
*
* @method static bool isPmDockerExecutorLuaInstalled()
* @method static bool isPmDockerExecutorNodeInstalled()
* @method static bool isPmDockerExecutorPhpInstalled()
* @method static bool isPmPackageProcessDocumenterInstalled()
* @method static bool isPmPackageWebentryInstalled()
*/
class PackageHelper
{
const PM_DOCKER_EXECUTOR_LUA = 'ProcessMaker\Package\DockerExecutorLua\DockerExecutorLuaServiceProvider';
const PM_DOCKER_EXECUTOR_NODE = 'ProcessMaker\Package\DockerExecutorNode\DockerExecutorNodeServiceProvider';
const PM_DOCKER_EXECUTOR_PHP = 'ProcessMaker\Package\DockerExecutorPhp\DockerExecutorPhpServiceProvider';
const PM_PACKAGE_PROCESS_DOCUMENTER = 'ProcessMaker\Package\PackageProcessDocumenter\PackageServiceProvider';
const PM_PACKAGE_WEBENTRY = 'ProcessMaker\Package\WebEntry\WebEntryServiceProvider';
const PM_PACKAGE_VERSIONS = 'ProcessMaker\Package\Versions\PluginServiceProvider';
const PM_PACKAGE_PROJECTS = 'ProcessMaker\Package\Projects\PackageServiceProvider';
const PM_PACKAGE_DATA_SOURCES = 'ProcessMaker\Packages\Connectors\DataSources\PluginServiceProvider';
const PM_PACKAGE_DECISION_ENGINE = 'ProcessMaker\Package\PackageDecisionEngine\PackageServiceProvider';
const PM_PACKAGE_AB_TESTING = 'ProcessMaker\Package\PackageABTesting\PackageServiceProvider';
const PM_PACKAGE_AI = 'ProcessMaker\Packages\PackageAi\AiServiceProvider';
const PM_PACKAGE_COLLECTIONS = 'ProcessMaker\Plugins\Collections\PluginServiceProvider';
public static function isPackageInstalled(string $serviceProviderClass): bool
{
if (!$serviceProviderClass) {
return false;
}
return class_exists($serviceProviderClass);
}
/**
* Handle "magic" invocation.
*
* @param $methodName
* @param $parameters
* @return bool
* @throws \Exception
*/
public static function __callStatic(string $methodName, array $parameters): bool
{
$matches = [];
$matchesMagicMethodSignature = preg_match('/^is(.+)Installed$/', $methodName, $matches);
if ($matchesMagicMethodSignature) {
$constantName = self::magicNameToConstantName($matches[1]);
if (!defined('self::' . $constantName)) {
throw new \Exception(
sprintf('%s: No constant named \'%s\' defined.', self::class, $constantName)
);
}
return self::isPackageInstalled(constant('self::' . $constantName));
}
throw new \Exception(sprintf('%s: No function named \'%s\' defined.', self::class, $methodName));
}
/**
* Turns the inside of a magic method invocation into its
* symbolic constant form (i.e. ALL_UPPER_SNAKE).
*/
private static function magicNameToConstantName(string $magicName): string
{
return strtoupper(Str::snake($magicName));
}
}