Skip to content

Instantly share code, notes, and snippets.

@arleighdickerson
Last active March 26, 2016 14:04
Show Gist options
  • Save arleighdickerson/b9621bac1a748ebf954e to your computer and use it in GitHub Desktop.
Save arleighdickerson/b9621bac1a748ebf954e to your computer and use it in GitHub Desktop.
Utility to make working with yii routes easier on the client side
<?php
namespace common\helpers;
use yii\base\InvalidParamException;
use yii\helpers\Json;
use yii\helpers\Url;
class ClientUrls {
private static $_urls = [];
public static function add($url) {
if (!is_array($url)) {
throw new InvalidParamException("Url must be an array");
}
if (!$url) {
$url = ['/'];
}
$route = array_shift($url);
foreach (self::permutations($url) as $permutation) {
self::registerPermutation($route, $permutation);
}
}
private static function registerPermutation($route, $params) {
$template = [];
$keys = [];
foreach ($params as $key => $value) {
$param = is_numeric($key) ? $value : $key;
$keys[] = $param;
$template[$param] = '{' . $param . '}';
}
$params = $template;
sort($params);
array_unshift($template, $route);
self::$_urls[$route][implode(",", $keys)] = [
'params' => $keys,
'url' => urldecode(Url::to($template))
];
}
private static function permutations($array) {
$results = [[]];
$array = array_combine($array, $array);
sort($array);
foreach ($array as $element) {
foreach ($results as $combination) {
$results[] = array_merge([$element], $combination);
}
}
return $results;
}
public static function register($view) {
$urls = Json::encode(self::$_urls);
$js = <<<JS
var Url = (function (urls) {
var matches = function (a, b) {
var alphabetize = function (a, b) {
var aName = a.toString().toLowerCase();
var bName = b.toString().toLowerCase();
return ((aName < bName) ? -1 : ((aName > bName) ? 1 : 0));
};
a = Object.keys(a);
b = Object.keys(b);
return a.every(function (val) {
return b.indexOf(val) >= 0;
}) && b.every(function (val) {
return a.indexOf(val) >= 0;
});
};
var multiReplace = function (str, params) {
$.each(params, function (key, value) {
});
var re = new RegExp(Object.keys(params).join("|"), "gi");
return str.replace(re, function (matched) {
return params[matched];
});
};
return function (route,params) {
var match = null;
if (typeof urls[route] != 'undefined') {
if (typeof params == 'undefined' || matches(params, {})) {
match = urls[route][''].url;
} else {
$.each(urls[route], function (key, elem) {
var cb = function (val, i) {
return val;
};
var p0 = $.map(elem.params, cb);
var p1 = $.map(Object.keys(params), cb);
var values = [];
$.each(params, function (i, elem) {
values.push(elem);
});
if (matches(p0, p1)) {
var keys = $.map(p0, function (val, i) {
return '{' + val + '}';
});
var sub = [];
for (var i = 0; i < keys.length; i++) {
sub[keys[i]] = values[i];
}
console.debug(sub);
match = multiReplace(elem.url, sub);
}
});
}
}
return match;
};
})({$urls});
JS;
$view->registerJs($js, 1);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment