-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Paul Vidal
committed
Oct 10, 2016
0 parents
commit 76663b1
Showing
2 changed files
with
57 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
<?php | ||
|
||
// global CDN link helper function | ||
if (! function_exists('cdn')) { | ||
function cdn($asset) { | ||
|
||
if (app()->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, "/"); | ||
|
||
} | ||
} |