RuntimeConfigurable::getChildren()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
/**
4
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
5
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
6
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
7
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
8
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
9
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
10
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
11
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
12
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
13
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
14
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
15
 *
16
 * This software consists of voluntary contributions made by many individuals
17
 * and is licensed under the LGPL. For more information please see
18
 * <http://phing.info>.
19
 */
20
21
namespace Phing;
22
23
use Exception;
24
use Phing\Exception\BuildException;
25
use Phing\Parser\ProjectConfigurator;
26
27
/**
28
 *  Wrapper class that holds the attributes of a Task (or elements
29
 *  nested below that level) and takes care of configuring that element
30
 *  at runtime.
31
 *
32
 *  <strong>SMART-UP INLINE DOCS</strong>
33
 *
34
 * @author  Andreas Aderhold <[email protected]>
35
 * @author  Hans Lellelid <[email protected]>
36
 */
37
class RuntimeConfigurable
38
{
39
    private $elementTag;
40
41
    /**
42
     * @var array
43
     */
44
    private $children = [];
45
46
    /**
47
     * @var object|Task
48
     */
49
    private $wrappedObject;
50
51
    /**
52
     * @var array
53
     */
54
    private $attributes = [];
55
56
    /**
57
     * @var string
58
     */
59
    private $characters = '';
60
61
    /**
62
     * @var bool
63
     */
64
    private $proxyConfigured = false;
65
66
    /**
67
     * @param object|Task $proxy
68
     * @param mixed       $elementTag the element to wrap
69
     */
70
    public function __construct($proxy, $elementTag)
71
    {
72
        $this->wrappedObject = $proxy;
73
        $this->elementTag = $elementTag;
74
75
        if ($proxy instanceof Task) {
76
            $proxy->setRuntimeConfigurableWrapper($this);
77
        }
78
    }
79
80
    /**
81
     * @return object|Task
82
     */
83
    public function getProxy()
84
    {
85
        return $this->wrappedObject;
86
    }
87
88
    /**
89
     * @param object|Task $proxy
90
     */
91
    public function setProxy($proxy)
92
    {
93
        $this->wrappedObject = $proxy;
94
        $this->proxyConfigured = false;
95
    }
96
97
    /**
98
     * Set's the attributes for the wrapped element.
99
     *
100
     * @param array $attributes
101
     */
102
    public function setAttributes($attributes)
103
    {
104
        $this->attributes = $attributes;
105
    }
106
107
    /**
108
     * Returns the AttributeList of the wrapped element.
109
     *
110
     * @return array
111
     */
112
    public function getAttributes()
113
    {
114
        return $this->attributes;
115
    }
116
117
    /**
118
     * Adds child elements to the wrapped element.
119
     */
120
    public function addChild(RuntimeConfigurable $child)
121
    {
122
        $this->children[] = $child;
123
    }
124
125
    /**
126
     * Returns the child with index.
127
     *
128
     * @param int $index
129
     *
130
     * @return RuntimeConfigurable
131
     */
132
    public function getChild($index)
133
    {
134
        return $this->children[(int) $index];
135
    }
136
137
    public function getChildren()
138
    {
139
        return $this->children;
140
    }
141
142
    /**
143
     * Add characters from #PCDATA areas to the wrapped element.
144
     *
145
     * @param string $data
146
     */
147
    public function addText($data)
148
    {
149
        $this->characters .= (string) $data;
150
    }
151
152
    /**
153
     * Get the text content of this element. Various text chunks are
154
     * concatenated, there is no way (currently) of keeping track of
155
     * multiple fragments.
156
     *
157
     * @return string the text content of this element
158
     */
159
    public function getText()
160
    {
161
        return (string) $this->characters;
162
    }
163
164
    public function getElementTag()
165
    {
166
        return $this->elementTag;
167
    }
168
169
    /**
170
     * Reconfigure the element, even if it has already been configured.
171
     *
172
     * @param Project $p the project instance for this configuration
173
     */
174
    public function reconfigure(Project $p)
175
    {
176
        $this->proxyConfigured = false;
177
        $this->maybeConfigure($p);
178
    }
179
180
    /**
181
     * Configure the wrapped element and all children.
182
     *
183
     * @throws BuildException
184
     * @throws Exception
185
     */
186
    public function maybeConfigure(Project $project)
187
    {
188
        if ($this->proxyConfigured) {
189
            return;
190
        }
191
192
        $id = null;
193
194
        if ($this->attributes || (isset($this->characters) && '' !== $this->characters)) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->attributes of type array 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...
195
            ProjectConfigurator::configure($this->wrappedObject, $this->attributes, $project);
196
197
            if (isset($this->attributes['id'])) {
198
                $id = $this->attributes['id'];
199
            }
200
201
            if ('' !== $this->characters) {
202
                ProjectConfigurator::addText($project, $this->wrappedObject, $this->characters);
203
            }
204
            if (null !== $id) {
205
                $project->addReference($id, $this->wrappedObject);
206
            }
207
        }
208
209
        $this->proxyConfigured = true;
210
    }
211
212
    public function setAttribute(string $name, $value)
213
    {
214
        $this->attributes[$name] = $value;
215
    }
216
217
    public function removeAttribute(string $name)
218
    {
219
        unset($this->attributes[$name]);
220
    }
221
222
    public function setElementTag(string $name)
223
    {
224
        $this->elementTag = $name;
225
    }
226
227
    public function getId()
228
    {
229
        return $this->attributes['id'] ?? null;
230
    }
231
}
232