1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace kalanis\kw_clipr; |
4
|
|
|
|
5
|
|
|
|
6
|
|
|
use kalanis\kw_input\Interfaces\IFiltered; |
7
|
|
|
|
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Class Clipr |
11
|
|
|
* @package kalanis\kw_clipr |
12
|
|
|
* Main class which runs the whole task system |
13
|
|
|
*/ |
14
|
|
|
class Clipr |
15
|
|
|
{ |
16
|
|
|
protected Interfaces\ILoader $loader; |
17
|
|
|
protected IFiltered $variables; |
18
|
|
|
protected Clipr\Sources $sources; |
19
|
|
|
|
20
|
2 |
|
public function __construct(Interfaces\ILoader $loader, Clipr\Sources $sources, IFiltered $variables) |
21
|
|
|
{ |
22
|
2 |
|
$this->loader = $loader; |
23
|
2 |
|
$this->sources = $sources; |
24
|
2 |
|
$this->variables = $variables; |
25
|
2 |
|
} |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @throws CliprException |
29
|
|
|
* @return int |
30
|
|
|
*/ |
31
|
2 |
|
public function run(): int |
32
|
|
|
{ |
33
|
|
|
// for parsing default params it's necessary to load another task |
34
|
2 |
|
$dummy = new Tasks\DummyTask(); |
35
|
2 |
|
$dummy->initTask(new Output\Clear(), $this->variables, $this->loader); |
36
|
2 |
|
$this->sources->determineInput((bool) $dummy->webOutput, (bool) $dummy->noColor); |
37
|
|
|
|
38
|
|
|
// now we know necessary input data, so we can initialize real task |
39
|
2 |
|
$inputs = $this->variables->getInArray(null, $this->sources->getEntryTypes()); |
40
|
2 |
|
$taskName = Clipr\Useful::getNthParam($inputs) ?? Interfaces\ILoader::DEFAULT_TASK; |
41
|
2 |
|
$task = $this->loader->getTask($taskName); |
42
|
2 |
|
if (!$task) { |
43
|
1 |
|
throw new CliprException(sprintf('Unknown task *%s* - check name, interface or your config paths.', $taskName), Interfaces\IStatuses::STATUS_CLI_USAGE); |
44
|
|
|
} |
45
|
1 |
|
$task->initTask($this->sources->getOutput(), $this->variables, $this->loader); |
46
|
|
|
|
47
|
1 |
|
if (Interfaces\ISources::OUTPUT_STD != $task->outputFile) { |
48
|
1 |
|
ob_start(); |
49
|
|
|
} |
50
|
|
|
|
51
|
1 |
|
if (false === $task->noHeaders) { |
52
|
1 |
|
$task->writeHeader(); |
53
|
|
|
} |
54
|
|
|
|
55
|
1 |
|
$result = $task->process(); |
56
|
|
|
|
57
|
1 |
|
if (false === $task->noHeaders) { |
58
|
1 |
|
$task->writeFooter(); |
59
|
|
|
} |
60
|
|
|
|
61
|
1 |
|
if (Interfaces\ISources::OUTPUT_STD != $task->outputFile) { |
62
|
1 |
|
file_put_contents($task->outputFile, ob_get_clean(), (false === $task->noAppend ? FILE_APPEND : 0)); |
63
|
|
|
} |
64
|
|
|
|
65
|
1 |
|
return $result; |
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
|