跳转到内容

Phalcon资源

注意

所有类都以前缀命名Phalcon

资源\Asset

GitHub上的源码

  • 命名空间

    • Phalcon\Assets
  • 使用

  • 继承

  • 实现

    • AssetInterface

表示一个资产

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

属性

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

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

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

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

/**
 * @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|null
 */
protected $version;

方法

public function __construct( string $type, string $path, bool $isLocal = bool, bool $filter = bool, array $attributes = [], string $version = null, bool $isAutoVersion = bool );
资产构造函数。

public function getAssetKey(): string;
获取资产的键。

public function getAttributes(): array;
获取额外的HTML属性。

public function getContent( string $basePath = null ): string;
返回资产的内容为字符串。可选地可以设置资产所在的基路径。

public function getFilter(): bool;
public function getPath(): string;

public function getRealSourcePath( string $basePath = null ): string;
返回资产所在的完整位置。

public function getRealTargetPath( string $basePath = null ): string;
返回资产必须写入的完整位置。

public function getRealTargetUri(): string;
返回生成的HTML的实际目标URI。

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

public function getVersion(): string | null;
资源版本

public function isAutoVersion(): bool;
检查资产是否使用自动版本

public function isLocal(): bool;
检查资产是否为本地或非本地

public function setAttributes( array $attributes ): AssetInterface;
设置额外的HTML属性

public function setAutoVersion( bool $flag ): AssetInterface;

public function setFilter( bool $filter ): AssetInterface;
设置资产是否需要过滤

public function setIsLocal( bool $flag ): AssetInterface;
设置资产是本地还是外部

public function setPath( string $path ): AssetInterface;
设置资产的路径

public function setSourcePath( string $sourcePath ): AssetInterface;
设置资产的源路径

public function setTargetPath( string $targetPath ): AssetInterface;
设置资产的目标路径

public function setTargetUri( string $targetUri ): AssetInterface;
为生成的HTML设置目标URI

public function setType( string $type ): AssetInterface;
设置资产的类型

public function setVersion( string $version ): AssetInterface;
设置资产的版本

protected function phpFileExists( string $filename ): bool;
@todo 当我们获取 traits 时将被移除

protected function phpFileGetContents( string $filename );

资源\Asset\Css

GitHub上的源码

  • 命名空间

    • Phalcon\Assets\Asset
  • 使用

    • Phalcon\Assets\Asset
  • 继承

    AssetBase

  • 实现

表示CSS资产

方法

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

资源\Asset\Js

GitHub上的源码

  • 命名空间

    • Phalcon\Assets\Asset
  • 使用

    • Phalcon\Assets\Asset
  • 继承

    AssetBase

  • 实现

表示JavaScript资产

方法

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

资源\AssetInterfaceInterface

GitHub上的源码

  • 命名空间

    • Phalcon\Assets
  • 使用

  • 继承

  • 实现

自定义Phalcon\Assets资源的接口

方法

public function getAssetKey(): string;
获取资产的键。

public function getAttributes(): array | null;
获取额外的HTML属性。

public function getFilter(): bool;
获取资产是否需要过滤。

public function getType(): string;
获取资产的类型。

public function setAttributes( array $attributes ): AssetInterface;
设置额外的HTML属性。

public function setFilter( bool $filter ): AssetInterface;
设置资产是否需要过滤。

public function setType( string $type ): AssetInterface;
设置资产的类型。

资源\Collection

GitHub上的源码

  • 命名空间

    • Phalcon\Assets
  • 使用

    • ArrayIterator
    • Countable
    • IteratorAggregate
  • 继承

  • 实现

    • Countable
    • IteratorAggregate

资产对象集合

属性

/**
 * @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 bool
 */
protected $isLocal = true;

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

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

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

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

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

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

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

方法

public function add( AssetInterface $asset ): Collection;
向集合中添加资产

public function addCss( string $path, mixed $isLocal = null, bool $filter = bool, array $attributes = [], string $version = null, bool $autoVersion = bool ): Collection;
向集合中添加CSS资产

public function addFilter( FilterInterface $filter ): Collection;
向集合中添加过滤器

public function addInline( Inline $code ): Collection;
向集合中添加内联代码

public function addInlineCss( string $content, bool $filter = bool, array $attributes = [] ): Collection;
向集合中添加内联CSS

public function addInlineJs( string $content, bool $filter = bool, array $attributes = [] ): Collection;
向集合中添加内联JavaScript

public function addJs( string $path, mixed $isLocal = null, bool $filter = bool, array $attributes = [], string $version = null, bool $autoVersion = bool ): Collection;
向集合中添加JavaScript资产

public function count(): int;
返回资产的数量

public function getAssets(): array;
public function getAttributes(): array;
public function getCodes(): array;
public function getFilters(): array;

public function getIterator(): \Traversable;
返回类的生成器

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

public function getJoin(): bool;
public function getPrefix(): string;

public function getRealTargetPath( string $basePath ): string;
返回连接/过滤集合必须写入的完整位置

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

public function has( AssetInterface $asset ): bool;
检查此资产是否已添加到集合中。

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;
检查集合是否使用自动版本

public function isLocal(): bool;

public function join( bool $flag ): Collection;
设置集合中所有过滤后的资产是否必须合并到单个结果文件中

public function setAttributes( array $attributes ): Collection;
设置额外的HTML属性

public function setAutoVersion( bool $flag ): Collection;

public function setFilters( array $filters ): Collection;
在集合中设置过滤器数组

public function setIsLocal( bool $flag ): Collection;
设置集合是否默认使用本地资产

public function setPrefix( string $prefix ): Collection;
为所有资产设置公共前缀

public function setSourcePath( string $sourcePath ): Collection;
为集合中的所有资产设置基本源路径

public function setTargetIsLocal( bool $flag ): Collection;
设置目标是否为本地或非本地

public function setTargetPath( string $targetPath ): Collection;
设置过滤/连接输出文件的目标路径

public function setTargetUri( string $targetUri ): Collection;
为生成的HTML设置目标URI

public function setVersion( string $version ): Collection;
设置版本

final protected function addAsset( AssetInterface $asset ): bool;
向集合中添加资产或内联代码

资源\Exception

GitHub上的源码

  • 命名空间

    • Phalcon\Assets
  • 使用

  • 继承

    \Exception

  • 实现

在Phalcon\Assets中抛出的异常将使用此类

资源\FilterInterfaceInterface

GitHub上的源码

  • 命名空间

    • Phalcon\Assets
  • 使用

  • 继承

  • 实现

自定义Phalcon\Assets过滤器的接口

方法

public function filter( string $content ): string;
过滤内容并返回包含过滤后内容的字符串

资源\Filters\Cssmin

GitHub上的源码

  • 命名空间

    • Phalcon\Assets\Filters
  • 使用

    • Phalcon\Assets\FilterInterface
  • 继承

  • 实现

    • FilterInterface

压缩CSS - 删除注释、删除换行符和回车符、保留最后一个属性的分号

方法

public function filter( string $content ): string;
使用CSSMIN过滤内容

注意:此功能当前不可用

资源\Filters\Jsmin

GitHub上的源码

  • 命名空间

    • Phalcon\Assets\Filters
  • 使用

    • Phalcon\Assets\FilterInterface
  • 继承

  • 实现

    • FilterInterface

删除对JavaScript而言无意义的字符。注释将被删除。制表符将被替换为空格。回车符将被替换为换行符。大多数空格和换行符将被删除。

方法

public function filter( string $content ): string;
使用JSMIN过滤内容

注意:此功能当前不可用

资源\Filters\None

GitHub上的源码

  • 命名空间

    • Phalcon\Assets\Filters
  • 使用

    • Phalcon\Assets\FilterInterface
  • 继承

  • 实现

    • FilterInterface

返回内容而不对原始源进行任何修改

方法

public function filter( string $content ): string;
返回内容原样

资源\Inline

GitHub上的源码

  • 命名空间

    • Phalcon\Assets
  • 使用

  • 继承

  • 实现

    • AssetInterface

表示一个内联资产

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

属性

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

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

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

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

方法

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

public function getAssetKey(): string;
获取资产的键。

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

public function setAttributes( array $attributes ): AssetInterface;
设置额外的HTML属性

public function setFilter( bool $filter ): AssetInterface;
设置资产是否需要过滤

public function setType( string $type ): AssetInterface;
设置内联类型

资源\Inline\Css

GitHub上的源码

  • 命名空间

    • Phalcon\Assets\Inline
  • 使用

    • Phalcon\Assets\Inline
  • 继承

    InlineBase

  • 实现

表示一个内嵌CSS

方法

public function __construct( string $content, bool $filter = bool, array $attributes = [] );
Phalcon\Assets\Inline\Css构造函数

资源\Inline\Js

GitHub上的源码

  • 命名空间

    • Phalcon\Assets\Inline
  • 使用

    • Phalcon\Assets\Inline
  • 继承

    InlineBase

  • 实现

表示一个内联JavaScript

方法

public function __construct( string $content, bool $filter = bool, array $attributes = [] );
Phalcon\Assets\Inline\Js构造函数

资源\Manager

GitHub上的源码

  • 命名空间

    • Phalcon\Assets
  • 使用

    • 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
  • 继承

    AbstractInjectionAware

  • 实现

管理CSS/JavaScript资产集合

属性

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

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

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

/**
 * @var TagFactory
 */
protected $tagFactory;

方法

public function __construct( TagFactory $tagFactory, array $options = [] );
Manager构造函数。

public function addAsset( Asset $asset ): Manager;
向管理器中添加原始资产

public function addAssetByType( string $type, Asset $asset ): Manager;
根据类型添加资产

public function addCss( string $path, bool $local = bool, bool $filter = bool, array $attributes = [], string $version = null, bool $autoVersion = bool ): Manager;
向'css'集合中添加CSS资产

public function addInlineCode( Inline $code ): Manager;
向管理器中添加原始内联代码

public function addInlineCodeByType( string $type, Inline $code ): Manager;
根据类型添加内联代码

public function addInlineCss( string $content, bool $filter = bool, array $attributes = [] ): Manager;
向'css'集合中添加内联CSS

public function addInlineJs( string $content, bool $filter = bool, array $attributes = [] ): Manager;
向'js'集合中添加内联JavaScript

public function addJs( string $path, bool $local = bool, bool $filter = bool, array $attributes = [], string $version = null, bool $autoVersion = bool ): Manager;
向'js'集合中添加JavaScript资产

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

public function collection( string $name ): Collection;
创建/返回资产集合

public function collectionAssetsByType( array $assets, string $type ): array;
按类型创建/返回资产集合

public function exists( string $name ): bool;
如果集合存在返回true或false。

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

public function get( string $name ): Collection;
根据其ID返回集合。

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

public function getCollections(): Collection[];
返回管理器中的现有集合

public function getCss(): Collection;
返回CSS资产集合

public function getJs(): Collection;
返回CSS资产集合

public function getOptions(): array;
返回管理器选项

public function has( string $name ): bool;
如果集合存在返回true或false。

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

public function output( Collection $collection, string $type ): string | null;
遍历集合调用回调生成其HTML

public function outputCss( string $name = null ): string;
打印CSS资产的HTML

public function outputInline( Collection $collection, mixed $type ): string;
遍历集合并生成其HTML

public function outputInlineCss( string $name = null ): string;
打印内联CSS的HTML

public function outputInlineJs( string $name = null ): string;
打印内联JS的HTML

public function outputJs( string $name = null ): string;
打印JS资产的HTML

public function set( string $name, Collection $collection ): Manager;
在资产管理器中设置集合

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

public function setOptions( array $options ): Manager;
设置管理器选项

public function useImplicitOutput( bool $implicitOutput ): Manager;
设置生成的HTML是否需要直接打印或返回