Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Paul Vidal committed Oct 10, 2016
0 parents commit 76663b1
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 0 deletions.
23 changes: 23 additions & 0 deletions composer.json
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"
}
34 changes: 34 additions & 0 deletions src/helpers.php
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, "/");

}
}

0 comments on commit 76663b1

Please sign in to comment.