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

Documentation generated on Mon, 11 Mar 2019 13:57:20 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.