Example #1
0
File: File.php Project: crodas/path
     */
    public static function generateFilepath()
    {
        $args = func_get_args();
        if (empty($args)) {
            throw new \RuntimeException("You would need to give us at least one identifier");
        }
        $gen = self::$gen;
        $dir = $gen($args[0]);
        if (!is_dir($dir)) {
            if (!mkdir($dir, 0777, true)) {
                throw new \RuntimeException("cannot create directory {$dir}");
            }
        }
        $path = $dir . sha1(implode("", $args));
        if (!is_file($path)) {
            File::write($path, '');
        }
        return $path;
    }
    public static function overrideFilepathGenerator($fnc)
    {
        if (!is_callable($fnc)) {
            throw new \InvalidArgumentException("Expecting a callable");
        }
        self::$gen = $fnc;
    }
}
File::overrideFilepathGenerator(function ($prefix) {
    return sys_get_temp_dir() . DIRECTORY_SEPARATOR . $prefix . DIRECTORY_SEPARATOR;
});