123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374 |
- <?php
- 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';
- class ProjectConfigurator {
- public $project;
- public $locator;
-
- public $buildFile;
- public $buildFileParent;
-
- private $currentTargets;
-
- private $parseEndTarget;
-
- private $currentProjectName;
- private $isParsing = true;
-
- private $ignoreProjectTag = false;
-
- public static function configureProject(Project $project, PhingFile $buildFile) {
- $pc = new ProjectConfigurator($project, $buildFile);
- $pc->parse();
- }
-
- 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();
- }
-
- public function getBuildFile() {
- return $this->buildFile;
- }
-
- public function getBuildFileParent() {
- return $this->buildFileParent;
- }
-
- public function getCurrentProjectName() {
- return $this->currentProjectName;
- }
-
- public function setCurrentProjectName($name) {
- $this->currentProjectName = $name;
- }
-
- public function isIgnoringProjectTag() {
- return $this->ignoreProjectTag;
- }
-
- public function setIgnoreProjectTag($flag) {
- $this->ignoreProjectTag = $flag;
- }
- public function &getCurrentTargets () {
- return $this->currentTargets;
- }
- public function isParsing () {
- return $this->isParsing;
- }
-
- protected function parse() {
- try {
-
- $ctx = $this->project->getReference("phing.parsing.context");
- if (null == $ctx) {
-
- $ctx = new PhingXMLContext($this->project);
- $this->project->addReference("phing.parsing.context", $ctx);
- }
-
- $ctx->addImport($this->buildFile);
- if (count($ctx->getImportStack()) > 1) {
-
-
- $this->setIgnoreProjectTag(true);
- }
-
- $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();
-
- $this->isParsing = false;
-
- $this->parseEndTarget->main();
-
- $ctx->endConfigure();
- } catch (Exception $exc) {
- throw new BuildException("Error reading project file", $exc);
- }
- }
-
- public function delayTaskUntilParseEnd ($task) {
- $this->parseEndTarget->addTask($task);
- }
-
- public static function configure($target, $attrs, Project $project) {
- if ($target instanceof TaskAdapter) {
- $target = $target->getProxy();
- }
-
-
-
-
-
- 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;
-
- }
- $value = self::replaceProperties($project, $value, $project->getProperties());
- try {
- $ih->setAttribute($project, $target, strtolower($key), $value);
- } catch (BuildException $be) {
-
- if ($key !== "id") {
- throw $be;
- }
- }
- }
- }
-
- 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);
- }
-
- public static function storeChild($project, $parent, $child, $tag) {
- $ih = IntrospectionHelper::getHelper(get_class($parent));
- $ih->storeElement($project, $parent, $child, $tag);
- }
-
-
-
-
-
-
- private static $propReplaceProject;
- private static $propReplaceProperties;
-
-
- public static function replaceProperties(Project $project, $value, $keys) {
-
- if ($value === null) {
- return null;
- }
-
-
-
-
- self::$propReplaceProperties = $keys;
- self::$propReplaceProject = $project;
-
-
-
-
-
- $sb = $value;
- $iteration = 0;
-
-
- while (strpos($sb, '${') !== false)
- {
- $sb = preg_replace_callback('/\$\{([^\$}]+)\}/', array('ProjectConfigurator', 'replacePropertyCallback'), $sb);
-
- $iteration++;
- if ($iteration == 5)
- {
- return $sb;
- }
- }
-
- return $sb;
- }
-
-
- 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];
- }
-
- public function configureId($target, $attr) {
- if (isset($attr['id']) && $attr['id'] !== null) {
- $this->project->addReference($attr['id'], $target);
- }
- }
- }
|