Completed
Pull Request — 2.1 (#4)
by Pierre
01:47
created

WebLibraryManager::jsToHtml()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 19
Code Lines 10

Duplication

Lines 19
Ratio 100 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 19
loc 19
rs 9.2
cc 4
eloc 10
nc 4
nop 0
1
<?php
2
namespace Mouf\Html\Utils\WebLibraryManager;
3
4
use Mouf\MoufManager;
5
6
use Mouf\Html\HtmlElement\HtmlElementInterface;
7
8
/**
9
 * This class is in charge of including and keeping track of Javascript and CSS libraries into an HTML page.
10
 * <p>JS and CSS files are grouped into <b>WebLibraries</b>. If you want to add a new library, just create an instance
11
 * of the <b>WebLibrary</b> class, and add it to the <b>WebLibraryManager</b>.</p>
12
 * <p>You can use the <b>WebLibraryManager</b> class to add JS/CSS libraries. It will keep track of dependencies and ensure each file is included
13
 * only once.</p>
14
15
 * <p>If you have specific needs and don't want to use the <b>WebLibrary</b> class, you can either create your own class
16
 * that implements the WebLibraryInterface, or provide your own "renderer".</p>
17
 *
18
 *
19
 * @author David Négrier
20
 * @Component
21
 */
22
class WebLibraryManager implements HtmlElementInterface {
23
24
    /**
25
     * The array of all included libraries.
26
     *
27
     * @var array<WebLibraryInterface>
28
     */
29
    private $webLibraries = array();
30
31
    /**
32
     * false if the toHtml method has not yet been called, true if it has been called.
33
     * @var boolean
34
     */
35
    private $rendered = false;
36
37
    /**
38
     * Adds a library to the list of libraries that should be loaded in the web page.
39
     * <p>The function will also load the dependencies (if any) and will have no effect if the library has already been loaded.</p>
40
     *
41
     * @param WebLibraryInterface $library
42
     */
43
    public function addLibrary(WebLibraryInterface $library) {
44
        if ($this->rendered) {
45
            throw new WebLibraryException("The libraries have already been rendered. This call to addLibrary should be performed BEFORE the toHtml method of WebLibraryManager is called.");
46
        }
47
        if (array_search($library, $this->webLibraries) === false) {
48
            // Let's start by adding dependencies.
49
            $dependencies = $library->getDependencies();
50
            if ($dependencies) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $dependencies of type Mouf\Html\Utils\WebLibra...r\WebLibraryInterface[] is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
51
                foreach ($dependencies as $dependency) {
52
                    /* @var $dependency WebLibraryInterface */
53
                    $this->addLibrary($dependency);
54
                }
55
            }
56
57
            $this->webLibraries[] = $library;
58
        }
59
    }
60
61
    // TODO: add: addJs and addCss file
62
63
    /**
64
     * The list of all libraries that should be loaded in the web page.
65
     * <p>If you do not pass all dependencies of a library, the dependencies will be loaded automatically.</p>
66
     *
67
     * @Property
68
     * @param array<WebLibraryInterface> $libraries
69
     */
70
    public function setWebLibraries($libraries) {
71
        foreach ($libraries as $library) {
72
            $this->addLibrary($library);
73
        }
74
    }
75
76
    /**
77
     * Renders the HTML in charge of loading CSS and JS files.
78
     * The Html is echoed directly into the output.
79
     * This function should be called within the head tag.
80
     *
81
     */
82
    public function toHtml()
83
    {
84
        /*if ($this->rendered) {
85
            throw new WebLibraryException("The library has already been rendered.");
86
        }*/
87
88
        $this->cssToHtml();
89
        $this->jsToHtml();
90
        $this->additionalToHtml();
91
92
        $this->rendered = true;
93
    }
94
95
    /**
96
     * Renders the HTML in charge of loading CSS files.
97
     * The Html is echoed directly into the output.
98
     *
99
     */
100 View Code Duplication
    public function cssToHtml()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
101
    {
102
        $defaultWebLibraryRenderer = null;
103
104
        foreach ($this->webLibraries as $library) {
105
            /* @var $library WebLibraryInterface */
106
            $renderer = $library->getRenderer();
107
            if ($renderer == null) {
108
                if ($defaultWebLibraryRenderer == null) {
109
                    $defaultWebLibraryRenderer = MoufManager::getMoufManager()->getInstance(
110
                        'defaultWebLibraryRenderer'
111
                    );
112
                }
113
                $renderer = $defaultWebLibraryRenderer;
114
            }
115
            /* @var $renderer WebLibraryRendererInterface */
116
            $renderer->toCssHtml($library);
117
        }
118
    }
119
120
    /**
121
     * Renders the HTML in charge of loading JS files.
122
     * The Html is echoed directly into the output.
123
     *
124
     */
125 View Code Duplication
    public function jsToHtml()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
126
    {
127
        $defaultWebLibraryRenderer = null;
128
129
        foreach ($this->webLibraries as $library) {
130
            /* @var $library WebLibraryInterface */
131
            $renderer = $library->getRenderer();
132
            if ($renderer == null) {
133
                if ($defaultWebLibraryRenderer == null) {
134
                    $defaultWebLibraryRenderer = MoufManager::getMoufManager()->getInstance(
135
                        'defaultWebLibraryRenderer'
136
                    );
137
                }
138
                $renderer = $defaultWebLibraryRenderer;
139
            }
140
            /* @var $renderer WebLibraryRendererInterface */
141
            $renderer->toJsHtml($library);
142
        }
143
    }
144
145
    /**
146
     * Renders the HTML in charge of loading additional files.
147
     * The Html is echoed directly into the output.
148
     *
149
     */
150 View Code Duplication
    public function additionalToHtml()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
151
    {
152
        $defaultWebLibraryRenderer = null;
153
154
        foreach ($this->webLibraries as $library) {
155
            /* @var $library WebLibraryInterface */
156
            $renderer = $library->getRenderer();
157
            if ($renderer == null) {
158
                if ($defaultWebLibraryRenderer == null) {
159
                    $defaultWebLibraryRenderer = MoufManager::getMoufManager()->getInstance(
160
                        'defaultWebLibraryRenderer'
161
                    );
162
                }
163
                $renderer = $defaultWebLibraryRenderer;
164
            }
165
            /* @var $renderer WebLibraryRendererInterface */
166
            $renderer->toAdditionalHtml($library);
167
        }
168
    }
169
}
170
?>
0 ignored issues
show
Best Practice introduced by
It is not recommended to use PHP's closing tag ?> in files other than templates.

Using a closing tag in PHP files that only contain PHP code is not recommended as you might accidentally add whitespace after the closing tag which would then be output by PHP. This can cause severe problems, for example headers cannot be sent anymore.

A simple precaution is to leave off the closing tag as it is not required, and it also has no negative effects whatsoever.

Loading history...