|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace SilverLeague\Console\Command; |
|
4
|
|
|
|
|
5
|
|
|
use SilverLeague\Console\Command\Dev\Tasks\AbstractTaskCommand; |
|
6
|
|
|
use SilverLeague\Console\Framework\ConsoleBase; |
|
7
|
|
|
use SilverStripe\Control\HTTPRequest; |
|
8
|
|
|
use SilverStripe\Core\Config\Config; |
|
9
|
|
|
use SilverStripe\Dev\BuildTask; |
|
10
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
|
11
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
|
12
|
|
|
use Symfony\Component\Console\Style\SymfonyStyle; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* The Command Factory class is reponsible for converting a SilverStripe BuildTask into a Symfony Console Command |
|
16
|
|
|
* |
|
17
|
|
|
* @package silverstripe-console |
|
18
|
|
|
* @author Robbie Averill <[email protected]> |
|
19
|
|
|
*/ |
|
20
|
|
|
class Factory extends ConsoleBase |
|
21
|
|
|
{ |
|
22
|
|
|
/** |
|
23
|
|
|
* Given a BuildTask, convert it to a runnable Symfony Command |
|
24
|
|
|
* |
|
25
|
|
|
* @param BuildTask $task |
|
26
|
|
|
* @return SilverStripeCommand |
|
27
|
|
|
*/ |
|
28
|
2 |
|
public function getCommandFromTask(BuildTask $task) |
|
29
|
|
|
{ |
|
30
|
2 |
|
if (!is_callable([$task, 'run']) || !$task->isEnabled()) { |
|
31
|
|
|
return false; |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
2 |
|
$command = new AbstractTaskCommand($this->getCommandName($task)); |
|
35
|
2 |
|
$command->setApplication($this->getApplication()); |
|
36
|
2 |
|
$command->setTask($task); |
|
37
|
2 |
|
$command->setDescription($task->getTitle()); |
|
38
|
2 |
|
$command->setHelp($task->getDescription()); |
|
39
|
2 |
|
$command->setCode($this->getTaskAsClosure($command)); |
|
40
|
|
|
|
|
41
|
2 |
|
return $command; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* Get a Command name from the BuildTask segment |
|
46
|
|
|
* |
|
47
|
|
|
* @return string |
|
48
|
|
|
*/ |
|
49
|
2 |
|
public function getCommandName(BuildTask $task) |
|
50
|
|
|
{ |
|
51
|
2 |
|
$taskSegment = Config::inst()->get(get_class($task), 'segment'); |
|
52
|
2 |
|
$segment = strtolower(preg_replace('/(?<=[a-z])([A-Z]+)/', '-$1', $taskSegment)); |
|
53
|
|
|
// We don't really need "-task" on the end of every task. |
|
54
|
2 |
|
if (substr($segment, -5, 5) === '-task') { |
|
55
|
2 |
|
$segment = substr($segment, 0, strlen($segment) - 5); |
|
56
|
|
|
} |
|
57
|
2 |
|
return sprintf('dev:tasks:%s', $segment); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* Get the BuildTask functionality as a closure |
|
62
|
|
|
* |
|
63
|
|
|
* @param AbstractTaskCommand $command |
|
64
|
|
|
* @return Closure |
|
65
|
|
|
*/ |
|
66
|
|
|
public function getTaskAsClosure(AbstractTaskCommand $command) |
|
67
|
|
|
{ |
|
68
|
2 |
|
return function (InputInterface $input, OutputInterface $output) use ($command) { |
|
69
|
|
|
$io = new SymfonyStyle($input, $output); |
|
70
|
|
|
|
|
71
|
|
|
$io->title(sprintf('%s: %s', $command->getName(), $command->getDescription())); |
|
72
|
|
|
$io->section('Running...'); |
|
73
|
|
|
|
|
74
|
|
|
$command->getTask()->run(new HTTPRequest('GET', '/')); |
|
75
|
2 |
|
}; |
|
76
|
|
|
} |
|
77
|
|
|
} |
|
78
|
|
|
|