1
|
|
|
<?php |
2
|
|
|
namespace Bookdown\Bookdown; |
3
|
|
|
|
4
|
|
|
use Aura\Cli\CliFactory; |
5
|
|
|
use Monolog\Logger; |
6
|
|
|
use Monolog\Handler\StreamHandler; |
7
|
|
|
use Monolog\Formatter\LineFormatter; |
8
|
|
|
|
9
|
|
|
class Container |
10
|
|
|
{ |
11
|
|
|
protected $stdout; |
12
|
|
|
protected $stderr; |
13
|
|
|
protected $logger; |
14
|
|
|
protected $cliFactory; |
15
|
|
|
protected $fsioClass; |
16
|
|
|
protected $fsio; |
17
|
|
|
|
18
|
27 |
|
public function __construct( |
19
|
|
|
$stdout = 'php://stdout', |
20
|
|
|
$stderr = 'php://stderr', |
21
|
|
|
$fsioClass = 'Bookdown\Bookdown\Fsio' |
22
|
|
|
) { |
23
|
27 |
|
$this->stdout = $stdout; |
24
|
27 |
|
$this->stderr = $stderr; |
25
|
27 |
|
$this->fsioClass = $fsioClass; |
26
|
27 |
|
} |
27
|
|
|
|
28
|
3 |
|
public function newCommand($globals) |
29
|
|
|
{ |
30
|
3 |
|
return new Command( |
31
|
3 |
|
$this->getCliFactory()->newContext($globals), |
32
|
3 |
|
$this->getLogger(), |
33
|
3 |
|
$this->newService() |
34
|
3 |
|
); |
35
|
|
|
} |
36
|
|
|
|
37
|
3 |
|
public function newService() |
38
|
|
|
{ |
39
|
3 |
|
return new Service\Service( |
40
|
3 |
|
$this->newCollector(), |
41
|
3 |
|
$this->newProcessorBuilder(), |
42
|
3 |
|
$this->newTimer() |
43
|
3 |
|
); |
44
|
|
|
} |
45
|
|
|
|
46
|
6 |
|
public function newCollector() |
47
|
|
|
{ |
48
|
6 |
|
return new Service\Collector( |
49
|
6 |
|
$this->getLogger(), |
50
|
6 |
|
$this->getFsio(), |
51
|
6 |
|
new Config\ConfigFactory(), |
52
|
6 |
|
new Content\PageFactory() |
53
|
6 |
|
); |
54
|
1 |
|
} |
55
|
|
|
|
56
|
21 |
|
public function newProcessorBuilder() |
57
|
|
|
{ |
58
|
21 |
|
return new Service\ProcessorBuilder( |
59
|
21 |
|
$this->getLogger(), |
60
|
21 |
|
$this->getFsio() |
61
|
21 |
|
); |
62
|
|
|
} |
63
|
|
|
|
64
|
3 |
|
public function newTimer() |
65
|
|
|
{ |
66
|
3 |
|
return new Service\Timer($this->getLogger()); |
67
|
|
|
} |
68
|
|
|
|
69
|
4 |
|
public function getCliFactory() |
70
|
|
|
{ |
71
|
4 |
|
if (! $this->cliFactory) { |
72
|
4 |
|
$this->cliFactory = new CliFactory(); |
73
|
4 |
|
} |
74
|
4 |
|
return $this->cliFactory; |
75
|
|
|
} |
76
|
|
|
|
77
|
25 |
|
public function getLogger() |
78
|
|
|
{ |
79
|
25 |
|
if (! $this->logger) { |
80
|
25 |
|
$formatter = new LineFormatter('%message%' . PHP_EOL); |
81
|
|
|
|
82
|
25 |
|
$stderr = new StreamHandler($this->stderr, Logger::ERROR, false); |
83
|
25 |
|
$stderr->setFormatter($formatter); |
84
|
|
|
|
85
|
25 |
|
$stdout = new StreamHandler($this->stdout, Logger::DEBUG, false); |
86
|
25 |
|
$stdout->setFormatter($formatter); |
87
|
|
|
|
88
|
25 |
|
$this->logger = new Logger('Bookdown', array($stderr, $stdout)); |
89
|
25 |
|
} |
90
|
|
|
|
91
|
25 |
|
return $this->logger; |
92
|
|
|
} |
93
|
|
|
|
94
|
25 |
|
public function getFsio() |
95
|
|
|
{ |
96
|
25 |
|
if (! $this->fsio) { |
97
|
25 |
|
$class = $this->fsioClass; |
98
|
25 |
|
$this->fsio = new $class(); |
99
|
25 |
|
} |
100
|
25 |
|
return $this->fsio; |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|