Completed
Push — master ( dab7b5...3dae04 )
by Vladimir
02:49
created

ThemeManager::isHandled()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
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
    /**
51
     * {@inheritdoc}
52
     */
53
    public function refreshItem($filePath)
54
    {
55
        $relativeFilePath = str_replace($this->themeFolderRelative . '/', '', $filePath);
56
57
        parent::refreshItem($relativeFilePath);
58
    }
59
60
    /**
61
     * {@inheritdoc}
62
     */
63
    public function isHandled($filePath)
64
    {
65
        $isThemeAsset = (substr($filePath, 0, strlen($this->themeFolderRelative)) === $this->themeFolderRelative);
66
67
        return $isThemeAsset && parent::isHandled($filePath);
68
    }
69
70
    /**
71
     * {@inheritdoc}
72
     */
73
    public function createNewItem($filePath)
74
    {
75
        $relativeFilePath = str_replace($this->themeFolderRelative . '/', '', $filePath);
76
77
        return $this->handleTrackableItem($relativeFilePath, array(
78
            'prefix' => $this->themeFolderRelative
79
        ));
80
    }
81
82
    public function copyFiles ()
83
    {
84
        $this->output->notice('Copying theme files...');
85
86
        $this->scanTrackableItems(
87
            $this->themeFolder,
88
            array(
89
                'prefix' => $this->themeFolderRelative
90
            ),
91
            array_merge(
92
                $this->includes,
93
                $this->themeData['include']
94
            ),
95
            array_merge(
96
                $this->excludes,
97
                $this->themeData['exclude']
98
            )
99
        );
100
    }
101
}