Completed
Push — master ( b4d6a5...352f6d )
by Basil
02:42
created

View   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 77
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 5

Importance

Changes 0
Metric Value
wmc 9
lcom 2
cbo 5
dl 0
loc 77
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A init() 0 14 5
A getAssetUrl() 0 10 2
A compress() 0 4 1
A getPublicHtml() 0 4 1
1
<?php
2
3
namespace luya\web;
4
5
use Yii;
6
7
use luya\Exception;
8
use luya\helpers\StringHelper;
9
10
/**
11
 * LUYA web view wrapper.
12
 *
13
 * Implements additional helper methods to the Yii web controller.
14
 *
15
 * @property string $publicHtml Return the relativ path to your public_html folder
16
 *
17
 * @author Basil Suter <[email protected]>
18
 * @since 1.0.0
19
 */
20
class View extends \yii\web\View
21
{
22
    /**
23
     * @var boolean If csrf validation is enabled in the request component, and autoRegisterCsrf is enabled, then
24
     * all the meta informations will be auto added to meta tags.
25
     */
26
    public $autoRegisterCsrf = true;
27
    
28
    /**
29
     * Init view object. Implements auto register csrf meta tokens.
30
     * @see \yii\base\View::init()
31
     */
32
    public function init()
33
    {
34
        // call parent initializer
35
        parent::init();
36
37
        if (empty($this->theme) && Yii::$app->themeManager->hasActiveTheme) {
38
            $this->theme = Yii::$app->themeManager->activeTheme;
39
        }
40
41
        // auto register csrf tags if enabled
42
        if ($this->autoRegisterCsrf && Yii::$app->request->enableCsrfValidation) {
43
            $this->registerCsrfMetaTags();
44
        }
45
    }
46
47
    /**
48
     * Get the url source for an asset.
49
     *
50
     * When registering an asset `\app\assets\ResoucesAsset::register($this)` the $assetName
51
     * is `app\assets\ResourcesAsset`.
52
     *
53
     * @param string $assetName The class name of the asset bundle (without the leading backslash)
54
     * @return string The internal base path to the asset file.
55
     * @throws Exception
56
     */
57
    public function getAssetUrl($assetName)
58
    {
59
        $assetName = ltrim($assetName, '\\');
60
        
61
        if (!isset($this->assetBundles[$assetName])) {
62
            throw new Exception("The AssetBundle '$assetName' is not registered.");
63
        }
64
        
65
        return $this->assetBundles[$assetName]->baseUrl;
66
    }
67
68
    /**
69
     * Removes redundant whitespaces (>1) and new lines (>1).
70
     *
71
     * @param string $content input string
72
     * @return string compressed string
73
     */
74
    public function compress($content)
75
    {
76
        return StringHelper::minify($content);
77
    }
78
79
    /**
80
     * Return the relativ path to your public_html folder.
81
     *
82
     * This wrapper function is commonly used to get the path for images or other files inside your
83
     * public_html directory. For instance you have put some images in our public folder `public_html/img/luya.png`
84
     * then you can access the image file inside your view files with:
85
     *
86
     * ```php
87
     * <img src="<?= $this->publicHtml; ?>/img/luya.png" />
88
     * ```
89
     *
90
     * @return string The relative baseUrl to your public_html folder.
91
     */
92
    public function getPublicHtml()
93
    {
94
        return Yii::$app->request->baseUrl;
95
    }
96
}
97