|
1
|
|
|
<?php |
|
|
|
|
|
|
2
|
|
|
|
|
3
|
|
|
// +---------------------------------------------------------------------------+ |
|
4
|
|
|
// | This file is part of the Agavi package. | |
|
5
|
|
|
// | Copyright (c) 2005-2011 the Agavi Project. | |
|
6
|
|
|
// | | |
|
7
|
|
|
// | For the full copyright and license information, please view the LICENSE | |
|
8
|
|
|
// | file that was distributed with this source code. You can also view the | |
|
9
|
|
|
// | LICENSE file online at http://www.agavi.org/LICENSE.txt | |
|
10
|
|
|
// | vi: set noexpandtab: | |
|
11
|
|
|
// | Local Variables: | |
|
12
|
|
|
// | indent-tabs-mode: t | |
|
13
|
|
|
// | End: | |
|
14
|
|
|
// +---------------------------------------------------------------------------+ |
|
15
|
|
|
|
|
16
|
|
|
require_once(__DIR__ . '/AgaviTask.php'); |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* Determines whether a file is available on the filesystem. |
|
20
|
|
|
* |
|
21
|
|
|
* @package agavi |
|
22
|
|
|
* @subpackage build |
|
23
|
|
|
* |
|
24
|
|
|
* @author Noah Fontes <[email protected]> |
|
25
|
|
|
* @copyright Authors |
|
26
|
|
|
* @copyright The Agavi Project |
|
27
|
|
|
* |
|
28
|
|
|
* @since 1.0.0 |
|
29
|
|
|
* |
|
30
|
|
|
* @version $Id$ |
|
31
|
|
|
*/ |
|
32
|
|
|
class AvailableTask extends AgaviTask |
|
|
|
|
|
|
33
|
|
|
{ |
|
34
|
|
|
const TYPE_ANY = 1; |
|
35
|
|
|
const TYPE_FILE = 2; |
|
36
|
|
|
const TYPE_DIRECTORY = 3; |
|
37
|
|
|
|
|
38
|
|
|
protected $property = null; |
|
39
|
|
|
protected $file = null; |
|
40
|
|
|
protected $value = true; |
|
41
|
|
|
protected $type = self::TYPE_ANY; |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* Sets the property that this task will modify. |
|
45
|
|
|
* |
|
46
|
|
|
* @param string The name of the property. |
|
47
|
|
|
*/ |
|
48
|
|
|
public function setProperty($property) |
|
49
|
|
|
{ |
|
50
|
|
|
$this->property = $property; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* Sets the file to find. |
|
55
|
|
|
* |
|
56
|
|
|
* @param PhingFile The file. |
|
57
|
|
|
*/ |
|
58
|
|
|
public function setFile(PhingFile $file) |
|
59
|
|
|
{ |
|
60
|
|
|
$this->file = $file; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* Sets the value to which the property will be set if the condition is |
|
65
|
|
|
* successfully evaluated. |
|
66
|
|
|
* |
|
67
|
|
|
* @param string The value. |
|
68
|
|
|
*/ |
|
69
|
|
|
public function setValue($value) |
|
70
|
|
|
{ |
|
71
|
|
|
$this->value = $value; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* Sets the type of the file. |
|
76
|
|
|
* |
|
77
|
|
|
* @param string One of <code>file</code> or <code>directory</code>. |
|
78
|
|
|
*/ |
|
79
|
|
|
public function setType($type) |
|
80
|
|
|
{ |
|
81
|
|
|
switch($type) { |
|
82
|
|
|
case 'any': |
|
83
|
|
|
$this->type = self::TYPE_ANY; |
|
84
|
|
|
break; |
|
85
|
|
|
case 'file': |
|
86
|
|
|
$this->type = self::TYPE_FILE; |
|
87
|
|
|
break; |
|
88
|
|
|
case 'directory': |
|
89
|
|
|
$this->type = self::TYPE_DIRECTORY; |
|
90
|
|
|
break; |
|
91
|
|
|
default: |
|
92
|
|
|
throw new \Agavi\Build\Exception\BuildException('The type attribute must be one of {any, file, directory}'); |
|
93
|
|
|
} |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* Executes this task. |
|
98
|
|
|
*/ |
|
99
|
|
|
public function main() |
|
100
|
|
|
{ |
|
101
|
|
|
if($this->property === null) { |
|
102
|
|
|
throw new \Agavi\Build\Exception\BuildException('The property attribute must be specified'); |
|
103
|
|
|
} |
|
104
|
|
|
if($this->file === null) { |
|
105
|
|
|
throw new \Agavi\Build\Exception\BuildException('The file attribute must be specified'); |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
if($this->evaluate()) { |
|
109
|
|
|
if($this->value !== null) { |
|
110
|
|
|
$this->project->setUserProperty($this->property, $this->value); |
|
|
|
|
|
|
111
|
|
|
} |
|
112
|
|
|
} else { |
|
113
|
|
|
/* Unset. */ |
|
114
|
|
|
$this->project->setUserProperty($this->property, null); |
|
115
|
|
|
} |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
/** |
|
119
|
|
|
* Determines whether the file successfully meets the specified criteria. |
|
120
|
|
|
*/ |
|
121
|
|
|
protected function evaluate() |
|
122
|
|
|
{ |
|
123
|
|
|
switch($this->type) { |
|
124
|
|
|
case self::TYPE_ANY: |
|
125
|
|
|
return $this->file->exists(); |
|
126
|
|
|
case self::TYPE_FILE: |
|
127
|
|
|
return $this->file->isFile(); |
|
128
|
|
|
case self::TYPE_DIRECTORY: |
|
129
|
|
|
return $this->file->isDirectory(); |
|
130
|
|
|
} |
|
131
|
|
|
} |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
?> |
|
|
|
|
|
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.