|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace SilverLeague\Console\Framework\Utility; |
|
4
|
|
|
|
|
5
|
|
|
use ReflectionClass; |
|
6
|
|
|
use ReflectionException; |
|
7
|
|
|
use SilverStripe\Core\Config\Config; |
|
8
|
|
|
use SilverStripe\Core\Manifest\ModuleManifest; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* Utility methods for handling SilverStripe Objects |
|
12
|
|
|
* |
|
13
|
|
|
* @package silverstripe-console |
|
14
|
|
|
* @author Robbie Averill <[email protected]> |
|
15
|
|
|
*/ |
|
16
|
|
|
trait ObjectUtilities |
|
17
|
|
|
{ |
|
18
|
|
|
/** |
|
19
|
|
|
* Get a ReflectionClass from the given class name |
|
20
|
|
|
* |
|
21
|
|
|
* @param string $className |
|
22
|
|
|
* @return ReflectionClass|false |
|
23
|
|
|
*/ |
|
24
|
|
|
public function getReflection($className) |
|
25
|
|
|
{ |
|
26
|
|
|
try { |
|
27
|
|
|
return new ReflectionClass($className); |
|
28
|
|
|
} catch (ReflectionException $ex) { |
|
29
|
|
|
return false; |
|
30
|
|
|
} |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* Given a class name, find and return which module it belongs to |
|
35
|
|
|
* |
|
36
|
|
|
* @param string $className |
|
37
|
|
|
* @return string |
|
38
|
|
|
*/ |
|
39
|
|
|
public function getModuleName($className) |
|
40
|
|
|
{ |
|
41
|
|
|
$class = $this->getReflection($className); |
|
42
|
|
|
if (!$class) { |
|
43
|
|
|
return ''; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
$relativePath = ltrim(substr($class->getFileName(), strlen(SILVERSTRIPE_ROOT_DIR)), '/'); |
|
47
|
|
|
$folders = explode('/', $relativePath); |
|
48
|
|
|
|
|
49
|
|
|
// Handle root level modules |
|
50
|
|
|
$folder = array_shift($folders); |
|
51
|
|
|
|
|
52
|
|
|
if (empty($folder)) { |
|
53
|
|
|
return ''; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
// Handle vendor modules |
|
57
|
|
|
if ($folder === 'vendor') { |
|
58
|
|
|
$folder .= '/' . array_shift($folders) . '/' . array_shift($folders); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
$composerConfig = $this->getModuleComposerConfiguration($folder); |
|
62
|
|
|
return !empty($composerConfig['name']) ? $composerConfig['name'] : ''; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* Given a folder name, load a possible composer.json configuration from inside it. This method will handle |
|
67
|
|
|
* the main project folder e.g. "mysite" by looking in the root folder rather than the folder itself. |
|
68
|
|
|
* |
|
69
|
|
|
* @param string $folderName |
|
70
|
|
|
* @return array |
|
71
|
|
|
*/ |
|
72
|
|
|
public function getModuleComposerConfiguration($folderName) |
|
73
|
|
|
{ |
|
74
|
|
|
$project = Config::inst()->get(ModuleManifest::class, 'project'); |
|
75
|
|
|
if (!empty($project) && $project === $folderName) { |
|
76
|
|
|
$folderName = '.'; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
$filePath = $folderName . '/composer.json'; |
|
80
|
|
|
if (!file_exists($filePath) || !is_readable($filePath)) { |
|
81
|
|
|
return []; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
$composerConfig = file_get_contents($filePath); |
|
85
|
|
|
if (!$composerConfig) { |
|
86
|
|
|
return []; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
return json_decode($composerConfig, true) ?: []; |
|
90
|
|
|
} |
|
91
|
|
|
} |
|
92
|
|
|
|