igaster\laravelTheme\Theme::url PHP Method

url() public method

Attach theme paths to a local Url. The Url must be a resource located on the asset path of the current theme or it's parents.
public url ( string $url ) : string
$url string
return string
    public function url($url)
    {
        // return external URLs unmodified
        if (preg_match('/^((http(s?):)?\\/\\/)/i', $url)) {
            return $url;
        }
        // Is it on AWS? Dont lookup parent themes...
        if (preg_match('/^((http(s?):)?\\/\\/)/i', $this->assetPath)) {
            return $this->assetPath . '/' . ltrim($url, '/');
        }
        // Lookup asset in current's theme asset path
        $fullUrl = (empty($this->assetPath) ? '' : '/') . $this->assetPath . '/' . ltrim($url, '/');
        if (file_exists($fullPath = public_path($fullUrl))) {
            return $fullUrl;
        }
        // If not found then lookup in parent's theme asset path
        if ($this->getParent()) {
            return $this->getParent()->url($url);
        }
        // Asset not found at all. Error handling
        $action = \Config::get('themes.asset_not_found', 'LOG_ERROR');
        if ($action == 'THROW_EXCEPTION') {
            throw new themeException("Asset not found [{$url}]");
        } elseif ($action == 'LOG_ERROR') {
            \Log::warning("Asset not found [{$url}] in Theme [" . \Theme::get() . "]");
        } elseif ($action === 'ASSUME_EXISTS') {
            $assetPath = \Theme::find(\Theme::get())->assetPath;
            return (empty($assetPath) ? '' : '/') . $assetPath . '/' . ltrim($url, '/');
        }
    }

Usage Example

 public function boot()
 {
     /*--------------------------------------------------------------------------
     		| Pulish configuration file
     		|--------------------------------------------------------------------------*/
     $this->publishes([__DIR__ . '/config.php' => config_path('themes.php')]);
     /*--------------------------------------------------------------------------
     		| Extend Blade to support Orcherstra\Asset (Asset Managment)
     		|
     		| Syntax:
     		|
     		|   @css (filename, alias, depends-on-alias)
     		|   @js  (filename, alias, depends-on-alias)
     		|--------------------------------------------------------------------------*/
     Blade::extend(function ($value) {
         return preg_replace_callback('/\\@js\\s*\\(\\s*([\\w\\-\\._:\\/]*)\\s*(?:,\\s*([\\w\\-\\._:\\/]*)\\s*,?\\s*(.*))?\\)/', function ($match) {
             $p1 = \Theme::url($match[1]);
             $p2 = empty($match[2]) ? $match[1] : $match[2];
             $p3 = empty($match[3]) ? '' : $match[3];
             if (empty($p2)) {
                 return "<?php Asset::script('{$p2}', '{$p1}');?>";
             } elseif (empty($p3)) {
                 return "<?php Asset::script('{$p2}', '{$p1}');?>";
             } else {
                 return "<?php Asset::script('{$p2}', '{$p1}', '{$p3}');?>";
             }
         }, $value);
     });
     Blade::extend(function ($value) {
         return preg_replace_callback('/\\@css\\s*\\(\\s*([\\w\\-\\._:\\/]*)\\s*(?:,\\s*([\\w\\-\\._:\\/]*)\\s*,?\\s*(.*))?\\)/', function ($match) {
             $p1 = \Theme::url($match[1]);
             $p2 = empty($match[2]) ? $match[1] : $match[2];
             $p3 = empty($match[3]) ? '' : $match[3];
             if (empty($p2)) {
                 return "<?php Asset::style('{$p2}', '{$p1}');?>";
             } elseif (empty($p3)) {
                 return "<?php Asset::style('{$p2}', '{$p1}');?>";
             } else {
                 return "<?php Asset::style('{$p2}', '{$p1}', '{$p3}');?>";
             }
         }, $value);
     });
 }