class Loader extends AbstractAssetsPackage

Class to manage assets paths

The class is based on three paths:

  • root_dir: the package root directory (must be the directory containing the composer.json file)
  • assets_dir: the package asssets directory related to root_dir
  • document_root: the path in the filesystem of the web assets root directory ; this is used to build all related assets paths to use in HTTP.

For these three paths, their defaults values are defined on a default package structure:

package_name/
|----------- src/
|----------- www/

$loader->root_dir = realpath(package_name)
$loader->assets_dir = www
$loader->document_root = www or the server DOCUMENT_ROOT

NOTE - These paths are stored in the object without the trailing slash.

Constants

PRESETS_NO_CONFLICT

Flag to use to avoid throwing an exception in case of presets conflicts

PRESETS_CONFLICT

Flag to use to throw exception in case of presets conflicts (default)

Methods

__construct(string $root_dir = null, string $assets_dir = null, string $document_root = null, int $conflict_flag = self::PRESETS_CONFLICT)

Loader protected constructor, use the class as a Singleton

setRootDirectory(string $path)

Set the project root directory absolute path

string
getRootDirectory()

Get the project root directory absolute path

setAssetsDirectory(string $path)

Set the project's assets directory, relative to $this->_root_dir

string
getAssetsDirectory()

Get the project's assets directory, relative to $this->_root_dir

setVendorDirectory(string $path)

Set the project's vendor directory, relative to $this->_root_dir

string
getVendorDirectory()

Get the project's vendor directory, relative to $this->_root_dir

setAssetsVendorDirectory(string $path)

Set the project's assets vendor directory, relative to $this->_assets_dir

string
getAssetsVendorDirectory()

Get the project's vendor directory, relative to $this->_root_dir

string
getFullPath(string $path, string $type = null, bool $out = false)

Get the absolute path in the package

string
getAssetsRealPath()

Get the assets full path

string
getVendorRealPath()

Get the assets full path

string
getAssetsVendorRealPath()

Get the assets vendor full path

static Loader
getInstance(string $root_dir = null, string $assets_dir = null, string $document_root = null, int $conflict_flag = self::PRESETS_CONFLICT)

Loader static instance constructor

void
init(string $root_dir = null, string $assets_dir = null, string $document_root = null)

Initializing a new loader populating all paths & packages

setDocumentRoot(string $path)

Set the document root directory

string
getDocumentRoot()

Get the document root directory

setAssetsDb(array $db)

Set the package's assets database

array
getAssetsDb()

Get the package's assets database

string
getAssetsWebPath()

Get the web path for assets

string
getPackageAssetsPath(string $package_name)

Get the assets full path for a specific package

string
getPackageAssetsWebPath(string $package_name)

Get the web path for assets of a specific package

bool
hasPackage(string $package_name)

Test if a package exists

getPackage(string $package_name)

Get a package instance

void
validatePresets()

Load and validate all packages presets in one table

bool
hasPreset(string $preset_name)

Test if a preset exists

getPreset(string $preset_name)

Get a preset instance

static array
getAssets()

Get the package's assets database

static Preset
findPreset(string $preset_name)

Get a preset instance from static loader

static AssetsPackage
findPackage(string $package_name)

Get a package instance from static loader

static string
buildWebPath(string $path)

Build a web path ready to use in HTML

static string|null
find(string $filename, string $package = null)

Find an asset file in the filesystem

static string|null
findInPackage(string $filename, string $package)

Find an asset file in the filesystem of a specific package

static string|null
findInPath(string $filename, string $path)

Find an asset file in a package's path

static bool
isUrl(string $str)

Test if a string is a classic url or an url like //domain.ext/asset

Details

at line 148
__construct(string $root_dir = null, string $assets_dir = null, string $document_root = null, int $conflict_flag = self::PRESETS_CONFLICT)

Loader protected constructor, use the class as a Singleton

Parameters

string $root_dir The project package root directory
string $assets_dir The project package assets directory, related from $root_dir
string $document_root The project assets root directory to build web accessible assets paths
int $conflict_flag Define if the class must throw exceptions in case of presets conflicts

Exceptions

Exception if the object is not called as a singleton

in AbstractAssetsPackage at line 112
AbstractAssetsPackage setRootDirectory(string $path)

Set the project root directory absolute path

Parameters

string $path

Return Value

AbstractAssetsPackage

Exceptions

InvalidArgumentException if the path doesn't exist

in AbstractAssetsPackage at line 129
string getRootDirectory()

Get the project root directory absolute path

Return Value

string

in AbstractAssetsPackage at line 141
AbstractAssetsPackage setAssetsDirectory(string $path)

Set the project's assets directory, relative to $this->_root_dir

Parameters

string $path

Return Value

AbstractAssetsPackage

Exceptions

InvalidArgumentException if the path doesn't exist

in AbstractAssetsPackage at line 159
string getAssetsDirectory()

Get the project's assets directory, relative to $this->_root_dir

Return Value

string

in AbstractAssetsPackage at line 171
AbstractAssetsPackage setVendorDirectory(string $path)

Set the project's vendor directory, relative to $this->_root_dir

Parameters

string $path

Return Value

AbstractAssetsPackage

Exceptions

InvalidArgumentException if the path doesn't exist

in AbstractAssetsPackage at line 189
string getVendorDirectory()

Get the project's vendor directory, relative to $this->_root_dir

Return Value

string

in AbstractAssetsPackage at line 201
AbstractAssetsPackage setAssetsVendorDirectory(string $path)

Set the project's assets vendor directory, relative to $this->_assets_dir

Parameters

string $path

Return Value

AbstractAssetsPackage

Exceptions

InvalidArgumentException if the path doesn't exist

in AbstractAssetsPackage at line 219
string getAssetsVendorDirectory()

Get the project's vendor directory, relative to $this->_root_dir

Return Value

string

in AbstractAssetsPackage at line 236
string getFullPath(string $path, string $type = null, bool $out = false)

Get the absolute path in the package

Parameters

string $path The relative path to complete
string $type Type of the original relative path (can be asset, vendor or assets_vendor - default is null)
bool $out Must we search in assets and vendor (if false) or not (if true)

Return Value

string

in AbstractAssetsPackage at line 277
string getAssetsRealPath()

Get the assets full path

Return Value

string

in AbstractAssetsPackage at line 287
string getVendorRealPath()

Get the assets full path

Return Value

string

in AbstractAssetsPackage at line 297
string getAssetsVendorRealPath()

Get the assets vendor full path

Return Value

string

at line 128
static Loader getInstance(string $root_dir = null, string $assets_dir = null, string $document_root = null, int $conflict_flag = self::PRESETS_CONFLICT)

Loader static instance constructor

Parameters

string $root_dir The project package root directory
string $assets_dir The project package assets directory, related from $root_dir
string $document_root The project assets root directory to build web accessible assets paths
int $conflict_flag Define if the class must throw exceptions in case of presets conflicts

Return Value

Loader

at line 171
void init(string $root_dir = null, string $assets_dir = null, string $document_root = null)

Initializing a new loader populating all paths & packages

Parameters

string $root_dir The project package root directory
string $assets_dir The project package assets directory, related from $root_dir
string $document_root The project assets root directory to build web accessible assets paths

Return Value

void

Exceptions

Exception : any caught exception
Exception if the package's ASSETS_DB_FILENAME was not found

at line 240
Loader setDocumentRoot(string $path)

Set the document root directory

Parameters

string $path The path of the document root directory

Return Value

Loader

Exceptions

InvalidArgumentException if the directory was not found

at line 259
string getDocumentRoot()

Get the document root directory

Return Value

string

at line 270
Loader setAssetsDb(array $db)

Set the package's assets database

Parameters

array $db The array of package's assets as written in package's ASSETS_DB_FILENAME

Return Value

Loader

at line 291
array getAssetsDb()

Get the package's assets database

Return Value

array

at line 306
string getAssetsWebPath()

Get the web path for assets

Return Value

string

See also

\AssetsManager\Package\Loader::buildWebPath()

at line 317
string getPackageAssetsPath(string $package_name)

Get the assets full path for a specific package

Parameters

string $package_name The name of the package to get assets path from

Return Value

string

at line 330
string getPackageAssetsWebPath(string $package_name)

Get the web path for assets of a specific package

Parameters

string $package_name The name of the package to get assets path from

Return Value

string

See also

\AssetsManager\Package\Loader::buildWebPath()

at line 346
bool hasPackage(string $package_name)

Test if a package exists

Parameters

string $package_name

Return Value

bool

at line 364
AssetsPackage getPackage(string $package_name)

Get a package instance

Parameters

string $package_name

Return Value

AssetsPackage

Exceptions

Exception : any caught exception

See also

self::_buildNewPackage()

at line 425
void validatePresets()

Load and validate all packages presets in one table

Return Value

void

Exceptions

Exception if the $conflict_flag is set on self::PRESETS_CONFLICT in case of duplicate preset name

at line 454
bool hasPreset(string $preset_name)

Test if a preset exists

Parameters

string $preset_name

Return Value

bool

at line 468
Preset getPreset(string $preset_name)

Get a preset instance

Parameters

string $preset_name

Return Value

Preset

Exceptions

InvalidArgumentException if the preset can't be found
Exception : any caught exception

See also

self::_buildNewPreset()

at line 536
static array getAssets()

Get the package's assets database

Return Value

array

at line 548
static Preset findPreset(string $preset_name)

Get a preset instance from static loader

Parameters

string $preset_name

Return Value

Preset

at line 560
static AssetsPackage findPackage(string $package_name)

Get a package instance from static loader

Parameters

string $package_name

Return Value

AssetsPackage

at line 578
static string buildWebPath(string $path)

Build a web path ready to use in HTML

This will build a relative path related to the object $document_root and ready-to-use in HTML attributes. It uses the "smart resolving" feature of the Library\Helper\Filesystem class: path is returned relative to $document_root even if it is not in it in the filesystem.

Parameters

string $path The path to transform

Return Value

string

See also

\Library\Helper\Filesystem::resolveRelatedPath()

at line 591
static string|null find(string $filename, string $package = null)

Find an asset file in the filesystem

Parameters

string $filename The asset filename to find
string $package The name of a package to search in (optional)

Return Value

string|null The web path of the asset if found, null otherwise

at line 608
static string|null findInPackage(string $filename, string $package)

Find an asset file in the filesystem of a specific package

Parameters

string $filename The asset filename to find
string $package The name of a package to search in

Return Value

string|null The web path of the asset if found, null otherwise

at line 628
static string|null findInPath(string $filename, string $path)

Find an asset file in a package's path

Parameters

string $filename The asset filename to find
string $path The path to search from

Return Value

string|null The web path of the asset if found, null otherwise

at line 643
static bool isUrl(string $str)

Test if a string is a classic url or an url like //domain.ext/asset

Parameters

string $str

Return Value

bool