1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types = 1); |
4
|
|
|
|
5
|
|
|
namespace PhpTaskman\Core; |
6
|
|
|
|
7
|
|
|
use Composer\Autoload\ClassLoader; |
8
|
|
|
use Consolidation\Config\Loader\ConfigProcessor; |
9
|
|
|
use League\Container\Container; |
10
|
|
|
use League\Container\ContainerInterface; |
11
|
|
|
use PhpTaskman\Core\Config\Loader\JsonConfigLoader; |
12
|
|
|
use Robo\Application; |
13
|
|
|
use Robo\Config\Config; |
14
|
|
|
use Robo\Robo; |
15
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
16
|
|
|
use Symfony\Component\Console\Input\InputOption; |
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 null|mixed $workingDir |
31
|
|
|
* @param mixed $paths |
32
|
|
|
* |
33
|
|
|
* @return Config |
34
|
|
|
*/ |
35
|
3 |
|
public static function createConfiguration($paths, $workingDir = null) |
36
|
|
|
{ |
37
|
3 |
|
$workingDir = $workingDir ?? \getcwd(); |
38
|
|
|
|
39
|
|
|
// Create a default configuration. |
40
|
3 |
|
$config = Robo::createConfiguration($paths); |
41
|
|
|
|
42
|
|
|
// Set the variable working_dir. |
43
|
3 |
|
if (false === $workingDir = \realpath($workingDir)) { |
44
|
1 |
|
return $config; |
45
|
|
|
} |
46
|
|
|
|
47
|
3 |
|
$config->set('taskman.working_dir', $workingDir); |
48
|
|
|
|
49
|
3 |
|
$paths = \PhpTaskman\Core\Config\Config::findFilesToIncludeInConfiguration($workingDir); |
50
|
|
|
|
51
|
|
|
// Load the configuration. |
52
|
3 |
|
Robo::loadConfiguration( |
53
|
3 |
|
$paths, |
54
|
3 |
|
$config |
55
|
|
|
); |
56
|
|
|
|
57
|
3 |
|
return $config; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Create and configure container. |
62
|
|
|
* |
63
|
|
|
* @param InputInterface $input |
64
|
|
|
* @param OutputInterface $output |
65
|
|
|
* @param Application $application |
66
|
|
|
* @param Config $config |
67
|
|
|
* @param ClassLoader $classLoader |
68
|
|
|
* |
69
|
|
|
* @return Container|\League\Container\ContainerInterface |
70
|
|
|
*/ |
71
|
2 |
|
public static function createContainer( |
72
|
|
|
InputInterface $input, |
73
|
|
|
OutputInterface $output, |
74
|
|
|
Application $application, |
75
|
|
|
Config $config, |
76
|
|
|
ClassLoader $classLoader |
77
|
|
|
) { |
78
|
2 |
|
$container = Robo::createDefaultContainer($input, $output, $application, $config, $classLoader); |
79
|
2 |
|
$container->get('commandFactory')->setIncludeAllPublicMethods(false); |
80
|
|
|
|
81
|
2 |
|
return $container; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @param null|string $appName |
86
|
|
|
* @param null|string $appVersion |
87
|
|
|
* @param null|string $workingDir |
88
|
|
|
* |
89
|
|
|
* @return Application |
90
|
|
|
*/ |
91
|
2 |
|
public static function createDefaultApplication($appName = null, $appVersion = null, $workingDir = null) |
92
|
|
|
{ |
93
|
2 |
|
$appName = $appName ?? self::APPLICATION_NAME; |
94
|
2 |
|
$appVersion = $appVersion ?? self::VERSION; |
95
|
|
|
|
96
|
2 |
|
$app = Robo::createDefaultApplication($appName, $appVersion); |
97
|
|
|
|
98
|
2 |
|
if (null === $workingDir || false === $workingDir = \realpath($workingDir)) { |
99
|
1 |
|
$workingDir = \getcwd(); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
$app |
103
|
2 |
|
->getDefinition() |
104
|
2 |
|
->addOption( |
105
|
2 |
|
new InputOption( |
106
|
2 |
|
'--working-dir', |
107
|
2 |
|
null, |
108
|
2 |
|
InputOption::VALUE_REQUIRED, |
109
|
2 |
|
'Working directory, defaults to current working directory.', |
110
|
2 |
|
$workingDir |
111
|
|
|
) |
112
|
|
|
); |
113
|
|
|
|
114
|
2 |
|
$app->setAutoExit(false); |
115
|
|
|
|
116
|
2 |
|
return $app; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* @param ContainerInterface $container |
121
|
|
|
* |
122
|
|
|
* @return \Robo\Runner |
123
|
|
|
*/ |
124
|
1 |
|
public static function createDefaultRunner(ContainerInterface $container) |
125
|
|
|
{ |
126
|
1 |
|
return (new \Robo\Runner()) |
127
|
1 |
|
->setRelativePluginNamespace('Robo\Plugin') |
128
|
1 |
|
->setContainer($container); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* @param string[] $paths |
133
|
|
|
* Array of JSON filepaths. |
134
|
|
|
* |
135
|
|
|
* @return Config |
136
|
|
|
* A config object. |
137
|
|
|
*/ |
138
|
3 |
|
public static function createJsonConfiguration(array $paths) |
139
|
|
|
{ |
140
|
3 |
|
$config = new Config(); |
141
|
3 |
|
self::loadJsonConfiguration($paths, $config); |
142
|
|
|
|
143
|
3 |
|
return $config; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* @param string $relativeNamespace |
148
|
|
|
* |
149
|
|
|
* @return array|string[] |
150
|
|
|
*/ |
151
|
|
|
public static function discoverTasksClasses($relativeNamespace) |
152
|
|
|
{ |
153
|
|
|
/** @var \Robo\ClassDiscovery\RelativeNamespaceDiscovery $discovery */ |
154
|
|
|
$discovery = Robo::service('relativeNamespaceDiscovery'); |
155
|
|
|
$discovery->setRelativeNamespace($relativeNamespace . '\Task') |
156
|
|
|
->setSearchPattern('*Task.php'); |
157
|
|
|
|
158
|
|
|
return $discovery->getClasses(); |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* @param string[] $paths |
163
|
|
|
* Array of JSON filepaths. |
164
|
|
|
* @param null|Config $config |
165
|
|
|
* A config object. |
166
|
|
|
*/ |
167
|
3 |
|
public static function loadJsonConfiguration(array $paths, ?Config $config) |
168
|
|
|
{ |
169
|
3 |
|
if (null === $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->import($processor->export()); |
|
|
|
|
182
|
3 |
|
} |
183
|
|
|
} |
184
|
|
|
|
This function has been deprecated. The supplier of the function has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.