Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 9
0.00% covered (danger)
0.00%
0 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
HeaderMiddleware
0.00% covered (danger)
0.00%
0 / 9
0.00% covered (danger)
0.00%
0 / 2
30
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 call
0.00% covered (danger)
0.00%
0 / 7
0.00% covered (danger)
0.00%
0 / 1
20
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
24namespace Wikimedia\Slimapp;
25
26use Slim\Middleware;
27
28/**
29 * Middleware to add headers to the response.
30 *
31 * Allows arbitrary headers to be added to each response.
32 *
33 * @author Bryan Davis <bd808@wikimedia.org>
34 * @copyright © 2015 Bryan Davis, Wikimedia Foundation and contributors.
35 */
36class HeaderMiddleware extends Middleware {
37
38    /**
39     * @var array
40     */
41    protected $headers;
42
43    /**
44     * @param array $headers Collection of headers to set. Array keys are header
45     *   names and values are either header values or callables that will return
46     *   header values. Callables will be passed the current Slim application as
47     *   the sole argument and must return either a string value to use as the
48     *   header content or null to cancel setting this header.
49     */
50    public function __construct( array $headers = [] ) {
51        $this->headers = $headers;
52    }
53
54    /**
55     * Add configured headers to response.
56     */
57    public function call() {
58        $resp = $this->app->response;
59        foreach ( $this->headers as $name => $value ) {
60            if ( is_callable( $value ) ) {
61                $value = $value( $this->app );
62            }
63
64            if ( $value !== null ) {
65                $resp->header( $name, $value );
66            }
67        }
68
69        $this->next->call();
70    }
71
72}