Return to Snippet

Revision: 20571
at November 19, 2009 10:35 by extraordinaire


Initial Code
<?php

require_once "phing/Task.php";

/**
 * Saves currently defined properties into a specified file
 * 
 * @author Andrei Serdeliuc
 * @extends Task
 */
class ExportProperties extends Task
{
    /**
     * Array of project properties
     * 
     * (default value: null)
     * 
     * @var array
     * @access private
     */
    private $_properties = null;
    
    /**
     * Target file for saved properties
     * 
     * (default value: null)
     * 
     * @var string
     * @access private
     */
    private $_targetFile = null;
    
    /**
     * Exclude properties starting with these prefixes
     * 
     * @var array
     * @access private
     */
    private $_disallowedPropertyPrefixes = array(
        'host.',
        'phing.',
        'os.',
        'php.',
        'line.',
        'env.',
        'user.'
    );

    /**
     * setter for _targetFile
     * 
     * @access public
     * @param string $file
     * @return bool
     */
    public function setTargetFile($file)
    {
        if(!is_dir(dirname($file))) {
            throw new BuildException("Parent directory of target file doesn't exist");
        }
        
        if(!is_writable(dirname($file)) && (file_exists($file) && !is_writable($file))) {
            throw new BuildException("Target file isn't writable");
        }
        
        $this->_targetFile = $file;
        return true;
    }

    /**
     * Sets the currently declared properties
     * 
     * @access public
     * @return void
     */
    public function init()
    {
        $this->_properties = $this->getProject()->getProperties();
    }

    public function main()
    {
        if(is_array($this->_properties) && !empty($this->_properties) && null !== $this->_targetFile) {
            $propertiesString = '';
            foreach($this->_properties as $propertyName => $propertyValue) {
                if(!$this->isDisallowedPropery($propertyName)) {
                    $propertiesString .= $propertyName . "=" . $propertyValue . PHP_EOL;
                }
            }
            
            if(!file_put_contents($this->_targetFile, $propertiesString)) {
                throw new BuildException('Failed writing to ' . $this->_targetFile);
            }
        }
    }
    
    /**
     * Checks if a property name is disallowed
     * 
     * @access protected
     * @param string $propertyName
     * @return bool
     */
    protected function isDisallowedPropery($propertyName)
    {
        foreach($this->_disallowedPropertyPrefixes as $property) {
            if(substr($propertyName, 0, strlen($property)) == $property) {
                return true;
            }
        }
        
        return false;
    }
}

?>

Initial URL
http://extraordinaire.me/post/249721396/phing-how-to-export-defined-properties-to-file

Initial Description


Initial Title
Phing task: export properties to file

Initial Tags


Initial Language
PHP