-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathPhpDocMdCommand.php
More file actions
executable file
·105 lines (91 loc) · 3.05 KB
/
PhpDocMdCommand.php
File metadata and controls
executable file
·105 lines (91 loc) · 3.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
<?php namespace Clean\PhpDocMd;
use ReflectionClass;
class PhpDocMdCommand
{
private $configFile = '.phpdoc-md';
private $config;
public function __construct()
{
$this->config = require $this->configFile;
if (is_array($this->config)) {
$this->config = (object) $this->config;
}
}
public function execute()
{
switch ($this->config->format) {
default:
$readme = new Markdown\GitHub\Readme($this->config->rootNamespace);
}
foreach ($this->config->classes as $class) {
try {
$reflection = new ReflectionClass($class);
} catch (\ReflectionException $e) {
$this->error('Config error: ' . $e->getMessage(), $e);
}
$destDir = $this->getDestinationDirectory(
$reflection,
$this->config->rootNamespace,
$this->config->destDirectory
);
$destFile = sprintf('%s.md', $reflection->getShortName());
switch ($this->config->format) {
case 'stash':
$markDown = new Markdown\Stash\ClassInfo($reflection);
break;
case 'github':
$markDown = new Markdown\GitHub\ClassInfo($reflection);
break;
default:
throw new \RuntimeException('Not supported markdown format given: ' . $this->config->format);
}
file_exists($destDir) ?: mkdir($destDir, 0777, true);
file_put_contents($destDir . DIRECTORY_SEPARATOR . $destFile, $markDown);
$readme->addLink(
$this->removeRootNamespace($reflection->getName(), $this->config->rootNamespace),
$this->namespaceToPath(
$this->removeRootNamespace(
$reflection->getName(),
$this->config->rootNamespace
)
) . '.md'
);
}
file_put_contents($this->config->destDirectory . '/README.md', $readme);
}
protected function getDestinationDirectory(ReflectionClass $reflection, $rootNamespace, $rootDir)
{
return $this->namespaceToPath(
rtrim(
sprintf(
'%s/%s',
$rootDir,
$this->removeRootNamespace($reflection->getNamespaceName(), $rootNamespace)
),
'/'
)
);
}
protected function namespaceToPath($namespace)
{
return str_replace(
'\\',
DIRECTORY_SEPARATOR,
$namespace
);
}
protected function removeRootNamespace($namespace, $root)
{
$re = preg_replace(
sprintf("/^%s/", addslashes($root)),
'',
$namespace
);
return ltrim($re, '\\');
}
protected function error($message, $exception = null)
{
echo sprintf("ERROR: '%s'", trim($message));
exit(1);
}
}