1 | <?php |
---|
2 | |
---|
3 | class DojoCommentBlock { |
---|
4 | public static $prefix = ''; |
---|
5 | public static $suffix = ':'; |
---|
6 | |
---|
7 | protected $blocks; |
---|
8 | |
---|
9 | protected $comments; |
---|
10 | protected $keys = array(); |
---|
11 | protected $key_sets = array(); |
---|
12 | |
---|
13 | public function __construct($comments, $keys, $key_sets = array()) { |
---|
14 | if (!is_array($comments)) { |
---|
15 | throw new Exception('DojoCommentBlock expects an array of comments to be passed'); |
---|
16 | } |
---|
17 | $this->comments = $comments; |
---|
18 | $this->keys = $keys; |
---|
19 | $this->key_sets = $key_sets; |
---|
20 | } |
---|
21 | |
---|
22 | public function __destruct() { |
---|
23 | unset($this->blocks); |
---|
24 | unset($this->comments); |
---|
25 | unset($this->keys); |
---|
26 | unset($this->key_sets); |
---|
27 | } |
---|
28 | |
---|
29 | public function add_key($key) { |
---|
30 | unset($this->blocks); |
---|
31 | $this->keys[] = $key; |
---|
32 | } |
---|
33 | |
---|
34 | public function return_type() { |
---|
35 | // TODO: Add return type(s) |
---|
36 | } |
---|
37 | |
---|
38 | public function get($key) { |
---|
39 | $comments = $this->all(); |
---|
40 | return $comments[$key] ? $comments[$key] : ''; |
---|
41 | } |
---|
42 | |
---|
43 | public function all() { |
---|
44 | if (isset($this->blocks)) { |
---|
45 | return $this->blocks; |
---|
46 | } |
---|
47 | |
---|
48 | $expression = '%^' . self::$prefix . '(' . implode('|', array_merge($this->keys, $this->key_sets)) . ')' . self::$suffix . '\W*%'; |
---|
49 | |
---|
50 | $blocks = array(); |
---|
51 | $buffer = array(); |
---|
52 | $key = NULL; |
---|
53 | foreach ($this->comments as $comment) { |
---|
54 | if (empty($comment) && $key) { |
---|
55 | $this->swallow($blocks, $key, $buffer); |
---|
56 | } |
---|
57 | |
---|
58 | $comment = preg_replace('%(^//\s*|^/\*\s*|\s*\*/$)%', '', $comment); |
---|
59 | foreach (explode("\n", $comment) as $line) { |
---|
60 | if (preg_match($expression, $line, $match)) { |
---|
61 | if ($key && !empty($buffer)) { |
---|
62 | $this->swallow($blocks, $key, $buffer); |
---|
63 | } |
---|
64 | $line = substr($line, strlen($match[0])); |
---|
65 | $key = $match[1]; |
---|
66 | } |
---|
67 | |
---|
68 | $line = trim($line); |
---|
69 | if ($line && $line{0} == '|') { |
---|
70 | $line = substr($line, 1); |
---|
71 | } |
---|
72 | |
---|
73 | if ($key) { |
---|
74 | $buffer[] = $line; |
---|
75 | } |
---|
76 | } |
---|
77 | } |
---|
78 | |
---|
79 | if ($key && !empty($buffer)) { |
---|
80 | $this->swallow($blocks, $key, $buffer); |
---|
81 | } |
---|
82 | |
---|
83 | return ($this->blocks = $blocks); |
---|
84 | } |
---|
85 | |
---|
86 | private function swallow(&$blocks, &$key, &$lines) { |
---|
87 | $lines = preg_replace('%(^\n+|\n+$)%', '', implode("\n", $lines)); |
---|
88 | if (in_array($key, $this->keys)) { |
---|
89 | $blocks[$key] = $lines; |
---|
90 | } |
---|
91 | else { |
---|
92 | $blocks[$key][] = $lines; |
---|
93 | } |
---|
94 | |
---|
95 | $key = NULL; |
---|
96 | $lines = array(); |
---|
97 | } |
---|
98 | } |
---|