|
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
|
|
|
* Lists all controllers in an Agavi module. |
|
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 ListControllersTask extends AgaviTask |
|
|
|
|
|
|
33
|
|
|
{ |
|
34
|
|
|
protected $property = null; |
|
35
|
|
|
protected $path = null; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* Sets the property that this task will modify. |
|
39
|
|
|
* |
|
40
|
|
|
* @param string The property to modify. |
|
41
|
|
|
*/ |
|
42
|
|
|
public function setProperty($property) |
|
43
|
|
|
{ |
|
44
|
|
|
$this->property = $property; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* Sets the path to the project directory from which this task will read. |
|
49
|
|
|
* |
|
50
|
|
|
* @param PhingFile Path to the project directory. |
|
51
|
|
|
*/ |
|
52
|
|
|
public function setPath(PhingFile $path) |
|
53
|
|
|
{ |
|
54
|
|
|
$this->path = $path; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* Executes this task. |
|
59
|
|
|
*/ |
|
60
|
|
|
public function main() |
|
61
|
|
|
{ |
|
62
|
|
|
if($this->property === null) { |
|
63
|
|
|
throw new BuildException('The property attribute must be specified'); |
|
64
|
|
|
} |
|
65
|
|
|
if($this->path === null) { |
|
66
|
|
|
throw new BuildException('The path attribute must be specified'); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
$check = new \Agavi\Build\Check\ModuleFilesystemCheck(); |
|
70
|
|
|
$check->setConfigDirectory($this->project->getProperty('module.config.directory')); |
|
71
|
|
|
|
|
72
|
|
|
$check->setPath($this->path->getAbsolutePath()); |
|
73
|
|
|
if(!$check->check()) { |
|
74
|
|
|
throw new BuildException('The path attribute must be a valid module base directory'); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/* We don't know whether the module is configured or not here, so load the |
|
78
|
|
|
* values we want properly. */ |
|
79
|
|
|
$this->tryLoadAgavi(); |
|
80
|
|
|
$this->tryBootstrapAgavi(); |
|
81
|
|
|
|
|
82
|
|
|
require_once(\Agavi\Config\ConfigCache::checkConfig( |
|
83
|
|
|
sprintf('%s/%s/module.xml', |
|
84
|
|
|
$this->path->getAbsolutePath(), |
|
85
|
|
|
(string)$this->project->getProperty('module.config.directory') |
|
86
|
|
|
) |
|
87
|
|
|
)); |
|
88
|
|
|
|
|
89
|
|
|
$controllerPath = \Agavi\Util\Toolkit::expandVariables( |
|
90
|
|
|
\Agavi\Util\Toolkit::expandDirectives(\Agavi\Config\Config::get( |
|
91
|
|
|
sprintf('modules.%s.agavi.controller.path', strtolower($this->path->getName())), |
|
92
|
|
|
'%core.module_dir%/${moduleName}/controllers/${controllerName}Controller.class.php' |
|
93
|
|
|
)), |
|
94
|
|
|
array( |
|
95
|
|
|
'moduleName' => $this->path->getName() |
|
96
|
|
|
) |
|
97
|
|
|
); |
|
98
|
|
|
$pattern = '#^' . \Agavi\Util\Toolkit::expandVariables( |
|
99
|
|
|
/* Blaaaaaaaaauuuuuughhhhhhh... */ |
|
100
|
|
|
str_replace('\\$\\{controllerName\\}', '${controllerName}', preg_quote($controllerPath, '#')), |
|
101
|
|
|
array('controllerName' => '(?P<controller_name>.*?)') |
|
102
|
|
|
) . '$#'; |
|
103
|
|
|
|
|
104
|
|
|
$controllers = array(); |
|
105
|
|
|
$iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($this->path->getAbsolutePath())); |
|
106
|
|
View Code Duplication |
for(; $iterator->valid(); $iterator->next()) { |
|
|
|
|
|
|
107
|
|
|
$rdi = $iterator->getInnerIterator(); |
|
108
|
|
|
if($rdi->isDot() || !$rdi->isFile()) { |
|
109
|
|
|
continue; |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
$file = $rdi->getPathname(); |
|
113
|
|
|
if(preg_match($pattern, $file, $matches)) { |
|
114
|
|
|
$controllers[] = str_replace(DIRECTORY_SEPARATOR, '.', $matches['controller_name']); |
|
115
|
|
|
} |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
$list = new \Agavi\Build\Transform\ArraytostringTransform(); |
|
119
|
|
|
$list->setInput($controllers); |
|
120
|
|
|
$list->setDelimiter(' '); |
|
121
|
|
|
|
|
122
|
|
|
$this->project->setUserProperty($this->property, $list->transform()); |
|
123
|
|
|
} |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
?> |
|
|
|
|
|
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.