Completed
Push — 4.0 ( 1bebd1...1533b9 )
by Marco
14:16
created

Configuration::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 9
rs 9.6666
cc 1
eloc 4
nc 1
nop 1
1
<?php namespace Comodojo\Dispatcher\Components;
2
3
/**
4
 *
5
 * @package     Comodojo dispatcher
6
 * @author      Marco Giovinazzi <[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
class Configuration {
26
27
    protected $attributes = array(
28
        'dispatcher-enabled' => true,
29
        'dispatcher-disabled-status' => 503,
30
        'dispatcher-disabled-message' => 'Dispatcher offline',
31
        'dispatcher-log-name' => 'dispatcher',
32
        'dispatcher-log-enabled' => false,
33
        'dispatcher-log-level' => 'INFO',
34
        'dispatcher-log-target' => '%dispatcher-log-folder%/dispatcher.log',
35
        'dispatcher-log-folder' => '/log',
36
        'dispatcher-supported-methods' => 'GET,PUT,POST,DELETE,OPTIONS,HEAD',
37
        'dispatcher-default-encoding' => 'UTF-8',
38
        'dispatcher-cache-enabled' => true,
39
        'dispatcher-cache-ttl' => 3600,
40
        'dispatcher-cache-folder' => '/cache',
41
        'dispatcher-cache-algorithm'  => 'PICK_FIRST'
42
        // should we implement this?
43
        //'dispatcher-autoroute' => false
44
    );
45
46
    public function __construct( $configuration = array() ) {
47
48
        $this->attributes['dispatcher-base-url'] = self::urlGetAbsolute();
49
        
50
        $this->attributes['dispatcher-real-path'] = self::pathGetAbsolute();
51
52
        $this->attributes = array_merge($this->attributes, $configuration);
53
54
    }
55
56
    final public function get($property) {
57
58
        if (array_key_exists($property, $this->attributes)) {
59
60
            $value = $this->attributes[$property];
61
            
62
            if ( preg_match_all('/%(.+?)%/', $value, $matches) ) {
63
                
64
                $substitutions = array();
65
                
66
                foreach ( $matches as $match ) {
67
                    
68
                    $backreference = $match[1];
69
                    
70
                    if ( $backreference != $property && !isset($substitutions['/%'.$backreference.'%/']) ) {
71
                        
72
                        $substitutions['/%'.$backreference.'%/'] = $this->$backreference;
73
                        
74
                    }
75
                    
76
                }
77
                
78
                $value = preg_replace(array_keys($substitutions), array_values($substitutions), $value);
79
                
80
            }
81
            
82
            return $value;
83
84
        }
85
86
        return null;
87
88
    }
89
    
90
    final public function set($property, $value) {
91
92
        $this->attributes[$property] = $value;
93
94
        return $this;
95
96
    }
97
98
    final public function isDefined($property) {
99
100
        return isset($this->attributes[$property]);
101
102
    }
103
    
104
    final public function erase() {
105
        
106
        $this->attributes = array();
107
        
108
        return $this;
109
        
110
    }
111
112
    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...
113
114
        $http = 'http' . ((isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on') ? 's' : '') . '://';
115
116
        $uri = preg_replace("/\/index.php(.*?)$/i", "", $_SERVER['PHP_SELF']);
117
118
        return ( $http . $_SERVER['HTTP_HOST'] . $uri . "/" );
119
120
    }
121
    
122
    private static function pathGetAbsolute() {
123
124
        return realpath(dirname(__FILE__)."/../../../../../")."/";
125
126
    }
127
128
}
129