* @category XML * @package XmlQuery * @author Gonzalo Chumillas <*****@*****.**> * @license https://raw.github.com/soloproyectos/core/master/LICENSE BSD 2-Clause License * @link https://github.com/soloproyectos/core */ use com\soloproyectos\common\sys\file\SysFileHelper; use com\soloproyectos\common\text\TextHelper; require_once __DIR__ . "/sys/file/sys-file-helper.php"; require_once __DIR__ . "/text/text-helper.php"; spl_autoload_register(function ($classname) { if (preg_match_all("/[A-Z][a-z,0-9]*/", $classname, $matches)) { // script filename $dir = __DIR__; $name = ""; $items = $matches[0]; foreach ($items as $item) { $item = strtolower($item); $d = SysFileHelper::concat($dir, $item); if (is_dir($d)) { $dir = $d; } $name = TextHelper::concat("-", $name, $item); } $filename = SysFileHelper::concat($dir, "{$name}.php"); if (!is_file($filename)) { throw new Exception("Script not found: {$filename}"); } include_once $filename; } });
/** * Gets an available name under a given directory. * * This function returns an available name under a given directory. For * example, if there's a file named 'test.txt' under de directory 'dir1', the * following command returns 'test_1.txt': * * ```php * // prints 'test_1.txt' if the name 'test.txt' is taken: * echo SysFile::getAvailName('dir1', 'test1.txt'); * ``` * * @param string $dir Directory * @param string $refname Filename used as reference (default is "") * @param string $refext Extension used as reference (default is "") * * @return string */ public static function getAvailName($dir, $refname = "", $refext = "") { // fixes arguments $dir = trim($dir); $refname = trim($refname); $refext = ltrim(trim($refext), "."); if (!is_dir($dir)) { throw new SysException("Directory not found: {$dir}"); } // default refname if (TextHelper::isEmpty($refname)) { $refname = "file"; } // gets name and extension $refname = basename($refname); $pos = strrpos($refname, "."); $name = $refname; $ext = $refext; if ($pos !== false) { $name = substr($refname, 0, $pos); if (TextHelper::isEmpty($refext)) { $ext = substr($refname, $pos + 1); } } // gets an available name for ($i = 0; $i < 100; $i++) { $basename = $i > 0 ? TextHelper::concat(".", $name . "_" . $i, $ext) : TextHelper::concat(".", $name, $ext); $filename = SysFileHelper::concat($dir, $basename); if (!is_file($filename)) { break; } } return $filename; }