|
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
|
|
|
* Selects the first available file from a list of paths. |
|
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 SelectpathTask extends AgaviTask |
|
|
|
|
|
|
33
|
|
|
{ |
|
34
|
|
|
const TYPE_FILE = 'file'; |
|
35
|
|
|
const TYPE_DIRECTORY = 'directory'; |
|
36
|
|
|
|
|
37
|
|
|
protected $property = null; |
|
38
|
|
|
protected $path = null; |
|
39
|
|
|
protected $type = null; |
|
40
|
|
|
protected $froms = array(); |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* Sets the property that this task will modify. |
|
44
|
|
|
* |
|
45
|
|
|
* @param string The property to modify. |
|
46
|
|
|
*/ |
|
47
|
|
|
public function setProperty($property) |
|
48
|
|
|
{ |
|
49
|
|
|
$this->property = $property; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* Sets the path to locate. |
|
54
|
|
|
* |
|
55
|
|
|
* @param string The path to locate. |
|
56
|
|
|
*/ |
|
57
|
|
|
public function setPath($path) |
|
58
|
|
|
{ |
|
59
|
|
|
if(!empty($path)) { |
|
60
|
|
|
/* This must be created here to prevent the directory from |
|
61
|
|
|
* becoming automatically converted to an absolute path. */ |
|
62
|
|
|
$this->path = new PhingFile($path); |
|
63
|
|
|
} else { |
|
64
|
|
|
$this->path = null; |
|
65
|
|
|
} |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* Sets the type that the path must have. |
|
70
|
|
|
* |
|
71
|
|
|
* @param string One of <code>file</code> or <code>directory</code>. |
|
72
|
|
|
*/ |
|
73
|
|
|
public function setType($type) |
|
74
|
|
|
{ |
|
75
|
|
|
$this->type = $type; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* Adds a new path to the search list. |
|
80
|
|
|
* |
|
81
|
|
|
* @param PhingFile The path to add. |
|
82
|
|
|
*/ |
|
83
|
|
|
public function createFrom() |
|
84
|
|
|
{ |
|
85
|
|
|
$from = new FromType(); |
|
86
|
|
|
$this->froms[] = $from; |
|
87
|
|
|
return $from; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* Executes the task. |
|
92
|
|
|
*/ |
|
93
|
|
|
public function main() |
|
94
|
|
|
{ |
|
95
|
|
|
if($this->property === null) { |
|
96
|
|
|
throw new \Agavi\Build\Exception\BuildException('The property attribute must be specified'); |
|
97
|
|
|
} |
|
98
|
|
|
if(count($this->froms) === 0) { |
|
99
|
|
|
throw new \Agavi\Build\Exception\BuildException('At least one from tag must be specified'); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
foreach($this->froms as $from) { |
|
103
|
|
|
if(null !== $this->path) { |
|
104
|
|
|
$path = new PhingFile($from->getPath()->getAbsolutePath() . DIRECTORY_SEPARATOR . $this->path->getPath()); |
|
105
|
|
|
} else { |
|
106
|
|
|
$path = new PhingFile($from->getPath()->getAbsolutePath()); |
|
107
|
|
|
} |
|
108
|
|
|
if( |
|
109
|
|
|
($this->type === null && file_exists($path->getPath())) || |
|
110
|
|
|
($this->type === self::TYPE_FILE && is_file($path->getPath())) || |
|
111
|
|
|
($this->type === self::TYPE_DIRECTORY && is_dir($path->getPath())) |
|
112
|
|
|
) { |
|
113
|
|
|
$this->project->setUserProperty($this->property, $path->getPath()); |
|
114
|
|
|
return; |
|
115
|
|
|
} |
|
116
|
|
|
} |
|
117
|
|
|
} |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
?> |
|
|
|
|
|
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.