Skip to content

Instantly share code, notes, and snippets.

@lastguest
Last active September 6, 2017 07:52
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lastguest/d55e8e182940632fc8be to your computer and use it in GitHub Desktop.
Save lastguest/d55e8e182940632fc8be to your computer and use it in GitHub Desktop.
[Include a gist file with local caching] #PHP #gist
<?php
/**
* Include a gist file with local caching
*
*
* @example :
* // Include lastguest/profiler gist
* // https://gist.github.com/lastguest/4344954
*
* include_gist('lastguest','4344954');
*
*
* @author lastguest@gmail.com
* @param string $user The user gist repository (GitHub username)
* @param string $name The ID of the gist to load
* @param string $tmpdir (optional) If passed this will be the cache directory
*
*/
function include_gist($user,$name,$tmpdir=null){
$tmp = $tmpdir ?: sys_get_temp_dir().'/.gists';
if (false == is_dir($tmp)) mkdir($tmp);
if (false == is_file($file = "$tmp/$user-$name.php")){
file_put_contents($file,file_get_contents("https://gist.githubusercontent.com/$user/$name/raw"));
}
return include($file);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment