Config
[ class tree: Config ] [ index: Config ] [ all elements ]

Source for file Container.php

Documentation is available at Container.php

  1. <?php
  2. // +---------------------------------------------------------------------+
  3. // | PHP Version 4                                                       |
  4. // +---------------------------------------------------------------------+
  5. // | Copyright (c) 1997, 1998, 1999, 2000, 2001 The PHP Group            |
  6. // +---------------------------------------------------------------------+
  7. // | This source file is subject to version 2.0 of the PHP license,      |
  8. // | that is bundled with this package in the file LICENSE, and is       |
  9. // | available at through the world-wide-web at                          |
  10. // | http://www.php.net/license/2_02.txt.                                |
  11. // | If you did not receive a copy of the PHP license and are unable to  |
  12. // | obtain it through the world-wide-web, please send a note to         |
  13. // | license@php.net so we can mail you a copy immediately.              |
  14. // +---------------------------------------------------------------------+
  15. // | Author: Bertrand Mansion <bmansion@mamasam.com>                     |
  16. // +---------------------------------------------------------------------+
  17. //
  18. // $Id: Container.php,v 1.40 2006/02/14 00:47:46 aashley Exp $
  19.  
  20. require_once 'Config.php';
  21.  
  22. /**
  23. * Interface for Config containers
  24. *
  25. @author   Bertrand Mansion <bmansion@mamasam.com>
  26. @package  Config
  27. */
  28.  
  29.     /**
  30.     * Container object type
  31.     * Ex: section, directive, comment, blank
  32.     * @var  string 
  33.     */
  34.     var $type;
  35.  
  36.     /**
  37.     * Container object name
  38.     * @var  string 
  39.     */
  40.     var $name = '';
  41.  
  42.     /**
  43.     * Container object content
  44.     * @var  string 
  45.     */
  46.     var $content = '';
  47.  
  48.     /**
  49.     * Container object children
  50.     * @var  array 
  51.     */
  52.     var $children = array();
  53.  
  54.     /**
  55.     * Reference to container object's parent
  56.     * @var  object 
  57.     */
  58.     var $parent;
  59.     
  60.     /**
  61.     * Array of attributes for this item
  62.     * @var  array 
  63.     */
  64.     var $attributes;
  65.  
  66.     /**
  67.     * Unique id to differenciate nodes
  68.     *
  69.     * This is used to compare nodes
  70.     * Will not be needed anymore when this class will use ZendEngine 2
  71.     *
  72.     * @var  int 
  73.     */
  74.     var $_id;
  75.  
  76.     /**
  77.     * Constructor
  78.     *
  79.     * @param  string  $type       Type of container object
  80.     * @param  string  $name       Name of container object
  81.     * @param  string  $content    Content of container object
  82.     * @param  array   $attributes Array of attributes for container object
  83.     */
  84.     function Config_Container($type 'section'$name ''$content ''$attributes = null)
  85.     {
  86.         $this->type       = $type;
  87.         $this->name       = $name;
  88.         $this->content    = $content;
  89.         $this->attributes = $attributes;
  90.         $this->parent     = null;
  91.         if (version_compare(PHP_VERSION'5.0.0''gt')) {
  92.             $this->_id    uniqid($name.$typetrue);
  93.         else {
  94.             $this->_id    uniqid(substr($name.$type0114)true);
  95.         }
  96.     // end constructor
  97.  
  98.     /**
  99.     * Create a child for this item.
  100.     * @param  string  $type       type of item: directive, section, comment, blank...
  101.     * @param  mixed   $item       item name
  102.     * @param  string  $content    item content
  103.     * @param  array   $attributes item attributes
  104.     * @param  string  $where      choose a position 'bottom', 'top', 'after', 'before'
  105.     * @param  object  $target     needed if you choose 'before' or 'after' for where
  106.     * @return object  reference to new item or Pear_Error
  107.     */
  108.     function &createItem($type$name$content$attributes = null$where 'bottom'$target = null)
  109.     {
  110.         $item =new Config_Container($type$name$content$attributes);
  111.         $result =$this->addItem($item$where$target);
  112.         return $result;
  113.     // end func &createItem
  114.     
  115.     /**
  116.     * Adds an item to this item.
  117.     * @param  object   $item      a container object
  118.     * @param  string   $where     choose a position 'bottom', 'top', 'after', 'before'
  119.     * @param  object   $target    needed if you choose 'before' or 'after' in $where
  120.     * @return mixed    reference to added container on success, Pear_Error on error
  121.     */
  122.     function &addItem(&$item$where 'bottom'$target = null)
  123.     {
  124.         if ($this->type != 'section'{
  125.             return PEAR::raiseError('Config_Container::addItem must be called on a section type object.'nullPEAR_ERROR_RETURN);
  126.         }
  127.         if (is_null($target)) {
  128.             $target =$this;
  129.         }
  130.         if (strtolower(get_class($target)) != 'config_container'{
  131.             return PEAR::raiseError('Target must be a Config_Container object in Config_Container::addItem.'nullPEAR_ERROR_RETURN);
  132.         }
  133.  
  134.         switch ($where{
  135.             case 'before':
  136.                 $index $target->getItemIndex();
  137.                 break;
  138.             case 'after':
  139.                 $index $target->getItemIndex()+1;
  140.                 break;
  141.             case 'top':
  142.                 $index = 0;
  143.                 break;
  144.             case 'bottom':
  145.                 $index = -1;
  146.                 break;
  147.             default:
  148.                 return PEAR::raiseError('Use only top, bottom, before or after in Config_Container::addItem.'nullPEAR_ERROR_RETURN);
  149.         }
  150.         if (isset($index&& $index >= 0{
  151.             array_splice($this->children$index0'tmp');
  152.         else {
  153.             $index count($this->children);
  154.         }
  155.         $this->children[$index=$item;
  156.         $this->children[$index]->parent =$this;
  157.  
  158.         return $item;
  159.     // end func addItem
  160.  
  161.     /**
  162.     * Adds a comment to this item.
  163.     * This is a helper method that calls createItem
  164.     *
  165.     * @param  string    $content        Object content
  166.     * @param  string    $where          Position : 'top', 'bottom', 'before', 'after'
  167.     * @param  object    $target         Needed when $where is 'before' or 'after'
  168.     * @return object  reference to new item or Pear_Error
  169.     */
  170.     function &createComment($content ''$where 'bottom'$target = null)
  171.     {
  172.         return $this->createItem('comment'null$contentnull$where$target);
  173.     // end func &createComment
  174.  
  175.     /**
  176.     * Adds a blank line to this item.
  177.     * This is a helper method that calls createItem
  178.     *
  179.     * @return object  reference to new item or Pear_Error
  180.     */
  181.     function &createBlank($where 'bottom'$target = null)
  182.     {
  183.         return $this->createItem('blank'nullnullnull$where$target);
  184.     // end func &createBlank
  185.  
  186.     /**
  187.     * Adds a directive to this item.
  188.     * This is a helper method that calls createItem
  189.     *
  190.     * @param  string    $name           Name of new directive
  191.     * @param  string    $content        Content of new directive
  192.     * @param  mixed     $attributes     Directive attributes
  193.     * @param  string    $where          Position : 'top', 'bottom', 'before', 'after'
  194.     * @param  object    $target         Needed when $where is 'before' or 'after'
  195.     * @return object  reference to new item or Pear_Error
  196.     */
  197.     function &createDirective($name$content$attributes = null$where 'bottom'$target = null)
  198.     {
  199.         return $this->createItem('directive'$name$content$attributes$where$target);
  200.     // end func &createDirective
  201.  
  202.     /**
  203.     * Adds a section to this item.
  204.     *
  205.     * This is a helper method that calls createItem
  206.     * If the section already exists, it won't create a new one.
  207.     * It will return reference to existing item.
  208.     *
  209.     * @param  string    $name           Name of new section
  210.     * @param  array     $attributes     Section attributes
  211.     * @param  string    $where          Position : 'top', 'bottom', 'before', 'after'
  212.     * @param  object    $target         Needed when $where is 'before' or 'after'
  213.     * @return object  reference to new item or Pear_Error
  214.     */
  215.     function &createSection($name$attributes = null$where 'bottom'$target = null)
  216.     {
  217.         return $this->createItem('section'$namenull$attributes$where$target);
  218.     // end func &createSection
  219.  
  220.     /**
  221.     * Tries to find the specified item(s) and returns the objects.
  222.     *
  223.     * Examples:
  224.     * $directives =& $obj->getItem('directive');
  225.     * $directive_bar_4 =& $obj->getItem('directive', 'bar', null, 4);
  226.     * $section_foo =& $obj->getItem('section', 'foo');
  227.     *
  228.     * This method can only be called on an object of type 'section'.
  229.     * Note that root is a section.
  230.     * This method is not recursive and tries to keep the current structure.
  231.     * For a deeper search, use searchPath()
  232.     *
  233.     * @param  string    $type        Type of item: directive, section, comment, blank...
  234.     * @param  mixed     $name        Item name
  235.     * @param  mixed     $content     Find item with this content
  236.     * @param  array     $attributes  Find item with attribute set to the given value
  237.     * @param  int       $index       Index of the item in the returned object list. If it is not set, will try to return the last item with this name.
  238.     * @return mixed  reference to item found or false when not found
  239.     * @see &searchPath()
  240.     */
  241.     function &getItem($type = null$name = null$content = null$attributes = null$index = -1)
  242.     {
  243.         if ($this->type != 'section'{
  244.             return PEAR::raiseError('Config_Container::getItem must be called on a section type object.'nullPEAR_ERROR_RETURN);
  245.         }
  246.         if (!is_null($type)) {
  247.             $testFields['type';
  248.         }
  249.         if (!is_null($name)) {
  250.             $testFields['name';
  251.         }
  252.         if (!is_null($content)) {
  253.             $testFields['content';
  254.         }
  255.         if (!is_null($attributes&& is_array($attributes)) {
  256.             $testFields['attributes';
  257.         }
  258.  
  259.         $itemsArr = array();
  260.         $fieldsToMatch count($testFields);
  261.         for ($i = 0$count count($this->children)$i $count$i++{
  262.             $match = 0;
  263.             reset($testFields);
  264.             foreach ($testFields as $field{
  265.                 if ($field != 'attributes'{
  266.                     if ($this->children[$i]->$field == ${$field}{
  267.                         $match++;
  268.                     }
  269.                 else {
  270.                     // Look for attributes in array
  271.                     $attrToMatch count($attributes);
  272.                     $attrMatch = 0;
  273.                     foreach ($attributes as $key => $value{
  274.                         if (isset($this->children[$i]->attributes[$key]&&
  275.                             $this->children[$i]->attributes[$key== $value{
  276.                             $attrMatch++;
  277.                         }
  278.                     }
  279.                     if ($attrMatch == $attrToMatch{
  280.                         $match++;
  281.                     }
  282.                 }
  283.             }
  284.             if ($match == $fieldsToMatch{
  285.                 $itemsArr[=$this->children[$i];
  286.             }
  287.         }
  288.         if ($index >= 0{
  289.             if (isset($itemsArr[$index])) {
  290.                 return $itemsArr[$index];
  291.             else {
  292.                 $return = false;
  293.                 return $return;
  294.             }
  295.         else {
  296.             if ($count count($itemsArr)) {
  297.                 return $itemsArr[$count-1];
  298.             else {
  299.                 $return = false;
  300.                 return $return;
  301.             }
  302.         }
  303.     // end func &getItem
  304.  
  305.     /**
  306.     * Finds a node using XPATH like format.
  307.     * 
  308.     * The search format is an array:
  309.     * array(item1, item2, item3, ...)
  310.     *
  311.     * Each item can be defined as the following:
  312.     * item = 'string' : will match the container named 'string'
  313.     * item = array('string', array('name' => 'xyz'))
  314.     * will match the container name 'string' whose attribute name is equal to "xyz"
  315.     * For example : <string name="xyz">
  316.     * 
  317.     * @param    mixed   Search path and attributes
  318.     * 
  319.     * @return   mixed   Config_Container object, array of Config_Container objects or false on failure.
  320.     * @access   public
  321.     */
  322.     function &searchPath($args)
  323.     {
  324.         if ($this->type != 'section'{
  325.             return PEAR::raiseError('Config_Container::searchPath must be called on a section type object.'nullPEAR_ERROR_RETURN);
  326.         }
  327.  
  328.         $arg array_shift($args);
  329.  
  330.         if (is_array($arg)) {
  331.             $name $arg[0];
  332.             $attributes $arg[1];
  333.         else {
  334.             $name $arg;
  335.             $attributes = null;
  336.         }
  337.         // find all the matches for first..
  338.         $match =$this->getItem(null$namenull$attributes);
  339.  
  340.         if (!$match{
  341.             $return = false;
  342.             return $return;
  343.         }
  344.         if (!empty($args)) {
  345.             return $match->searchPath($args);
  346.         }
  347.         return $match;
  348.     // end func &searchPath
  349.  
  350.     /**
  351.     * Return a child directive's content.
  352.     * 
  353.     * This method can use two different search approach, depending on
  354.     * the parameter it is given. If the parameter is an array, it will use
  355.     * the {@link Config_Container::searchPath()} method. If it is a string,
  356.     * it will use the {@link Config_Container::getItem()} method.
  357.     *
  358.     * Example:
  359.     * <code>
  360.     * require_once 'Config.php';
  361.     * $ini = new Config();
  362.     * $conf =& $ini->parseConfig('/path/to/config.ini', 'inicommented');
  363.     *
  364.     * // Will return the value found at :
  365.     * // [Database]
  366.     * // host=localhost
  367.     * echo $conf->directiveContent(array('Database', 'host')));
  368.     *
  369.     * // Will return the value found at :
  370.     * // date="dec-2004"
  371.     * echo $conf->directiveContent('date');
  372.     *
  373.     * </code>
  374.     *
  375.     * @param    mixed   Search path and attributes or a directive name
  376.     * @param    int     Index of the item in the returned directive list.
  377.     *                    Eventually used if args is a string.
  378.     * 
  379.     * @return   mixed   Content of directive or false if not found.
  380.     * @access   public
  381.     */
  382.     function directiveContent($args$index = -1)
  383.     {
  384.         if (is_array($args)) {
  385.             $item =$this->searchPath($args);
  386.         else {
  387.             $item =$this->getItem('directive'$argsnullnull$index);
  388.         }
  389.         if ($item{
  390.             return $item->getContent();
  391.         }
  392.         return false;
  393.     // end func getDirectiveContent
  394.  
  395.     /**
  396.     * Returns how many children this container has
  397.     *
  398.     * @param  string    $type    type of children counted
  399.     * @param  string    $name    name of children counted
  400.     * @return int  number of children found
  401.     */
  402.     function countChildren($type = null$name = null)
  403.     {
  404.         if (is_null($type&& is_null($name)) {
  405.             return count($this->children);
  406.         }
  407.         $count = 0;
  408.         if (isset($name&& isset($type)) {
  409.             for ($i = 0$children count($this->children)$i $children$i++{
  410.                 if ($this->children[$i]->name === $name && 
  411.                     $this->children[$i]->type == $type{
  412.                     $count++;
  413.                 }
  414.             }
  415.             return $count;
  416.         }
  417.         if (isset($type)) {
  418.             for ($i = 0$children count($this->children)$i $children$i++{
  419.                 if ($this->children[$i]->type == $type{
  420.                     $count++;
  421.                 }
  422.             }
  423.             return $count;
  424.         }
  425.         if (isset($name)) {
  426.             // Some directives can have the same name
  427.             for ($i = 0$children count($this->children)$i $children$i++{
  428.                 if ($this->children[$i]->name === $name{
  429.                     $count++;
  430.                 }
  431.             }
  432.             return $count;
  433.         }
  434.     // end func &countChildren
  435.  
  436.     /**
  437.     * Deletes an item (section, directive, comment...) from the current object
  438.     * TODO: recursive remove in sub-sections
  439.     * @return mixed  true if object was removed, false if not, or PEAR_Error if root
  440.     */
  441.     function removeItem()
  442.     {
  443.         if ($this->isRoot()) {
  444.             return PEAR::raiseError('Cannot remove root item in Config_Container::removeItem.'nullPEAR_ERROR_RETURN);
  445.         }
  446.         $index $this->getItemIndex();
  447.         if (!is_null($index)) {
  448.             array_splice($this->parent->children$index1);
  449.             return true;
  450.         }
  451.         return false;
  452.     // end func removeItem
  453.  
  454.     /**
  455.     * Returns the item index in its parent children array.
  456.     * @return int  returns int or null if root object
  457.     */
  458.     function getItemIndex()
  459.     {
  460.         if (is_object($this->parent)) {
  461.             // This will be optimized with Zend Engine 2
  462.             $pchildren =$this->parent->children;
  463.             for ($i = 0$count count($pchildren)$i $count$i++{
  464.                 if ($pchildren[$i]->_id == $this->_id{
  465.                     return $i;
  466.                 }
  467.             }
  468.         }
  469.         return;
  470.     // end func getItemIndex
  471.  
  472.     /**
  473.     * Returns the item rank in its parent children array
  474.     * according to other items with same type and name.
  475.     * @return int  returns int or null if root object
  476.     */
  477.     function getItemPosition()
  478.     {
  479.         if (is_object($this->parent)) {
  480.             $pchildren =$this->parent->children;
  481.             for ($i = 0$count count($pchildren)$i $count$i++{
  482.                 if ($pchildren[$i]->name == $this->name &&
  483.                     $pchildren[$i]->type == $this->type{
  484.                     $obj[=$pchildren[$i];
  485.                 }
  486.             }
  487.             for ($i = 0$count count($obj)$i $count$i++{
  488.                 if ($obj[$i]->_id == $this->_id{
  489.                     return $i;
  490.                 }
  491.             }
  492.         }
  493.         return;
  494.     // end func getItemPosition
  495.  
  496.     /**
  497.     * Returns the item parent object.
  498.     * @return object  returns reference to parent object or null if root object
  499.     */
  500.     function &getParent()
  501.     {
  502.         return $this->parent;
  503.     // end func &getParent
  504.  
  505.     /**
  506.     * Returns the item parent object.
  507.     * @return mixed  returns reference to child object or false if child does not exist
  508.     */
  509.     function &getChild($index = 0)
  510.     {
  511.         if (!empty($this->children[$index])) {
  512.             return $this->children[$index];
  513.         else {
  514.             return false;
  515.         }
  516.     // end func &getChild
  517.  
  518.     /**
  519.     * Set this item's name.
  520.     * @return void 
  521.     */
  522.     function setName($name)
  523.     {
  524.         $this->name = $name;
  525.     // end func setName
  526.  
  527.     /**
  528.     * Get this item's name.
  529.     * @return string    item's name
  530.     */
  531.     function getName()
  532.     {
  533.         return $this->name;
  534.     // end func getName
  535.  
  536.     /**
  537.     * Set this item's content.
  538.     * @return void 
  539.     */
  540.     function setContent($content)
  541.     {
  542.         $this->content = $content;
  543.     // end func setContent
  544.     
  545.     /**
  546.     * Get this item's content.
  547.     * @return string    item's content
  548.     */
  549.     function getContent()
  550.     {
  551.         return $this->content;
  552.     // end func getContent
  553.  
  554.     /**
  555.     * Set this item's type.
  556.     * @return void 
  557.     */
  558.     function setType($type)
  559.     {
  560.         $this->type = $type;
  561.     // end func setType
  562.  
  563.     /**
  564.     * Get this item's type.
  565.     * @return string    item's type
  566.     */
  567.     function getType()
  568.     {
  569.         return $this->type;
  570.     // end func getType
  571.  
  572.     /**
  573.     * Set this item's attributes.
  574.     * @param  array    $attributes        Array of attributes
  575.     * @return void 
  576.     */
  577.     function setAttributes($attributes)
  578.     {
  579.         $this->attributes = $attributes;
  580.     // end func setAttributes
  581.  
  582.     /**
  583.     * Set this item's attributes.
  584.     * @param  array    $attributes        Array of attributes
  585.     * @return void 
  586.     */
  587.     function updateAttributes($attributes)
  588.     {
  589.         if (is_array($attributes)) {
  590.             foreach ($attributes as $key => $value{
  591.                 $this->attributes[$key$value;
  592.             }
  593.         }
  594.     // end func updateAttributes
  595.  
  596.     /**
  597.     * Get this item's attributes.
  598.     * @return array    item's attributes
  599.     */
  600.     function getAttributes()
  601.     {
  602.         return $this->attributes;
  603.     // end func getAttributes
  604.     
  605.     /**
  606.     * Get one attribute value of this item
  607.     * @param  string   $attribute        Attribute key
  608.     * @return mixed    item's attribute value
  609.     */
  610.     function getAttribute($attribute)
  611.     {
  612.         if (isset($this->attributes[$attribute])) {
  613.             return $this->attributes[$attribute];
  614.         }
  615.         return null;
  616.     // end func getAttribute
  617.  
  618.     /**
  619.     * Set a children directive content.
  620.     * This is an helper method calling getItem and addItem or setContent for you.
  621.     * If the directive does not exist, it will be created at the bottom.
  622.     *
  623.     * @param  string    $name        Name of the directive to look for
  624.     * @param  mixed     $content     New content
  625.     * @param  int       $index       Index of the directive to set,
  626.     *                                 in case there are more than one directive
  627.     *                                 with the same name
  628.     * @return object    newly set directive
  629.     */
  630.     function &setDirective($name$content$index = -1)
  631.     {
  632.         $item =$this->getItem('directive'$namenullnull$index);
  633.         if ($item === false || PEAR::isError($item)) {
  634.             // Directive does not exist, will create one
  635.             unset($item);
  636.             return $this->createDirective($name$contentnull);
  637.         else {
  638.             // Change existing directive value
  639.             $item->setContent($content);
  640.             return $item;
  641.         }
  642.     // end func setDirective
  643.  
  644.     /**
  645.     * Is this item root, in a config container object
  646.     * @return bool    true if item is root
  647.     */
  648.     function isRoot()
  649.     {
  650.         if (is_null($this->parent)) {
  651.             return true;
  652.         }
  653.         return false;
  654.     // end func isRoot
  655.  
  656.     /**
  657.     * Call the toString methods in the container plugin
  658.     * @param    string  $configType  Type of configuration used to generate the string
  659.     * @param    array   $options     Specify special options used by the parser
  660.     * @return   mixed   true on success or PEAR_ERROR
  661.     */
  662.     function toString($configType$options = array())
  663.     {
  664.         $configType strtolower($configType);
  665.         if (!isset($GLOBALS['CONFIG_TYPES'][$configType])) {
  666.             return PEAR::raiseError("Configuration type '$configType' is not registered in Config_Container::toString."nullPEAR_ERROR_RETURN);
  667.         }
  668.         $includeFile $GLOBALS['CONFIG_TYPES'][$configType][0];
  669.         $className   $GLOBALS['CONFIG_TYPES'][$configType][1];
  670.         include_once($includeFile);
  671.         $renderer = new $className($options);
  672.         return $renderer->toString($this);
  673.     // end func toString
  674.  
  675.     /**
  676.     * Returns a key/value pair array of the container and its children.
  677.     *
  678.     * Format : section[directive][index] = value
  679.     * If the container has attributes, it will use '@' and '#'
  680.     * index is here because multiple directives can have the same name.
  681.     *
  682.     * @param    bool    $useAttr        Whether to return the attributes too
  683.     * @return array 
  684.     */
  685.     function toArray($useAttr = true)
  686.     {
  687.         $array[$this->name= array();
  688.         switch ($this->type{
  689.             case 'directive':
  690.                 if ($useAttr && count($this->attributes> 0{
  691.                     $array[$this->name]['#'$this->content;
  692.                     $array[$this->name]['@'$this->attributes;
  693.                 else {
  694.                     $array[$this->name$this->content;
  695.                 }
  696.                 break;
  697.             case 'section':
  698.                 if ($useAttr && count($this->attributes> 0{
  699.                     $array[$this->name]['@'$this->attributes;
  700.                 }
  701.                 if ($count count($this->children)) {
  702.                     for ($i = 0; $i $count$i++{
  703.                         $newArr $this->children[$i]->toArray($useAttr);
  704.                         if (!is_null($newArr)) {
  705.                             foreach ($newArr as $key => $value{
  706.                                 if (isset($array[$this->name][$key])) {
  707.                                     // duplicate name/type
  708.                                     if (!is_array($array[$this->name][$key]||
  709.                                         !isset($array[$this->name][$key][0])) {
  710.                                         $old $array[$this->name][$key];
  711.                                         unset($array[$this->name][$key]);
  712.                                         $array[$this->name][$key][0$old;
  713.                                     }
  714.                                     $array[$this->name][$key][$value;
  715.                                 else {
  716.                                     $array[$this->name][$key$value;
  717.                                 }
  718.                             }
  719.                         }
  720.                     }
  721.                 }
  722.                 break;
  723.             default:
  724.                 return null;
  725.         }
  726.         return $array;
  727.     // end func toArray
  728.     
  729.     /**
  730.     * Writes the configuration to a file
  731.     * 
  732.     * @param  mixed  $datasrc        Info on datasource such as path to the configuraton file or dsn...
  733.     * @param  string $configType     Type of configuration
  734.     * @param  array  $options        Options for writer
  735.     * @access public
  736.     * @return mixed     true on success or PEAR_ERROR
  737.     */
  738.     function writeDatasrc($datasrc$configType$options = array())
  739.     {
  740.         $configType strtolower($configType);
  741.         if (!isset($GLOBALS['CONFIG_TYPES'][$configType])) {
  742.             return PEAR::raiseError("Configuration type '$configType' is not registered in Config_Container::writeDatasrc."nullPEAR_ERROR_RETURN);
  743.         }
  744.         $includeFile $GLOBALS['CONFIG_TYPES'][$configType][0];
  745.         $className $GLOBALS['CONFIG_TYPES'][$configType][1];
  746.         include_once($includeFile);
  747.  
  748.         $writeMethodName (version_compare(phpversion()'5''<')) 'writedatasrc' 'writeDatasrc';
  749.         if (in_array($writeMethodNameget_class_methods($className))) {
  750.             $writer = new $className($options);
  751.             return $writer->writeDatasrc($datasrc$this);
  752.         }
  753.  
  754.         // Default behaviour
  755.         $fp @fopen($datasrc'w');
  756.         if ($fp{
  757.             $string $this->toString($configType$options);
  758.             $len strlen($string);
  759.             @flock($fpLOCK_EX);
  760.             @fwrite($fp$string$len);
  761.             @flock($fpLOCK_UN);
  762.             @fclose($fp);
  763.             return true;
  764.         else {
  765.             return PEAR::raiseError('Cannot open datasource for writing.'1PEAR_ERROR_RETURN);
  766.         }
  767.     // end func writeDatasrc
  768. // end class Config_Container
  769. ?>

Documentation generated on Mon, 11 Mar 2019 14:36:35 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.