Passed
Push — develop ( 91c1d2...446210 )
by Schlaefer
41s
created

Router::getCurrentUrl()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 19
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 19
ccs 10
cts 10
cp 1
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 10
nc 2
nop 0
crap 3
1
<?php
2
/*
3
 * @author  PhileCMS
4
 * @link    https://philecms.com
5
 * @license http://opensource.org/licenses/MIT
6
 */
7
8
namespace Phile\Core;
9
10
/**
11
 * this Router class is responsible for Phile's basic URL management
12
 */
13
class Router
14
{
15
16
    /**
17
     * @var array with $_SERVER environment
18
     */
19
    protected $server;
20
21
    /**
22
     * @param array $server $_SERVER environment
23
     */
24 36
    public function __construct(array $server = [])
0 ignored issues
show
Coding Style introduced by
__construct 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...
25
    {
26 36
        if (empty($server)) {
27 36
            $server = $_SERVER;
28
        }
29 36
        $this->server = $server;
30 36
    }
31
32
    /**
33
     * get request-URL relative to Phile base-URL
34
     *
35
     * @return string relative URL e.g. `index`, `sub/`, `sub/page`
36
     */
37 7
    public function getCurrentUrl()
38
    {
39 7
        $url = $this->server['REQUEST_URI'];
40
41
        // remove query string
42 7
        list($url) = explode('?', $url);
43
44
        // resolve root-relative URL-path
45 7
        $baseUrl = $this->getBaseUrl();
46 7
        $basePath = $this->getUrlPath($baseUrl);
47 7
        if (!empty($basePath) && strpos($url, $basePath) === 0) {
48 1
            $url = substr($url, strlen($basePath));
49
        }
50 7
        $url = ltrim($url, '/');
51
52 7
        $url = rawurldecode($url);
53
54 7
        return $url;
55
    }
56
57
    /**
58
     * Get base-URL of the Phile installation
59
     *
60
     * @return string `scheme://host/path/phile-root`
61
     */
62 34
    public function getBaseUrl()
63
    {
64 34
        $baseUrl = Container::getInstance()->get('Phile_Config')->get('base_url');
65 34
        if (!empty($baseUrl)) {
66 32
            return $baseUrl;
67
        }
68
69 27
        $url = '';
70
71 27
        if (isset($this->server['PHP_SELF'])) {
72 27
            $url = preg_replace('/index\.php(.*)?$/', '', $this->server['PHP_SELF']);
73
        }
74
75 27
        if (isset($this->server['HTTP_HOST'])) {
76 2
            $host = $this->server['HTTP_HOST'];
77 2
            $protocol = $this->getProtocol();
78 2
            $url = $protocol . '://' . $host . $url;
79
        }
80
81 27
        $url = rtrim($url, '/');
82 27
        return $url;
83
    }
84
85
    /**
86
     * get the URL for a page-Id
87
     *
88
     * e.g. `sub/index` --> `http://host/phile-root/sub`
89
     *
90
     * @param  string $pageId
91
     * @param  bool   $base   return a full or root-relative URL
92
     * @return string URL
93
     */
94 6
    public function urlForPage($pageId, $base = true)
95
    {
96 6
        $url = $pageId;
97 6
        if ($base) {
98 2
            $url = $this->url($url);
99
        }
100 6
        return $url;
101
    }
102
103
    /**
104
     * converts Phile-root relative URL to full URL
105
     *
106
     * e.g. `foo/bar.ext` --> `http://host/phile-root/foo/bar.ext`
107
     *
108
     * @param  string $url
109
     * @return string
110
     */
111 3
    public function url($url)
112
    {
113 3
        return $this->getBaseUrl() . '/' . ltrim($url, '/');
114
    }
115
116
    /**
117
     * get the HTTP-protocol
118
     *
119
     * @return string
120
     */
121 3
    public function getProtocol()
122
    {
123 3
        if (empty($this->server['HTTP_HOST'])) {
124 1
            return null;
125
        }
126 3
        $protocol = 'http';
127 3
        if (isset($this->server['HTTPS']) && strtolower($this->server['HTTPS']) !== 'off') {
128 1
            $protocol = 'https';
129
        }
130 3
        return $protocol;
131
    }
132
133
    /**
134
     * get path of an URL
135
     *
136
     * `scheme://host/path/sub` --> `/path/sub`
137
     *
138
     * @param  string $url
139
     * @return string
140
     */
141 7
    protected function getUrlPath($url)
142
    {
143 7
        $path = '';
144 7
        if (strpos($url, '://') !== false) {
145 4
            $parsed = parse_url($url);
146 4
            if (isset($parsed['path'])) {
147 1
                $path = $parsed['path'];
148
            }
149
        }
150 7
        return $path;
151
    }
152
}
153