類別 yii\web\AssetManager
繼承關係 | yii\web\AssetManager » yii\base\Component » yii\base\BaseObject |
---|---|
實作介面 | yii\base\Configurable |
自版本起可用 | 2.0 |
原始碼 | https://github.com/yiisoft/yii2/blob/master/framework/web/AssetManager.php |
AssetManager 管理資源包配置與載入。
AssetManager 預設在 yii\web\Application 中配置為應用程式元件。您可以透過 Yii::$app->assetManager
存取該實例。
您可以透過在應用程式配置中的 components
下新增一個陣列來修改其配置,如下列範例所示
'assetManager' => [
'bundles' => [
// you can override AssetBundle configs here
],
]
關於 AssetManager 的更多詳細資訊與使用方法,請參閱資源指南文章。
公開屬性
屬性 | 類型 | 描述 | 定義於 |
---|---|---|---|
$afterCopy | callable|null | 在子目錄或檔案成功複製後呼叫的 PHP 回呼函數。 | yii\web\AssetManager |
$appendTimestamp | boolean | 是否在每個發布的資源 URL 後附加時間戳記。 | yii\web\AssetManager |
$assetMap | string[] | 從來源資源檔案 (鍵) 到目標資源檔案 (值) 的映射。 | yii\web\AssetManager |
$basePath | string | 儲存已發布資源檔案的根目錄。 | yii\web\AssetManager |
$baseUrl | string | 可透過此基礎 URL 存取已發布的資源檔案。 | yii\web\AssetManager |
$beforeCopy | callable|null | 在複製每個子目錄或檔案之前呼叫的 PHP 回呼函數。 | yii\web\AssetManager |
$behaviors | yii\base\Behavior[] | 附加到此元件的行為列表。 | yii\base\Component |
$bundles | array|false | 資源包配置列表。 | yii\web\AssetManager |
$converter | yii\web\AssetConverterInterface | 資源轉換器。 | yii\web\AssetManager |
$dirMode | integer | 為新產生的資源目錄設定的權限。 | yii\web\AssetManager |
$fileMode | integer|null | 為新發布的資源檔案設定的權限。 | yii\web\AssetManager |
$forceCopy | boolean | 即使在目標目錄中找到,是否仍應複製正在發布的目錄。 | yii\web\AssetManager |
$hashCallback | callable|null | 將被呼叫以產生資源目錄雜湊值的回呼函數。 | yii\web\AssetManager |
$linkAssets | boolean | 是否使用符號連結來發布資源檔案。 | yii\web\AssetManager |
公開方法
保護方法
方法 | 描述 | 定義於 |
---|---|---|
hash() | 為目錄路徑產生 CRC32 雜湊值。碰撞機率高於 MD5,但產生更小的雜湊字串。 | yii\web\AssetManager |
loadBundle() | 依名稱載入資源包類別。 | yii\web\AssetManager |
loadDummyBundle() | 依名稱載入虛擬資源包。 | yii\web\AssetManager |
publishDirectory() | 發布目錄。 | yii\web\AssetManager |
publishFile() | 發布檔案。 | yii\web\AssetManager |
resolveAsset() | yii\web\AssetManager |
屬性詳細資訊
在子目錄或檔案成功複製後呼叫的 PHP 回呼函數。此選項僅在發佈目錄時使用。回呼函數的簽名與 $beforeCopy 相同。這會作為參數 afterCopy
傳遞給 yii\helpers\FileHelper::copyDirectory()。
是否將時間戳記附加到每個發佈資源的 URL。當此值為 true 時,發佈資源的 URL 可能看起來像 /path/to/asset?v=timestamp
,其中 timestamp
是已發佈資源檔案的最後修改時間。當您為資源啟用 HTTP 快取時,通常會希望將此屬性設定為 true,因為它允許您在資源更新時清除快取。
從來源資源檔案 (鍵) 到目標資源檔案 (值) 的映射。
此屬性用於支援修正某些資源包中不正確的資源檔案路徑。當資源包在視圖中註冊時,將針對此映射檢查其 css 和 js 陣列中的每個相對資源檔案。如果找到任何鍵是資源檔案的最後一部分(如果可用,則以 yii\web\AssetBundle::$sourcePath 為前綴),則對應的值將取代該資源並在視圖中註冊。例如,資源檔案 my/path/to/jquery.js
符合鍵 jquery.js
。
請注意,目標資源檔案應為絕對 URL、網域相對 URL(從 '/' 開始)或相對於 $baseUrl 和 $basePath 的路徑。
在以下範例中,任何以 jquery.min.js
結尾的資源都將被 jquery/dist/jquery.js
取代,後者是相對於 $baseUrl 和 $basePath 的。
[
'jquery.min.js' => 'jquery/dist/jquery.js',
]
您也可以在使用映射值時使用別名,例如
[
'jquery.min.js' => '@web/js/jquery/jquery.js',
]
在複製每個子目錄或檔案之前呼叫的 PHP 回呼函數。此選項僅在發佈目錄時使用。如果回呼函數傳回 false,則將取消子目錄或檔案的複製操作。
回呼函數的簽名應為:function ($from, $to)
,其中 $from
是要複製的子目錄或檔案,而 $to
是複製目標。
這會作為參數 beforeCopy
傳遞給 yii\helpers\FileHelper::copyDirectory()。
資源包組態列表。此屬性用於自訂資源包。當 getBundle() 載入資源包時,如果此處指定了對應的組態,則該組態將套用於該資源包。
陣列鍵是資源包名稱,通常是沒有前導反斜線的資源包類別名稱。陣列值是相應的組態。如果值為 false,則表示對應的資源包已停用,且 getBundle() 應傳回 null。
如果此屬性為 false,則表示整個資源包功能已停用,且 getBundle() 將始終傳回 null。
以下範例示範如何停用 Bootstrap 小工具使用的 bootstrap css 檔案(因為您想使用自己的樣式)
[
'yii\bootstrap\BootstrapAsset' => [
'css' => [],
],
]
資源轉換器。請注意,此屬性的類型在 getter 和 setter 中有所不同。請參閱 getConverter() 和 setConverter() 以取得詳細資訊。
為新產生的資源目錄設定的權限。此值將由 PHP chmod() 函數使用。不會套用 umask。預設值為 0775,表示目錄可由擁有者和群組讀寫,但對於其他使用者而言為唯讀。
為新發佈的資源檔案設定的權限。此值將由 PHP chmod() 函數使用。不會套用 umask。如果未設定,則權限將由目前環境決定。
即使在目標目錄中找到要發佈的目錄,是否仍應複製該目錄。此選項僅在發佈目錄時使用。您可能希望在開發階段將其設定為 true
,以確保發佈的目錄始終是最新的。請勿在生產伺服器上將其設定為 true,因為這會顯著降低效能。
將呼叫以產生資源目錄雜湊的回呼函數。回呼函數的簽名應如下所示
function ($path)
其中 $path
是資源路徑。請注意,$path
可以是資源檔案所在的目錄,也可以是單個檔案。對於在 url()
中使用相對路徑的 CSS 檔案,雜湊實作應使用檔案的目錄路徑,而不是檔案路徑,以將相對資源檔案包含在複製中。
如果未設定此項,資源管理器將在 hash
方法中使用預設的 CRC32 和 filemtime。
使用 MD4 雜湊的實作範例
function ($path) {
return hash('md4', $path);
}
是否使用符號連結來發佈資源檔案。預設值為 false,表示資源檔案會複製到 $basePath。使用符號連結的好處是,已發佈的資源將始終與來源資源保持一致,並且不需要複製操作。這在開發期間特別有用。
但是,託管環境需要滿足特殊要求才能使用符號連結。特別是,符號連結僅在 Linux/Unix 和 Windows Vista/2008 或更高版本上受支援。
此外,某些 Web 伺服器需要正確設定,以便 Web 使用者可以存取連結的資源。例如,對於 Apache Web 伺服器,應為 Web 資料夾新增以下組態指令
Options FollowSymLinks
方法詳情
定義於: yii\base\Component::__call()
呼叫指定的非類別方法。
此方法將檢查是否有任何附加的行為具有指定的名稱方法,並在可用時執行它。
請勿直接呼叫此方法,因為它是 PHP 魔術方法,當呼叫未知方法時將會隱式呼叫。
public mixed __call ( $name, $params ) | ||
$name | string |
方法名稱 |
$params | array |
方法參數 |
return | mixed |
方法的傳回值 |
---|---|---|
throws | yii\base\UnknownMethodException |
當呼叫未知方法時 |
public function __call($name, $params)
{
$this->ensureBehaviors();
foreach ($this->_behaviors as $object) {
if ($object->hasMethod($name)) {
return call_user_func_array([$object, $name], $params);
}
}
throw new UnknownMethodException('Calling unknown method: ' . get_class($this) . "::$name()");
}
public void __clone ( ) |
public function __clone()
{
$this->_events = [];
$this->_eventWildcards = [];
$this->_behaviors = null;
}
定義於: yii\base\BaseObject::__construct()
建構子。
預設實作執行兩項操作
- 使用給定的組態
$config
初始化物件。 - 呼叫 init()。
如果在子類別中覆寫此方法,建議
- 建構函式的最後一個參數是組態陣列,例如此處的
$config
。 - 在建構函式的結尾呼叫父級實作。
public void __construct ( $config = [] ) | ||
$config | array |
將用於初始化物件屬性的名稱-值對 |
public function __construct($config = [])
{
if (!empty($config)) {
Yii::configure($this, $config);
}
$this->init();
}
定義於: yii\base\Component::__get()
傳回元件屬性的值。
此方法將按以下順序檢查並採取相應措施
- getter 定義的屬性:傳回 getter 結果
- 行為的屬性:傳回行為屬性值
請勿直接呼叫此方法,因為它是 PHP 魔術方法,當執行 $value = $component->property;
時將會隱式呼叫。
另請參閱 __set()。
public mixed __get ( $name ) | ||
$name | string |
屬性名稱 |
return | mixed |
屬性值或行為的屬性值 |
---|---|---|
throws | yii\base\UnknownPropertyException |
如果未定義屬性 |
throws | yii\base\InvalidCallException |
如果屬性為唯寫。 |
public function __get($name)
{
$getter = 'get' . $name;
if (method_exists($this, $getter)) {
// read property, e.g. getName()
return $this->$getter();
}
// behavior property
$this->ensureBehaviors();
foreach ($this->_behaviors as $behavior) {
if ($behavior->canGetProperty($name)) {
return $behavior->$name;
}
}
if (method_exists($this, 'set' . $name)) {
throw new InvalidCallException('Getting write-only property: ' . get_class($this) . '::' . $name);
}
throw new UnknownPropertyException('Getting unknown property: ' . get_class($this) . '::' . $name);
}
定義於: yii\base\Component::__isset()
檢查屬性是否已設定,即已定義且非 null。
此方法將按以下順序檢查並採取相應措施
- setter 定義的屬性:傳回是否已設定屬性
- 行為的屬性:傳回是否已設定屬性
- 對於不存在的屬性,傳回
false
請勿直接呼叫此方法,因為它是 PHP 魔術方法,當執行 isset($component->property)
時將會隱式呼叫。
public boolean __isset ( $name ) | ||
$name | string |
屬性名稱或事件名稱 |
return | boolean |
指定的屬性是否已設定 |
---|
public function __isset($name)
{
$getter = 'get' . $name;
if (method_exists($this, $getter)) {
return $this->$getter() !== null;
}
// behavior property
$this->ensureBehaviors();
foreach ($this->_behaviors as $behavior) {
if ($behavior->canGetProperty($name)) {
return $behavior->$name !== null;
}
}
return false;
}
定義於: yii\base\Component::__set()
設定元件屬性的值。
此方法將按以下順序檢查並採取相應措施
- setter 定義的屬性:設定屬性值
- 格式為 "on xyz" 的事件:將處理常式附加到事件 "xyz"
- 格式為 "as xyz" 的行為:附加名為 "xyz" 的行為
- 行為的屬性:設定行為屬性值
請勿直接呼叫此方法,因為它是 PHP 魔術方法,當執行 $component->property = $value;
時將會隱式呼叫。
另請參閱 __get()。
public void __set ( $name, $value ) | ||
$name | string |
屬性名稱或事件名稱 |
$value | mixed |
屬性值 |
throws | yii\base\UnknownPropertyException |
如果未定義屬性 |
---|---|---|
throws | yii\base\InvalidCallException |
如果屬性為唯讀。 |
public function __set($name, $value)
{
$setter = 'set' . $name;
if (method_exists($this, $setter)) {
// set property
$this->$setter($value);
return;
} elseif (strncmp($name, 'on ', 3) === 0) {
// on event: attach event handler
$this->on(trim(substr($name, 3)), $value);
return;
} elseif (strncmp($name, 'as ', 3) === 0) {
// as behavior: attach behavior
$name = trim(substr($name, 3));
$this->attachBehavior($name, $value instanceof Behavior ? $value : Yii::createObject($value));
return;
}
// behavior property
$this->ensureBehaviors();
foreach ($this->_behaviors as $behavior) {
if ($behavior->canSetProperty($name)) {
$behavior->$name = $value;
return;
}
}
if (method_exists($this, 'get' . $name)) {
throw new InvalidCallException('Setting read-only property: ' . get_class($this) . '::' . $name);
}
throw new UnknownPropertyException('Setting unknown property: ' . get_class($this) . '::' . $name);
}
定義於: yii\base\Component::__unset()
將元件屬性設定為 null。
此方法將按以下順序檢查並採取相應措施
- setter 定義的屬性:將屬性值設定為 null
- 行為的屬性:將屬性值設定為 null
請勿直接呼叫此方法,因為它是 PHP 魔術方法,當執行 unset($component->property)
時將會隱式呼叫。
public void __unset ( $name ) | ||
$name | string |
屬性名稱 |
throws | yii\base\InvalidCallException |
如果屬性為唯讀。 |
---|
public function __unset($name)
{
$setter = 'set' . $name;
if (method_exists($this, $setter)) {
$this->$setter(null);
return;
}
// behavior property
$this->ensureBehaviors();
foreach ($this->_behaviors as $behavior) {
if ($behavior->canSetProperty($name)) {
$behavior->$name = null;
return;
}
}
throw new InvalidCallException('Unsetting an unknown or read-only property: ' . get_class($this) . '::' . $name);
}
定義於: yii\base\Component::attachBehavior()
將行為附加到此元件。
此方法將根據給定的組態建立行為物件。之後,將透過呼叫 yii\base\Behavior::attach() 方法將行為物件附加到此元件。
另請參閱 detachBehavior()。
public yii\base\Behavior attachBehavior ( $name, $behavior ) | ||
$name | string |
行為的名稱。 |
$behavior | string|array|yii\base\Behavior |
行為組態。這可以是以下其中之一
|
return | yii\base\Behavior |
行為物件 |
---|
public function attachBehavior($name, $behavior)
{
$this->ensureBehaviors();
return $this->attachBehaviorInternal($name, $behavior);
}
定義於: yii\base\Component::attachBehaviors()
將行為列表附加到元件。
每個行為都由其名稱編製索引,並且應為 yii\base\Behavior 物件、指定行為類別的字串或用於建立行為的組態陣列。
另請參閱 attachBehavior()。
public void attachBehaviors ( $behaviors ) | ||
$behaviors | array |
要附加到元件的行為列表 |
public function attachBehaviors($behaviors)
{
$this->ensureBehaviors();
foreach ($behaviors as $name => $behavior) {
$this->attachBehaviorInternal($name, $behavior);
}
}
定義於: yii\base\Component::behaviors()
傳回此元件應表現為的行為列表。
子類別可以覆寫此方法,以指定它們要表現為的行為。
此方法的傳回值應為行為物件或組態的陣列,以行為名稱編製索引。行為組態可以是指定行為類別的字串,也可以是以下結構的陣列
'behaviorName' => [
'class' => 'BehaviorClass',
'property1' => 'value1',
'property2' => 'value2',
]
請注意,行為類別必須從 yii\base\Behavior 擴充。行為可以使用名稱或匿名方式附加。當使用名稱作為陣列鍵時,使用此名稱,稍後可以使用 getBehavior() 檢索行為,或使用 detachBehavior() 分離行為。匿名行為無法檢索或分離。
在此方法中宣告的行為將自動(按需)附加到元件。
public array behaviors ( ) | ||
return | array |
行為組態。 |
---|
public function behaviors()
{
return [];
}
定義於: yii\base\Component::canGetProperty()
傳回一個值,指示是否可以讀取屬性。
如果滿足以下條件,則可以讀取屬性
- 類別具有與指定名稱相關聯的 getter 方法(在此情況下,屬性名稱不區分大小寫);
- 類別具有帶有指定名稱的成員變數(當
$checkVars
為 true 時); - 附加的行為具有給定名稱的可讀屬性(當
$checkBehaviors
為 true 時)。
另請參閱 canSetProperty()。
public boolean canGetProperty ( $name, $checkVars = true, $checkBehaviors = true ) | ||
$name | string |
屬性名稱 |
$checkVars | boolean |
是否將成員變數視為屬性 |
$checkBehaviors | boolean |
是否將行為的屬性視為此組件的屬性 |
return | boolean |
屬性是否可讀 |
---|
public function canGetProperty($name, $checkVars = true, $checkBehaviors = true)
{
if (method_exists($this, 'get' . $name) || $checkVars && property_exists($this, $name)) {
return true;
} elseif ($checkBehaviors) {
$this->ensureBehaviors();
foreach ($this->_behaviors as $behavior) {
if ($behavior->canGetProperty($name, $checkVars)) {
return true;
}
}
}
return false;
}
定義於: yii\base\Component::canSetProperty()
傳回一個值,指示是否可以設定屬性。
如果符合以下條件,屬性即可寫入
- 類別具有與指定名稱相關聯的 setter 方法(在此情況下,屬性名稱不區分大小寫);
- 類別具有帶有指定名稱的成員變數(當
$checkVars
為 true 時); - 附加的行為具有給定名稱的可寫屬性(當
$checkBehaviors
為 true 時)。
另請參閱 canGetProperty()。
public boolean canSetProperty ( $name, $checkVars = true, $checkBehaviors = true ) | ||
$name | string |
屬性名稱 |
$checkVars | boolean |
是否將成員變數視為屬性 |
$checkBehaviors | boolean |
是否將行為的屬性視為此組件的屬性 |
return | boolean |
屬性是否可寫入 |
---|
public function canSetProperty($name, $checkVars = true, $checkBehaviors = true)
{
if (method_exists($this, 'set' . $name) || $checkVars && property_exists($this, $name)) {
return true;
} elseif ($checkBehaviors) {
$this->ensureBehaviors();
foreach ($this->_behaviors as $behavior) {
if ($behavior->canSetProperty($name, $checkVars)) {
return true;
}
}
}
return false;
}
檢查 basePath 是否存在且可寫入。
public void checkBasePathPermission ( ) |
public function checkBasePathPermission()
{
// if the check is been done already, skip further checks
if ($this->_isBasePathPermissionChecked) {
return;
}
if (!is_dir($this->basePath)) {
throw new InvalidConfigException("The directory does not exist: {$this->basePath}");
}
if (!is_writable($this->basePath)) {
throw new InvalidConfigException("The directory is not writable by the Web process: {$this->basePath}");
}
$this->_isBasePathPermissionChecked = true;
}
::class
。
定義於: yii\base\BaseObject::className()
傳回此類別的完整限定名稱。
public static string className ( ) | ||
return | string |
此類別的完整限定名稱。 |
---|
public static function className()
{
return get_called_class();
}
public yii\base\Behavior|null detachBehavior ( $name ) | ||
$name | string |
行為的名稱。 |
return | yii\base\Behavior|null |
分離的行為。如果行為不存在,則為 Null。 |
---|
public function detachBehavior($name)
{
$this->ensureBehaviors();
if (isset($this->_behaviors[$name])) {
$behavior = $this->_behaviors[$name];
unset($this->_behaviors[$name]);
$behavior->detach();
return $behavior;
}
return null;
}
定義於: yii\base\Component::detachBehaviors()
從元件分離所有行為。
public void detachBehaviors ( ) |
public function detachBehaviors()
{
$this->ensureBehaviors();
foreach ($this->_behaviors as $name => $behavior) {
$this->detachBehavior($name);
}
}
定義於: yii\base\Component::ensureBehaviors()
確保在 behaviors() 中宣告的行為已附加到此元件。
public void ensureBehaviors ( ) |
public function ensureBehaviors()
{
if ($this->_behaviors === null) {
$this->_behaviors = [];
foreach ($this->behaviors() as $name => $behavior) {
$this->attachBehaviorInternal($name, $behavior);
}
}
}
傳回指定資源的實際 URL。不帶參數。
實際 URL 是通過將 yii\web\AssetBundle::$baseUrl 或 yii\web\AssetManager::$baseUrl 前置到給定的 asset 路徑來獲得的。
public string getActualAssetUrl ( $bundle, $asset ) | ||
$bundle | yii\web\AssetBundle |
asset 檔案所屬的 asset bundle |
$asset | string |
asset 路徑。這應該是 yii\web\AssetBundle::$js 或 yii\web\AssetBundle::$css 中列出的 asset 之一。 |
return | string |
指定 asset 的實際 URL。 |
---|
public function getActualAssetUrl($bundle, $asset)
{
if (($actualAsset = $this->resolveAsset($bundle, $asset)) !== false) {
if (strncmp($actualAsset, '@web/', 5) === 0) {
$asset = substr($actualAsset, 5);
$baseUrl = Yii::getAlias('@web');
} else {
$asset = Yii::getAlias($actualAsset);
$baseUrl = $this->baseUrl;
}
} else {
$baseUrl = $bundle->baseUrl;
}
if (!Url::isRelative($asset) || strncmp($asset, '/', 1) === 0) {
return $asset;
}
return "$baseUrl/$asset";
}
傳回指定資源的實際檔案路徑。
public string|false getAssetPath ( $bundle, $asset ) | ||
$bundle | yii\web\AssetBundle |
asset 檔案所屬的 asset bundle |
$asset | string |
asset 路徑。這應該是 yii\web\AssetBundle::$js 或 yii\web\AssetBundle::$css 中列出的 asset 之一。 |
return | string|false |
實際檔案路徑,或者如果 asset 被指定為絕對 URL,則為 |
---|
public function getAssetPath($bundle, $asset)
{
if (($actualAsset = $this->resolveAsset($bundle, $asset)) !== false) {
return Url::isRelative($actualAsset) ? $this->basePath . '/' . $actualAsset : false;
}
return Url::isRelative($asset) ? $bundle->basePath . '/' . $asset : false;
}
傳回指定資源的實際 URL。
實際 URL 是通過將 yii\web\AssetBundle::$baseUrl 或 yii\web\AssetManager::$baseUrl 前置到給定的 asset 路徑來獲得的。
public string getAssetUrl ( $bundle, $asset, $appendTimestamp = null ) | ||
$bundle | yii\web\AssetBundle |
asset 檔案所屬的 asset bundle |
$asset | string |
asset 路徑。這應該是 yii\web\AssetBundle::$js 或 yii\web\AssetBundle::$css 中列出的 asset 之一。 |
$appendTimestamp | boolean|null |
是否將時間戳記附加到 URL。 |
return | string |
指定 asset 的實際 URL。 |
---|
public function getAssetUrl($bundle, $asset, $appendTimestamp = null)
{
$assetUrl = $this->getActualAssetUrl($bundle, $asset);
$assetPath = $this->getAssetPath($bundle, $asset);
$withTimestamp = $this->appendTimestamp;
if ($appendTimestamp !== null) {
$withTimestamp = $appendTimestamp;
}
if ($withTimestamp && $assetPath && ($timestamp = @filemtime($assetPath)) > 0) {
return "$assetUrl?v=$timestamp";
}
return $assetUrl;
}
定義於: yii\base\Component::getBehavior()
傳回指定的行為物件。
public yii\base\Behavior|null getBehavior ( $name ) | ||
$name | string |
行為名稱 |
return | yii\base\Behavior|null |
行為物件,如果行為不存在,則為 null |
---|
public function getBehavior($name)
{
$this->ensureBehaviors();
return isset($this->_behaviors[$name]) ? $this->_behaviors[$name] : null;
}
定義於: yii\base\Component::getBehaviors()
傳回附加到此元件的所有行為。
public yii\base\Behavior[] getBehaviors ( ) | ||
return | yii\base\Behavior[] |
附加到此組件的行為列表 |
---|
public function getBehaviors()
{
$this->ensureBehaviors();
return $this->_behaviors;
}
傳回指定的資源包。
此方法將首先在 $bundles 中查找 bundle。如果找不到,它會將 $name
視為 asset bundle 的類別,並建立一個新的實例。
public yii\web\AssetBundle getBundle ( $name, $publish = true ) | ||
$name | string |
asset bundle 的類別名稱(不帶前導反斜線) |
$publish | boolean |
是否在返回之前發布 asset bundle 中的 asset 檔案。如果將此項設定為 false,您必須手動調用 |
return | yii\web\AssetBundle |
asset bundle 實例 |
---|---|---|
throws | yii\base\InvalidConfigException |
如果 $name 沒有指向有效的 asset bundle |
public function getBundle($name, $publish = true)
{
if ($this->bundles === false) {
return $this->loadDummyBundle($name);
} elseif (!isset($this->bundles[$name])) {
return $this->bundles[$name] = $this->loadBundle($name, [], $publish);
} elseif ($this->bundles[$name] instanceof AssetBundle) {
return $this->bundles[$name];
} elseif (is_array($this->bundles[$name])) {
return $this->bundles[$name] = $this->loadBundle($name, $this->bundles[$name], $publish);
} elseif ($this->bundles[$name] === false) {
return $this->loadDummyBundle($name);
}
throw new InvalidConfigException("Invalid asset bundle configuration: $name");
}
傳回資源轉換器。
public yii\web\AssetConverterInterface getConverter ( ) | ||
return | yii\web\AssetConverterInterface |
資源轉換器。 |
---|
public function getConverter()
{
if ($this->_converter === null) {
$this->_converter = Yii::createObject(AssetConverter::className());
} elseif (is_array($this->_converter) || is_string($this->_converter)) {
if (is_array($this->_converter) && !isset($this->_converter['class'])) {
$this->_converter['class'] = AssetConverter::className();
}
$this->_converter = Yii::createObject($this->_converter);
}
return $this->_converter;
}
傳回檔案路徑的已發布路徑。
此方法不執行任何發布操作。它僅僅告訴您檔案或目錄是否已發布,以及它將會去哪裡。
public string|false getPublishedPath ( $path ) | ||
$path | string |
正在發布的目錄或檔案路徑 |
return | string|false |
字串,已發布的檔案路徑。如果檔案或目錄不存在,則為 False |
---|
public function getPublishedPath($path)
{
$path = Yii::getAlias($path);
if (isset($this->_published[$path])) {
return $this->_published[$path][0];
}
if (is_string($path) && ($path = realpath($path)) !== false) {
return $this->basePath . DIRECTORY_SEPARATOR . $this->hash($path) . (is_file($path) ? DIRECTORY_SEPARATOR . basename($path) : '');
}
return false;
}
傳回已發布檔案路徑的 URL。
此方法不執行任何發布操作。它僅僅告訴您檔案路徑是否已發布,以及訪問它的 URL 將會是什麼。
public string|false getPublishedUrl ( $path ) | ||
$path | string |
正在發布的目錄或檔案路徑 |
return | string|false |
字串,檔案或目錄的已發布 URL。如果檔案或目錄不存在,則為 False。 |
---|
public function getPublishedUrl($path)
{
$path = Yii::getAlias($path);
if (isset($this->_published[$path])) {
return $this->_published[$path][1];
}
if (is_string($path) && ($path = realpath($path)) !== false) {
return $this->baseUrl . '/' . $this->hash($path) . (is_file($path) ? '/' . basename($path) : '');
}
return false;
}
定義於: yii\base\Component::hasEventHandlers()
傳回一個值,指示是否有名稱事件附加任何處理常式。
public boolean hasEventHandlers ( $name ) | ||
$name | string |
事件名稱 |
return | boolean |
是否有任何處理程序附加到事件。 |
---|
public function hasEventHandlers($name)
{
$this->ensureBehaviors();
if (!empty($this->_events[$name])) {
return true;
}
foreach ($this->_eventWildcards as $wildcard => $handlers) {
if (!empty($handlers) && StringHelper::matchWildcard($wildcard, $name)) {
return true;
}
}
return Event::hasHandlers($this, $name);
}
定義於: yii\base\Component::hasMethod()
傳回一個值,指示是否已定義方法。
如果符合以下條件,則定義方法
- 類別具有帶有指定名稱的方法
- 附加的行為具有給定名稱的方法(當
$checkBehaviors
為 true 時)。
public boolean hasMethod ( $name, $checkBehaviors = true ) | ||
$name | string |
屬性名稱 |
$checkBehaviors | boolean |
是否將行為的方法視為此組件的方法 |
return | boolean |
方法是否已定義 |
---|
public function hasMethod($name, $checkBehaviors = true)
{
if (method_exists($this, $name)) {
return true;
} elseif ($checkBehaviors) {
$this->ensureBehaviors();
foreach ($this->_behaviors as $behavior) {
if ($behavior->hasMethod($name)) {
return true;
}
}
}
return false;
}
定義於: yii\base\Component::hasProperty()
傳回一個值,指示是否為此元件定義了屬性。
如果符合以下條件,則定義屬性
- 類別具有與指定名稱相關聯的 getter 或 setter 方法(在此情況下,屬性名稱不區分大小寫);
- 類別具有帶有指定名稱的成員變數(當
$checkVars
為 true 時); - 附加的行為具有給定名稱的屬性(當
$checkBehaviors
為 true 時)。
另請參閱
public boolean hasProperty ( $name, $checkVars = true, $checkBehaviors = true ) | ||
$name | string |
屬性名稱 |
$checkVars | boolean |
是否將成員變數視為屬性 |
$checkBehaviors | boolean |
是否將行為的屬性視為此組件的屬性 |
return | boolean |
屬性是否已定義 |
---|
public function hasProperty($name, $checkVars = true, $checkBehaviors = true)
{
return $this->canGetProperty($name, $checkVars, $checkBehaviors) || $this->canSetProperty($name, false, $checkBehaviors);
}
為目錄路徑產生 CRC32 雜湊值。碰撞機率高於 MD5,但產生更小的雜湊字串。
protected string hash ( $path ) | ||
$path | string |
要雜湊的字串。 |
return | string |
雜湊字串。 |
---|
protected function hash($path)
{
if (is_callable($this->hashCallback)) {
return call_user_func($this->hashCallback, $path);
}
$path = (is_file($path) ? dirname($path) : $path) . filemtime($path);
return sprintf('%x', crc32($path . Yii::getVersion() . '|' . $this->linkAssets));
}
初始化元件。
public void init ( ) | ||
throws | yii\base\InvalidConfigException |
如果 $basePath 不存在。 |
---|
public function init()
{
parent::init();
$this->basePath = Yii::getAlias($this->basePath);
$this->basePath = realpath($this->basePath);
$this->baseUrl = rtrim(Yii::getAlias($this->baseUrl), '/');
}
依名稱載入資源包類別。
protected yii\web\AssetBundle loadBundle ( $name, $config = [], $publish = true ) | ||
$name | string |
Bundle 名稱 |
$config | array |
Bundle 物件配置 |
$publish | boolean |
是否應發布 bundle |
throws | yii\base\InvalidConfigException |
如果配置無效 |
---|
protected function loadBundle($name, $config = [], $publish = true)
{
if (!isset($config['class'])) {
$config['class'] = $name;
}
/* @var $bundle AssetBundle */
$bundle = Yii::createObject($config);
if ($publish) {
$bundle->publish($this);
}
return $bundle;
}
依名稱載入虛擬資源包。
protected yii\web\AssetBundle loadDummyBundle ( $name ) | ||
$name | string |
protected function loadDummyBundle($name)
{
if (!isset($this->_dummyBundles[$name])) {
$bundle = Yii::createObject(['class' => $name]);
$bundle->sourcePath = null;
$bundle->js = [];
$bundle->css = [];
$this->_dummyBundles[$name] = $bundle;
}
return $this->_dummyBundles[$name];
}
定義於: yii\base\Component::off()
從此元件分離現有的事件處理常式。
此方法與 on() 相反。
注意:如果為事件名稱傳遞了萬用字元模式,則只會移除使用此萬用字元註冊的處理程序,而使用與此萬用字元匹配的純名稱註冊的處理程序將會保留。
另請參閱 on()。
public boolean off ( $name, $handler = null ) | ||
$name | string |
事件名稱 |
$handler | callable|null |
要移除的事件處理程序。如果為 null,則會移除附加到具名事件的所有處理程序。 |
return | boolean |
如果找到並分離處理程序 |
---|
public function off($name, $handler = null)
{
$this->ensureBehaviors();
if (empty($this->_events[$name]) && empty($this->_eventWildcards[$name])) {
return false;
}
if ($handler === null) {
unset($this->_events[$name], $this->_eventWildcards[$name]);
return true;
}
$removed = false;
// plain event names
if (isset($this->_events[$name])) {
foreach ($this->_events[$name] as $i => $event) {
if ($event[0] === $handler) {
unset($this->_events[$name][$i]);
$removed = true;
}
}
if ($removed) {
$this->_events[$name] = array_values($this->_events[$name]);
return true;
}
}
// wildcard event names
if (isset($this->_eventWildcards[$name])) {
foreach ($this->_eventWildcards[$name] as $i => $event) {
if ($event[0] === $handler) {
unset($this->_eventWildcards[$name][$i]);
$removed = true;
}
}
if ($removed) {
$this->_eventWildcards[$name] = array_values($this->_eventWildcards[$name]);
// remove empty wildcards to save future redundant regex checks:
if (empty($this->_eventWildcards[$name])) {
unset($this->_eventWildcards[$name]);
}
}
}
return $removed;
}
將事件處理常式附加到事件。
事件處理程序必須是有效的 PHP 回調。以下是一些範例
function ($event) { ... } // anonymous function
[$object, 'handleClick'] // $object->handleClick()
['Page', 'handleClick'] // Page::handleClick()
'handleClick' // global function handleClick()
事件處理程序必須使用以下簽名定義:
function ($event)
其中 $event
是一個 yii\base\Event 物件,其中包含與事件相關聯的參數。
自 2.0.14 版本起,您可以將事件名稱指定為萬用字元模式
$component->on('event.group.*', function ($event) {
Yii::trace($event->name . ' is triggered.');
});
另請參閱 off()。
public void on ( $name, $handler, $data = null, $append = true ) | ||
$name | string |
事件名稱 |
$handler | callable |
事件處理程序 |
$data | mixed |
當事件被觸發時,要傳遞給事件處理程序的資料。當調用事件處理程序時,可以通過 yii\base\Event::$data 訪問此資料。 |
$append | boolean |
是否將新的事件處理程序附加到現有處理程序列表的末尾。如果為 false,則新的處理程序將被插入到現有處理程序列表的開頭。 |
public function on($name, $handler, $data = null, $append = true)
{
$this->ensureBehaviors();
if (strpos($name, '*') !== false) {
if ($append || empty($this->_eventWildcards[$name])) {
$this->_eventWildcards[$name][] = [$handler, $data];
} else {
array_unshift($this->_eventWildcards[$name], [$handler, $data]);
}
return;
}
if ($append || empty($this->_events[$name])) {
$this->_events[$name][] = [$handler, $data];
} else {
array_unshift($this->_events[$name], [$handler, $data]);
}
}
發布檔案或目錄。
此方法會將指定的檔案或目錄複製到 $basePath,以便可以通過 Web 伺服器訪問它。
如果 asset 是一個檔案,將會檢查其檔案修改時間,以避免不必要的文件複製。
如果 asset 是一個目錄,則其下的所有檔案和子目錄都將被遞迴發布。請注意,如果 $forceCopy 為 false,則此方法僅檢查目標目錄的存在,以避免重複複製(這非常耗費資源)。
預設情況下,當發布目錄時,名稱以點「.」開頭的子目錄和檔案將不會被發布。如果您想更改此行為,您可以指定「beforeCopy」選項,如 $options
參數中所述。
注意:在極少數情況下,可能會出現競爭條件,這將導致在建立保存已發布 asset 的目錄時,一次性地出現非嚴重問題。通過在系統上線之前的應用程式部署階段,預先「請求」所有應觸發「publish()」調用的資源,可以完全避免此問題。請參閱以下討論以了解更多資訊:https://code.google.com/archive/p/yii/issues/2579
public array publish ( $path, $options = [] ) | ||
$path | string |
要發布的 asset(檔案或目錄) |
$options | array |
發布目錄時要應用的選項。支援以下選項
|
return | array |
asset 發布為的路徑(目錄或檔案路徑)和 URL。 |
---|---|---|
throws | yii\base\InvalidArgumentException |
如果要發布的 asset 不存在。 |
throws | yii\base\InvalidConfigException |
如果目標目錄 $basePath 不可寫入。 |
public function publish($path, $options = [])
{
$path = Yii::getAlias($path);
if (isset($this->_published[$path])) {
return $this->_published[$path];
}
if (!is_string($path) || ($src = realpath($path)) === false) {
throw new InvalidArgumentException("The file or directory to be published does not exist: $path");
}
if (!is_readable($path)) {
throw new InvalidArgumentException("The file or directory to be published is not readable: $path");
}
if (is_file($src)) {
return $this->_published[$path] = $this->publishFile($src);
}
return $this->_published[$path] = $this->publishDirectory($src, $options);
}
發布目錄。
protected string[] publishDirectory ( $src, $options ) | ||
$src | string |
要發布的資源目錄 |
$options | array |
發布目錄時要應用的選項。支援以下選項
|
return | string[] |
資源發布所在的路徑目錄和 URL。 |
---|---|---|
throws | yii\base\InvalidArgumentException |
如果要發布的 asset 不存在。 |
protected function publishDirectory($src, $options)
{
$this->checkBasePathPermission();
$dir = $this->hash($src);
$dstDir = $this->basePath . DIRECTORY_SEPARATOR . $dir;
if ($this->linkAssets) {
if (!is_dir($dstDir)) {
FileHelper::createDirectory(dirname($dstDir), $this->dirMode, true);
try { // fix #6226 symlinking multi threaded
symlink($src, $dstDir);
} catch (\Exception $e) {
if (!is_dir($dstDir)) {
throw $e;
}
}
}
} elseif (!empty($options['forceCopy']) || ($this->forceCopy && !isset($options['forceCopy'])) || !is_dir($dstDir)) {
$opts = array_merge(
$options,
[
'dirMode' => $this->dirMode,
'fileMode' => $this->fileMode,
'copyEmptyDirectories' => false,
]
);
if (!isset($opts['beforeCopy'])) {
if ($this->beforeCopy !== null) {
$opts['beforeCopy'] = $this->beforeCopy;
} else {
$opts['beforeCopy'] = function ($from, $to) {
return strncmp(basename($from), '.', 1) !== 0;
};
}
}
if (!isset($opts['afterCopy']) && $this->afterCopy !== null) {
$opts['afterCopy'] = $this->afterCopy;
}
FileHelper::copyDirectory($src, $dstDir, $opts);
}
return [$dstDir, $this->baseUrl . '/' . $dir];
}
發布檔案。
protected string[] publishFile ( $src ) | ||
$src | string |
要發布的資源檔案 |
return | string[] |
資源發布所在的路徑和 URL。 |
---|---|---|
throws | yii\base\InvalidArgumentException |
如果要發布的 asset 不存在。 |
protected function publishFile($src)
{
$this->checkBasePathPermission();
$dir = $this->hash($src);
$fileName = basename($src);
$dstDir = $this->basePath . DIRECTORY_SEPARATOR . $dir;
$dstFile = $dstDir . DIRECTORY_SEPARATOR . $fileName;
if (!is_dir($dstDir)) {
FileHelper::createDirectory($dstDir, $this->dirMode, true);
}
if ($this->linkAssets) {
if (!is_file($dstFile)) {
try { // fix #6226 symlinking multi threaded
symlink($src, $dstFile);
} catch (\Exception $e) {
if (!is_file($dstFile)) {
throw $e;
}
}
}
} elseif (@filemtime($dstFile) < @filemtime($src)) {
copy($src, $dstFile);
if ($this->fileMode !== null) {
@chmod($dstFile, $this->fileMode);
}
}
if ($this->appendTimestamp && ($timestamp = @filemtime($dstFile)) > 0) {
$fileName = $fileName . "?v=$timestamp";
}
return [$dstFile, $this->baseUrl . "/$dir/$fileName"];
}
protected string|false resolveAsset ( $bundle, $asset ) | ||
$bundle | yii\web\AssetBundle | |
$asset | string |
protected function resolveAsset($bundle, $asset)
{
if (isset($this->assetMap[$asset])) {
return $this->assetMap[$asset];
}
if ($bundle->sourcePath !== null && Url::isRelative($asset)) {
$asset = $bundle->sourcePath . '/' . $asset;
}
$n = mb_strlen($asset, Yii::$app->charset);
foreach ($this->assetMap as $from => $to) {
$n2 = mb_strlen($from, Yii::$app->charset);
if ($n2 <= $n && substr_compare($asset, $from, $n - $n2, $n2) === 0) {
return $to;
}
}
return false;
}
設定資源轉換器。
public void setConverter ( $value ) | ||
$value | array|yii\web\AssetConverterInterface |
資源轉換器。這可以是實作 yii\web\AssetConverterInterface 的物件,或是一個可以用來建立資源轉換器物件的組態陣列。 |
public function setConverter($value)
{
$this->_converter = $value;
}
public void trigger ( $name, yii\base\Event $event = null ) | ||
$name | string |
事件名稱 |
$event | yii\base\Event|null |
事件實例。如果未設定,將會建立預設的 yii\base\Event 物件。 |
public function trigger($name, Event $event = null)
{
$this->ensureBehaviors();
$eventHandlers = [];
foreach ($this->_eventWildcards as $wildcard => $handlers) {
if (StringHelper::matchWildcard($wildcard, $name)) {
$eventHandlers[] = $handlers;
}
}
if (!empty($this->_events[$name])) {
$eventHandlers[] = $this->_events[$name];
}
if (!empty($eventHandlers)) {
$eventHandlers = call_user_func_array('array_merge', $eventHandlers);
if ($event === null) {
$event = new Event();
}
if ($event->sender === null) {
$event->sender = $this;
}
$event->handled = false;
$event->name = $name;
foreach ($eventHandlers as $handler) {
$event->data = $handler[1];
call_user_func($handler[0], $event);
// stop further handling if the event is handled
if ($event->handled) {
return;
}
}
}
// invoke class-level attached handlers
Event::trigger($this, $name, $event);
}
註冊 或 登入 以發表評論。