0 追蹤者

Class yii\web\YiiAsset

繼承關係yii\web\YiiAsset » yii\web\AssetBundle » yii\base\BaseObject
實作介面yii\base\Configurable
起始版本2.0
原始碼 https://github.com/yiisoft/yii2/blob/master/framework/web/YiiAsset.php

此資源包為 Yii 框架提供基礎 JavaScript 檔案。

公開屬性

隱藏繼承屬性

屬性 類型 描述 定義於
$basePath string 此資源包中包含可透過 Web 存取的資源檔案目錄。 yii\web\AssetBundle
$baseUrl string $js$css 中列出的相對資源檔案之基礎 URL。 yii\web\AssetBundle
$css array 此資源包中包含的 CSS 檔案列表。 yii\web\AssetBundle
$cssOptions array 當註冊此資源包中的 CSS 檔案時,將傳遞給 yii\web\View::registerCssFile() 的選項。 yii\web\AssetBundle
$depends yii\web\YiiAsset
$js yii\web\YiiAsset
$jsOptions array 當註冊此資源包中的 JS 檔案時,將傳遞給 yii\web\View::registerJsFile() 的選項。 yii\web\AssetBundle
$publishOptions array 當發佈資源包時,將傳遞給 yii\web\AssetManager::publish() 的選項。 yii\web\AssetBundle
$sourcePath yii\web\YiiAsset

公開方法

隱藏繼承方法

方法 描述 定義於
__call() 呼叫指定的非類別方法。 yii\base\BaseObject
__construct() 建構子。 yii\base\BaseObject
__get() 傳回物件屬性的值。 yii\base\BaseObject
__isset() 檢查屬性是否已設定,亦即已定義且非 null。 yii\base\BaseObject
__set() 設定物件屬性的值。 yii\base\BaseObject
__unset() 將物件屬性設為 null。 yii\base\BaseObject
canGetProperty() 傳回值,指出屬性是否可讀取。 yii\base\BaseObject
canSetProperty() 傳回值,指出屬性是否可設定。 yii\base\BaseObject
className() 傳回此類別的完整限定名稱。 yii\base\BaseObject
hasMethod() 傳回值,指出是否已定義方法。 yii\base\BaseObject
hasProperty() 傳回值,指出是否已定義屬性。 yii\base\BaseObject
init() 初始化套件。 yii\web\AssetBundle
publish() 發布 asset bundle,如果其原始碼不在可透過 Web 存取的目錄下。 yii\web\AssetBundle
register() 向視圖註冊此 asset bundle。 yii\web\AssetBundle
registerAssetFiles() 向給定的視圖註冊 CSS 和 JS 檔案。 yii\web\AssetBundle

屬性詳細資訊

隱藏繼承屬性

$depends public property
public $depends = [
    
'yii\web\JqueryAsset',
]
$js public property
public $js = [
    
'yii.js',
]
$sourcePath public property
public $sourcePath '@yii/assets'

方法詳細資訊

隱藏繼承方法

__call() public method

定義於: yii\base\BaseObject::__call()

呼叫指定的非類別方法。

請勿直接呼叫此方法,因為它是 PHP magic method,當呼叫未知方法時會隱含地呼叫它。

public mixed __call ( $name, $params )
$name string

方法名稱

$params array

方法參數

return mixed

方法傳回值

throws yii\base\UnknownMethodException

當呼叫未知方法時

                public function __call($name, $params)
{
    throw new UnknownMethodException('Calling unknown method: ' . get_class($this) . "::$name()");
}

            
__construct() public method

定義於: 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();
}

            
__get() public method

定義於: yii\base\BaseObject::__get()

傳回物件屬性的值。

請勿直接呼叫此方法,因為它是 PHP magic method,當執行 $value = $object->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)) {
        return $this->$getter();
    } elseif (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);
}

            
__isset() public method

定義於: yii\base\BaseObject::__isset()

檢查屬性是否已設定,亦即已定義且非 null。

請勿直接呼叫此方法,因為它是 PHP magic method,當執行 isset($object->property) 時會隱含地呼叫它。

請注意,如果未定義屬性,將會傳回 false。

另請參閱 https://php.dev.org.tw/manual/en/function.isset.php

public boolean __isset ( $name )
$name string

屬性名稱或事件名稱

return boolean

具名屬性是否已設定(非 null)。

                public function __isset($name)
{
    $getter = 'get' . $name;
    if (method_exists($this, $getter)) {
        return $this->$getter() !== null;
    }
    return false;
}

            
__set() public method

定義於: yii\base\BaseObject::__set()

設定物件屬性的值。

請勿直接呼叫此方法,因為它是 PHP magic method,當執行 $object->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)) {
        $this->$setter($value);
    } elseif (method_exists($this, 'get' . $name)) {
        throw new InvalidCallException('Setting read-only property: ' . get_class($this) . '::' . $name);
    } else {
        throw new UnknownPropertyException('Setting unknown property: ' . get_class($this) . '::' . $name);
    }
}

            
__unset() public method

定義於: yii\base\BaseObject::__unset()

將物件屬性設為 null。

請勿直接呼叫此方法,因為它是 PHP magic method,當執行 unset($object->property) 時會隱含地呼叫它。

請注意,如果未定義屬性,此方法將不會執行任何動作。如果屬性為唯讀,它將拋出例外。

另請參閱 https://php.dev.org.tw/manual/en/function.unset.php

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);
    } elseif (method_exists($this, 'get' . $name)) {
        throw new InvalidCallException('Unsetting read-only property: ' . get_class($this) . '::' . $name);
    }
}

            
canGetProperty() public method

定義於: yii\base\BaseObject::canGetProperty()

傳回值,指出屬性是否可讀取。

如果屬性可讀取:

  • 類別具有與指定名稱相關聯的 getter 方法(在此情況下,屬性名稱不區分大小寫);
  • 類別具有具有指定名稱的成員變數(當 $checkVars 為 true 時);

另請參閱 canSetProperty()

public boolean canGetProperty ( $name, $checkVars true )
$name string

屬性名稱

$checkVars boolean

是否將成員變數視為屬性

return boolean

屬性是否可讀取

                public function canGetProperty($name, $checkVars = true)
{
    return method_exists($this, 'get' . $name) || $checkVars && property_exists($this, $name);
}

            
canSetProperty() public method

定義於: yii\base\BaseObject::canSetProperty()

傳回值,指出屬性是否可設定。

如果屬性可寫入:

  • 類別具有與指定名稱相關聯的 setter 方法(在此情況下,屬性名稱不區分大小寫);
  • 類別具有具有指定名稱的成員變數(當 $checkVars 為 true 時);

另請參閱 canGetProperty()

public boolean canSetProperty ( $name, $checkVars true )
$name string

屬性名稱

$checkVars boolean

是否將成員變數視為屬性

return boolean

屬性是否可寫入

                public function canSetProperty($name, $checkVars = true)
{
    return method_exists($this, 'set' . $name) || $checkVars && property_exists($this, $name);
}

            
className() public static method
自 2.0.14 版本起已過時。在 PHP >=5.5 上,請改用 ::class

定義於: yii\base\BaseObject::className()

傳回此類別的完整限定名稱。

public static string className ( )
return string

此類別的完整限定名稱。

                public static function className()
{
    return get_called_class();
}

            
hasMethod() public method

定義於: yii\base\BaseObject::hasMethod()

傳回值,指出是否已定義方法。

預設實作是對 php 函數 method_exists() 的呼叫。當您實作 php magic method __call() 時,您可以覆寫此方法。

public boolean hasMethod ( $name )
$name string

方法名稱

return boolean

方法是否已定義

                public function hasMethod($name)
{
    return method_exists($this, $name);
}

            
hasProperty() public method

定義於: yii\base\BaseObject::hasProperty()

傳回值,指出是否已定義屬性。

如果屬性已定義:

  • 類別具有與指定名稱相關聯的 getter 或 setter 方法(在此情況下,屬性名稱不區分大小寫);
  • 類別具有具有指定名稱的成員變數(當 $checkVars 為 true 時);

另請參閱

public boolean hasProperty ( $name, $checkVars true )
$name string

屬性名稱

$checkVars boolean

是否將成員變數視為屬性

return boolean

屬性是否已定義

                public function hasProperty($name, $checkVars = true)
{
    return $this->canGetProperty($name, $checkVars) || $this->canSetProperty($name, false);
}

            
init() public method

定義於: yii\web\AssetBundle::init()

初始化套件。

如果您覆寫此方法,請確保在最後呼叫父類別實作。

public void init ( )

                public function init()
{
    if ($this->sourcePath !== null) {
        $this->sourcePath = rtrim(Yii::getAlias($this->sourcePath), '/\\');
    }
    if ($this->basePath !== null) {
        $this->basePath = rtrim(Yii::getAlias($this->basePath), '/\\');
    }
    if ($this->baseUrl !== null) {
        $this->baseUrl = rtrim(Yii::getAlias($this->baseUrl), '/');
    }
}

            
publish() public method

定義於: yii\web\AssetBundle::publish()

發布 asset bundle,如果其原始碼不在可透過 Web 存取的目錄下。

它也會嘗試使用 asset converter 將非 CSS 或 JS 檔案(例如 LESS、Sass)轉換為對應的 CSS 或 JS 檔案。

public void publish ( $am )
$am yii\web\AssetManager

執行 asset 發布的 asset 管理器

                public function publish($am)
{
    if ($this->sourcePath !== null && !isset($this->basePath, $this->baseUrl)) {
        list($this->basePath, $this->baseUrl) = $am->publish($this->sourcePath, $this->publishOptions);
    }
    if (isset($this->basePath, $this->baseUrl) && ($converter = $am->getConverter()) !== null) {
        foreach ($this->js as $i => $js) {
            if (is_array($js)) {
                $file = array_shift($js);
                if (Url::isRelative($file)) {
                    $js = ArrayHelper::merge($this->jsOptions, $js);
                    array_unshift($js, $converter->convert($file, $this->basePath));
                    $this->js[$i] = $js;
                }
            } elseif (Url::isRelative($js)) {
                $this->js[$i] = $converter->convert($js, $this->basePath);
            }
        }
        foreach ($this->css as $i => $css) {
            if (is_array($css)) {
                $file = array_shift($css);
                if (Url::isRelative($file)) {
                    $css = ArrayHelper::merge($this->cssOptions, $css);
                    array_unshift($css, $converter->convert($file, $this->basePath));
                    $this->css[$i] = $css;
                }
            } elseif (Url::isRelative($css)) {
                $this->css[$i] = $converter->convert($css, $this->basePath);
            }
        }
    }
}

            
register() public static method

定義於: yii\web\AssetBundle::register()

向視圖註冊此 asset bundle。

public static static register ( $view )
$view yii\web\View

要註冊的視圖

return yii\web\AssetBundle

已註冊的 asset bundle 實例

                public static function register($view)
{
    return $view->registerAssetBundle(get_called_class());
}

            
registerAssetFiles() public method

定義於: yii\web\AssetBundle::registerAssetFiles()

向給定的視圖註冊 CSS 和 JS 檔案。

public void registerAssetFiles ( $view )
$view yii\web\View

要向其註冊 asset 檔案的視圖。

                public function registerAssetFiles($view)
{
    $manager = $view->getAssetManager();
    foreach ($this->js as $js) {
        if (is_array($js)) {
            $file = array_shift($js);
            $options = ArrayHelper::merge($this->jsOptions, $js);
            $view->registerJsFile($manager->getAssetUrl($this, $file, ArrayHelper::getValue($options, 'appendTimestamp')), $options);
        } elseif ($js !== null) {
            $view->registerJsFile($manager->getAssetUrl($this, $js), $this->jsOptions);
        }
    }
    foreach ($this->css as $css) {
        if (is_array($css)) {
            $file = array_shift($css);
            $options = ArrayHelper::merge($this->cssOptions, $css);
            $view->registerCssFile($manager->getAssetUrl($this, $file, ArrayHelper::getValue($options, 'appendTimestamp')), $options);
        } elseif ($css !== null) {
            $view->registerCssFile($manager->getAssetUrl($this, $css), $this->cssOptions);
        }
    }
}