Ticket #269 (new enhancement)

Opened 5 months ago

Last modified 3 months ago

Allow properties to be recursively named.

Reported by: elaboosh@yahoo.co.uk Assigned to: hans
Priority: major Milestone: 3.0
Component: phing-core Version: 2.3.1RC1
Keywords: properties Cc:

Description

It would be useful to allow properties to be named according to previously set properties.

e.g. if in a build.properties file I have:

project1.development.db.user = dev-user1
project1.staging.db.user = staging-user1
project2.development.db.user = dev-user2
project2.staging.db.user = staging-user2

In my build file, I have a property called 'project' for the name of the project to build, and also 'type' for whether to build the development or staging environment. These can both be overridden on the command line to select the project and environment to build to.

So, if I want to be able to perform operations on the correct database, I need to be able to specify properties like ${${project}.${type}.db.user}. At the moment, this fails because there is no support for recursively named properties like this.

Attachments

Change History

08/21/08 10:34:51 changed by elaboosh@yahoo.co.uk

I've implemented this myself. ProjectConfigurator::replaceProperties should be edited as follows:

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.

        // Following loop added 21/08/08 by Ela Boosh to allow replacement
        // of nested tokens.
	$sb = $value;
	while (strpos($sb, '${') !== false)
	{ 
                // old reg exp for single passes
        	//$sb = preg_replace_callback('/\$\{([^}]+)\}/', array('ProjectConfigurator', 'replacePropertyCallback'), $value);
                // new reg exp.
		$sb = preg_replace_callback('/\$\{([^\$}]+)\}/', array('ProjectConfigurator', 'replacePropertyCallback'), $sb);
	}

        return $sb; 
}

08/21/08 13:48:42 changed by elaboosh@yahoo.co.uk

a better version that will not allow for an infinite loop when the value can't be replaced is below:

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.

        // Following loop added 21/08/08 by Ela Boosh to allow replacement
        // of nested tokens.
        $sb = $value;
        $iteration = 0;
	while (strpos($sb, '${') !== false)
	{ 
                // old reg exp for single passes
        	//$sb = preg_replace_callback('/\$\{([^}]+)\}/', array('ProjectConfigurator', 'replacePropertyCallback'), $value);
                // new reg exp.
		$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; 
}

10/06/08 19:02:26 changed by mrook

  • type changed from defect to enhancement.
  • milestone changed from 2.3.1 to 3.0.

Add/Change #269 (Allow properties to be recursively named.)