Completed
Pull Request — master (#11)
by Vladimir
02:26
created

ThemeManager::refreshItem()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 1
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace allejo\stakx\Manager;
4
5
use Symfony\Component\Filesystem\Exception\FileNotFoundException;
6
use Symfony\Component\Yaml\Yaml;
7
8
class ThemeManager extends AssetManager
9
{
10
    const THEME_FOLDER = "_themes";
11
    const THEME_DEFINITION_FILE = "stakx-theme.yml";
12
13
    private $themeFolderRelative;
14
    private $themeFolder;
15
    private $themeFile;
16
    private $themeData;
17
    private $themeName;
18
19
    public function __construct ($themeName)
20
    {
21
        parent::__construct();
22
23
        $this->themeFolderRelative = $this->fs->appendPath(self::THEME_FOLDER, $themeName);
24
        $this->themeFolder = $this->fs->absolutePath(self::THEME_FOLDER, $themeName);
25
        $this->themeName   = $themeName;
26
        $this->themeFile   = $this->fs->appendPath($this->themeFolder, self::THEME_DEFINITION_FILE);
27
        $this->themeData   = array(
28
            'exclude' => array(),
29
            'include'  => array()
30
        );
31
32
        if (!$this->fs->exists($this->themeFolder))
33
        {
34
            throw new FileNotFoundException("The '${themeName}' theme folder could not be found.'");
35
        }
36
37
        if ($this->fs->exists($this->themeFile))
38
        {
39
            $themeData = Yaml::parse(file_get_contents($this->themeFile));
40
41
            $this->themeData = array_merge_recursive($this->themeData, $themeData);
42
        }
43
44
        foreach ($this->themeData['include'] as &$include)
45
        {
46
            $include = $this->fs->appendPath($this->themeFolder, $include);
47
        }
48
    }
49
50
    public function isTracked($filePath)
51
    {
52
        $relativeFilePath = str_replace($this->themeFolderRelative . '/', '', $filePath);
53
54
        return parent::isTracked($relativeFilePath);
55
    }
56
57
    public function refreshItem($filePath)
58
    {
59
        $relativeFilePath = str_replace($this->themeFolderRelative . '/', '', $filePath);
60
61
        parent::refreshItem($relativeFilePath);
62
    }
63
64
    public function copyFiles ()
65
    {
66
        $this->output->notice('Copying theme files...');
67
68
        $this->scanTrackableItems(
69
            $this->themeFolder,
70
            array(
71
                'prefix' => $this->themeFolderRelative
72
            ),
73
            array_merge(
74
                $this->includes,
75
                $this->themeData['include']
76
            ),
77
            array_merge(
78
                $this->excludes,
79
                $this->themeData['exclude'],
80
                array('.twig')
81
            )
82
        );
83
    }
84
}