[ Index ] |
|
Code source de Symfony 1.0.0 |
1 <?php 2 /* 3 * $Id: ProjectConfigurator.php 3076 2006-12-18 08:52:12Z fabien $ 4 * 5 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 6 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 7 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 8 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 9 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 10 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 11 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 12 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 13 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 14 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 15 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 16 * 17 * This software consists of voluntary contributions made by many individuals 18 * and is licensed under the LGPL. For more information please see 19 * <http://phing.info>. 20 */ 21 22 include_once 'phing/system/io/BufferedReader.php'; 23 include_once 'phing/system/io/FileReader.php'; 24 include_once 'phing/BuildException.php'; 25 include_once 'phing/system/lang/FileNotFoundException.php'; 26 include_once 'phing/system/io/PhingFile.php'; 27 28 /** 29 * The datatype handler class. 30 * 31 * This class handles the occurance of registered datatype tags like 32 * FileSet 33 * 34 * @author Andreas Aderhold <andi@binarycloud.com> 35 * @copyright © 2001,2002 THYRELL. All rights reserved 36 * @version $Revision: 1.17 $ $Date: 2006/01/06 14:57:18 $ 37 * @access public 38 * @package phing.parser 39 */ 40 class ProjectConfigurator { 41 42 public $project; 43 public $locator; 44 45 public $buildFile; 46 public $buildFileParent; 47 48 /** 49 * Static call to ProjectConfigurator. Use this to configure a 50 * project. Do not use the new operator. 51 * 52 * @param object the Project instance this configurator should use 53 * @param object the buildfile object the parser should use 54 * @access public 55 */ 56 public static function configureProject(Project $project, PhingFile $buildFile) { 57 $pc = new ProjectConfigurator($project, $buildFile); 58 $pc->parse(); 59 } 60 61 /** 62 * Constructs a new ProjectConfigurator object 63 * This constructor is private. Use a static call to 64 * <code>configureProject</code> to configure a project. 65 * 66 * @param object the Project instance this configurator should use 67 * @param object the buildfile object the parser should use 68 * @access private 69 */ 70 function __construct(Project $project, PhingFile $buildFile) { 71 $this->project = $project; 72 $this->buildFile = new PhingFile($buildFile->getAbsolutePath()); 73 $this->buildFileParent = new PhingFile($this->buildFile->getParent()); 74 } 75 76 /** 77 * Creates the ExpatParser, sets root handler and kick off parsing 78 * process. 79 * 80 * @throws BuildException if there is any kind of execption during 81 * the parsing process 82 * @access private 83 */ 84 protected function parse() { 85 try { 86 $reader = new BufferedReader(new FileReader($this->buildFile)); 87 $reader->open(); 88 $parser = new ExpatParser($reader); 89 $parser->parserSetOption(XML_OPTION_CASE_FOLDING,0); 90 $parser->setHandler(new RootHandler($parser, $this)); 91 $this->project->log("parsing buildfile ".$this->buildFile->getName(), PROJECT_MSG_VERBOSE); 92 $parser->parse(); 93 $reader->close(); 94 } catch (Exception $exc) { 95 throw new BuildException("Error reading project file", $exc); 96 } 97 } 98 99 /** 100 * Configures an element and resolves eventually given properties. 101 * 102 * @param object the element to configure 103 * @param array the element's attributes 104 * @param object the project this element belongs to 105 * @throws Exception if arguments are not valid 106 * @throws BuildException if attributes can not be configured 107 * @access public 108 */ 109 public static function configure($target, $attrs, Project $project) { 110 111 if ($target instanceof TaskAdapter) { 112 $target = $target->getProxy(); 113 } 114 115 // if the target is an UnknownElement, this means that the tag had not been registered 116 // when the enclosing element (task, target, etc.) was configured. It is possible, however, 117 // that the tag was registered (e.g. using <taskdef>) after the original configuration. 118 // ... so, try to load it again: 119 if ($target instanceof UnknownElement) { 120 $tryTarget = $project->createTask($target->getTaskType()); 121 if ($tryTarget) { 122 $target = $tryTarget; 123 } 124 } 125 126 $bean = get_class($target); 127 $ih = IntrospectionHelper::getHelper($bean); 128 129 foreach ($attrs as $key => $value) { 130 if ($key == 'id') { 131 continue; 132 // throw new BuildException("Id must be set Extermnally"); 133 } 134 $value = self::replaceProperties($project, $value, $project->getProperties()); 135 try { // try to set the attribute 136 $ih->setAttribute($project, $target, strtolower($key), $value); 137 } catch (BuildException $be) { 138 // id attribute must be set externally 139 if ($key !== "id") { 140 throw $be; 141 } 142 } 143 } 144 } 145 146 /** 147 * Configures the #CDATA of an element. 148 * 149 * @param object the project this element belongs to 150 * @param object the element to configure 151 * @param string the element's #CDATA 152 * @access public 153 */ 154 public static function addText($project, $target, $text = null) { 155 if ($text === null || strlen(trim($text)) === 0) { 156 return; 157 } 158 $ih = IntrospectionHelper::getHelper(get_class($target)); 159 $text = self::replaceProperties($project, $text, $project->getProperties()); 160 $ih->addText($project, $target, $text); 161 } 162 163 /** 164 * Stores a configured child element into its parent object 165 * 166 * @param object the project this element belongs to 167 * @param object the parent element 168 * @param object the child element 169 * @param string the XML tagname 170 * @access public 171 */ 172 public static function storeChild($project, $parent, $child, $tag) { 173 $ih = IntrospectionHelper::getHelper(get_class($parent)); 174 $ih->storeElement($project, $parent, $child, $tag); 175 } 176 177 // The following two properties are a sort of hack 178 // to enable a static function to serve as the callback 179 // for preg_replace_callback(). Clearly we cannot use object 180 // variables, since the replaceProperties() is called statically. 181 // This is IMO better than using global variables in the callback. 182 183 private static $propReplaceProject; 184 private static $propReplaceProperties; 185 186 /** 187 * Replace ${} style constructions in the given value with the 188 * string value of the corresponding data types. This method is 189 * static. 190 * 191 * @param object the project that should be used for property look-ups 192 * @param string the string to be scanned for property references 193 * @param array proeprty keys 194 * @return string the replaced string or <code>null</code> if the string 195 * itself was null 196 */ 197 public static function replaceProperties(Project $project, $value, $keys) { 198 199 if ($value === null) { 200 return null; 201 } 202 203 // These are a "hack" to support static callback for preg_replace_callback() 204 205 // make sure these get initialized every time 206 self::$propReplaceProperties = $keys; 207 self::$propReplaceProject = $project; 208 209 // Because we're not doing anything special (like multiple passes), 210 // regex is the simplest / fastest. PropertyTask, though, uses 211 // the old parsePropertyString() method, since it has more stringent 212 // requirements. 213 214 $sb = preg_replace_callback('/\$\{([^}]+)\}/', array('ProjectConfigurator', 'replacePropertyCallback'), $value); 215 return $sb; 216 } 217 218 /** 219 * Private [static] function for use by preg_replace_callback to replace a single param. 220 * This method makes use of a static variable to hold the 221 */ 222 private static function replacePropertyCallback($matches) 223 { 224 $propertyName = $matches[1]; 225 if (!isset(self::$propReplaceProperties[$propertyName])) { 226 self::$propReplaceProject->log('Property ${'.$propertyName.'} has not been set.', PROJECT_MSG_VERBOSE); 227 return $matches[0]; 228 } else { 229 self::$propReplaceProject->log('Property ${'.$propertyName.'} => ' . self::$propReplaceProperties[$propertyName], PROJECT_MSG_DEBUG); 230 } 231 return self::$propReplaceProperties[$propertyName]; 232 } 233 234 /** 235 * Scan Attributes for the id attribute and maybe add a reference to 236 * project. 237 * 238 * @param object the element's object 239 * @param array the element's attributes 240 */ 241 function configureId(&$target, $attr) { 242 if (isset($attr['id']) && $attr['id'] !== null) { 243 $this->project->addReference($attr['id'], $target); 244 } 245 } 246 }
titre
Description
Corps
titre
Description
Corps
titre
Description
Corps
titre
Corps
Généré le : Fri Mar 16 22:42:14 2007 | par Balluche grâce à PHPXref 0.7 |