home *** CD-ROM | disk | FTP | other *** search
/ Cricao de Sites - 650 Layouts Prontos / WebMasters.iso / Servidores / xampp-win32-1.6.7-installer.exe / php / PEAR / phing / system / util / Properties.php < prev    next >
Encoding:
PHP Script  |  2007-08-30  |  8.0 KB  |  270 lines

  1. <?php
  2.  
  3. /*
  4.  *  $Id: Properties.php 229 2007-08-30 12:45:08Z hans $
  5.  *
  6.  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  7.  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  8.  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  9.  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  10.  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  11.  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  12.  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  13.  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  14.  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  15.  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  16.  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  17.  *
  18.  * This software consists of voluntary contributions made by many individuals
  19.  * and is licensed under the LGPL. For more information please see
  20.  * <http://phing.info>.
  21.  */
  22.  
  23. include_once 'phing/system/io/PhingFile.php';
  24. include_once 'phing/system/io/FileWriter.php';
  25.  
  26. /**
  27.  * Convenience class for reading and writing property files.
  28.  * 
  29.  * FIXME
  30.  *        - Add support for arrays (separated by ',')
  31.  *
  32.  * @package    phing.system.util
  33.  * @version $Revision: 1.13 $
  34.  */
  35. class Properties {
  36.  
  37.     private $properties = array();
  38.  
  39.     /**
  40.      * Load properties from a file.
  41.      *
  42.      * @param PhingFile $file
  43.      * @return void
  44.      * @throws IOException - if unable to read file.
  45.      */
  46.     function load(PhingFile $file) {
  47.         if ($file->canRead()) {
  48.             $this->parse($file->getPath(), false);                    
  49.         } else {
  50.             throw new IOException("Can not read file ".$file->getPath());
  51.         }
  52.         
  53.     }
  54.     
  55.     /**
  56.      * Replaces parse_ini_file() or better_parse_ini_file().
  57.      * Saves a step since we don't have to parse and then check return value
  58.      * before throwing an error or setting class properties.
  59.      * 
  60.      * @param string $filePath
  61.      * @param boolean $processSections Whether to honor [SectionName] sections in INI file.
  62.      * @return array Properties loaded from file (no prop replacements done yet).
  63.      */
  64.     protected function parse($filePath) {
  65.  
  66.         // load() already made sure that file is readable                
  67.         // but we'll double check that when reading the file into 
  68.         // an array
  69.         
  70.         if (($lines = @file($filePath)) === false) {
  71.             throw new IOException("Unable to parse contents of $filePath");
  72.         }
  73.         
  74.         $this->properties = array();
  75.         $sec_name = "";
  76.         
  77.         foreach($lines as $line) {
  78.             
  79.             $line = trim($line);
  80.     
  81.             if($line == "")
  82.                 continue;
  83.                     
  84.             if ($line{0} == '#' or $line{0} == ';') {
  85.                 // it's a comment, so continue to next line
  86.                 continue;
  87.             } else {
  88.                 $pos = strpos($line, '=');
  89.                 $property = trim(substr($line, 0, $pos));
  90.                 $value = trim(substr($line, $pos + 1));                
  91.                 $this->properties[$property] = $this->inVal($value);
  92.             }
  93.             
  94.         } // for each line        
  95.     }
  96.     
  97.     /**
  98.      * Process values when being read in from properties file.
  99.      * does things like convert "true" => true
  100.      * @param string $val Trimmed value.
  101.      * @return mixed The new property value (may be boolean, etc.)
  102.      */
  103.     protected function inVal($val) {
  104.         if ($val === "true") { 
  105.             $val = true;
  106.         } elseif ($val === "false") { 
  107.             $val = false; 
  108.         }
  109.         return $val;
  110.     }
  111.     
  112.     /**
  113.      * Process values when being written out to properties file.
  114.      * does things like convert true => "true"
  115.      * @param mixed $val The property value (may be boolean, etc.)
  116.      * @return string
  117.      */
  118.     protected function outVal($val) {
  119.         if ($val === true) {
  120.             $val = "true";
  121.         } elseif ($val === false) {
  122.             $val = "false";
  123.         }
  124.         return $val;
  125.     }
  126.     
  127.     /**
  128.      * Create string representation that can be written to file and would be loadable using load() method.
  129.      * 
  130.      * Essentially this function creates a string representation of properties that is ready to
  131.      * write back out to a properties file.  This is used by store() method.
  132.      *
  133.      * @return string
  134.      */
  135.     public function toString() {
  136.         $buf = "";        
  137.         foreach($this->properties as $key => $item) {
  138.             $buf .= $key . "=" . $this->outVal($item) . PHP_EOL;
  139.         }
  140.         return $buf;    
  141.     }
  142.     
  143.     /**
  144.      * Stores current properties to specified file.
  145.      * 
  146.      * @param PhingFile $file File to create/overwrite with properties.
  147.      * @param string $header Header text that will be placed (within comments) at the top of properties file.
  148.      * @return void
  149.      * @throws IOException - on error writing properties file.
  150.      */
  151.     function store(PhingFile $file, $header = null) {
  152.         // stores the properties in this object in the file denoted
  153.         // if file is not given and the properties were loaded from a
  154.         // file prior, this method stores them in the file used by load()        
  155.         try {
  156.             $fw = new FileWriter($file);
  157.             if ($header !== null) {
  158.                 $fw->write( "# " . $header . PHP_EOL );
  159.             }
  160.             $fw->write($this->toString());
  161.             $fw->close();
  162.         } catch (IOException $e) {
  163.             throw new IOException("Error writing property file: " . $e->getMessage());
  164.         }                
  165.     }
  166.     
  167.     /**
  168.      * Returns copy of internal properties hash.
  169.      * Mostly for performance reasons, property hashes are often
  170.      * preferable to passing around objects.
  171.      *
  172.      * @return array
  173.      */
  174.     function getProperties() {
  175.         return $this->properties;
  176.     }
  177.     
  178.     /**
  179.      * Get value for specified property.
  180.      * This is the same as get() method.
  181.      *
  182.      * @param string $prop The property name (key).
  183.      * @return mixed
  184.      * @see get()
  185.      */
  186.     function getProperty($prop) {
  187.         if (!isset($this->properties[$prop])) {
  188.             return null;
  189.         }
  190.         return $this->properties[$prop];
  191.     }
  192.  
  193.     /**
  194.      * Get value for specified property.
  195.      * This function exists to provide a hashtable-like interface for
  196.      * properties.
  197.      *
  198.      * @param string $prop The property name (key).
  199.      * @return mixed
  200.      * @see getProperty()
  201.      */    
  202.     function get($prop) {
  203.          if (!isset($this->properties[$prop])) {
  204.             return null;
  205.         }
  206.         return $this->properties[$prop];
  207.     }
  208.     
  209.     /**
  210.      * Set the value for a property.
  211.      *
  212.      * @param string $key
  213.      * @param mixed $value
  214.      * @return mixed Old property value or NULL if none was set.
  215.      */
  216.     function setProperty($key, $value) {
  217.         $oldValue = @$this->properties[$key];       
  218.         $this->properties[$key] = $value;
  219.         return $oldValue;
  220.     }
  221.     
  222.     /**
  223.      * Set the value for a property.
  224.      * This function exists to provide hashtable-lie
  225.      * interface for properties.
  226.      *
  227.      * @param string $key
  228.      * @param mixed $value
  229.      */
  230.     function put($key, $value) {
  231.         return $this->setProperty($key, $value);
  232.     }
  233.     
  234.     /**
  235.      * Same as keys() function, returns an array of property names.
  236.      * @return array
  237.      */
  238.     function propertyNames() {
  239.         return $this->keys();
  240.     }
  241.     
  242.     /**
  243.      * Whether loaded properties array contains specified property name.
  244.      * @return boolean
  245.      */
  246.     function containsKey($key) {
  247.         return isset($this->properties[$key]);
  248.     }
  249.  
  250.     /**
  251.      * Returns properties keys.
  252.      * Use this for foreach() {} iterations, as this is
  253.      * faster than looping through property values.
  254.      * @return array
  255.      */
  256.     function keys() {
  257.         return array_keys($this->properties);
  258.     }
  259.     
  260.     /**
  261.      * Whether properties list is empty.
  262.      * @return boolean
  263.      */
  264.     function isEmpty() {
  265.         return empty($this->properties);
  266.     }
  267.  
  268. }
  269. ?>
  270.