|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* For licensing information, please see the LICENSE file accompanied with this file. |
|
4
|
|
|
* |
|
5
|
|
|
* @author Gerard van Helden <[email protected]> |
|
6
|
|
|
* @copyright 2012 Gerard van Helden <http://melp.nl> |
|
7
|
|
|
*/ |
|
8
|
|
|
|
|
9
|
|
|
namespace Zicht\Tool\Script\Node\Task; |
|
10
|
|
|
|
|
11
|
|
|
use Zicht\Tool\Script\Buffer; |
|
12
|
|
|
use Zicht\Tool\Script\Node\Branch; |
|
13
|
|
|
use Zicht\Tool\Util; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* A node for the "args" section of a task |
|
17
|
|
|
*/ |
|
18
|
|
|
class ArgNode extends Branch |
|
19
|
|
|
{ |
|
20
|
|
|
public $multiple = false; |
|
21
|
|
|
public $conditional = false; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* Constructor. |
|
25
|
|
|
* |
|
26
|
|
|
* @param string $name |
|
27
|
|
|
* @param \Zicht\Tool\Script\Node\Node $expr |
|
28
|
|
|
* @param bool $conditional |
|
29
|
|
|
*/ |
|
30
|
|
|
public function __construct($name, $expr, $conditional) |
|
31
|
|
|
{ |
|
32
|
|
|
parent::__construct(); |
|
33
|
|
|
$this->nodes[0] = $expr; |
|
34
|
|
|
$this->name = $name; |
|
|
|
|
|
|
35
|
|
|
if (substr($this->name, -2) === '[]') { |
|
36
|
|
|
$this->multiple = true; |
|
37
|
|
|
$this->name = substr($this->name, 0, -2); |
|
38
|
|
|
} |
|
39
|
|
|
$this->conditional = $conditional; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* Compiles the arg node. |
|
45
|
|
|
* |
|
46
|
|
|
* @param \Zicht\Tool\Script\Buffer $buffer |
|
47
|
|
|
* @return void |
|
48
|
|
|
*/ |
|
49
|
|
|
public function compile(Buffer $buffer) |
|
50
|
|
|
{ |
|
51
|
|
|
$name = explode('.', $this->name); |
|
52
|
|
|
$phpName = Util::toPhp($name); |
|
53
|
|
|
|
|
54
|
|
|
if ($this->conditional) { |
|
55
|
|
|
if ($this->multiple) { |
|
56
|
|
|
$buffer->writeln(sprintf('if ($z->isEmpty(%1$s) || array() === $z->get(%1$s)) {', $phpName))->indent(1); |
|
57
|
|
|
} else { |
|
58
|
|
|
$buffer->writeln(sprintf('if ($z->isEmpty(%s)) {', $phpName))->indent(1); |
|
59
|
|
|
} |
|
60
|
|
|
if (!$this->nodes[0]) { |
|
61
|
|
|
$buffer->writeln( |
|
62
|
|
|
sprintf( |
|
63
|
|
|
'throw new \RuntimeException(\'required variable %s is not defined\');', |
|
64
|
|
|
join('.', $name) |
|
65
|
|
|
) |
|
66
|
|
|
); |
|
67
|
|
|
} |
|
68
|
|
|
} |
|
69
|
|
|
if ($this->nodes[0]) { |
|
70
|
|
|
$buffer->write('$z->set(')->raw($phpName)->raw(', $z->value('); |
|
71
|
|
|
if ($this->multiple) { |
|
72
|
|
|
$buffer->raw('(array)('); |
|
73
|
|
|
} |
|
74
|
|
|
$this->nodes[0]->compile($buffer); |
|
75
|
|
|
if ($this->multiple) { |
|
76
|
|
|
$buffer->raw(')'); |
|
77
|
|
|
} |
|
78
|
|
|
$buffer->raw('));')->eol(); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
if ($this->conditional) { |
|
82
|
|
|
$buffer->indent(-1)->writeln('}'); |
|
83
|
|
|
} |
|
84
|
|
|
} |
|
85
|
|
|
} |
|
86
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: