Completed
Push — 4.0 ( 4d1c3b...6206cc )
by Marco
11:58
created

Configuration::get()   C

Complexity

Conditions 7
Paths 3

Size

Total Lines 33
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 33
rs 6.7272
cc 7
eloc 12
nc 3
nop 1
1
<?php namespace Comodojo\Dispatcher\Components;
2
3
/**
4
 * @package     Comodojo Dispatcher
5
 * @author      Marco Giovinazzi <[email protected]>
6
 * @author      Marco Castiello <[email protected]>
7
 * @license     GPL-3.0+
8
 *
9
 * LICENSE:
10
 *
11
 * This program is free software: you can redistribute it and/or modify
12
 * it under the terms of the GNU Affero General Public License as
13
 * published by the Free Software Foundation, either version 3 of the
14
 * License, or (at your option) any later version.
15
 *
16
 * This program is distributed in the hope that it will be useful,
17
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19
 * GNU Affero General Public License for more details.
20
 *
21
 * You should have received a copy of the GNU Affero General Public License
22
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
23
 */
24
25
26
class Configuration {
27
28
    protected $attributes = array(
29
        'dispatcher-enabled' => true,
30
        'dispatcher-disabled-status' => 503,
31
        'dispatcher-disabled-message' => 'Dispatcher offline',
32
        'dispatcher-log-name' => 'dispatcher',
33
        'dispatcher-log-enabled' => false,
34
        'dispatcher-log-level' => 'INFO',
35
        'dispatcher-log-target' => '%dispatcher-log-folder%/dispatcher.log',
36
        'dispatcher-log-folder' => '/log',
37
        'dispatcher-supported-methods' => array('GET','PUT','POST','DELETE','OPTIONS','HEAD'),
38
        'dispatcher-default-encoding' => 'UTF-8',
39
        'dispatcher-cache-enabled' => true,
40
        'dispatcher-cache-ttl' => 3600,
41
        'dispatcher-cache-folder' => '/cache',
42
        'dispatcher-cache-algorithm'  => 'PICK_FIRST'
43
        // should we implement this?
44
        //'dispatcher-autoroute' => false
45
    );
46
47
    public function __construct( $configuration = array() ) {
48
49
        $this->attributes['dispatcher-base-url'] = self::urlGetAbsolute();
50
51
        $this->attributes['dispatcher-real-path'] = self::pathGetAbsolute();
52
53
        $this->attributes = array_merge($this->attributes, $configuration);
54
55
    }
56
57
    final public function get($property) {
58
59
        if (array_key_exists($property, $this->attributes)) {
60
61
            $value = $this->attributes[$property];
62
63
            if ( is_scalar($value) && preg_match_all('/%(.+?)%/', $value, $matches) ) {
64
65
                $substitutions = array();
66
67
                foreach ( $matches as $match ) {
68
69
                    $backreference = $match[1];
70
71
                    if ( $backreference != $property && !isset($substitutions['/%'.$backreference.'%/']) ) {
72
73
                        $substitutions['/%'.$backreference.'%/'] = $this->$backreference;
74
75
                    }
76
77
                }
78
79
                $value = preg_replace(array_keys($substitutions), array_values($substitutions), $value);
80
81
            }
82
83
            return $value;
84
85
        }
86
87
        return null;
88
89
    }
90
91
    final public function set($property, $value) {
92
93
        $this->attributes[$property] = $value;
94
95
        return $this;
96
97
    }
98
99
    final public function isDefined($property) {
100
101
        return isset($this->attributes[$property]);
102
103
    }
104
105
    final public function erase() {
106
107
        $this->attributes = array();
108
109
        return $this;
110
111
    }
112
113
    private static function urlGetAbsolute() {
0 ignored issues
show
Coding Style introduced by
urlGetAbsolute uses the super-global variable $_SERVER which is generally not recommended.

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:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
114
115
        $http = 'http' . ((isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on') ? 's' : '') . '://';
116
117
        $uri = preg_replace("/\/index.php(.*?)$/i", "", $_SERVER['PHP_SELF']);
118
119
        return ( $http . $_SERVER['HTTP_HOST'] . $uri . "/" );
120
121
    }
122
123
    private static function pathGetAbsolute() {
124
125
        return realpath(dirname(__FILE__)."/../../../../../")."/";
126
127
    }
128
129
}
130