Created
March 3, 2016 12:00
-
-
Save snetty/133db2b09cccb262f503 to your computer and use it in GitHub Desktop.
adds git_hash helper for laravel
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function http_build_url(array $parsed) { | |
$get = function ($key) use ($parsed) { | |
return isset($parsed[$key]) ? $parsed[$key] : null; | |
}; | |
$pass = $get('pass'); | |
$user = $get('user'); | |
$userinfo = $pass !== null ? "$user:$pass" : $user; | |
$port = $get('port'); | |
$scheme = $get('scheme'); | |
$query = $get('query'); | |
$fragment = $get('fragment'); | |
$authority = | |
($userinfo !== null ? "$userinfo@" : '') . | |
$get('host') . | |
($port ? ":$port" : ''); | |
return | |
(strlen($scheme) ? "$scheme:" : '') . | |
(strlen($authority) ? "//$authority" : '') . | |
$get('path') . | |
(strlen($query) ? "?$query" : '') . | |
(strlen($fragment) ? "#$fragment" : ''); | |
} | |
function url_with_query($path = null, $parameters = [], $secure = null, $query = []) | |
{ | |
$url = app('Illuminate\Contracts\Routing\UrlGenerator')->to($path, $parameters, $secure); | |
$parsed = parse_url($url); | |
$parsed['query'] = http_build_query(array_key_exists('query', $parsed) ? array_merge_recursive(parse_str($parsed['query']), $query) : $query); | |
return http_build_url($parsed); | |
} | |
function git_hash($url=null, $parameters = [], $secure = null, $query = []){ | |
$git_hash = Cache::rememberForever('git_hash', function() { return file_get_contents(base_path('.git/refs/heads/master')); }); | |
if(!$url){ | |
return $git_hash; | |
} else { | |
return url_with_query($url, $parameters, $secure, array_merge_recursive($query, ['hash' => $git_hash])); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment