0 follower

Final Class Yiisoft\Assets\AssetPublisher

InheritanceYiisoft\Assets\AssetPublisher
ImplementsYiisoft\Assets\AssetPublisherInterface

AssetPublisher is responsible for executing the publication of the assets from {@see AssetBundle::$sourcePath} to {@see AssetBundle::$basePath}.

Psalm Types

Name Value
HashCallback callable

Public Methods

Hide inherited methods

Method Description Defined By
__construct() Yiisoft\Assets\AssetPublisher
getPublishedPath() Yiisoft\Assets\AssetPublisher
getPublishedUrl() Yiisoft\Assets\AssetPublisher
publish() Yiisoft\Assets\AssetPublisher
withDirMode() Returns a new instance with the specified directory mode. Yiisoft\Assets\AssetPublisher
withFileMode() Returns a new instance with the specified files mode. Yiisoft\Assets\AssetPublisher
withForceCopy() Returns a new instance with the specified force copy value. Yiisoft\Assets\AssetPublisher
withHashCallback() Returns a new instance with the specified force hash callback. Yiisoft\Assets\AssetPublisher
withLinkAssets() Returns a new instance with the specified link assets value. Yiisoft\Assets\AssetPublisher

Method Details

Hide inherited methods

__construct() public method

public mixed __construct ( \Yiisoft\Aliases\Aliases $aliases, boolean $forceCopy false, boolean $linkAssets false )
$aliases \Yiisoft\Aliases\Aliases

The aliases instance.

$forceCopy boolean

Whether the directory being published should be copied even if it is found in the target directory. See {@see \Yiisoft\Assets\withForceCopy()}.

$linkAssets boolean

Whether to use symbolic link to publish asset files. See {@see \Yiisoft\Assets\withLinkAssets()}.

                public function __construct(
    private readonly Aliases $aliases,
    private bool $forceCopy = false,
    private bool $linkAssets = false,
) {
}

            
getPublishedPath() public method

public string|null getPublishedPath ( string $sourcePath )
$sourcePath string

                public function getPublishedPath(string $sourcePath): ?string
{
    $sourcePath = $this->aliases->get($sourcePath);
    if (isset($this->published[$sourcePath])) {
        return $this->published[$sourcePath][0];
    }
    return null;
}

            
getPublishedUrl() public method

public string|null getPublishedUrl ( string $sourcePath )
$sourcePath string

                public function getPublishedUrl(string $sourcePath): ?string
{
    $sourcePath = $this->aliases->get($sourcePath);
    if (isset($this->published[$sourcePath])) {
        return $this->published[$sourcePath][1];
    }
    return null;
}

            
publish() public method

public array publish ( Yiisoft\Assets\AssetBundle $bundle )
$bundle Yiisoft\Assets\AssetBundle

                public function publish(AssetBundle $bundle): array
{
    if (empty($bundle->sourcePath)) {
        throw new InvalidConfigException(
            'The sourcePath must be defined in AssetBundle property public ?string $sourcePath = $path.',
        );
    }
    $sourcePath = $this->aliases->get($bundle->sourcePath);
    if (isset($this->published[$sourcePath])) {
        return $this->published[$sourcePath];
    }
    if (empty($bundle->basePath)) {
        throw new InvalidConfigException(
            'The basePath must be defined in AssetBundle property public ?string $basePath = $path.',
        );
    }
    if ($bundle->baseUrl === null) {
        throw new InvalidConfigException(
            'The baseUrl must be defined in AssetBundle property public ?string $baseUrl = $path.',
        );
    }
    if (!file_exists($sourcePath)) {
        throw new InvalidConfigException("The sourcePath to be published does not exist: {$sourcePath}");
    }
    return $this->published[$sourcePath] = $this->publishBundleDirectory($bundle);
}

            
withDirMode() public method

Returns a new instance with the specified directory mode.

public self withDirMode ( integer $dirMode )
$dirMode integer

The permission to be set for newly generated asset directories. This value will be used by PHP chmod() function. No umask will be applied. Defaults to 0775, meaning the directory is read-writable by owner and group, but read-only for other users.

                public function withDirMode(int $dirMode): self
{
    $new = clone $this;
    $new->dirMode = $dirMode;
    return $new;
}

            
withFileMode() public method

Returns a new instance with the specified files mode.

public self withFileMode ( integer $fileMode )
$fileMode integer

He permission to be set for newly published asset files. This value will be used by PHP chmod() function. No umask will be applied. If not set, the permission will be determined by the current environment.

                public function withFileMode(int $fileMode): self
{
    $new = clone $this;
    $new->fileMode = $fileMode;
    return $new;
}

            
withForceCopy() public method

Returns a new instance with the specified force copy value.

public self withForceCopy ( boolean $forceCopy )
$forceCopy boolean

Whether the directory being published should be copied even if it is found in the target directory. This option is used only when publishing a directory. You may want to set this to be true during the development stage to make sure the published directory is always up-to-date. Do not set this to true on production servers as it will significantly degrade the performance.

                public function withForceCopy(bool $forceCopy): self
{
    $new = clone $this;
    $new->forceCopy = $forceCopy;
    return $new;
}

            
withHashCallback() public method

Returns a new instance with the specified force hash callback.

public self withHashCallback ( callable $hashCallback )
$hashCallback callable

A callback that will be called to produce hash for asset directory generation. The signature of the callback should be as follows:

function (string $path): string;

Where $path is the asset path. Note that the $path can be either directory where the asset files reside or a single file. For a CSS file that uses relative path in url(), the hash implementation should use the directory path of the file instead of the file path to include the relative asset files in the copying.

If this is not set, the asset manager will use the default CRC32 and filemtime in the hash method.

Example of an implementation using MD4 hash:

function (string $path): string {
    return hash('md4', $path);
}

                public function withHashCallback(callable $hashCallback): self
{
    $new = clone $this;
    $new->hashCallback = $hashCallback;
    return $new;
}

            
withLinkAssets() public method

Returns a new instance with the specified link assets value.

public self withLinkAssets ( boolean $linkAssets )
$linkAssets boolean

Whether to use symbolic link to publish asset files. Default is false, meaning asset files are copied to {@see \Yiisoft\Assets\AssetBundle::$basePath}. Using symbolic links has the benefit that the published assets will always be consistent with the source assets and there is no copy operation required. This is especially useful during development.

However, there are special requirements for hosting environments in order to use symbolic links. In particular, symbolic links are supported only on Linux/Unix, and Windows Vista/2008 or greater.

Moreover, some Web servers need to be properly configured so that the linked assets are accessible to Web users. For example, for Apache Web server, the following configuration directive should be added for the Web folder:

Options FollowSymLinks

                public function withLinkAssets(bool $linkAssets): self
{
    $new = clone $this;
    $new->linkAssets = $linkAssets;
    return $new;
}