This is a comment on Plugins Directory, posted by WikiAdmin at 14.02.2024 10:41

View source for Re: Plugins Directory

Not sure why it needs ##$checkIfFileExists## at all, if it checks only for the path and not the file.

  * ##explode()## consumes less resources.
  * The function itself should be placed in Ut class, e.g. ##Ut::join_multi_path()##
  * Perhaps it is better to mirror the folder structure by just set the ##'plugin'## folder as prefix in the function
    * ##const PLUGIN_DIR = 'plugin'##,
    * ##$paths =  [PLUGIN_DIR . '/' . $path, $path]##

%%(php)
<?php

function BuildFullpathFromMultipath($filename, $pathlist, $checkIfFileExists = true): ?string
{
    $paths = explode(':', $pathlist);

    if(empty($paths[0])) return null;

    if (!$checkIfFileExists)
    {
        // Just return first directory that exists
        foreach ($paths as $path)
        {
            $path = trim($path);

            if(file_exists($path)) // check for file
            {
                return Ut::join_path($path, $filename);
            }
        }

        return null;
    }

    foreach ($paths as $path)
    {
        $path = trim($path);
        $fqfn = Ut::join_path($path, $filename);

        if (file_exists($fqfn))
        {
            return $fqfn;
        }
    }

    return null;
}
%%