diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..950ebfc --- /dev/null +++ b/composer.json @@ -0,0 +1,23 @@ +{ + "name": "tecactus/keycdn", + "description": "Laravel KeyCDN package", + "keywords": ["keycdn", "laravel", "key", "cdn"], + "type": "library", + "license": "MIT", + "authors": [ + { + "name": "Paul Vidal", + "email": "paul@tecactus.com" + } + ], + "require": { + "php": ">=5.6.4", + "laravel/framework": "5.1.* || 5.2.* || 5.3.*" + }, + "autoload": { + "files": [ + "src/helpers.php" + ] + }, + "minimum-stability": "dev" +} diff --git a/src/helpers.php b/src/helpers.php new file mode 100644 index 0000000..3be9c6c --- /dev/null +++ b/src/helpers.php @@ -0,0 +1,34 @@ +isLocal()) { + return asset($asset); + } + + // Verify if KeyCDN URLs are present in the config file + if( !Config::get('keycdn.cdn') ) + return asset( $asset ); + + // Get file name incl extension and CDN URLs + $cdns = Config::get('keycdn.cdn'); + $assetName = basename( $asset ); + + // Remove query string + $assetName = explode("?", $assetName); + $assetName = $assetName[0]; + + // Select the CDN URL based on the extension + foreach( $cdns as $cdn => $types ) { + if( preg_match('/^.*\.(' . $types . ')$/i', $assetName) ) + return cdnPath($cdn, $asset); + } + + // In case of no match use the last in the array + end($cdns); + return "//" . rtrim(key( $cdns ), "/") . "/" . ltrim( $asset, "/"); + + } +} \ No newline at end of file