Completed
Push — master ( 1de6e2...2a0597 )
by Dmitry
02:18
created

Manifest::getAssetBasePathFromProjectRoot()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace Dmitrynaum\SAM\Component;
4
5
/**
6
 * Класс для работы с манифестом
7
 *
8
 * @author Naumov Dmitry <[email protected]>
9
 */
10
class Manifest
11
{
12
13
    /**
14
     * Базовый путь до папки где будут храниться ассеты
15
     * @var string
16
     */
17
    protected $assetBasePath;
18
19
    /**
20
     * Путь до файла с картой построенных ассетов
21
     * @var string
22
     */
23
    protected $resultMapPath;
24
25
    /**
26
     * Путь до файла с манифестом
27
     * @var string
28
     */
29
    protected $filePath;
30
31
    /**
32
     * Список js ассетов
33
     * @var array
34
     */
35
    protected $jsAssets;
36
37
    /**
38
     * Список css ассетов
39
     * @var array
40
     */
41
    protected $cssAssets;
42
    
43
    /**
44
     * Карта скомпилированных asset`ов
45
     * @var AssetMap
46
     */
47
    protected $resultMap;
48
    
49
    /**
50
     * Адрес по которому распологается сервер
51
     * для билдинга ассетов в реальном времени
52
     * @var string
53
     */
54
    protected $serverAddress;
55
    
56
    /**
57
     * Путь root папки в которой запускается веб морда
58
     * @var string
59
     */
60
    protected $rootDir;
61
    
62
    /**
63
     * @param string $filePath - путь до файла sam.json
64
     */
65
    public function __construct($filePath)
66
    {
67
        $this->filePath  = $filePath;
68
        $this->jsAssets  = [];
69
        $this->cssAssets = [];
70
71
        $this->init();
72
    }
73
74
    /**
75
     * Считать настройки из файла и инициализироваться
76
     */
77
    protected function init()
78
    {
79
        // Считываем файл
80
        $content = file_get_contents($this->filePath);
81
        $json    = json_decode($content);
82
83
        // Заполняем свойства
84
        $this->assetBasePath = $json->assetBasePath;
85
        $this->rootDir       = $json->rootDir;
86
        $this->resultMapPath = dirname($this->filePath).'/'.$json->resultMapPath;
87
        $this->serverAddress = $json->devServerAddress;
88
89
        foreach ($json->assets as $assetName => $files) {
90
            $assetType = pathinfo($assetName, PATHINFO_EXTENSION);
91
92
            switch ($assetType) {
93
                case 'js':
94
                    $this->jsAssets[$assetName]  = $files;
95
                    break;
96
                case 'css':
97
                    $this->cssAssets[$assetName] = $files;
98
                    break;
99
            }
100
        }
101
    }
102
103
    /**
104
     * Получить все js asset`ы которые необходимо собрать
105
     * @return array
106
     */
107
    public function getJsAssets()
108
    {
109
        return $this->jsAssets;
110
    }
111
112
    /**
113
     * Получить все css asset`ы которые необходимо собрать
114
     * @return array
115
     */
116
    public function getCssAssets()
117
    {
118
        return $this->cssAssets;
119
    }
120
121
    /**
122
     * Получить объект карты результатов билдинга
123
     * @return AssetMap
124
     */
125
    public function resultMap()
126
    {
127
        if (!$this->resultMap) {
128
            $this->resultMap = new AssetMap($this->resultMapPath);
129
        }
130
        return $this->resultMap;
131
    }
132
133
    /**
134
     * Получить путь до папки где хранятся все asset`ы
135
     * @return string
136
     */
137
    public function getAssetBasePath()
138
    {
139
        return $this->assetBasePath;
140
    }
141
    
142
    /**
143
     * Получить путь до публичной папки приложения
144
     * в которую можно попасть через веб
145
     * @return string
146
     */
147
    public function getAssetBasePathFromProjectRoot()
148
    {
149
        return $this->rootDir.$this->assetBasePath;
150
    }
151
    
152
    /**
153
     * Получить адрес Dev сервера
154
     * @return string
155
     */
156
    public function getServerAddress()
157
    {
158
        return $this->serverAddress;
159
    }
160
}
161