X::makeStoragePath('foo/bar', 'd/m/Y'); // (string) "/foo/bar/27/06/2021/1/"
X::makeStoragePath('foo/bar'); // (string) "/foo/bar/2021/06/27/1/"
X::makeStoragePath('foo/bar', 'Y/m/d', 1); // path contains a "1" dir which contains 2 dirs or files // (string) "/foo/bar/2021/06/27/2/"
function(string $path, $format = 'Y/m/d', $max = 100, bbn\File\System $fs = NULL)
{
$format = 'Y/m/d',
$max = 100,
File\System $fs = null
): ?string
{
if (empty($format)) {
$format = 'Y/m/d';
}
if (!$max) {
$max = 100;
}
if (!$fs) {
$fs = new File\System();
}
// One dir per $format
$spath = date($format);
if ($spath) {
$path = $fs->createPath($path.(substr($path, -1) === '/' ? '' : '/').$spath);
if ($fs->isDir($path)) {
$num = count($fs->getDirs($path));
if ($num) {
// Dir or files
$num_files = count($fs->getFiles($path.'/'.$num, true));
if ($num_files >= $max) {
$num++;
}
}
else {
$num = 1;
}
if ($fs->createPath($path.'/'.$num)) {
return $path.'/'.$num.'/';
}
}
}
return null;
}