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 306597 2010-12-24 05:11:09Z aharvey $
  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.     * @param bool  count items differently by type
  476.     * @return int  returns int or null if root object
  477.     */
  478.     function getItemPosition($byType = true)
  479.     {
  480.         if (is_object($this->parent)) {
  481.             $pchildren =$this->parent->children;
  482.             for ($i = 0$count count($pchildren)$i $count$i++{
  483.                 if ($pchildren[$i]->name == $this->name{
  484.                     if ($byType == true{
  485.                         if ($pchildren[$i]->type == $this->type{
  486.                             $obj[=$pchildren[$i];
  487.                         }
  488.                     else {
  489.                         $obj[=$pchildren[$i];
  490.                     }
  491.                 }
  492.             }
  493.             for ($i = 0$count count($obj)$i $count$i++{
  494.                 if ($obj[$i]->_id == $this->_id{
  495.                     return $i;
  496.                 }
  497.             }
  498.         }
  499.         return;
  500.     // end func getItemPosition
  501.  
  502.     /**
  503.     * Returns the item parent object.
  504.     * @return object  returns reference to parent object or null if root object
  505.     */
  506.     function &getParent()
  507.     {
  508.         return $this->parent;
  509.     // end func &getParent
  510.  
  511.     /**
  512.     * Returns the item parent object.
  513.     * @return mixed  returns reference to child object or false if child does not exist
  514.     */
  515.     function &getChild($index = 0)
  516.     {
  517.         if (!empty($this->children[$index])) {
  518.             return $this->children[$index];
  519.         else {
  520.             return false;
  521.         }
  522.     // end func &getChild
  523.  
  524.     /**
  525.     * Set this item's name.
  526.     * @return void 
  527.     */
  528.     function setName($name)
  529.     {
  530.         $this->name = $name;
  531.     // end func setName
  532.  
  533.     /**
  534.     * Get this item's name.
  535.     * @return string    item's name
  536.     */
  537.     function getName()
  538.     {
  539.         return $this->name;
  540.     // end func getName
  541.  
  542.     /**
  543.     * Set this item's content.
  544.     * @return void 
  545.     */
  546.     function setContent($content)
  547.     {
  548.         $this->content = $content;
  549.     // end func setContent
  550.     
  551.     /**
  552.     * Get this item's content.
  553.     * @return string    item's content
  554.     */
  555.     function getContent()
  556.     {
  557.         return $this->content;
  558.     // end func getContent
  559.  
  560.     /**
  561.     * Set this item's type.
  562.     * @return void 
  563.     */
  564.     function setType($type)
  565.     {
  566.         $this->type = $type;
  567.     // end func setType
  568.  
  569.     /**
  570.     * Get this item's type.
  571.     * @return string    item's type
  572.     */
  573.     function getType()
  574.     {
  575.         return $this->type;
  576.     // end func getType
  577.  
  578.     /**
  579.     * Set this item's attributes.
  580.     * @param  array    $attributes        Array of attributes
  581.     * @return void 
  582.     */
  583.     function setAttributes($attributes)
  584.     {
  585.         $this->attributes = $attributes;
  586.     // end func setAttributes
  587.  
  588.     /**
  589.     * Set this item's attributes.
  590.     * @param  array    $attributes        Array of attributes
  591.     * @return void 
  592.     */
  593.     function updateAttributes($attributes)
  594.     {
  595.         if (is_array($attributes)) {
  596.             foreach ($attributes as $key => $value{
  597.                 $this->attributes[$key$value;
  598.             }
  599.         }
  600.     // end func updateAttributes
  601.  
  602.     /**
  603.     * Get this item's attributes.
  604.     * @return array    item's attributes
  605.     */
  606.     function getAttributes()
  607.     {
  608.         return $this->attributes;
  609.     // end func getAttributes
  610.     
  611.     /**
  612.     * Get one attribute value of this item
  613.     * @param  string   $attribute        Attribute key
  614.     * @return mixed    item's attribute value
  615.     */
  616.     function getAttribute($attribute)
  617.     {
  618.         if (isset($this->attributes[$attribute])) {
  619.             return $this->attributes[$attribute];
  620.         }
  621.         return null;
  622.     // end func getAttribute
  623.  
  624.     /**
  625.     * Set a children directive content.
  626.     * This is an helper method calling getItem and addItem or setContent for you.
  627.     * If the directive does not exist, it will be created at the bottom.
  628.     *
  629.     * @param  string    $name        Name of the directive to look for
  630.     * @param  mixed     $content     New content
  631.     * @param  int       $index       Index of the directive to set,
  632.     *                                 in case there are more than one directive
  633.     *                                 with the same name
  634.     * @return object    newly set directive
  635.     */
  636.     function &setDirective($name$content$index = -1)
  637.     {
  638.         $item =$this->getItem('directive'$namenullnull$index);
  639.         if ($item === false || PEAR::isError($item)) {
  640.             // Directive does not exist, will create one
  641.             unset($item);
  642.             return $this->createDirective($name$contentnull);
  643.         else {
  644.             // Change existing directive value
  645.             $item->setContent($content);
  646.             return $item;
  647.         }
  648.     // end func setDirective
  649.  
  650.     /**
  651.     * Is this item root, in a config container object
  652.     * @return bool    true if item is root
  653.     */
  654.     function isRoot()
  655.     {
  656.         if (is_null($this->parent)) {
  657.             return true;
  658.         }
  659.         return false;
  660.     // end func isRoot
  661.  
  662.     /**
  663.     * Call the toString methods in the container plugin
  664.     * @param    string  $configType  Type of configuration used to generate the string
  665.     * @param    array   $options     Specify special options used by the parser
  666.     * @return   mixed   true on success or PEAR_ERROR
  667.     */
  668.     function toString($configType$options = array())
  669.     {
  670.         $configType strtolower($configType);
  671.         if (!isset($GLOBALS['CONFIG_TYPES'][$configType])) {
  672.             return PEAR::raiseError("Configuration type '$configType' is not registered in Config_Container::toString."nullPEAR_ERROR_RETURN);
  673.         }
  674.         $includeFile $GLOBALS['CONFIG_TYPES'][$configType][0];
  675.         $className   $GLOBALS['CONFIG_TYPES'][$configType][1];
  676.         include_once($includeFile);
  677.         $renderer = new $className($options);
  678.         return $renderer->toString($this);
  679.     // end func toString
  680.  
  681.     /**
  682.     * Returns a key/value pair array of the container and its children.
  683.     *
  684.     * Format : section[directive][index] = value
  685.     * If the container has attributes, it will use '@' and '#'
  686.     * index is here because multiple directives can have the same name.
  687.     *
  688.     * @param    bool    $useAttr        Whether to return the attributes too
  689.     * @return array 
  690.     */
  691.     function toArray($useAttr = true)
  692.     {
  693.         $array[$this->name= array();
  694.         switch ($this->type{
  695.             case 'directive':
  696.                 if ($useAttr && count($this->attributes> 0{
  697.                     $array[$this->name]['#'$this->content;
  698.                     $array[$this->name]['@'$this->attributes;
  699.                 else {
  700.                     $array[$this->name$this->content;
  701.                 }
  702.                 break;
  703.             case 'section':
  704.                 if ($useAttr && count($this->attributes> 0{
  705.                     $array[$this->name]['@'$this->attributes;
  706.                 }
  707.                 if ($count count($this->children)) {
  708.                     for ($i = 0; $i $count$i++{
  709.                         $newArr $this->children[$i]->toArray($useAttr);
  710.                         if (!is_null($newArr)) {
  711.                             foreach ($newArr as $key => $value{
  712.                                 if (isset($array[$this->name][$key])) {
  713.                                     // duplicate name/type
  714.                                     if (!is_array($array[$this->name][$key]||
  715.                                         !isset($array[$this->name][$key][0])) {
  716.                                         $old $array[$this->name][$key];
  717.                                         unset($array[$this->name][$key]);
  718.                                         $array[$this->name][$key][0$old;
  719.                                     }
  720.                                     $array[$this->name][$key][$value;
  721.                                 else {
  722.                                     $array[$this->name][$key$value;
  723.                                 }
  724.                             }
  725.                         }
  726.                     }
  727.                 }
  728.                 break;
  729.             default:
  730.                 return null;
  731.         }
  732.         return $array;
  733.     // end func toArray
  734.     
  735.     /**
  736.     * Writes the configuration to a file
  737.     * 
  738.     * @param  mixed  $datasrc        Info on datasource such as path to the configuraton file or dsn...
  739.     * @param  string $configType     Type of configuration
  740.     * @param  array  $options        Options for writer
  741.     * @access public
  742.     * @return mixed     true on success or PEAR_ERROR
  743.     */
  744.     function writeDatasrc($datasrc$configType$options = array())
  745.     {
  746.         $configType strtolower($configType);
  747.         if (!isset($GLOBALS['CONFIG_TYPES'][$configType])) {
  748.             return PEAR::raiseError("Configuration type '$configType' is not registered in Config_Container::writeDatasrc."nullPEAR_ERROR_RETURN);
  749.         }
  750.         $includeFile $GLOBALS['CONFIG_TYPES'][$configType][0];
  751.         $className $GLOBALS['CONFIG_TYPES'][$configType][1];
  752.         include_once($includeFile);
  753.  
  754.         $writeMethodName (version_compare(phpversion()'5''<')) 'writedatasrc' 'writeDatasrc';
  755.         if (in_array($writeMethodNameget_class_methods($className))) {
  756.             $writer = new $className($options);
  757.             return $writer->writeDatasrc($datasrc$this);
  758.         }
  759.  
  760.         // Default behaviour
  761.         $fp @fopen($datasrc'w');
  762.         if ($fp{
  763.             $string $this->toString($configType$options);
  764.             $len strlen($string);
  765.             @flock($fpLOCK_EX);
  766.             @fwrite($fp$string$len);
  767.             @flock($fpLOCK_UN);
  768.             @fclose($fp);
  769.             return true;
  770.         else {
  771.             return PEAR::raiseError('Cannot open datasource for writing.'1PEAR_ERROR_RETURN);
  772.         }
  773.     // end func writeDatasrc
  774. // end class Config_Container
  775. ?>

Documentation generated on Mon, 11 Mar 2019 15:42:04 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.