1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This software package is licensed under `AGPL, Commercial` license[s]. |
5
|
|
|
* |
6
|
|
|
* @package maslosoft/zamm |
7
|
|
|
* @license AGPL, Commercial |
8
|
|
|
* |
9
|
|
|
* @copyright Copyright (c) Peter Maselkowski <[email protected]> |
10
|
|
|
* @link https://maslosoft.com/zamm/ |
11
|
|
|
*/ |
12
|
|
|
|
13
|
|
|
namespace Maslosoft\Zamm; |
14
|
|
|
|
15
|
|
|
use Maslosoft\Zamm\Helpers\Tabs; |
16
|
|
|
use Maslosoft\Zamm\Helpers\Wrapper; |
17
|
|
|
use Maslosoft\Zamm\Interfaces\SourceAccessorInterface; |
18
|
|
|
use ReflectionClass; |
19
|
|
|
use ReflectionProperty; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Source extractor for code fragments. |
23
|
|
|
* This class extracts source code for specified code elements. |
24
|
|
|
* |
25
|
|
|
* @author Piotr Maselkowski <pmaselkowski at gmail.com> |
26
|
|
|
*/ |
27
|
|
|
class Source implements SourceAccessorInterface |
28
|
|
|
{ |
29
|
|
|
|
30
|
|
|
use Traits\SourceMagic; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Reflection information |
34
|
|
|
* @var ReflectionClass |
35
|
|
|
*/ |
36
|
|
|
private $info = null; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Source as array |
40
|
|
|
* @var string[] |
41
|
|
|
*/ |
42
|
|
|
private $source = []; |
43
|
|
|
|
44
|
|
|
public function __construct($className = null) |
45
|
|
|
{ |
46
|
|
|
$this->info = new ReflectionClass($className); |
47
|
|
|
$this->source = file($this->info->getFileName()); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Should return source of working class |
52
|
|
|
*/ |
53
|
|
|
public function __toString() |
54
|
|
|
{ |
55
|
|
|
return $this->getTypeSource(); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
public static function __callStatic($name, $arguments) |
59
|
|
|
{ |
60
|
|
|
|
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Get source code of method |
65
|
|
|
* @param string $name |
66
|
|
|
* @return Wrapper |
67
|
|
|
*/ |
68
|
|
|
public function method($name) |
69
|
|
|
{ |
70
|
|
|
assert($this->info->hasMethod($name), sprintf('Method `%s` does not exists on class `%s`', $name, $this->info->name)); |
71
|
|
|
$method = $this->info->getMethod($name); |
72
|
|
|
$code = $this->getMethodSource($method->getStartLine(), $method->getEndLine()); |
73
|
|
|
$result = []; |
74
|
|
|
$comment = $method->getDocComment(); |
75
|
|
|
Tabs::trim($comment); |
76
|
|
|
$result[] = $comment . PHP_EOL; |
77
|
|
|
$result[] = $code . PHP_EOL; |
78
|
|
|
return new Wrapper(implode('', $result)); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Get source code of property |
83
|
|
|
* @param string $name |
84
|
|
|
* @return Wrapper |
85
|
|
|
*/ |
86
|
|
|
public function property($name) |
87
|
|
|
{ |
88
|
|
|
assert($this->info->hasProperty($name), sprintf('Property `%s` does not exists on class `%s`', $name, $this->info->name)); |
89
|
|
|
$property = $this->info->getProperty($name); |
90
|
|
|
$code = $this->getPropertySource($property); |
91
|
|
|
$result = []; |
92
|
|
|
$comment = $property->getDocComment(); |
93
|
|
|
Tabs::trim($comment); |
94
|
|
|
$result[] = $comment . PHP_EOL; |
95
|
|
|
$result[] = $code . PHP_EOL; |
96
|
|
|
|
97
|
|
|
return new Wrapper(implode('', $result)); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
private function getMethodSource($start, $end) |
101
|
|
|
{ |
102
|
|
|
// Assume blank line after method name |
103
|
|
|
// So start one line above |
104
|
|
|
if (preg_match('~^\s*\{~', $this->source[$start])) |
105
|
|
|
{ |
106
|
|
|
$start--; |
107
|
|
|
} |
108
|
|
|
$source = array_values($this->source); |
109
|
|
|
$result = array_splice($source, $start, $end - $start); |
110
|
|
|
Tabs::trim($result); |
111
|
|
|
return implode('', $result); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
public function getPropertySource(ReflectionProperty $property) |
115
|
|
|
{ |
116
|
|
|
$name = []; |
117
|
|
|
|
118
|
|
|
switch (true) |
119
|
|
|
{ |
120
|
|
|
case $property->isPublic(): |
121
|
|
|
$name[] = 'public'; |
122
|
|
|
break; |
123
|
|
|
case $property->isProtected(): |
124
|
|
|
$name[] = 'public'; |
125
|
|
|
break; |
126
|
|
|
case $property->isPrivate(): |
127
|
|
|
$name[] = 'public'; |
128
|
|
|
break; |
129
|
|
|
} |
130
|
|
|
if ($property->isStatic()) |
131
|
|
|
{ |
132
|
|
|
$name[] = 'static'; |
133
|
|
|
} |
134
|
|
|
$name[] = sprintf('$%s', $property->name); |
135
|
|
|
|
136
|
|
|
return implode(' ', $name) . ';'; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
private function getTypeSource() |
140
|
|
|
{ |
141
|
|
|
return ''; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
} |
145
|
|
|
|