|
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
|
|
|
* Locates the base project directory given a directory within it. |
|
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 LocateprojectTask extends AgaviTask |
|
|
|
|
|
|
33
|
|
|
{ |
|
34
|
|
|
protected $property = null; |
|
35
|
|
|
protected $path = null; |
|
36
|
|
|
protected $ignoreIfSet = false; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* Sets the property that this task will modify. |
|
40
|
|
|
* |
|
41
|
|
|
* @param string The property to modify. |
|
42
|
|
|
*/ |
|
43
|
|
|
public function setProperty($property) |
|
44
|
|
|
{ |
|
45
|
|
|
$this->property = $property; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* Sets the path to use to locate the project. |
|
50
|
|
|
* |
|
51
|
|
|
* @param string The path to use. |
|
52
|
|
|
*/ |
|
53
|
|
|
public function setPath(PhingFile $path) |
|
54
|
|
|
{ |
|
55
|
|
|
$this->path = $path; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* Sets whether to ignore this check if the property is already set. |
|
60
|
|
|
* |
|
61
|
|
|
* @param bool Whether to bypass the check if the property is set. |
|
62
|
|
|
*/ |
|
63
|
|
|
public function setIgnoreIfSet($ignoreIfSet) |
|
64
|
|
|
{ |
|
65
|
|
|
$this->ignoreIfSet = StringHelper::booleanValue($ignoreIfSet); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* Executes the task. |
|
70
|
|
|
*/ |
|
71
|
|
|
public function main() |
|
72
|
|
|
{ |
|
73
|
|
|
if($this->property === null) { |
|
74
|
|
|
throw new BuildException('The property attribute must be specified'); |
|
75
|
|
|
} |
|
76
|
|
|
if($this->path === null) { |
|
77
|
|
|
throw new BuildException('The path attribute must be specified'); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
if($this->ignoreIfSet && $this->project->getProperty($this->property) !== null) { |
|
81
|
|
|
return; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
if(!$this->path->exists()) { |
|
85
|
|
|
throw new BuildException('The path ' . $this->path->getAbsolutePath() . ' does not exist'); |
|
86
|
|
|
} |
|
87
|
|
|
$this->path = $this->path->getAbsoluteFile(); |
|
88
|
|
|
if(!$this->path->isDirectory()) { |
|
89
|
|
|
$this->path = $this->path->getParentFile(); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/* Check if the current directory is a project directory. */ |
|
93
|
|
|
$check = new \Agavi\Build\Check\ProjectFilesystemCheck(); |
|
94
|
|
|
$check->setAppDirectory($this->project->getProperty('project.directory.app')); |
|
95
|
|
|
$check->setPubDirectory($this->project->getProperty('project.directory.pub')); |
|
96
|
|
|
|
|
97
|
|
|
$check->setPath($this->path->getAbsolutePath()); |
|
98
|
|
View Code Duplication |
if($check->check()) { |
|
|
|
|
|
|
99
|
|
|
/* The current path is the project directory. */ |
|
100
|
|
|
$this->log('Project base directory: ' . $this->path->getPath()); |
|
101
|
|
|
$this->project->setUserProperty($this->property, $this->path->getAbsolutePath()); |
|
102
|
|
|
return; |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
/* Check if "app" or "pub" are in the current path. */ |
|
106
|
|
|
if(preg_match(sprintf('#^(.+?)/(?:%s|%s)(?:/|$)#', $this->project->getProperty('project.directory.app'), $this->project->getProperty('project.directory.pub')), $this->path->getPath(), $matches)) { |
|
107
|
|
|
$directory = new PhingFile($matches[1]); |
|
108
|
|
|
$check->setPath($directory->getAbsolutePath()); |
|
109
|
|
|
if($check->check()) { |
|
110
|
|
|
$this->log('Project base directory: ' . $directory); |
|
111
|
|
|
$this->project->setUserProperty($this->property, $directory->getAbsolutePath()); |
|
112
|
|
|
return; |
|
113
|
|
|
} |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
/* Last chance: recurse upward and check for a project directory. */ |
|
117
|
|
|
$directory = $this->path; |
|
118
|
|
View Code Duplication |
while(($directory = $directory->getParentFile()) !== null) { |
|
|
|
|
|
|
119
|
|
|
$check->setPath($directory->getAbsolutePath()); |
|
120
|
|
|
if($check->check()) { |
|
121
|
|
|
$this->log('Project base directory: ' . $directory); |
|
122
|
|
|
$this->project->setUserProperty($this->property, $directory->getAbsolutePath()); |
|
123
|
|
|
return; |
|
124
|
|
|
} |
|
125
|
|
|
} |
|
126
|
|
|
} |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
?> |
|
|
|
|
|
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.