Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 29 |
|
0.00% |
0 / 12 |
CRAP | |
0.00% |
0 / 1 |
Controller | |
0.00% |
0 / 29 |
|
0.00% |
0 / 12 |
272 | |
0.00% |
0 / 1 |
__construct | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
6 | |||
setDao | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
setForm | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
setMailer | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
setI18nContext | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
handle | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
__invoke | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
6 | |||
__call | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
6 | |||
__get | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
flashGet | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
6 | |||
msg | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
pagination | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
2 |
1 | <?php |
2 | /** |
3 | * @section LICENSE |
4 | * This file is part of Wikimedia Slim application library |
5 | * |
6 | * Wikimedia Slim application library is free software: you can |
7 | * redistribute it and/or modify it under the terms of the GNU General Public |
8 | * License as published by the Free Software Foundation, either version 3 of |
9 | * the License, or (at your option) any later version. |
10 | * |
11 | * Wikimedia Slim application library is distributed in the hope that it |
12 | * will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty |
13 | * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
14 | * General Public License for more details. |
15 | * |
16 | * You should have received a copy of the GNU General Public License along |
17 | * with Wikimedia Grants Review application. If not, see |
18 | * <http://www.gnu.org/licenses/>. |
19 | * |
20 | * @file |
21 | * @copyright © 2015 Bryan Davis, Wikimedia Foundation and contributors. |
22 | */ |
23 | |
24 | namespace Wikimedia\Slimapp; |
25 | |
26 | use Slim\Slim; |
27 | use Wikimedia\SimpleI18n\I18nContext; |
28 | use Wikimedia\SimpleI18n\Message; |
29 | use Wikimedia\Slimapp\Dao\AbstractDao; |
30 | |
31 | /** |
32 | * Page controller. |
33 | * |
34 | * @author Bryan Davis <bd808@wikimedia.org> |
35 | * @copyright © 2015 Bryan Davis, Wikimedia Foundation and contributors. |
36 | */ |
37 | class Controller { |
38 | |
39 | /** |
40 | * @var Slim |
41 | */ |
42 | protected $slim; |
43 | |
44 | /** |
45 | * @var AbstractDao |
46 | */ |
47 | protected $dao; |
48 | |
49 | /** |
50 | * @var Form |
51 | */ |
52 | protected $form; |
53 | |
54 | /** |
55 | * @var Mailer |
56 | */ |
57 | protected $mailer; |
58 | |
59 | /** |
60 | * @var I18nContext |
61 | */ |
62 | protected $i18nctx; |
63 | |
64 | /** |
65 | * @param Slim|null $slim |
66 | */ |
67 | public function __construct( Slim $slim = null ) { |
68 | $this->slim = $slim ?: Slim::getInstance(); |
69 | $this->form = new Form( $this->slim->log ); |
70 | } |
71 | |
72 | /** |
73 | * Set default DAO |
74 | * @param AbstractDao $dao |
75 | */ |
76 | public function setDao( AbstractDao $dao ) { |
77 | $this->dao = $dao; |
78 | } |
79 | |
80 | /** |
81 | * Set default form |
82 | * @param Form $form |
83 | */ |
84 | public function setForm( Form $form ) { |
85 | $this->form = $form; |
86 | } |
87 | |
88 | /** |
89 | * Set mailer |
90 | * @param Mailer $mailer |
91 | */ |
92 | public function setMailer( Mailer $mailer ) { |
93 | $this->mailer = $mailer; |
94 | } |
95 | |
96 | /** |
97 | * Set i18n context |
98 | * @param I18nContext $i18nctx |
99 | */ |
100 | public function setI18nContext( I18nContext $i18nctx ) { |
101 | $this->i18nctx = $i18nctx; |
102 | } |
103 | |
104 | /** |
105 | * Default request handler. |
106 | * |
107 | * Default implementation will pass() |
108 | */ |
109 | protected function handle() { |
110 | $this->slim->pass(); |
111 | } |
112 | |
113 | /** |
114 | * Handle request by calling handleMethod on self. |
115 | * |
116 | * If no method matching the current request method is present then fall |
117 | * back to self::handle(). |
118 | */ |
119 | public function __invoke() { |
120 | $argv = func_get_args(); |
121 | $method = $this->slim->request->getMethod(); |
122 | $mname = 'handle' . ucfirst( strtolower( $method ) ); |
123 | if ( method_exists( $this, $mname ) ) { |
124 | call_user_func_array( [ $this, $mname ], $argv ); |
125 | } else { |
126 | call_user_func_array( [ $this, 'handle' ], $argv ); |
127 | } |
128 | } |
129 | |
130 | /** |
131 | * Handle calls to undefined methods by proxying to the Slim member. |
132 | * |
133 | * @param string $name Method name |
134 | * @param array $args Call arguments |
135 | * @return mixed |
136 | */ |
137 | public function __call( $name, $args ) { |
138 | if ( method_exists( $this->slim, $name ) ) { |
139 | return call_user_func_array( [ $this->slim, $name ], $args ); |
140 | } |
141 | // emulate default PHP behavior |
142 | trigger_error( |
143 | 'Call to undefined method ' . __CLASS__ . '::' . $name . '()', |
144 | E_USER_ERROR |
145 | ); |
146 | } |
147 | |
148 | /** |
149 | * Handle access to undefined member variables by proxying to the Slim |
150 | * member. |
151 | * |
152 | * @param string $name Member name |
153 | * @return mixed |
154 | */ |
155 | public function __get( $name ) { |
156 | return $this->slim->{$name}; |
157 | } |
158 | |
159 | /** |
160 | * Get a flash message. |
161 | * |
162 | * @param string $key Message key |
163 | * @return mixed|null |
164 | */ |
165 | protected function flashGet( $key ) { |
166 | if ( isset( $this->slim->environment['slim.flash'] ) ) { |
167 | return $this->slim->environment['slim.flash'][$key]; |
168 | } |
169 | |
170 | return null; |
171 | } |
172 | |
173 | /** |
174 | * Get a message from the I18nContext. |
175 | * |
176 | * @param string $key Message name |
177 | * @param array $params Parameters to add to the message |
178 | * @return Message |
179 | */ |
180 | protected function msg( $key, $params = [] ) { |
181 | return $this->i18nctx->message( $key, $params ); |
182 | } |
183 | |
184 | /** |
185 | * Compute pagination data. |
186 | * |
187 | * @param int $total Total records |
188 | * @param int $current Current page number (0-indexed) |
189 | * @param int $pageSize Number of items per page |
190 | * @param int $around Number of pages to show on each side of current |
191 | * @return array Page count, first page index, last page index |
192 | */ |
193 | protected function pagination( $total, $current, $pageSize, $around = 4 ) { |
194 | $pageCount = ceil( $total / $pageSize ); |
195 | $first = max( 0, $current - $around ); |
196 | $last = min( max( 0, $pageCount - 1 ), $current + 4 ); |
197 | return [ $pageCount, $first, $last ]; |
198 | } |
199 | |
200 | } |