1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373
|
<?php
/*
* $Id: ProjectConfigurator.php 552 2009-08-29 12:18:13Z mrook $
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* This software consists of voluntary contributions made by many individuals
* and is licensed under the LGPL. For more information please see
* <http://phing.info>.
*/
include_once 'phing/system/io/BufferedReader.php';
include_once 'phing/system/io/FileReader.php';
include_once 'phing/BuildException.php';
include_once 'phing/system/lang/FileNotFoundException.php';
include_once 'phing/system/io/PhingFile.php';
include_once 'phing/parser/PhingXMLContext.php';
include_once 'phing/IntrospectionHelper.php';
/**
* The datatype handler class.
*
* This class handles the occurance of registered datatype tags like
* FileSet
*
* @author Andreas Aderhold <andi@binarycloud.com>
* @copyright � 2001,2002 THYRELL. All rights reserved
* @version $Revision: 552 $ $Date: 2009-08-29 14:18:13 +0200 (Sat, 29 Aug 2009) $
* @access public
* @package phing.parser
*/
class ProjectConfigurator {
public $project;
public $locator;
public $buildFile;
public $buildFileParent;
/** Targets in current file */
private $currentTargets;
/** Synthetic target that will be called at the end to the parse phase */
private $parseEndTarget;
/** Name of the current project */
private $currentProjectName;
private $isParsing = true;
/**
* Indicates whether the project tag attributes are to be ignored
* when processing a particular build file.
*/
private $ignoreProjectTag = false;
/**
* Static call to ProjectConfigurator. Use this to configure a
* project. Do not use the new operator.
*
* @param object the Project instance this configurator should use
* @param object the buildfile object the parser should use
* @access public
*/
public static function configureProject(Project $project, PhingFile $buildFile) {
$pc = new ProjectConfigurator($project, $buildFile);
$pc->parse();
}
/**
* Constructs a new ProjectConfigurator object
* This constructor is private. Use a static call to
* <code>configureProject</code> to configure a project.
*
* @param object the Project instance this configurator should use
* @param object the buildfile object the parser should use
* @access private
*/
function __construct(Project $project, PhingFile $buildFile) {
$this->project = $project;
$this->buildFile = new PhingFile($buildFile->getAbsolutePath());
$this->buildFileParent = new PhingFile($this->buildFile->getParent());
$this->currentTargets = array();
$this->parseEndTarget = new Target();
}
/**
* find out the build file
* @return the build file to which the xml context belongs
*/
public function getBuildFile() {
return $this->buildFile;
}
/**
* find out the parent build file of this build file
* @return the parent build file of this build file
*/
public function getBuildFileParent() {
return $this->buildFileParent;
}
/**
* find out the current project name
* @return current project name
*/
public function getCurrentProjectName() {
return $this->currentProjectName;
}
/**
* set the name of the current project
* @param name name of the current project
*/
public function setCurrentProjectName($name) {
$this->currentProjectName = $name;
}
/**
* tells whether the project tag is being ignored
* @return whether the project tag is being ignored
*/
public function isIgnoringProjectTag() {
return $this->ignoreProjectTag;
}
/**
* sets the flag to ignore the project tag
* @param flag to ignore the project tag
*/
public function setIgnoreProjectTag($flag) {
$this->ignoreProjectTag = $flag;
}
public function &getCurrentTargets () {
return $this->currentTargets;
}
public function isParsing () {
return $this->isParsing;
}
/**
* Creates the ExpatParser, sets root handler and kick off parsing
* process.
*
* @throws BuildException if there is any kind of execption during
* the parsing process
* @access private
*/
protected function parse() {
try {
// get parse context
$ctx = $this->project->getReference("phing.parsing.context");
if (null == $ctx) {
// make a new context and register it with project
$ctx = new PhingXMLContext($this->project);
$this->project->addReference("phing.parsing.context", $ctx);
}
//record this parse with context
$ctx->addImport($this->buildFile);
if (count($ctx->getImportStack()) > 1) {
// this is an imported file
// modify project tag parse behavior
$this->setIgnoreProjectTag(true);
}
// push action onto global stack
$ctx->startConfigure($this);
$reader = new BufferedReader(new FileReader($this->buildFile));
$parser = new ExpatParser($reader);
$parser->parserSetOption(XML_OPTION_CASE_FOLDING,0);
$parser->setHandler(new RootHandler($parser, $this));
$this->project->log("parsing buildfile ".$this->buildFile->getName(), Project::MSG_VERBOSE);
$parser->parse();
$reader->close();
// mark parse phase as completed
$this->isParsing = false;
// execute delayed tasks
$this->parseEndTarget->main();
// pop this action from the global stack
$ctx->endConfigure();
} catch (Exception $exc) {
throw new BuildException("Error reading project file", $exc);
}
}
/**
* Delay execution of a task until after the current parse phase has
* completed.
*
* @param Task $task Task to execute after parse
*/
public function delayTaskUntilParseEnd ($task) {
$this->parseEndTarget->addTask($task);
}
/**
* Configures an element and resolves eventually given properties.
*
* @param object the element to configure
* @param array the element's attributes
* @param object the project this element belongs to
* @throws Exception if arguments are not valid
* @throws BuildException if attributes can not be configured
* @access public
*/
public static function configure($target, $attrs, Project $project) {
if ($target instanceof TaskAdapter) {
$target = $target->getProxy();
}
// if the target is an UnknownElement, this means that the tag had not been registered
// when the enclosing element (task, target, etc.) was configured. It is possible, however,
// that the tag was registered (e.g. using <taskdef>) after the original configuration.
// ... so, try to load it again:
if ($target instanceof UnknownElement) {
$tryTarget = $project->createTask($target->getTaskType());
if ($tryTarget) {
$target = $tryTarget;
}
}
$bean = get_class($target);
$ih = IntrospectionHelper::getHelper($bean);
foreach ($attrs as $key => $value) {
if ($key == 'id') {
continue;
// throw new BuildException("Id must be set Extermnally");
}
$value = self::replaceProperties($project, $value, $project->getProperties());
try { // try to set the attribute
$ih->setAttribute($project, $target, strtolower($key), $value);
} catch (BuildException $be) {
// id attribute must be set externally
if ($key !== "id") {
throw $be;
}
}
}
}
/**
* Configures the #CDATA of an element.
*
* @param object the project this element belongs to
* @param object the element to configure
* @param string the element's #CDATA
* @access public
*/
public static function addText($project, $target, $text = null) {
if ($text === null || strlen(trim($text)) === 0) {
return;
}
$ih = IntrospectionHelper::getHelper(get_class($target));
$text = self::replaceProperties($project, $text, $project->getProperties());
$ih->addText($project, $target, $text);
}
/**
* Stores a configured child element into its parent object
*
* @param object the project this element belongs to
* @param object the parent element
* @param object the child element
* @param string the XML tagname
* @access public
*/
public static function storeChild($project, $parent, $child, $tag) {
$ih = IntrospectionHelper::getHelper(get_class($parent));
$ih->storeElement($project, $parent, $child, $tag);
}
// The following two properties are a sort of hack
// to enable a static function to serve as the callback
// for preg_replace_callback(). Clearly we cannot use object
// variables, since the replaceProperties() is called statically.
// This is IMO better than using global variables in the callback.
private static $propReplaceProject;
private static $propReplaceProperties;
/**
* Replace ${} style constructions in the given value with the
* string value of the corresponding data types. This method is
* static.
*
* @param object the project that should be used for property look-ups
* @param string the string to be scanned for property references
* @param array proeprty keys
* @return string the replaced string or <code>null</code> if the string
* itself was null
*/
public static function replaceProperties(Project $project, $value, $keys) {
if ($value === null) {
return null;
}
// These are a "hack" to support static callback for preg_replace_callback()
// make sure these get initialized every time
self::$propReplaceProperties = $keys;
self::$propReplaceProject = $project;
// Because we're not doing anything special (like multiple passes),
// regex is the simplest / fastest. PropertyTask, though, uses
// the old parsePropertyString() method, since it has more stringent
// requirements.
$sb = $value;
$iteration = 0;
// loop to recursively replace tokens
while (strpos($sb, '${') !== false)
{
$sb = preg_replace_callback('/\$\{([^\$}]+)\}/', array('ProjectConfigurator', 'replacePropertyCallback'), $sb);
// keep track of iterations so we can break out of otherwise infinite loops.
$iteration++;
if ($iteration == 5)
{
return $sb;
}
}
return $sb;
}
/**
* Private [static] function for use by preg_replace_callback to replace a single param.
* This method makes use of a static variable to hold the
*/
private static function replacePropertyCallback($matches)
{
$propertyName = $matches[1];
if (!isset(self::$propReplaceProperties[$propertyName])) {
self::$propReplaceProject->log('Property ${'.$propertyName.'} has not been set.', Project::MSG_VERBOSE);
return $matches[0];
} else {
self::$propReplaceProject->log('Property ${'.$propertyName.'} => ' . self::$propReplaceProperties[$propertyName], Project::MSG_DEBUG);
}
return self::$propReplaceProperties[$propertyName];
}
/**
* Scan Attributes for the id attribute and maybe add a reference to
* project.
*
* @param object the element's object
* @param array the element's attributes
*/
public function configureId($target, $attr) {
if (isset($attr['id']) && $attr['id'] !== null) {
$this->project->addReference($attr['id'], $target);
}
}
}
|