-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAPI.php
More file actions
152 lines (142 loc) · 4.1 KB
/
API.php
File metadata and controls
152 lines (142 loc) · 4.1 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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
<?php
/**
* Class to load and parse the files generated by the PhD IDE Package.
*
* PHP version 5.3+
*
* @category PhD
* @package PhD_IDE
* @author Moacir de Oliveira <moacir@php.net>
* @license http://www.opensource.org/licenses/bsd-license.php BSD Style
* @link https://doc.php.net/phd/
*/
namespace phpdotnet\phd;
/**
* Class to load and parse the files generated by the PhD IDE Package.
* Make sure that the ide-xml/ dir and the ide-funclist.txt file
* exist in the PhD output directory given in the __construct.
*
* To generate those files run:
* $ phd --docbook .manual.xml --package IDE --output <your output dir>
*
* Use the value passed with --output to create this class.
*
* @category PhD
* @package PhD_IDE
* @author Moacir de Oliveira <moacir@php.net>
* @license http://www.opensource.org/licenses/bsd-license.php BSD Style
* @link https://doc.php.net/phd/
*/
class Package_IDE_API
{
/**
* Output directory of the functions format in the IDE Package.
*
* @var string
*/
const string FUNCTIONS_DIR = 'ide-xml';
/**
* Output file of the funclist format in the IDE Package.
*
* @var string
*/
const string FUNCLIST_FILE = 'ide-funclist.txt';
/**
* PhD output directory.
*
* @var string
*/
private $dir;
/**
* Array with the functions of the file ide-funclist.txt.
*
* @var array
*/
private $funclist;
/**
* Creates a new instace of Package_IDE_API.
*
* @param string $dir PhD output directory.
*/
public function __construct($dir)
{
if (file_exists($dir)) {
if (is_dir($dir)) {
$this->dir = substr($dir, -1) === DIRECTORY_SEPARATOR
? $dir
: $dir . DIRECTORY_SEPARATOR;
$this->funclist = file($this->dir . self::FUNCLIST_FILE, FILE_IGNORE_NEW_LINES);
} else {
throw new \Error('Is the PhD output directory a file?');
}
} else {
throw new \Error('The PhD output directory does not exist.');
}
}
/**
* Loads a function file and returns a Package_IDE_API_Function.
*
* @param string $filename File of the function.
* @return ?Package_IDE_API_Function Object representing the function.
*/
private function loadFunction($filename)
{
if (!file_exists($filename)) {
return NULL;
}
$xml = simplexml_load_file($filename);
return new Package_IDE_API_Function($xml);
}
/**
* Converts a function name to the correct .xml file.
*
* @param string $function Function name.
* @return string File name.
*/
private function functionToFilename($function)
{
return str_replace(
array('->', '::', '()'),
array('.', '.', ''), $function) . '.xml';
}
/**
* Returns an array with the functions in the ide-funclist.txt file.
*
* @return array Functions list.
*/
public function getFunctionList()
{
return $this->funclist;
}
/**
* Returns the function object of the name given.
*
* @param string $functionName Function name.
* @param string $class TODO.
* @return Package_IDE_API_Function A function object.
*/
public function getFunctionByName($functionName, $class = null)
{
return $this->loadFunction($this->dir
. self::FUNCTIONS_DIR
. DIRECTORY_SEPARATOR
. $this->functionToFilename($functionName));
}
/**
* Returns the list of methods of a class.
*
* @param string $className Name of the class.
* @return array An array of Package_IDE_API_Function.
*/
public function getMethodsByClass($className)
{
$methods = array();
$class = $className . '.';
foreach ($this->funclist as $function) {
if (strcmp($class, substr($function, 0, strlen($class))) === 0) {
$methods[] = $this->getFunctionByName(trim($function));
}
}
return $methods;
}
}