You can download a zip file and extract it
OR
if you use a version control client you can checkout the sources of the project.
It will make it easier to keep the files up to date.
Checkout sources with git client.
The following commands create a directory cdnorlocal below webroot and put all files there:
cd [webroot-directory]/[install-dir]/
git clone https://github.com/axelhahn/cdnorlocal.git [optional-name-of-subdir]
Leaving [optional-name-of-subdir] empty will create a subdir named "cdnorlocal"
Get the latest version:
Download
Extract all files somewhere below webroot on your webserver. You can put to any subdirectory. It is not a must to have it in the webroot.
In the ./classes/ directory you find 2 classes.
In your webapp you only need the single file "cdnorlocal.class.php"
What you need to know is that the structure of all libraries is
[name of the lib]/ [version] / [path+filename]
Because we don't have a local copy at of it it will be loaded
from cdnjs.com.
With the method getHtmlInclude([relative path]) you get html code to
load a css or js file (put it int the header of the html document).
// load the class require_once('[PATH]/cdnorlocal.class.php'); // init class $oCdn = new axelhahn\cdnorlocal(); // load a js or css file with getHtmlInclude echo $oCdn->getHtmlInclude("jquery/3.2.1/jquery.min.js");It returns:
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
The method getHtmlInclude returns very basic html code.
To customize it or you want to handle a non CSS or JS file
use the method getFullUrl(). It returns the url only.
$oCdn = new axelhahn\cdnorlocal(); echo '<script src="'.$oCdn->getFullUrl("jquery/3.2.1/jquery.min.js").'" [... your attributes ...]></script>';
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js" [... your attributes ...]></script>
You see in the examples that you always need to build the structure.
Therefor you need to know the valid libraries, its versions and files.
There are two possibilities to do it:
The tool is shipped with a webgui. You need to open ./admin/ with your
browser i.e. http://localhost/cdnorlocal/admin/
With this webgui you can
You need to download all files of a library and put it below the
./vendor/ directory.
The structure must be ./vendor/ [name of the lib] / [version] / [path+filename]
I suggest to use the admin gui to dowload the libraries.
If the local structure of a needed library exists then the
getFullUrl() and getHtmlInclude()
return a path of a current domain.
Now you may say ... how can the class match a local directory to a
url?
If you initialize with the defaults:
$oCdn = new axelhahn\cdnorlocal();... then the default url is "/vendor" and the path "[webroot]/vendor".
$oCdn = new axelhahn\cdnorlocal( 'vendordir' => __DIR__ . '/../vendor_dir', 'vendorurl' => '/vendor_dir' );OR
$oCdn = new axelhahn\cdnorlocal( 'vendorrelpath'=>'../vendor/cache', );
Mostly a web app uses several classes. You can set its name and version in a flat array:
$oCdn->setLibs(array( "font-awesome/5.8.1", "jquery/3.4.0" ));
echo $oCdn->getHtmlInclude($oCdn->getLibRelpath('jquery')."/jquery.min.js") . "\n";
print_r($oCdn->getLibs(true));
Array ( [font-awesome/5.8.1] => Array ( [lib] => font-awesome [version] => 5.8.1 [relpath] => font-awesome/5.8.1 [islocal] => [isunused] => [files] => Array ( ) ) [jquery/3.4.0] => Array ( [lib] => jquery [version] => 3.4.0 [relpath] => jquery/3.4.0 [islocal] => [isunused] => [files] => Array ( ) ) )