srchub

srchub Git Source Tree


Root/pluf/src/Pluf/HTTP/URL.php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
<?php
/* -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
# ***** BEGIN LICENSE BLOCK *****
# This file is part of Plume Framework, a simple PHP Application Framework.
# Copyright (C) 2001-2007 Loic d'Anterroches and contributors.
#
# Plume Framework is free software; you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation; either version 2.1 of the License, or
# (at your option) any later version.
#
# Plume Framework is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
#
# ***** END LICENSE BLOCK ***** */
 
/**
 * Generate a ready to use URL to be used in location/redirect or forms.
 *
 * When redirecting a user, depending of the format of the url with
 * mod_rewrite or not, the parameters must all go in the GET or
 * everything but the action. This class provide a convinient way to
 * generate those url and parse the results for the dispatcher.
 */
class Pluf_HTTP_URL
{
    /**
     * Generate the URL.
     *
     * The & is encoded as & in the url.
     *
     * @param string Action url
     * @param array Associative array of the parameters (array())
     * @param bool Encode the & in the url (true)
     * @return string Ready to use URL.
     */
    public static function generate($action, $params=array(), $encode=true)
    {
        $url = $action;
        if (count($params)) {
            $url .= '?' . http_build_query($params, '', ($encode) ? '&' : '&');
        }
        return $url;
    }
 
    /**
     * Get the action of the request.
     *
     * We directly get the PATH_INFO variable or return '/'
     *
     * @return string Action
     */
    public static function getAction()
    {
        if (isset($_GET['_pluf_action'])) {
            return $_GET['_pluf_action'];
        }
        return (isset($_SERVER['REDIRECT_URL'])) ?
            $_SERVER['REDIRECT_URL'] : '/';
    }
}
 
/**
 * Provide the full URL (without domain) to a view.
 *
 * @param string View.
 * @param array Parameters for the view (array()).
 * @param array Extra GET parameters for the view (array()).
 * @param bool Should the URL be encoded (true).
 * @return string URL.
 */
function Pluf_HTTP_URL_urlForView($view, $params=array(),
                                  $get_params=array(), $encoded=true)
{
    return Pluf_HTTP_URL::generate(Pluf_HTTP_URL_reverse($view, $params),
                                   $get_params, $encoded);
}
 
/**
 * Reverse an URL.
 *
 * @param string View in the form 'class::method' or string of the name.
 * @param array Possible parameters for the view (array()).
 * @return string URL.
 */
function Pluf_HTTP_URL_reverse($view, $params=array())
{
    $model = '';
    $method = '';
    if (false !== strpos($view, '::')) {
        list($model, $method) = explode('::', $view);
    }
    $vdef = array($model, $method, $view);
    $regbase = array('', array());
    $regbase = Pluf_HTTP_URL_find($GLOBALS['_PX_views'], $vdef, $regbase);
    if ($regbase === false) {
        throw new Exception(sprintf('Error, the view: %s has not been found.', $view));
    }
    $url = '';
    foreach ($regbase[1] as $regex) {
        $url .= Pluf_HTTP_URL_buildReverseUrl($regex, $params);
    }
    if (!defined('IN_UNIT_TESTS')) {
        $url = $regbase[0].$url;
    }
    return $url;
}
 
 
/**
 * Go in the list of views to find the matching one.
 *
 * @param array Views
 * @param array View definition array(model, method, name)
 * @param array Regex of the view up to now and base
 * @return mixed Regex of the view or false
 */
function Pluf_HTTP_URL_find($views, $vdef, $regbase)
{
    foreach ($views as $dview) {
        if (isset($dview['sub'])) {
            $regbase2 = $regbase;
            if (empty($regbase2[0])) {
                $regbase2[0] = $dview['base'];
            }
            $regbase2[1][] = $dview['regex'];
            $res = Pluf_HTTP_URL_find($dview['sub'], $vdef, $regbase2);
            if ($res) {
                return $res;
            }
            continue;
        }
        if (
            (isset($dview['name']) && $dview['name'] == $vdef[2])
            or
            ($dview['model'] == $vdef[0] && $dview['method'] == $vdef[1])
            ) {
            $regbase[1][] = $dview['regex'];
            if (!empty($dview['base'])) {
                $regbase[0] = $dview['base'];
            }
            return $regbase;
        }
    }
    return false;
}
 
/**
 * Build the reverse URL without the path base.
 *
 * Credits to Django, again...
 *
 * @param string Regex for the URL.
 * @param array Parameters
 * @return string URL filled with the parameters.
 */
function Pluf_HTTP_URL_buildReverseUrl($url_regex, $params=array())
{
    $url = str_replace(array('\\.', '\\-'), array('.', '-'), $url_regex);
    if (count($params)) {
        $groups = array_fill(0, count($params), '#\(([^)]+)\)#');
        $url = preg_replace($groups, $params, $url, 1);
    }
    preg_match('/^#\^?([^#\$]+)/', $url, $matches);
    return $matches[1];
}

Archive Download this file

Branches

Number of commits:
Page rendered in 0.12368s using 11 queries.