Skip to content

Phalcon assets

NOTE

All classes are prefixed with Phalcon

Assets\Asset

Class Source on GitHub

Represents an asset

$asset = new \Phalcon\Assets\Asset("js", "js/jquery.js");

Uses Phalcon\Assets\Exceptions\CannotReadAsset

Method Summary

public __construct(string $type,string $path,bool $isLocal = true,bool $filter = true,array $attributes = [],string $version = null,bool $isAutoVersion = false) Asset constructor. public string getAssetKey() Gets the asset's key. public array getAttributes() Gets extra HTML attributes. public string getContent( string $basePath = null ) Returns the content of the asset as an string public bool getFilter() public string getPath() public string getRealSourcePath( string $basePath = null ) Returns the complete location where the asset is located public string getRealTargetPath( string $basePath = null ) Returns the complete location where the asset must be written public string getRealTargetUri() Returns the real target uri for the generated HTML public string getSourcePath() public string getTargetPath() public string getTargetUri() public string getType() public string|null getVersion() Version of resource public bool isAutoVersion() Checks if the asset is using auto version public bool isLocal() Checks if the asset is local or not public AssetInterface setAttributes( array $attributes ) Sets extra HTML attributes public AssetInterface setAutoVersion( bool $flag ) public AssetInterface setFilter( bool $filter ) Sets if the asset must be filtered or not public AssetInterface setIsLocal( bool $flag ) Sets if the asset is local or external public AssetInterface setPath( string $path ) Sets the asset's path public AssetInterface setSourcePath( string $sourcePath ) Sets the asset's source path public AssetInterface setTargetPath( string $targetPath ) Sets the asset's target path public AssetInterface setTargetUri( string $targetUri ) Sets a target uri for the generated HTML public AssetInterface setType( string $type ) Sets the asset's type public AssetInterface setVersion( string $version ) Sets the asset's version protected bool phpFileExists( string $filename ) @todo to be removed when we get traits protected phpFileGetContents( string $filename )

Properties

protected array $attributes
protected bool $filter
protected bool $isAutoVersion = false
protected bool $isLocal
protected string $path
protected string $sourcePath
protected string $targetPath
protected string $targetUri
protected string $type
protected string|null $version Version of resource

Methods

Public · 26

__construct()

public function __construct(
    string $type,
    string $path,
    bool $isLocal = true,
    bool $filter = true,
    array $attributes = [],
    string $version = null,
    bool $isAutoVersion = false
);

Asset constructor.

getAssetKey()

public function getAssetKey(): string;

Gets the asset's key.

getAttributes()

public function getAttributes(): array;

Gets extra HTML attributes.

getContent()

public function getContent( string $basePath = null ): string;

Returns the content of the asset as an string Optionally a base path where the asset is located can be set

getFilter()

public function getFilter(): bool;

getPath()

public function getPath(): string;

getRealSourcePath()

public function getRealSourcePath( string $basePath = null ): string;

Returns the complete location where the asset is located

getRealTargetPath()

public function getRealTargetPath( string $basePath = null ): string;

Returns the complete location where the asset must be written

getRealTargetUri()

public function getRealTargetUri(): string;

Returns the real target uri for the generated HTML

getSourcePath()

public function getSourcePath(): string;

getTargetPath()

public function getTargetPath(): string;

getTargetUri()

public function getTargetUri(): string;

getType()

public function getType(): string;

getVersion()

public function getVersion(): string|null;

Version of resource

isAutoVersion()

public function isAutoVersion(): bool;

Checks if the asset is using auto version

isLocal()

public function isLocal(): bool;

Checks if the asset is local or not

setAttributes()

public function setAttributes( array $attributes ): AssetInterface;

Sets extra HTML attributes

setAutoVersion()

public function setAutoVersion( bool $flag ): AssetInterface;

setFilter()

public function setFilter( bool $filter ): AssetInterface;

Sets if the asset must be filtered or not

setIsLocal()

public function setIsLocal( bool $flag ): AssetInterface;

Sets if the asset is local or external

setPath()

public function setPath( string $path ): AssetInterface;

Sets the asset's path

setSourcePath()

public function setSourcePath( string $sourcePath ): AssetInterface;

Sets the asset's source path

setTargetPath()

public function setTargetPath( string $targetPath ): AssetInterface;

Sets the asset's target path

setTargetUri()

public function setTargetUri( string $targetUri ): AssetInterface;

Sets a target uri for the generated HTML

setType()

public function setType( string $type ): AssetInterface;

Sets the asset's type

setVersion()

public function setVersion( string $version ): AssetInterface;

Sets the asset's version

Protected · 2

phpFileExists()

protected function phpFileExists( string $filename ): bool;

@todo to be removed when we get traits

phpFileGetContents()

protected function phpFileGetContents( string $filename );

Assets\AssetInterface

Interface Source on GitHub

Phalcon\Assets\AssetInterface

Uses Phalcon\Contracts\Assets\Asset

Assets\Asset\Css

Class Source on GitHub

Represents CSS assets

Uses Phalcon\Assets\Asset

Method Summary

Methods

Public · 1

__construct()

public function __construct(
    string $path,
    bool $local = true,
    bool $filter = true,
    array $attributes = [],
    string $version = null,
    bool $autoVersion = false
);

Phalcon\Assets\Asset\Css constructor

Assets\Asset\Js

Class Source on GitHub

Represents JavaScript assets

Uses Phalcon\Assets\Asset

Method Summary

Methods

Public · 1

__construct()

public function __construct(
    string $path,
    bool $local = true,
    bool $filter = true,
    array $attributes = [],
    string $version = null,
    bool $autoVersion = false
);

Phalcon\Assets\Asset\Js constructor

Assets\Collection

Class Source on GitHub

Collection of asset objects

  • Phalcon\Assets\Collection — implements Countable, IteratorAggregate

Uses ArrayIterator · Countable · IteratorAggregate

Method Summary

public static add( AssetInterface $asset ) Adds an asset to the collection public static addCss(string $path,mixed $isLocal = null,bool $filter = true,array $attributes = [],string $version = null,bool $autoVersion = false) Adds a CSS asset to the collection public static addFilter( FilterInterface $filter ) Adds a filter to the collection public static addInline( Inline $code ) Adds an inline code to the collection public static addInlineCss(string $content,bool $filter = true,array $attributes = []) Adds an inline CSS to the collection public static addInlineJs(string $content,bool $filter = true,array $attributes = []) Adds an inline JavaScript to the collection public static addJs(string $path,mixed $isLocal = null,bool $filter = true,array $attributes = [],string $version = null,bool $autoVersion = false) Adds a JavaScript asset to the collection public int count() Return the count of the assets public array getAssets() public array getAttributes() public array getCodes() public array getFilters() public \Traversable getIterator() Returns the generator of the class public bool getJoin() public string getPrefix() public string getRealTargetPath( string $basePath ) Returns the complete location where the joined/filtered collection must public string getSourcePath() public bool getTargetIsLocal() public string getTargetPath() public string getTargetUri() public string getVersion() public bool has( AssetInterface $asset ) Checks this the asset is added to the collection. public bool isAutoVersion() Checks if collection is using auto version public bool isLocal() public static join( bool $flag ) Sets if all filtered assets in the collection must be joined in a single public static setAttributes( array $attributes ) Sets extra HTML attributes public static setAutoVersion( bool $flag ) public static setFilters( array $filters ) Sets an array of filters in the collection public static setIsLocal( bool $flag ) Sets if the collection uses local assets by default public static setPrefix( string $prefix ) Sets a common prefix for all the assets public static setSourcePath( string $sourcePath ) Sets a base source path for all the assets in this collection public static setTargetIsLocal( bool $flag ) Sets if the target local or not public static setTargetPath( string $targetPath ) Sets the target path of the file for the filtered/join output public static setTargetUri( string $targetUri ) Sets a target uri for the generated HTML public static setVersion( string $version ) Sets the version protected bool addAsset( AssetInterface $asset ) Adds an asset or inline-code to the collection

Properties

protected array $assets = []
protected array $attributes = []
protected bool $autoVersion = false Should version be determined from file modification time
protected array $codes = []
protected array $filters = []
protected bool $isLocal = true
protected bool $join = true
protected string $prefix = ""
protected string $sourcePath = ""
protected bool $targetIsLocal = true
protected string $targetPath = ""
protected string $targetUri = ""
protected string $version = ""

Methods

Public · 35

add()

public function add( AssetInterface $asset ): static;

Adds an asset to the collection

addCss()

public function addCss(
    string $path,
    mixed $isLocal = null,
    bool $filter = true,
    array $attributes = [],
    string $version = null,
    bool $autoVersion = false
): static;

Adds a CSS asset to the collection

addFilter()

public function addFilter( FilterInterface $filter ): static;

Adds a filter to the collection

addInline()

public function addInline( Inline $code ): static;

Adds an inline code to the collection

addInlineCss()

public function addInlineCss(
    string $content,
    bool $filter = true,
    array $attributes = []
): static;

Adds an inline CSS to the collection

addInlineJs()

public function addInlineJs(
    string $content,
    bool $filter = true,
    array $attributes = []
): static;

Adds an inline JavaScript to the collection

addJs()

public function addJs(
    string $path,
    mixed $isLocal = null,
    bool $filter = true,
    array $attributes = [],
    string $version = null,
    bool $autoVersion = false
): static;

Adds a JavaScript asset to the collection

count()

public function count(): int;

Return the count of the assets

@link https://php.net/manual/en/countable.count.php

getAssets()

public function getAssets(): array;

getAttributes()

public function getAttributes(): array;

getCodes()

public function getCodes(): array;

getFilters()

public function getFilters(): array;

getIterator()

public function getIterator(): \Traversable;

Returns the generator of the class

@link https://php.net/manual/en/iteratoraggregate.getiterator.php

getJoin()

public function getJoin(): bool;

getPrefix()

public function getPrefix(): string;

getRealTargetPath()

public function getRealTargetPath( string $basePath ): string;

Returns the complete location where the joined/filtered collection must be written

getSourcePath()

public function getSourcePath(): string;

getTargetIsLocal()

public function getTargetIsLocal(): bool;

getTargetPath()

public function getTargetPath(): string;

getTargetUri()

public function getTargetUri(): string;

getVersion()

public function getVersion(): string;

has()

public function has( AssetInterface $asset ): bool;

Checks this the asset is added to the collection.

use Phalcon\Assets\Asset;
use Phalcon\Assets\Collection;

$collection = new Collection();

$asset = new Asset("js", "js/jquery.js");

$collection->add($asset);
$collection->has($asset); // true

isAutoVersion()

public function isAutoVersion(): bool;

Checks if collection is using auto version

isLocal()

public function isLocal(): bool;

join()

public function join( bool $flag ): static;

Sets if all filtered assets in the collection must be joined in a single result file

setAttributes()

public function setAttributes( array $attributes ): static;

Sets extra HTML attributes

setAutoVersion()

public function setAutoVersion( bool $flag ): static;

setFilters()

public function setFilters( array $filters ): static;

Sets an array of filters in the collection

setIsLocal()

public function setIsLocal( bool $flag ): static;

Sets if the collection uses local assets by default

setPrefix()

public function setPrefix( string $prefix ): static;

Sets a common prefix for all the assets

setSourcePath()

public function setSourcePath( string $sourcePath ): static;

Sets a base source path for all the assets in this collection

setTargetIsLocal()

public function setTargetIsLocal( bool $flag ): static;

Sets if the target local or not

setTargetPath()

public function setTargetPath( string $targetPath ): static;

Sets the target path of the file for the filtered/join output

setTargetUri()

public function setTargetUri( string $targetUri ): static;

Sets a target uri for the generated HTML

setVersion()

public function setVersion( string $version ): static;

Sets the version

Protected · 1

addAsset()

final protected function addAsset( AssetInterface $asset ): bool;

Adds an asset or inline-code to the collection

Assets\Exception

Class Source on GitHub

Exceptions thrown in Phalcon\Assets will use this class

Assets\Exceptions\AssetSourceTargetCollision

Class Source on GitHub

Uses Phalcon\Assets\Exception

Method Summary

Methods

Public · 1

__construct()

public function __construct( string $path );

Assets\Exceptions\CannotReadAsset

Class Source on GitHub

Uses Phalcon\Assets\Exception

Method Summary

Methods

Public · 1

__construct()

public function __construct( string $path );

Assets\Exceptions\CollectionNotFound

Class Source on GitHub

Uses Phalcon\Assets\Exception

Method Summary

Methods

Public · 1

__construct()

public function __construct( string $name = "" );

Assets\Exceptions\InvalidAssetSourcePath

Class Source on GitHub

Uses Phalcon\Assets\Exception

Method Summary

Methods

Public · 1

__construct()

public function __construct( string $path );

Assets\Exceptions\InvalidAssetTargetPath

Class Source on GitHub

Uses Phalcon\Assets\Exception

Method Summary

Methods

Public · 1

__construct()

public function __construct( string $path );

Assets\Exceptions\InvalidFilter

Class Source on GitHub

Uses Phalcon\Assets\Exception

Method Summary

Methods

Public · 1

__construct()

public function __construct();

Assets\Exceptions\InvalidTargetPath

Class Source on GitHub

Uses Phalcon\Assets\Exception

Method Summary

Methods

Public · 1

__construct()

public function __construct( string $path );

Assets\Exceptions\TargetPathIsDirectory

Class Source on GitHub

Uses Phalcon\Assets\Exception

Method Summary

Methods

Public · 1

__construct()

public function __construct( string $path );

Assets\FilterInterface

Interface Source on GitHub

Phalcon\Assets\FilterInterface

Uses Phalcon\Contracts\Assets\Filter

Assets\Filters\Cssmin

Class Source on GitHub

Filter intended to minify CSS content (remove comments, newlines, and line feeds, and drop the last semicolon of the last property).

NOTE: This functionality is not currently available; filter() returns the content unchanged.

Uses Phalcon\Assets\FilterInterface

Method Summary

Methods

Public · 1

filter()

public function filter( string $content ): string;

Filters the content using CSSMIN

Assets\Filters\Jsmin

Class Source on GitHub

Filter intended to minify JavaScript content (remove comments and the characters that are insignificant to JavaScript - tabs, carriage returns, and most spaces and linefeeds).

NOTE: This functionality is not currently available; filter() returns the content unchanged.

Uses Phalcon\Assets\FilterInterface

Method Summary

Methods

Public · 1

filter()

public function filter( string $content ): string;

Filters the content using JSMIN

Assets\Filters\None

Class Source on GitHub

Returns the content without make any modification to the original source

Uses Phalcon\Assets\FilterInterface

Method Summary

Methods

Public · 1

filter()

public function filter( string $content ): string;

Returns the content as is

Assets\Inline

Class Source on GitHub

Represents an inline asset

$inline = new \Phalcon\Assets\Inline("js", "alert('hello world');");

Method Summary

Properties

protected array $attributes
protected string $content
protected bool $filter
protected string $type

Methods

Public · 9

__construct()

public function __construct(
    string $type,
    string $content,
    bool $filter = true,
    array $attributes = []
);

Phalcon\Assets\Inline constructor

getAssetKey()

public function getAssetKey(): string;

Gets the asset's key.

getAttributes()

public function getAttributes(): array;

getContent()

public function getContent(): string;

getFilter()

public function getFilter(): bool;

getType()

public function getType(): string;

setAttributes()

public function setAttributes( array $attributes ): AssetInterface;

Sets extra HTML attributes

setFilter()

public function setFilter( bool $filter ): AssetInterface;

Sets if the asset must be filtered or not

setType()

public function setType( string $type ): AssetInterface;

Sets the inline's type

Assets\Inline\Css

Class Source on GitHub

Represents an inlined CSS

Uses Phalcon\Assets\Inline

Method Summary

Methods

Public · 1

__construct()

public function __construct(
    string $content,
    bool $filter = true,
    array $attributes = []
);

Phalcon\Assets\Inline\Css constructor

Assets\Inline\Js

Class Source on GitHub

Represents an inline JavaScript

Uses Phalcon\Assets\Inline

Method Summary

Methods

Public · 1

__construct()

public function __construct(
    string $content,
    bool $filter = true,
    array $attributes = []
);

Phalcon\Assets\Inline\Js constructor

Assets\Manager

Class Source on GitHub

Manages collections of CSS/JavaScript assets

Uses Phalcon\Assets\Asset\Css · Phalcon\Assets\Asset\Js · Phalcon\Assets\Exceptions\AssetSourceTargetCollision · Phalcon\Assets\Exceptions\CollectionNotFound · Phalcon\Assets\Exceptions\InvalidAssetSourcePath · Phalcon\Assets\Exceptions\InvalidAssetTargetPath · Phalcon\Assets\Exceptions\InvalidFilter · Phalcon\Assets\Exceptions\InvalidTargetPath · Phalcon\Assets\Exceptions\TargetPathIsDirectory · Phalcon\Assets\Inline\Css · Phalcon\Assets\Inline\Js · Phalcon\Di\AbstractInjectionAware · Phalcon\Html\Helper\Element · Phalcon\Html\Helper\Link · Phalcon\Html\Helper\Script · Phalcon\Html\TagFactory

Method Summary

public __construct(TagFactory $tagFactory,array $options = []) Manager constructor. public static addAsset( Asset $asset ) Adds a raw asset to the manager public static addAssetByType(string $type,Asset $asset) Adds a asset by its type public static addCss(string $path,bool $local = true,bool $filter = true,array $attributes = [],string $version = null,bool $autoVersion = false) Adds a CSS asset to the 'css' collection public static addInlineCode( Inline $code ) Adds a raw inline code to the manager public static addInlineCodeByType(string $type,Inline $code) Adds an inline code by its type public static addInlineCss(string $content,bool $filter = true,array $attributes = []) Adds an inline CSS to the 'css' collection public static addInlineJs(string $content,bool $filter = true,array $attributes = []) Adds an inline JavaScript to the 'js' collection public static addJs(string $path,bool $local = true,bool $filter = true,array $attributes = [],string $version = null,bool $autoVersion = false) Adds a JavaScript asset to the 'js' collection public Collection collection( string $name ) Creates/Returns a collection of assets public array collectionAssetsByType(array $assets,string $type) Creates/Returns a collection of assets by type public bool exists( string $name ) Returns true or false if collection exists. public Collection get( string $name ) Returns a collection by its id. public Collection[] getCollections() Returns existing collections in the manager public Collection getCss() Returns the CSS collection of assets public Collection getJs() Returns the CSS collection of assets public array getOptions() Returns the manager options public bool has( string $name ) Returns true or false if collection exists. public string|null output(Collection $collection,string $type) Traverses a collection calling the callback to generate its HTML public string outputCss( string $name = null ) Prints the HTML for CSS assets public string outputInline(Collection $collection,mixed $type) Traverses a collection and generate its HTML public string outputInlineCss( string $name = null ) Prints the HTML for inline CSS public string outputInlineJs( string $name = null ) Prints the HTML for inline JS public string outputJs( string $name = null ) Prints the HTML for JS assets public static set(string $name,Collection $collection) Sets a collection in the Assets Manager public static setOptions( array $options ) Sets the manager options public static useImplicitOutput( bool $implicitOutput ) Sets if the HTML generated must be directly printed or returned

Properties

protected array $collections = []
protected bool $implicitOutput = true
protected array $options = []
protected TagFactory $tagFactory

Methods

Public · 27

__construct()

public function __construct(
    TagFactory $tagFactory,
    array $options = []
);

Manager constructor.

addAsset()

public function addAsset( Asset $asset ): static;

Adds a raw asset to the manager

addAssetByType()

public function addAssetByType(
    string $type,
    Asset $asset
): static;

Adds a asset by its type

addCss()

public function addCss(
    string $path,
    bool $local = true,
    bool $filter = true,
    array $attributes = [],
    string $version = null,
    bool $autoVersion = false
): static;

Adds a CSS asset to the 'css' collection

addInlineCode()

public function addInlineCode( Inline $code ): static;

Adds a raw inline code to the manager

addInlineCodeByType()

public function addInlineCodeByType(
    string $type,
    Inline $code
): static;

Adds an inline code by its type

addInlineCss()

public function addInlineCss(
    string $content,
    bool $filter = true,
    array $attributes = []
): static;

Adds an inline CSS to the 'css' collection

addInlineJs()

public function addInlineJs(
    string $content,
    bool $filter = true,
    array $attributes = []
): static;

Adds an inline JavaScript to the 'js' collection

addJs()

public function addJs(
    string $path,
    bool $local = true,
    bool $filter = true,
    array $attributes = [],
    string $version = null,
    bool $autoVersion = false
): static;

Adds a JavaScript asset to the 'js' collection

$assets->addJs("scripts/jquery.js");
$assets->addJs("http://jquery.my-cdn.com/jquery.js", false);

collection()

public function collection( string $name ): Collection;

Creates/Returns a collection of assets

collectionAssetsByType()

public function collectionAssetsByType(
    array $assets,
    string $type
): array;

Creates/Returns a collection of assets by type

exists()

public function exists( string $name ): bool;

Returns true or false if collection exists.

if ($manager->exists("jsHeader")) {
    // \Phalcon\Assets\Collection
    $collection = $manager->get("jsHeader");
}

get()

public function get( string $name ): Collection;

Returns a collection by its id.

$scripts = $assets->get("js");

getCollections()

public function getCollections(): Collection[];

Returns existing collections in the manager

getCss()

public function getCss(): Collection;

Returns the CSS collection of assets

getJs()

public function getJs(): Collection;

Returns the CSS collection of assets

getOptions()

public function getOptions(): array;

Returns the manager options

has()

public function has( string $name ): bool;

Returns true or false if collection exists.

if ($manager->has("jsHeader")) {
    // \Phalcon\Assets\Collection
    $collection = $manager->get("jsHeader");
}

output()

public function output(
    Collection $collection,
    string $type
): string|null;

Traverses a collection calling the callback to generate its HTML

outputCss()

public function outputCss( string $name = null ): string;

Prints the HTML for CSS assets

outputInline()

public function outputInline(
    Collection $collection,
    mixed $type
): string;

Traverses a collection and generate its HTML

outputInlineCss()

public function outputInlineCss( string $name = null ): string;

Prints the HTML for inline CSS

outputInlineJs()

public function outputInlineJs( string $name = null ): string;

Prints the HTML for inline JS

outputJs()

public function outputJs( string $name = null ): string;

Prints the HTML for JS assets

set()

public function set(
    string $name,
    Collection $collection
): static;

Sets a collection in the Assets Manager

$assets->set("js", $collection);

setOptions()

public function setOptions( array $options ): static;

Sets the manager options

useImplicitOutput()

public function useImplicitOutput( bool $implicitOutput ): static;

Sets if the HTML generated must be directly printed or returned