Pluf Framework

Sign in or create your account | Project List | Help

Pluf Framework Commit Details

Date:2009-04-25 14:22:38 (10 months 24 days ago)
Author:Loïc d'Anterroches
Commit:d5c95dc2751d97ecf05e576ad106168f47bc02eb
Message:Optimized the reverse URL generation.

Files: src/Pluf/HTTP/URL.php (2 diffs)

Change Details

src/Pluf/HTTP/URL.php
8989function Pluf_HTTP_URL_urlForView($view, $params=array(),
9090                                  $get_params=array(), $encoded=true)
9191{
92    $action = Pluf_HTTP_URL_reverse($view, $params);
93    if (!is_array($get_params)) {
94        throw new Exception('Bad call to urlForView.');
95    }
96    return Pluf_HTTP_URL::generate($action, $get_params, $encoded);
92    return Pluf_HTTP_URL::generate(Pluf_HTTP_URL_reverse($view, $params),
93                                   $get_params, $encoded);
9794}
9895
9996/**
...... 
172169 */
173170function Pluf_HTTP_URL_buildReverseUrl($url_regex, $params=array())
174171{
175    $url_regex = str_replace('\\.', '.', $url_regex);
176    $url_regex = str_replace('\\-', '-', $url_regex);
177    $url = $url_regex;
178    $groups = '#\(([^)]+)\)#';
179    $matches = array();
180    preg_match_all($groups, $url_regex, $matches);
181    reset($params);
182    if (count($matches[0]) && count($matches[0]) == count($params)) {
183        // Test the params against the pattern
184        foreach ($matches[0] as $pattern) {
185            $in = current($params);
186            if (0 === preg_match('#'.$pattern.'#', $in)) {
187                throw new Exception('Error, param: '.$in.' is not matching the pattern: '.$pattern);
188            }
189            next($params);
190        }
191        $func = create_function('$matches',
192                                'static $p = '.var_export($params, true).'; '.
193                                '$a = current($p); '.
194                                'next($p); '.
195                                'return $a;');
196        $url = preg_replace_callback($groups, $func, $url_regex);
172    $url = str_replace(array('\\.', '\\-'), array('.', '-'), $url_regex);
173    if (count($params)) {
174        $groups = array_fill(0, count($params), '#\(([^)]+)\)#');
175        $url = preg_replace($groups, $params, $url, 1);
197176    }
198177    preg_match('/^#\^?([^#\$]+)/', $url, $matches);
199178    return $matches[1];

Archive Download the corresponding diff file

Branches:
master