Skip to content

Instantly share code, notes, and snippets.

@meglio
Created September 19, 2012 10:39
Show Gist options
  • Save meglio/3748935 to your computer and use it in GitHub Desktop.
Save meglio/3748935 to your computer and use it in GitHub Desktop.
Generates file.ddd.js/css filenames based on their mtime
<?php
namespace proj\utils;
class TimebasedUrl
{
static function js($path)
{
return '<script type="text/javascript" src="'.self::url($path).'"></script>';
}
static function css($path)
{
return '<link rel="stylesheet" href="'.self::url($path).'">';
}
static function url($relPath)
{
$domain = Web::protocolAgnostic(COOKIE_FREE_URL);
$file = ROOT.$relPath;
if (!file_exists($file) || !is_readable($file)
|| !($ext = pathinfo($relPath, PATHINFO_EXTENSION))
|| false === ($time = filemtime($file)))
return $domain.$relPath;
return $domain.preg_replace('|\.'.$ext.'$|is', '.'.$time.'.'.$ext, $relPath);
}
}
Copy link

ghost commented Sep 22, 2012

Web::protocolAgnostic ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment