|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the webmozart/console package. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Bernhard Schussek <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace Webmozart\Console\Handler\Help; |
|
13
|
|
|
|
|
14
|
|
|
use Symfony\Component\Process\ExecutableFinder; |
|
15
|
|
|
use Webmozart\Assert\Assert; |
|
16
|
|
|
use Webmozart\Console\Api\Args\Args; |
|
17
|
|
|
use Webmozart\Console\Api\IO\IO; |
|
18
|
|
|
use Webmozart\Console\Process\ProcessLauncher; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* Displays the application/command help as AsciiDoc. |
|
22
|
|
|
* |
|
23
|
|
|
* @since 1.0 |
|
24
|
|
|
* |
|
25
|
|
|
* @author Bernhard Schussek <[email protected]> |
|
26
|
|
|
*/ |
|
27
|
|
|
class HelpAsciiDocHandler |
|
28
|
|
|
{ |
|
29
|
|
|
/** |
|
30
|
|
|
* @var string |
|
31
|
|
|
*/ |
|
32
|
|
|
private $path; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @var string |
|
36
|
|
|
*/ |
|
37
|
|
|
private $lessBinary; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @var ExecutableFinder |
|
41
|
|
|
*/ |
|
42
|
|
|
private $executableFinder; |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* @var ProcessLauncher |
|
46
|
|
|
*/ |
|
47
|
|
|
private $processLauncher; |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* Creates the handler. |
|
51
|
|
|
* |
|
52
|
|
|
* @param string $path The path to the AsciiDoc file. |
|
53
|
|
|
* @param ExecutableFinder $executableFinder The finder used to find the |
|
|
|
|
|
|
54
|
|
|
* "less" binary. |
|
55
|
|
|
* @param ProcessLauncher $processLauncher The launcher for executing the |
|
|
|
|
|
|
56
|
|
|
* "less" binary. |
|
57
|
|
|
*/ |
|
58
|
17 |
View Code Duplication |
public function __construct($path, ExecutableFinder $executableFinder = null, ProcessLauncher $processLauncher = null) |
|
|
|
|
|
|
59
|
|
|
{ |
|
60
|
17 |
|
Assert::file($path); |
|
61
|
|
|
|
|
62
|
17 |
|
$this->path = $path; |
|
63
|
17 |
|
$this->executableFinder = $executableFinder ?: new ExecutableFinder(); |
|
64
|
17 |
|
$this->processLauncher = $processLauncher ?: new ProcessLauncher(); |
|
65
|
17 |
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* {@inheritdoc} |
|
69
|
|
|
*/ |
|
70
|
15 |
|
public function handle(Args $args, IO $io) |
|
|
|
|
|
|
71
|
|
|
{ |
|
72
|
15 |
|
if ($this->processLauncher->isSupported()) { |
|
73
|
13 |
|
if (!$this->lessBinary) { |
|
74
|
12 |
|
$this->lessBinary = $this->executableFinder->find('less'); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
13 |
|
if ($this->lessBinary) { |
|
78
|
12 |
|
return $this->processLauncher->launchProcess( |
|
79
|
12 |
|
escapeshellcmd($this->lessBinary).' %path%', |
|
80
|
12 |
|
array('path' => $this->path), |
|
81
|
12 |
|
false |
|
82
|
|
|
); |
|
83
|
|
|
} |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
3 |
|
$io->write(file_get_contents($this->path)); |
|
87
|
|
|
|
|
88
|
3 |
|
return 0; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* Returns the "less" binary used to display the AsciiDoc page. |
|
93
|
|
|
* |
|
94
|
|
|
* @return string The "less" binary or `null` if the binary is auto-detected. |
|
95
|
|
|
*/ |
|
96
|
|
|
public function getLessBinary() |
|
97
|
|
|
{ |
|
98
|
|
|
return $this->lessBinary; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* Sets the "less" binary used to display the AsciiDoc page. |
|
103
|
|
|
* |
|
104
|
|
|
* @param string $lessBinary The "less" binary to use. |
|
105
|
|
|
*/ |
|
106
|
12 |
View Code Duplication |
public function setLessBinary($lessBinary) |
|
|
|
|
|
|
107
|
|
|
{ |
|
108
|
12 |
|
if (null !== $lessBinary) { |
|
109
|
1 |
|
Assert::string($lessBinary, 'The less binary must be a string or null. Got: %s'); |
|
110
|
1 |
|
Assert::notEmpty($lessBinary, 'The less binary must not be empty.'); |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
12 |
|
$this->lessBinary = $lessBinary; |
|
114
|
12 |
|
} |
|
115
|
|
|
} |
|
116
|
|
|
|
This check looks for
@paramannotations where the type inferred by our type inference engine differs from the declared type.It makes a suggestion as to what type it considers more descriptive.
Most often this is a case of a parameter that can be null in addition to its declared types.