Skip to content

Phalcon assets

NOTE

All classes are prefixed with Phalcon

Assets\Asset

Source on GitHub

  • Namespace

    • Phalcon\Assets
  • Uses

  • Extends

  • Implements

    • AssetInterface

Represents an asset

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

Properties

/**
 * @var array | null
 */
protected $attributes;

/**
 * @var bool
 */
protected $autoVersion = false;

/**
 * @var bool
 */
protected $filter;

/**
 * @var bool
 */
protected $local;

/**
 * @var string
 */
protected $path;

/**
 * @var string
 */
protected $sourcePath;

/**
 * @var string
 */
protected $targetPath;

/**
 * @var string
 */
protected $targetUri;

/**
 * @var string
 */
protected $type;

/**
 * Version of resource
 *
 * @var string
 */
protected $version;

Methods

public function __construct( string $type, string $path, bool $local = bool, bool $filter = bool, array $attributes = [], string $version = null, bool $autoVersion = bool );
Asset constructor.

public function getAssetKey(): string;
Gets the asset's key.

public function getAttributes(): array | null
Gets extra HTML attributes.

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

public function getFilter(): bool;

public function getLocal(): bool
Checks if the asset is local or not

public function getPath(): string;

public function getRealSourcePath( string $basePath = null ): string;
Returns the complete location where the asset is located

public function getRealTargetPath( string $basePath = null ): string;
Returns the complete location where the asset must be written

public function getRealTargetUri(): string;
Returns the real target uri for the generated HTML

public function getSourcePath(): string;
public function getTargetPath(): string;
public function getTargetUri(): string;
public function getType(): string;

public function getVersion(): string;
Version of resource

public function isAutoVersion(): bool;
Checks if the asset is using auto version

public function setAttributes( array $attributes ): AssetInterface;
Sets extra HTML attributes

public function setAutoVersion( bool $autoVersion )l

public function setFilter( bool $filter ): AssetInterface;
Sets if the asset must be filtered or not

public function setLocal( bool $local ): AssetInterface;
Sets if the asset is local or external

public function setPath( string $path ): AssetInterface;
Sets the asset's path

public function setSourcePath( string $sourcePath ): AssetInterface;
Sets the asset's source path

public function setTargetPath( string $targetPath ): AssetInterface;
Sets the asset's target path

public function setTargetUri( string $targetUri ): AssetInterface;
Sets a target uri for the generated HTML

public function setType( string $type ): AssetInterface;
Sets the asset's type

public function setVersion( string $version );
Sets the asset's version

Assets\Asset\Css

Source on GitHub

  • Namespace

    • Phalcon\Assets\Asset
  • Uses

    • Phalcon\Assets\Asset
  • Extends

    AssetBase

  • Implements

Represents CSS assets

Methods

public function __construct( string $path, bool $local = bool, bool $filter = bool, array $attributes = [], string $version = null, bool $autoVersion = bool );
Phalcon\Assets\Asset\Css constructor

Assets\Asset\Js

Source on GitHub

  • Namespace

    • Phalcon\Assets\Asset
  • Uses

    • Phalcon\Assets\Asset
  • Extends

    AssetBase

  • Implements

Represents JavaScript assets

Methods

public function __construct( string $path, bool $local = bool, bool $filter = bool, array $attributes = [], string $version = null, bool $autoVersion = bool );
Phalcon\Assets\Asset\Js constructor

Assets\AssetInterface Interface

Source on GitHub

  • Namespace

    • Phalcon\Assets
  • Uses

  • Extends

  • Implements

Interface for custom Phalcon\Assets resources

Methods

public function getAssetKey(): string;
Gets the asset's key.

public function getAttributes(): array | null;
Gets extra HTML attributes.

public function getFilter(): bool;
Gets if the asset must be filtered or not.

public function getType(): string;
Gets the asset's type.

public function setAttributes( array $attributes ): AssetInterface;
Sets extra HTML attributes.

public function setFilter( bool $filter ): AssetInterface;
Sets if the asset must be filtered or not.

public function setType( string $type ): AssetInterface;
Sets the asset's type.

Assets\Collection

Source on GitHub

  • Namespace

    • Phalcon\Assets
  • Uses

    • ArrayIterator
    • Countable
    • IteratorAggregate
  • Extends

  • Implements

    • Countable
    • IteratorAggregate

Collection of asset objects

Properties

/**
 * @var array
 */
protected $assets;

/**
 * @var array
 */
protected $attributes;

/**
 * Should version be determined from file modification time
 *
 * @var bool
 */
protected $autoVersion = false;

/**
 * @var array
 */
protected $codes;

/**
 * @var array
 */
protected $filters;

/**
 * @var array
 */
protected $includedAssets;

/**
 * @var bool
 */
protected $join = true;

/**
 * @var bool
 */
protected $local = true;

/**
 * @var string
 */
protected $prefix = '';

/**
 * @var int
 */
protected $position = 0;

/**
 * @var string
 */
protected $sourcePath = '';

/**
 * @var bool
 */
protected $targetLocal = true;

/**
 * @var string
 */
protected $targetPath = '';

/**
 * @var string
 */
protected $targetUri = '';

/**
 * @var string
 */
protected $version = '';

Methods

public function add( AssetInterface $asset ): Collection;
Adds an asset to the collection

public function addCss( string $path, mixed $local = null, bool $filter = bool, mixed $attributes = null, string $version = null, bool $autoVersion = bool ): Collection;
Adds a CSS asset to the collection

public function addFilter( FilterInterface $filter ): Collection;
Adds a filter to the collection

public function addInline( Inline $code ): Collection;
Adds an inline code to the collection

public function addInlineCss( string $content, bool $filter = bool, mixed $attributes = null ): Collection;
Adds an inline CSS to the collection

public function addInlineJs( string $content, bool $filter = bool, mixed $attributes = null ): Collection;
Adds an inline JavaScript to the collection

public function addJs( string $path, mixed $local = null, bool $filter = bool, mixed $attributes = null, string $version = null, bool $autoVersion = bool ): Collection;
Adds a JavaScript asset to the collection

public function count(): int;
Returns the number of elements in the form

public function current(): Asset;
Returns the current asset in the iterator

public function getAssets(): array;
public function getAttributes(): array;
public function getCodes(): array;
public function getFilters(): array;
public function getJoin(): bool;
public function getLocal(): bool;
public function getPosition(): int;
public function getPrefix(): string;

public function getRealTargetPath( string $basePath ): string;
Returns the complete location where the joined/filtered collection must be written

public function getSourcePath(): string;
public function getTargetLocal(): bool;
public function getTargetPath(): string;
public function getTargetUri(): string;
public function getVersion(): string;

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

public function isAutoVersion(): bool;
Checks if collection is using auto version

public function join( bool $join ): Collection;
Sets if all filtered assets in the collection must be joined in a single result file

public function key(): int;
Returns the current position/key in the iterator

public function next(): void;
Moves the internal iteration pointer to the next position

public function rewind(): void;
Rewinds the internal iterator

public function setAttributes( array $attributes ): Collection;
Sets extra HTML attributes

public function setAutoVersion( bool $autoVersion )

public function setFilters( array $filters ): Collection;
Sets an array of filters in the collection

public function setLocal( bool $local ): Collection;
Sets if the collection uses local assets by default

public function setPrefix( string $prefix ): Collection;
Sets a common prefix for all the assets

public function setSourcePath( string $sourcePath ): Collection;
Sets a base source path for all the assets in this collection

public function setTargetLocal( bool $targetLocal ): Collection;
Sets if the target local or not

public function setTargetPath( string $targetPath ): Collection;
Sets the target path of the file for the filtered/join output

public function setTargetUri( string $targetUri ): Collection;
Sets a target uri for the generated HTML

public function setVersion( string $version )
Sets the version

public function valid(): bool;
Check if the current element in the iterator is valid

final protected function addAsset( AssetInterface $asset ): bool;
Adds an asset or inline-code to the collection

Assets\Exception

Source on GitHub

  • Namespace

    • Phalcon\Assets
  • Uses

  • Extends

    \Exception

  • Implements

Exceptions thrown in Phalcon\Assets will use this class

Assets\FilterInterface Interface

Source on GitHub

  • Namespace

    • Phalcon\Assets
  • Uses

  • Extends

  • Implements

Interface for custom Phalcon\Assets filters

Methods

public function filter( string $content ): string;
Filters the content returning a string with the filtered content

Assets\Filters\Cssmin

Source on GitHub

  • Namespace

    • Phalcon\Assets\Filters
  • Uses

    • Phalcon\Assets\FilterInterface
  • Extends

  • Implements

    • FilterInterface

Minify the CSS - removes comments removes newlines and line feeds keeping removes last semicolon from last property

Methods

public function filter( string $content ): string;
Filters the content using CSSMIN

NOTE

This functionality is not currently available

Assets\Filters\Jsmin

Source on GitHub

  • Namespace

    • Phalcon\Assets\Filters
  • Uses

    • Phalcon\Assets\FilterInterface
  • Extends

  • Implements

    • FilterInterface

Deletes the characters which are insignificant to JavaScript. Comments will be removed. Tabs will be replaced with spaces. Carriage returns will be replaced with linefeeds. Most spaces and linefeeds will be removed.

Methods

public function filter( string $content ): string;
Filters the content using JSMIN

NOTE

This functionality is not currently available

NOTE: This functionality is not currently available

Assets\Filters\None

Source on GitHub

  • Namespace

    • Phalcon\Assets\Filters
  • Uses

    • Phalcon\Assets\FilterInterface
  • Extends

  • Implements

    • FilterInterface

Returns the content without make any modification to the original source

Methods

public function filter( string $content ): string;
Returns the content as is

Assets\Inline

Source on GitHub

  • Namespace

    • Phalcon\Assets
  • Uses

  • Extends

  • Implements

    • AssetInterface

Represents an inline asset

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

Properties

/**
 * @var array | null
 */
protected $attributes;

/**
 * @var string
 */
protected $content;

/**
 * @var bool
 */
protected $filter;

/**
 * @var string
 */
protected $type;

Methods

public function __construct( string $type, string $content, bool $filter = bool, array $attributes = [] );
Phalcon\Assets\Inline constructor

public function getAssetKey(): string;
Gets the asset's key.

public function getAttributes(): array;
public function getContent(): string;
public function getFilter(): bool;
public function getType(): string;

public function setAttributes( array $attributes ): AssetInterface;
Sets extra HTML attributes

public function setFilter( bool $filter ): AssetInterface;
Sets if the asset must be filtered or not

public function setType( string $type ): AssetInterface;
Sets the inline's type

Assets\Inline\Css

Source on GitHub

  • Namespace

    • Phalcon\Assets\Inline
  • Uses

    • Phalcon\Assets\Inline
  • Extends

    InlineBase

  • Implements

Represents an inlined CSS

Methods

public function __construct( string $content, bool $filter = bool, mixed $attributes = null );
Phalcon\Assets\Inline\Css constructor

Assets\Inline\Js

Source on GitHub

  • Namespace

    • Phalcon\Assets\Inline
  • Uses

    • Phalcon\Assets\Inline
  • Extends

    InlineBase

  • Implements

Represents an inline JavaScript

Methods

public function __construct( string $content, bool $filter = bool, mixed $attributes = null );
Phalcon\Assets\Inline\Js constructor

Assets\Manager

Source on GitHub

  • Namespace

    • Phalcon\Assets
  • Uses

    • Phalcon\Assets\Asset\Css
    • Phalcon\Assets\Asset\Js
    • 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
  • Extends

    AbstractInjectionAware

  • Implements

Manages collections of CSS/JavaScript assets

Properties

/**
 * @var array
 */
protected $collections;

/**
 * @var bool
 */
protected $implicitOutput = true;

/**
 * @var array
 */
protected $options;

Methods

public function __construct( array $options = [] );
Manager constructor.

public function addAsset( Asset $asset ): Manager;
Adds a raw asset to the manager

$assets->addAsset(
    new Phalcon\Assets\Asset("css", "css/style.css")
);

public function addAssetByType( string $type, Asset $asset ): Manager;
Adds a asset by its type

$assets->addAssetByType(
    "css",
    new \Phalcon\Assets\Asset\Css("css/style.css")
);

public function addCss( string $path, mixed $local = bool, bool $filter = bool, mixed $attributes = null, string $version = null, bool $autoVersion = bool ): Manager;
Adds a CSS asset to the 'css' collection

$assets->addCss("css/bootstrap.css");
$assets->addCss("http://bootstrap.my-cdn.com/style.css", false);

public function addInlineCode( Inline $code ): Manager;
Adds a raw inline code to the manager

public function addInlineCodeByType( string $type, Inline $code ): Manager;
Adds an inline code by its type

public function addInlineCss( string $content, mixed $filter = bool, mixed $attributes = null ): Manager;
Adds an inline CSS to the 'css' collection

public function addInlineJs( string $content, mixed $filter = bool, mixed $attributes = null ): Manager;
Adds an inline JavaScript to the 'js' collection

public function addJs( string $path, mixed $local = bool, bool $filter = bool, mixed $attributes = null, string $version = null, bool $autoVersion = bool ): Manager;
Adds a JavaScript asset to the 'js' collection

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

public function collection( string $name ): Collection;
Creates/Returns a collection of assets

public function collectionAssetsByType( array $assets, string $type ): array;
Creates/Returns a collection of assets by type

public function exists( string $id ): bool;
Returns true or false if collection exists.

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

public function get( string $id ): Collection;
Returns a collection by its id.

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

public function getCollections(): Collection[];
Returns existing collections in the manager

public function getCss(): Collection;
Returns the CSS collection of assets

public function getJs(): Collection;
Returns the CSS collection of assets

public function getOptions(): array;
Returns the manager options

public function output( Collection $collection, mixed $callback, mixed $type ): string | null;
Traverses a collection calling the callback to generate its HTML

public function outputCss( string $collectionName = null ): string;
Prints the HTML for CSS assets

public function outputInline( Collection $collection, mixed $type ): string;
Traverses a collection and generate its HTML

public function outputInlineCss( string $collectionName = null ): string;
Prints the HTML for inline CSS

public function outputInlineJs( string $collectionName = null ): string;
Prints the HTML for inline JS

public function outputJs( string $collectionName = null ): string;
Prints the HTML for JS assets

public function set( string $id, Collection $collection ): Manager;
Sets a collection in the Assets Manager

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

public function setOptions( array $options ): Manager;
Sets the manager options

public function useImplicitOutput( bool $implicitOutput ): Manager;
Sets if the HTML generated must be directly printed or returned