Completed
Push — master ( dbc6fd...a15748 )
by Dmitry
02:23
created

Manifest::getServerAddress()   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
     * @param string $filePath - путь до файла sam.json
58
     */
59
    public function __construct($filePath)
60
    {
61
        $this->filePath  = $filePath;
62
        $this->jsAssets  = [];
63
        $this->cssAssets = [];
64
65
        $this->init();
66
    }
67
68
    /**
69
     * Считать настройки из файла и инициализироваться
70
     */
71
    protected function init()
72
    {
73
        // Считываем файл
74
        $content = file_get_contents($this->filePath);
75
        $json    = json_decode($content);
76
77
        // Заполняем свойства
78
        $this->assetBasePath = $json->assetBasePath;
79
        $this->resultMapPath = $json->resultMapPath;
80
        $this->serverAddress = $json->devServerAddress;
81
82
        foreach ($json->assets as $assetName => $files) {
83
            $assetType = pathinfo($assetName, PATHINFO_EXTENSION);
84
85
            switch ($assetType) {
86
                case 'js':
87
                    $this->jsAssets[$assetName]  = $files;
88
                    break;
89
                case 'css':
90
                    $this->cssAssets[$assetName] = $files;
91
                    break;
92
            }
93
        }
94
    }
95
96
    /**
97
     * Получить все js asset`ы которые необходимо собрать
98
     * @return array
99
     */
100
    public function getJsAssets()
101
    {
102
        return $this->jsAssets;
103
    }
104
105
    /**
106
     * Получить все css asset`ы которые необходимо собрать
107
     * @return array
108
     */
109
    public function getCssAssets()
110
    {
111
        return $this->cssAssets;
112
    }
113
114
    /**
115
     * Получить объект карты результатов билдинга
116
     * @return AssetMap
117
     */
118
    public function resultMap()
119
    {
120
        if (!$this->resultMap) {
121
            $this->resultMap = new AssetMap($this->resultMapPath);
122
        }
123
        return $this->resultMap;
124
    }
125
126
    /**
127
     * Получить путь до папки где хранятся все asset`ы
128
     * @return string
129
     */
130
    public function getAssetBasePath()
131
    {
132
        return $this->assetBasePath;
133
    }
134
    
135
    public function getServerAddress()
136
    {
137
        return $this->serverAddress;
138
    }
139
}
140