src/Koldy/Route/AbstractRoute.php
public function asset(string $path, string $assetSite = null): string
    {
        if (strlen($path) == 0) {
            throw new \InvalidArgumentException('Expected non-empty string');
        }

        // if you pass the full URL that contains "//" part, it'll be immediately
        // returned without any kind of building or parsing

        $pos = strpos($path, '//');
        if (($pos !== false && $pos < 10) || substr($path, 0, 2) == '//') {
            return $path;
        }

        $assets = Application::getConfig('application')->get('assets') ?? [];

        if ($path[0] != '/') {
            $path = '/' . $path;
        }

        $url = null;

        if ($assetSite != null) {
            if (isset($assets[$assetSite])) {
                $url = $assets[$assetSite];
            } else {
                $backtrace = debug_backtrace();
                $caller = $backtrace[1]['function'] ?? '[unknown caller]'; // TODO: Possible fix needed
                Log::warning("Asset site {$assetSite} is used in {$caller}, but not set in application config; using first asset site if any");

                if (count($assets) > 0) {
                    $url = array_values($assets)[0];
                }
            }
        } else {
            if (count($assets) > 0) {
                $url = array_values($assets)[0];
            }
        }

        if ($url == null) {
            return static::makeUrl($path);
        } else {
            if (substr($url, -1) != '/') {
                $url .= '/';
            }

            if (substr($path, 0, 1) == '/') {
                $path = substr($path, 1);
            }

            return $url . $path;
        }
    }
public function asset(string $path, string $assetSite = null): string
    {
        if (strlen($path) == 0) {
            throw new \InvalidArgumentException('Expected non-empty string');
        }

        // if you pass the full URL that contains "//" part, it'll be immediately
        // returned without any kind of building or parsing

        $pos = strpos($path, '//');
        if (($pos !== false && $pos < 10) || substr($path, 0, 2) == '//') {
            return $path;
        }

        $assets = Application::getConfig('application')->get('assets') ?? [];

        if ($path[0] != '/') {
            $path = '/' . $path;
        }

        $url = null;

        if ($assetSite != null) {
            if (isset($assets[$assetSite])) {
                $url = $assets[$assetSite];
            } else {
                $backtrace = debug_backtrace();
                $caller = $backtrace[1]['function'] ?? '[unknown caller]'; // TODO: Possible fix needed
                Log::warning("Asset site {$assetSite} is used in {$caller}, but not set in application config; using first asset site if any");

                if (count($assets) > 0) {
                    $url = array_values($assets)[0];
                }
            }
        } else {
            if (count($assets) > 0) {
                $url = array_values($assets)[0];
            }
        }

        if ($url == null) {
            return static::makeUrl($path);
        } else {
            if (substr($url, -1) != '/') {
                $url .= '/';
            }

            if (substr($path, 0, 1) == '/') {
                $path = substr($path, 1);
            }

            return $url . $path;
        }
    }
public function asset ( string $ path , string $ assetSite = null ) : string { if ( strlen ( $ path ) == 0 ) { throw new \ InvalidArgumentException ( 'Expected non-empty string' ) ; } // if you pass the full URL that contains "//" part, it'll be immediately // returned without any kind of building or parsing $ pos = strpos ( $ path , '//' ) ; if ( ( $ pos !== false && $ pos < 10 ) || substr ( $ path , 0 , 2 ) == '//' ) { return $ path ; } $ assets = Application :: getConfig ( 'application' ) -> get ( 'assets' ) ?? [ ] ; if ( $ path [ 0 ] != '/' ) { $ path = '/' . $ path ; } $ url = null ; if ( $ assetSite != null ) { if ( isset ( $ assets [ $ assetSite ] ) ) { $ url = $ assets [ $ assetSite ] ; } else { $ backtrace = debug_backtrace ( ) ; $ caller = $ backtrace [ 1 ] [ 'function' ] ?? '[unknown caller]' ; // TODO: Possible fix needed Log :: warning ( "Asset site {$assetSite} is used in {$caller}, but not set in application config; using first asset site if any" ) ; if ( count ( $ assets ) > 0 ) { $ url = array_values ( $ assets ) [ 0 ] ; } } } else { if ( count ( $ assets ) > 0 ) { $ url = array_values ( $ assets ) [ 0 ] ; } } if ( $ url == null ) { return static :: makeUrl ( $ path ) ; } else { if ( substr ( $ url , - 1 ) != '/' ) { $ url .= '/' ; } if ( substr ( $ path , 0 , 1 ) == '/' ) { $ path = substr ( $ path , 1 ) ; } return $ url . $ path ; } }
Generate link to the resource file on the same domain

@param string $path
@param string $assetSite [optional]

@return string
@throws \InvalidArgumentException
Generate link to the resource file on the same domain