Completed
Pull Request — 2.2 (#5)
by Pierre
02:03
created

WebLibraryManager::setWebLibraries()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 5
rs 9.4285
cc 2
eloc 3
nc 2
nop 1
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
     * The array of all included libraries.
25
     *
26
     * @var array<WebLibraryInterface>
27
     */
28
    private $webLibraries = array();
29
30
    /**
31
     * false if the toHtml method has not yet been called, true if it has been called.
32
     * @var boolean
33
     */
34
    private $rendered = false;
35
36
    /**
37
     * Adds a library to the list of libraries that should be loaded in the web page.
38
     * <p>The function will also load the dependencies (if any) and will have no effect if the library has already been loaded.</p>
39
     *
40
     * @param WebLibraryInterface $library
41
     */
42
    public function addLibrary(WebLibraryInterface $library) {
43
        if ($this->rendered) {
44
            throw new WebLibraryException("The libraries have already been rendered. This call to addLibrary should be performed BEFORE the toHtml method of WebLibraryManager is called.");
45
        }
46
        if (array_search($library, $this->webLibraries) === false) {
47
            // Let's start by adding dependencies.
48
            $dependencies = $library->getDependencies();
49
            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...
50
                foreach ($dependencies as $dependency) {
51
                    /* @var $dependency WebLibraryInterface */
52
                    $this->addLibrary($dependency);
53
                }
54
            }
55
56
            $this->webLibraries[] = $library;
57
        }
58
    }
59
60
    // TODO: add: addJs and addCss file
61
62
    /**
63
     * The list of all libraries that should be loaded in the web page.
64
     * <p>If you do not pass all dependencies of a library, the dependencies will be loaded automatically.</p>
65
     *
66
     * @Property
67
     * @param array<WebLibraryInterface> $libraries
68
     */
69
    public function setWebLibraries($libraries) {
70
        foreach ($libraries as $library) {
71
            $this->addLibrary($library);
72
        }
73
    }
74
75
    /**
76
     * Renders the HTML in charge of loading CSS and JS files.
77
     * The Html is echoed directly into the output.
78
     * This function should be called within the head tag.
79
     *
80
     */
81
    public function toHtml()
82
    {
83
        /*if ($this->rendered) {
84
            throw new WebLibraryException("The library has already been rendered.");
85
        }*/
86
87
        $this->toCss();
88
        $this->toJs();
89
        $this->toAdditional();
90
91
        $this->rendered = true;
92
    }
93
94
    /**
95
     * Renders the HTML in charge of loading CSS files.
96
     * The Html is echoed directly into the output.
97
     *
98
     */
99 View Code Duplication
    public function toCss()
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...
100
    {
101
        $defaultWebLibraryRenderer = null;
102
103
        foreach ($this->webLibraries as $library) {
104
            /* @var $library WebLibraryInterface */
105
            $renderer = $library->getRenderer();
106
            if ($renderer == null) {
107
                if ($defaultWebLibraryRenderer == null) {
108
                    $defaultWebLibraryRenderer = MoufManager::getMoufManager()->getInstance(
109
                        'defaultWebLibraryRenderer'
110
                    );
111
                }
112
                $renderer = $defaultWebLibraryRenderer;
113
            }
114
            /* @var $renderer WebLibraryRendererInterface */
115
            $renderer->toCssHtml($library);
116
        }
117
    }
118
119
    /**
120
     * Renders the HTML in charge of loading JS files.
121
     * The Html is echoed directly into the output.
122
     *
123
     */
124 View Code Duplication
    public function toJs()
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...
125
    {
126
        $defaultWebLibraryRenderer = null;
127
128
        foreach ($this->webLibraries as $library) {
129
            /* @var $library WebLibraryInterface */
130
            $renderer = $library->getRenderer();
131
            if ($renderer == null) {
132
                if ($defaultWebLibraryRenderer == null) {
133
                    $defaultWebLibraryRenderer = MoufManager::getMoufManager()->getInstance(
134
                        'defaultWebLibraryRenderer'
135
                    );
136
                }
137
                $renderer = $defaultWebLibraryRenderer;
138
            }
139
            /* @var $renderer WebLibraryRendererInterface */
140
            $renderer->toJsHtml($library);
141
        }
142
    }
143
144
    /**
145
     * Renders the HTML in charge of loading additional files.
146
     * The Html is echoed directly into the output.
147
     *
148
     */
149 View Code Duplication
    public function toAdditional()
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...
150
    {
151
        $defaultWebLibraryRenderer = null;
152
153
        foreach ($this->webLibraries as $library) {
154
            /* @var $library WebLibraryInterface */
155
            $renderer = $library->getRenderer();
156
            if ($renderer == null) {
157
                if ($defaultWebLibraryRenderer == null) {
158
                    $defaultWebLibraryRenderer = MoufManager::getMoufManager()->getInstance(
159
                        'defaultWebLibraryRenderer'
160
                    );
161
                }
162
                $renderer = $defaultWebLibraryRenderer;
163
            }
164
            /* @var $renderer WebLibraryRendererInterface */
165
            $renderer->toAdditionalHtml($library);
166
        }
167
    }
168
}
169
?>
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...