The Tonik\Gin\Asset\Asset
class provides simple API for retrieving information and details about theme asset files. You can easily pull their URLs, directory paths or check if actually exist.
Usage
Initializing asset
Asset requires the Tonik\Gin\Foundation\Config
class to be injected with paths
and directories
properties set up.
use Tonik\Gin\Asset\Asset;
use Tonik\Gin\Foundation\Config;
$config = new Config([
'paths' => [
'directory' => get_template_directory(),
'uri' => get_template_directory_uri(),
],
'directories' => [
'assets' => 'resources/assets',
'public' => 'public',
]
]);
$asset = new Asset($config);
Now, you can set a path to the file you are looking for. This path should be relative to the public
directory.
$asset->setFile('js/app.js');
Getting asset relative path
Gets a relative path of an asset to the project root directory.
$asset->getRelativePath();
// 'public/js/app.js'
Getting asset URI
Gets URI to the asset on your server.
$asset->getUri();
// 'http://website.com/wp-content/themes/<theme-name>/public/js/app.js'
Getting asset path
Gets full directory path to the asset on your server.
$asset->getPath();
// 'website/directory/wp-content/themes/<theme-name>/public/js/app.js'
Checking if asset file exist
You can check if asset file actually exists with fileExist
method.
$asset->fileExists();
// true or false