1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* |
5
|
|
|
* This file is part of the Apix Project. |
6
|
|
|
* |
7
|
|
|
* (c) Franck Cassedanne <franck at ouarz.net> |
8
|
|
|
* |
9
|
|
|
* @license http://opensource.org/licenses/BSD-3-Clause New BSD License |
10
|
|
|
* |
11
|
|
|
*/ |
12
|
|
|
|
13
|
|
|
namespace Apix\Plugin; |
14
|
|
|
|
15
|
|
|
use Apix\View\View; |
16
|
|
|
use Apix\Service; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Adds a human-friendly API Manual. |
20
|
|
|
* |
21
|
|
|
* @author Franck Cassedanne <franck at ouarz.net> |
22
|
|
|
*/ |
23
|
|
|
class ManPage extends PluginAbstract |
24
|
|
|
{ |
25
|
|
|
|
26
|
|
|
public static $hook = array( |
27
|
|
|
'response', |
28
|
|
|
'early', |
29
|
|
|
'interface' => 'Apix\View\Template\Adapter' |
30
|
|
|
); |
31
|
|
|
|
32
|
|
|
protected $options = array( |
33
|
|
|
'enable' => true, // wether to enable or not |
34
|
|
|
|
35
|
|
|
// Append the given string to the named group annotation. |
36
|
|
|
'example' => null, |
37
|
|
|
'see' => null, |
38
|
|
|
'link' => null, |
39
|
|
|
'copyright' => 'Powered by APIx-server, copyright (C) 2010 Franck Cassedanne.', |
40
|
|
|
'license' => null, |
41
|
|
|
|
42
|
|
|
'view_dir' => null, // to set the view dir. |
43
|
|
|
'rel_path' => '/help', // the relative path to help (no version prefix) |
44
|
|
|
'templater' => 'Apix\View\Template\Mustache', // the template adapter |
45
|
|
|
|
46
|
|
|
// Anything below is automatically populated (extracted). |
47
|
|
|
'version' => 'v1', // the version string (default value). |
48
|
|
|
'url_api' => null, // the API absolute URL. |
49
|
|
|
'url_help' => null, // the Manual absolute URL (url_api+rel_path). |
50
|
|
|
); |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Constructor. |
54
|
|
|
* |
55
|
|
|
* @param array $options Array of options. |
56
|
|
|
*/ |
57
|
|
|
public function __construct(array $options = null) |
|
|
|
|
58
|
|
|
{ |
59
|
|
|
if ( !isset($options['enable']) || $options['enable']) { |
60
|
|
|
|
61
|
|
|
if (!isset($options['url_api'])) { |
62
|
|
|
|
63
|
|
|
// review thsi |
64
|
|
|
$uri = isset($_SERVER['SCRIPT_URI']) |
65
|
|
|
? $_SERVER['SCRIPT_URI'] |
66
|
|
|
: $_SERVER['REQUEST_URI']; |
67
|
|
|
|
68
|
|
|
preg_match( |
69
|
|
|
'@^(.*(/?v[0-9]+))' |
70
|
|
|
. preg_quote($this->options['rel_path']) |
71
|
|
|
. '(.+)?$@i', |
72
|
|
|
$uri, |
73
|
|
|
$m |
74
|
|
|
); |
75
|
|
|
|
76
|
|
View Code Duplication |
if(isset($m[1]) && !empty($m[1])) $options['url_api'] = $m[1]; |
|
|
|
|
77
|
|
View Code Duplication |
if(isset($m[2]) && !empty($m[2])) $options['version'] = $m[2]; |
|
|
|
|
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
$this->setOptions($options); |
|
|
|
|
81
|
|
|
$this->options['url_help'] = $this->options['url_api'] |
82
|
|
|
. $this->options['rel_path']; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
public function update(\SplSubject $response) |
86
|
|
|
{ |
87
|
|
|
if ( |
88
|
|
|
false === $this->options['enable'] |
89
|
|
|
|| 'html' !== $response->getFormat() |
|
|
|
|
90
|
|
|
) { |
91
|
|
|
return false; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
$type = key($response->results); |
|
|
|
|
95
|
|
|
|
96
|
|
|
if ($type == 'error') { |
97
|
|
|
$response->results[$type]['items'] = array(); |
|
|
|
|
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
$view_model = '\Apix\View\ViewModel\\' . ucfirst($type); |
101
|
|
|
$view = new View(new $view_model(), $response->results[$type]); |
|
|
|
|
102
|
|
|
|
103
|
|
|
$config = Service::get('config'); |
104
|
|
|
$view->getViewModel()->set('config', $config); |
105
|
|
|
if (null === $this->options['view_dir']) { |
106
|
|
|
$distrib_path = $config['distrib_path']; |
107
|
|
|
$this->options['view_dir'] = $distrib_path . '/../templates/html'; |
108
|
|
|
} |
109
|
|
|
$view->getViewModel()->set('options', $this->options); |
110
|
|
|
|
111
|
|
|
$view->setTemplate( |
112
|
|
|
$this->options['templater'], |
113
|
|
|
array('view_dir' => $this->options['view_dir']) |
114
|
|
|
); |
115
|
|
|
|
116
|
|
|
$response->setOutput( |
|
|
|
|
117
|
|
|
$view->render() |
118
|
|
|
); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* TODO |
123
|
|
|
* $uri = $_SERVER['SCRIPT_URI'] |
124
|
|
|
* $rel_path = $this->options['rel_path'] |
125
|
|
|
*/ |
126
|
|
|
public static function getUrlApiAndVersion($uri, $rel_path) |
127
|
|
|
{ |
128
|
|
|
preg_match( |
129
|
|
|
'@^(.*(/?v[0-9]+))' . preg_quote($rel_path) . '@i', |
130
|
|
|
$uri, |
131
|
|
|
$m |
132
|
|
|
); |
133
|
|
|
|
134
|
|
|
return $m; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
} |
138
|
|
|
|
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: