Passed
Push — 1.11.x ( bce6cd...c146d9 )
by Angel Fernando Quiroz
12:25
created

src/Chamilo/ThemeBundle/Theme/ThemeManager.php (2 issues)

1
<?php
2
/**
3
 * ThemeManager.php
4
 * publisher
5
 * Date: 18.04.14.
6
 */
7
8
namespace Chamilo\ThemeBundle\Theme;
9
10
use Chamilo\FoundationBundle\Util\DependencyResolverInterface;
11
use Symfony\Component\DependencyInjection\Container;
12
use Symfony\Component\HttpKernel\Config\FileLocator;
13
14
/**
15
 * Class ThemeManager.
16
 *
17
 * @package Chamilo\ThemeBundle\Theme
18
 */
19
class ThemeManager
20
{
21
    /** @var Container */
22
    protected $container;
23
24
    protected $stylesheets = [];
25
26
    protected $javascripts = [];
27
28
    protected $locations = [];
29
30
    protected $resolverClass;
31
32
    /**
33
     * ThemeManager constructor.
34
     *
35
     * @param $container
36
     * @param null $resolverClass
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $resolverClass is correct as it would always require null to be passed?
Loading history...
37
     */
38
    public function __construct($container, $resolverClass = null)
39
    {
40
        $this->container = $container;
41
        $this->resolverClass = $resolverClass ?: 'Chamilo\ThemeBundle\Util\DependencyResolver';
0 ignored issues
show
$resolverClass is of type null, thus it always evaluated to false.
Loading history...
42
    }
43
44
    public function registerScript($id, $src, $deps = [], $location = "bottom")
45
    {
46
        if (!isset($this->javascripts[$id])) {
47
            $this->javascripts[$id] = [
48
                'src' => $src,
49
                'deps' => $deps,
50
                'location' => $location,
51
            ];
52
        }
53
    }
54
55
    public function registerStyle($id, $src, $deps = [])
56
    {
57
        if (!isset($this->stylesheets[$id])) {
58
            $this->stylesheets[$id] = [
59
                'src' => $src,
60
                'deps' => $deps,
61
            ];
62
        }
63
    }
64
65
    public function getScripts($location = 'bottom')
66
    {
67
        $unsorted = [];
68
        $srcList = [];
69
        $assetList = [];
70
        foreach ($this->javascripts as $id => $scriptDefinition) {
71
            if ($scriptDefinition['location'] == $location) {
72
                $unsorted[$id] = $scriptDefinition;
73
            }
74
        }
75
76
        $queue = $this->getResolver()->register($unsorted)->resolveAll();
77
        foreach ($queue as $def) {
78
            $srcList[] = $def['src'];
79
        }
80
81
        return $srcList;
82
    }
83
84
    public function getStyles()
85
    {
86
        $srcList = [];
87
        $queue = $this->getResolver()->register($this->stylesheets)->resolveAll();
88
        foreach ($queue as $def) {
89
            $srcList[] = $def['src'];
90
        }
91
92
        return $srcList;
93
    }
94
95
    /**
96
     * @return DependencyResolverInterface
97
     */
98
    protected function getResolver()
99
    {
100
        $class = $this->resolverClass;
101
102
        return new $class();
103
    }
104
105
    /**
106
     * @return FileLocator
107
     */
108
    protected function getLocator()
109
    {
110
        return $this->container->get('file_locator');
111
    }
112
}
113