Completed
Push — master ( 864841...a16bbd )
by Dmitry
02:18
created

AssetServerBuilder::getFilesPaths()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 0
loc 14
rs 9.4285
cc 3
eloc 8
nc 3
nop 1
1
<?php
2
3
namespace Dmitrynaum\SAM;
4
5
/**
6
 * Построитель asset`ов для realtime сервера
7
 *
8
 * @author Naumov Dmitry <[email protected]>
9
 */
10
class AssetServerBuilder extends AssetBuilder
11
{
12
    /**
13
     * Путь до приложения
14
     * @var string
15
     */
16
    protected $appPath;
17
    
18
    /**
19
     * @param string $manifestFilePath - путь до файла sam.json
20
     * @param string $appPath - Путь до приложения
21
     */
22
    public function __construct($manifestFilePath, $appPath)
23
    {
24
        $this->appPath = $appPath;
25
        
26
        parent::__construct($manifestFilePath);
27
    }
28
29
    /**
30
     * Получить содержимое asset`а по его имени
31
     * @param string $assetName
32
     * @return string
33
     * @throws \Exception
34
     */
35
    public function getAssetContentByName($assetName)
36
    {
37
        $manifest = $this->manifest();
38
        $assets   = array_merge($manifest->getCssAssets(), $manifest->getJsAssets());
39
        
40
        if (!isset($assets[$assetName])) {
41
            throw new \Exception('Asset not found', 404);
42
        }
43
                
44
        $assetsFiles = $this->getFilesPaths($assets);
45
        $assetData   = $this->readFiles($assetsFiles[$assetName]);
46
        
47
        return $assetData;
48
    }
49
    
50
    /**
51
     * Получить список asset`ов и файлы которые в них используются
52
     * @param array $assets
53
     * @return array список файлов используемых в ассетах
54
     */
55
    protected function getFilesPaths($assets)
56
    {
57
        $assetFilePaths = [];
58
        foreach ($assets as $assetName => $assetFiles) {
59
            $filePaths = [];
60
            foreach ($this->resolveAssetFilesPaths($assets, $assetFiles) as $filePath) {
61
                $filePaths[] = $this->appPath.$filePath;
62
            }
63
            
64
            $assetFilePaths[$assetName] = $filePaths;
65
        }
66
        
67
        return $assetFilePaths;
68
    }
69
}
70