1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types = 1); |
4
|
|
|
|
5
|
|
|
namespace PhpTaskman\Core; |
6
|
|
|
|
7
|
|
|
use Composer\Autoload\ClassLoader; |
8
|
|
|
use Consolidation\Config\ConfigInterface; |
9
|
|
|
use Consolidation\Config\Loader\ConfigProcessor; |
10
|
|
|
use League\Container\Container; |
11
|
|
|
use League\Container\ContainerInterface; |
12
|
|
|
use PhpTaskman\Core\Config\Loader\JsonConfigLoader; |
13
|
|
|
use Robo\Application; |
14
|
|
|
use Robo\Config\Config; |
15
|
|
|
use Robo\Robo; |
16
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
17
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Class Taskman. |
21
|
|
|
*/ |
22
|
|
|
final class Taskman |
23
|
|
|
{ |
24
|
|
|
public const APPLICATION_NAME = 'Taskman'; |
25
|
|
|
public const VERSION = 'dev-master'; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Create default configuration. |
29
|
|
|
* |
30
|
|
|
* @param mixed $paths |
31
|
|
|
* |
32
|
|
|
* @return \Consolidation\Config\ConfigInterface |
33
|
|
|
*/ |
34
|
3 |
|
public static function createConfiguration($paths) |
35
|
|
|
{ |
36
|
|
|
// Create a default configuration. |
37
|
3 |
|
$config = Robo::createConfiguration($paths); |
38
|
|
|
|
39
|
3 |
|
if (false !== $cwd = getcwd()) { |
40
|
3 |
|
$paths = \PhpTaskman\Core\Config\Config::findFilesToIncludeInConfiguration($cwd); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
// Load the configuration. |
44
|
3 |
|
Robo::loadConfiguration( |
45
|
3 |
|
$paths, |
46
|
3 |
|
$config |
47
|
|
|
); |
48
|
|
|
|
49
|
3 |
|
[$scriptPath] = get_included_files(); |
50
|
3 |
|
$config->setDefault('options.bin', $scriptPath); |
51
|
|
|
|
52
|
3 |
|
foreach (getenv() as $key => $value) { |
|
|
|
|
53
|
3 |
|
$config->setDefault('globals.env.' . $key, $value); |
54
|
|
|
} |
55
|
|
|
|
56
|
3 |
|
return $config; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Create and configure container. |
61
|
|
|
* |
62
|
|
|
* @param InputInterface $input |
63
|
|
|
* @param OutputInterface $output |
64
|
|
|
* @param Application $application |
65
|
|
|
* @param ConfigInterface $config |
66
|
|
|
* @param ClassLoader $classLoader |
67
|
|
|
* |
68
|
|
|
* @return Container|\League\Container\ContainerInterface |
69
|
|
|
*/ |
70
|
2 |
|
public static function createContainer( |
71
|
|
|
InputInterface $input, |
72
|
|
|
OutputInterface $output, |
73
|
|
|
Application $application, |
74
|
|
|
ConfigInterface $config, |
75
|
|
|
ClassLoader $classLoader |
76
|
|
|
) { |
77
|
2 |
|
$container = Robo::createDefaultContainer($input, $output, $application, $config, $classLoader); |
78
|
2 |
|
$container->get('commandFactory')->setIncludeAllPublicMethods(false); |
79
|
|
|
|
80
|
2 |
|
return $container; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @param null|string $appName |
85
|
|
|
* @param null|string $appVersion |
86
|
|
|
* @param null|string $workingDir |
87
|
|
|
* |
88
|
|
|
* @return Application |
89
|
|
|
*/ |
90
|
2 |
|
public static function createDefaultApplication($appName = null, $appVersion = null, $workingDir = null) |
|
|
|
|
91
|
|
|
{ |
92
|
2 |
|
$appName = $appName ?? self::APPLICATION_NAME; |
93
|
2 |
|
$appVersion = $appVersion ?? self::VERSION; |
94
|
|
|
|
95
|
2 |
|
$app = Robo::createDefaultApplication($appName, $appVersion); |
96
|
|
|
|
97
|
2 |
|
$app->setAutoExit(false); |
98
|
|
|
|
99
|
2 |
|
return $app; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* @param ContainerInterface $container |
104
|
|
|
* |
105
|
|
|
* @throws \Exception |
106
|
|
|
* |
107
|
|
|
* @return \Robo\Runner |
108
|
|
|
*/ |
109
|
1 |
|
public static function createDefaultRunner(ContainerInterface $container) |
110
|
|
|
{ |
111
|
1 |
|
$cwd = getcwd(); |
112
|
|
|
|
113
|
1 |
|
$workingDir = $container->get('input')->getParameterOption('--working-dir', $cwd); |
114
|
|
|
|
115
|
1 |
|
if (null === $workingDir) { |
116
|
|
|
$workingDir = $cwd; |
117
|
|
|
} |
118
|
|
|
|
119
|
1 |
|
if (false === realpath($workingDir)) { |
120
|
|
|
throw new \Exception(sprintf('Working directory "%s" does not exists.', $workingDir)); |
121
|
|
|
} |
122
|
|
|
|
123
|
1 |
|
return (new \Robo\Runner()) |
124
|
1 |
|
->setRelativePluginNamespace('Robo\Plugin') |
125
|
1 |
|
->setContainer($container); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* @param string[] $paths |
130
|
|
|
* Array of JSON filepaths. |
131
|
|
|
* |
132
|
|
|
* @return Config |
133
|
|
|
* A config object. |
134
|
|
|
*/ |
135
|
3 |
|
public static function createJsonConfiguration(array $paths) |
136
|
|
|
{ |
137
|
3 |
|
$config = new Config(); |
138
|
3 |
|
self::loadJsonConfiguration($paths, $config); |
139
|
|
|
|
140
|
3 |
|
return $config; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* @param string $relativeNamespace |
145
|
|
|
* |
146
|
|
|
* @return array|string[] |
147
|
|
|
*/ |
148
|
|
|
public static function discoverTasksClasses($relativeNamespace) |
149
|
|
|
{ |
150
|
|
|
/** @var \Robo\ClassDiscovery\RelativeNamespaceDiscovery $discovery */ |
151
|
|
|
$discovery = Robo::service('relativeNamespaceDiscovery'); |
152
|
|
|
$discovery->setRelativeNamespace($relativeNamespace . '\Task') |
153
|
|
|
->setSearchPattern('*Task.php'); |
154
|
|
|
|
155
|
|
|
return $discovery->getClasses(); |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* @param string[] $paths |
160
|
|
|
* Array of JSON filepaths. |
161
|
|
|
* @param null|Config $config |
162
|
|
|
* A config object. |
163
|
|
|
*/ |
164
|
3 |
|
public static function loadJsonConfiguration(array $paths, ?Config $config): void |
165
|
|
|
{ |
166
|
3 |
|
if (null === $config) { |
167
|
|
|
// This needs to be removed when Robo will have the method replace() |
168
|
|
|
// in the ConfigInterface interface. |
169
|
|
|
/** @var Config $config */ |
170
|
|
|
$config = Robo::config(); |
171
|
|
|
} |
172
|
|
|
|
173
|
3 |
|
$loader = new JsonConfigLoader(); |
174
|
3 |
|
$processor = new ConfigProcessor(); |
175
|
3 |
|
$processor->add($config->export()); |
176
|
|
|
|
177
|
3 |
|
foreach ($paths as $path) { |
178
|
3 |
|
$processor->extend($loader->load($path)); |
179
|
|
|
} |
180
|
|
|
|
181
|
3 |
|
$config->replace($processor->export()); |
182
|
3 |
|
} |
183
|
|
|
} |
184
|
|
|
|