Support Joomla!

Joomla! 1.5 Documentation

Packages

Package: Joomla-Framework

Developer Network License

The Joomla! Developer Network content is © copyright 2006 by the individual contributors and can be used in accordance with the Creative Commons License, Attribution- NonCommercial- ShareAlike 2.5
Source code for file /joomla/registry/registry.php

Documentation is available at registry.php

  1. <?php
  2. /**
  3.  * @version        $Id: registry.php 6472 2007-02-03 10:47:26Z pasamio $
  4.  * @package        Joomla.Framework
  5.  * @subpackage    Registry
  6.  * @copyright    Copyright (C) 2005 - 2007 Open Source Matters. All rights reserved.
  7.  * @license        GNU/GPL, see LICENSE.php
  8.  *  Joomla! is free software. This version may have been modified pursuant
  9.  *  to the GNU General Public License, and as distributed it includes or
  10.  *  is derivative of works licensed under the GNU General Public License or
  11.  *  other free or open source software licenses.
  12.  *  See COPYRIGHT.php for copyright notices and details.
  13.  */
  14.  
  15. // Check to ensure this file is within the rest of the framework
  16. defined('JPATH_BASE'or die();
  17.  
  18. jimport'joomla.registry.format' );
  19.  
  20. /**
  21.  * JRegistry class
  22.  *
  23.  * @author         Louis Landry <[email protected]>
  24.  * @package     Joomla.Framework
  25.  * @subpackage    Registry
  26.  * @since         1.5
  27.  */
  28. class JRegistry extends JObject
  29. {
  30.     /**
  31.      * Default NameSpace
  32.      * @var string 
  33.      */
  34.     var $_defaultNameSpace = null;
  35.  
  36.     /**
  37.      * Registry Object
  38.      *  - actually an array of namespace objects
  39.      * @var array 
  40.      */
  41.     var $_registry = array ();
  42.  
  43.     /**
  44.      * Constructor
  45.      *
  46.      * @access    protected
  47.      * @param    string    $namespace    Default registry namespace
  48.      * @return    void 
  49.      * @since    1.5
  50.      */
  51.     function __construct($namespace 'default')
  52.     {
  53.         $this->_defaultNameSpace = $namespace;
  54.         $this->makeNameSpace($namespace);
  55.     }
  56.  
  57.     /**
  58.      * Returns a reference to a global JRegistry object, only creating it
  59.      * if it doesn't already exist.
  60.      *
  61.      * This method must be invoked as:
  62.      *         <pre>$registry =& JRegistry::getInstance($id[, $namespace]);</pre>
  63.      *
  64.      * @static
  65.      * @param    string    $id            An ID for the registry instance
  66.      * @param    string    $namespace    The default namespace for the registry object [optional]
  67.      * @return    object    The JRegistry object.
  68.      * @since    1.5
  69.      */
  70.     function &getInstance($id$namespace 'default')
  71.     {
  72.         static $instances;
  73.  
  74.         if (!isset ($instances)) {
  75.             $instances array ();
  76.         }
  77.  
  78.         if (empty ($instances[$id])) {
  79.             $instances[$idnew JRegistry($namespace);
  80.         }
  81.  
  82.         return $instances[$id];
  83.     }
  84.  
  85.     /**
  86.      * Create a namespace
  87.      *
  88.      * @access    public
  89.      * @param    string    $namespace    Name of the namespace to create
  90.      * @return    boolean    True on success
  91.      * @since    1.5
  92.      */
  93.     function makeNameSpace($namespace)
  94.     {
  95.         $this->_registry[$namespacearray('data' => new stdClass());
  96.         return true;
  97.     }
  98.  
  99.     /**
  100.      * Get the list of namespaces
  101.      *
  102.      * @access    public
  103.      * @return    array    List of namespaces
  104.      * @since    1.5
  105.      */
  106.     function getNameSpaces()
  107.     {
  108.         return array_keys($this->_registry);
  109.     }
  110.  
  111.     /**
  112.      * Get a registry value
  113.      *
  114.      * @access    public
  115.      * @param    string    $regpath    Registry path (e.g. joomla.content.showauthor)
  116.      * @param    mixed    $default    Optional default value
  117.      * @return    mixed    Value of entry or null
  118.      * @since    1.5
  119.      */
  120.     function getValue($regpath$default=null)
  121.     {
  122.         $result $default;
  123.  
  124.         // Explode the registry path into an array
  125.         if ($nodes explode('.'$regpath))
  126.         {
  127.             // Get the namespace
  128.             //$namespace = array_shift($nodes);
  129.             if (count($nodes)<2{
  130.                 $namespace    $this->_defaultNameSpace;
  131.                 $nodes[1]    $nodes[0];
  132.             else {
  133.                 $namespace $nodes[0];
  134.             }
  135.  
  136.             if (isset($this->_registry[$namespace])) {
  137.                 $ns $this->_registry[$namespace]['data'];
  138.                 $pathNodes count($nodes1;
  139.  
  140.                 //for ($i = 0; $i < $pathNodes; $i ++) {
  141.                 for ($i 1$i $pathNodes$i ++{
  142.                     $ns =$ns->$nodes[$i];
  143.                 }
  144.  
  145.                 if(isset($ns->$nodes[$i])) {
  146.                     $result $ns->$nodes[$i];
  147.                 }
  148.             }
  149.         }
  150.         return $result;
  151.     }
  152.  
  153.     /**
  154.      * Set a registry value
  155.      *
  156.      * @access    public
  157.      * @param    string    $regpath     Registry Path (e.g. joomla.content.showauthor)
  158.      * @param     mixed    $value        Value of entry
  159.      * @return     mixed    Value of old value or boolean false if operation failed
  160.      * @since    1.5
  161.      */
  162.     function setValue($regpath$value)
  163.     {
  164.         // Explode the registry path into an array
  165.         $nodes explode('.'$regpath);
  166.  
  167.         // Get the namespace
  168.         if (count($nodes)<2{
  169.             $namespace $this->_defaultNameSpace;
  170.         else {
  171.             $namespace array_shift($nodes);
  172.         }
  173.  
  174.         if (!isset($this->_registry[$namespace])) {
  175.             $this->makeNameSpace($namespace);
  176.         }
  177.  
  178.         $ns $this->_registry[$namespace]['data'];
  179.  
  180.         $pathNodes count($nodes1;
  181.  
  182.         if ($pathNodes 0{
  183.             $pathNodes 0;
  184.         }
  185.  
  186.         for ($i 0$i $pathNodes$i ++)
  187.         {
  188.             // If any node along the registry path does not exist, create it
  189.             if (!isset($ns->$nodes[$i])) {
  190.                 $ns->$nodes[$inew stdClass();
  191.             }
  192.             $ns =$ns->$nodes[$i];
  193.         }
  194.  
  195.         // Get the old value if exists so we can return it
  196.         @$retval =$ns->$nodes[$i];
  197.         $ns->$nodes[$i=$value;
  198.  
  199.         return $retval;
  200.     }
  201.  
  202.     /**
  203.      * Load a associative array of values into the default namespace
  204.      *
  205.      * @access    public
  206.      * @param    array    $array        Associative array of value to load
  207.      * @param    string    $namepsace     The name of the namespace
  208.      * @return    boolean    True on success
  209.      * @since    1.5
  210.      */
  211.     function loadArray($array$namespace null)
  212.     {
  213.         // If namespace is not set, get the default namespace
  214.         if ($namespace == null{
  215.             $namespace $this->_defaultNameSpace;
  216.         }
  217.  
  218.         if (!isset($this->_registry[$namespace])) {
  219.             // If namespace does not exist, make it and load the data
  220.             $this->makeNameSpace($namespace);
  221.         }
  222.  
  223.         // Load the variables into the registry's default namespace.
  224.         foreach ($array as $k => $v)
  225.         {
  226.             $this->_registry[$namespace]['data']->$k $v;
  227.         }
  228.  
  229.         return true;
  230.     }
  231.  
  232.     /**
  233.      * Load the public variables of the object into the default namespace.
  234.      *
  235.      * @access    public
  236.      * @param    object    $object        The object holding the public vars to load
  237.      * @param    string    $namespace     Namespace to load the INI string into [optional]
  238.      * @return    boolean    True on success
  239.      * @since    1.5
  240.      */
  241.     function loadObject(&$object$namespace null)
  242.     {
  243.         // If namespace is not set, get the default namespace
  244.         if ($namespace == null{
  245.             $namespace $this->_defaultNameSpace;
  246.         }
  247.  
  248.         if (!isset($this->_registry[$namespace])) {
  249.             // If namespace does not exist, make it and load the data
  250.             $this->makeNameSpace($namespace);
  251.         }
  252.  
  253.         /*
  254.          * We want to leave groups that are already in the namespace and add the
  255.          * groups loaded into the namespace.  This overwrites any existing group
  256.          * with the same name
  257.          */
  258.         foreach (get_object_vars($objectas $k => $v{
  259.             if (substr($k0,1!= '_' || $k == '_name'{
  260.                 $this->_registry[$namespace]['data']->$k $v;
  261.             }
  262.         }
  263.  
  264.         return true;
  265.     }
  266.  
  267.     /**
  268.      * Load the contents of a file into the registry
  269.      *
  270.      * @access    public
  271.      * @param    string    $file        Path to file to load
  272.      * @param    string    $format        Format of the file [optional: defaults to INI]
  273.      * @param    string    $namespace    Namespace to load the INI string into [optional]
  274.      * @return    boolean    True on success
  275.      * @since    1.5
  276.      */
  277.     function loadFile($file$format 'INI'$namespace null)
  278.     {
  279.         // Load a file into the given namespace [or default namespace if not given]
  280.         $handler =$this->_loadFormat($format);
  281.  
  282.         // If namespace is not set, get the default namespace
  283.         if ($namespace == null{
  284.             $namespace $this->_defaultNameSpace;
  285.         }
  286.  
  287.         // Get the contents of the file
  288.         jimport('joomla.filesystem.file');
  289.         $data JFile::read($file);
  290.  
  291.         if (!isset($this->_registry[$namespace]))
  292.         {
  293.             // If namespace does not exist, make it and load the data
  294.             $this->makeNameSpace($namespace);
  295.             $this->_registry[$namespace]['data'$handler->stringToObject($data);
  296.         }
  297.         else
  298.         {
  299.             // Get the data in object format
  300.             $ns $handler->stringToObject($data);
  301.  
  302.             /*
  303.              * We want to leave groups that are already in the namespace and add the
  304.              * groups loaded into the namespace.  This overwrites any existing group
  305.              * with the same name
  306.              */
  307.             foreach (get_object_vars($nsas $k => $v{
  308.                 $this->_registry[$namespace]['data']->$k $v;
  309.             }
  310.         }
  311.     }
  312.  
  313.     /**
  314.      * Load an XML string into the registry into the given namespace [or default if a namespace is not given]
  315.      *
  316.      * @access    public
  317.      * @param    string    $data        XML formatted string to load into the registry
  318.      * @param    string    $namespace    Namespace to load the XML string into [optional]
  319.      * @return    boolean    True on success
  320.      * @since    1.5
  321.      */
  322.     function loadXML($data$namespace null)
  323.     {
  324.         // Load a string into the given namespace [or default namespace if not given]
  325.         $handler =JRegistryFormat::getInstance('XML');
  326.  
  327.         // If namespace is not set, get the default namespace
  328.         if ($namespace == null{
  329.             $namespace $this->_defaultNameSpace;
  330.         }
  331.  
  332.         if (!isset($this->_registry[$namespace])) {
  333.             // If namespace does not exist, make it and load the data
  334.             $this->makeNameSpace($namespace);
  335.             $this->_registry[$namespace]['data'=$handler->stringToObject($data);
  336.         else {
  337.             // Get the data in object format
  338.             $ns =$handler->stringToObject($data);
  339.  
  340.             /*
  341.              * We want to leave groups that are already in the namespace and add the
  342.              * groups loaded into the namespace.  This overwrites any existing group
  343.              * with the same name
  344.              */
  345.             foreach (get_object_vars($nsas $k => $v{
  346.                 $this->_registry[$namespace]['data']->$k $v;
  347.             }
  348.         }
  349.     }
  350.  
  351.     /**
  352.      * Load an INI string into the registry into the given namespace [or default if a namespace is not given]
  353.      *
  354.      * @access    public
  355.      * @param    string    $data        INI formatted string to load into the registry
  356.      * @param    string    $namespace    Namespace to load the INI string into [optional]
  357.      * @return    boolean True on success
  358.      * @since    1.5
  359.      */
  360.     function loadINI($data$namespace null)
  361.     {
  362.         // Load a string into the given namespace [or default namespace if not given]
  363.         $handler =JRegistryFormat::getInstance('INI');
  364.  
  365.         // If namespace is not set, get the default namespace
  366.         if ($namespace == null{
  367.             $namespace $this->_defaultNameSpace;
  368.         }
  369.  
  370.         if (!isset($this->_registry[$namespace])) {
  371.             // If namespace does not exist, make it and load the data
  372.             $this->makeNameSpace($namespace);
  373.             $this->_registry[$namespace]['data'=$handler->stringToObject($data);
  374.         else {
  375.             // Get the data in object format
  376.             $ns $handler->stringToObject($data);
  377.  
  378.             /*
  379.              * We want to leave groups that are already in the namespace and add the
  380.              * groups loaded into the namespace.  This overwrites any existing group
  381.              * with the same name
  382.              */
  383.             foreach (get_object_vars($nsas $k => $v{
  384.                 $this->_registry[$namespace]['data']->$k $v;
  385.             }
  386.         }
  387.     }
  388.  
  389.     /**
  390.      * Merge a JRegistry object into this one
  391.      *
  392.      * @access    public
  393.      * @param    object    $source    Source JRegistry object ot merge
  394.      * @return    boolean    True on success
  395.      * @since    1.5
  396.      */
  397.     function merge(&$source)
  398.     {
  399.         if (is_a($source'JRegistry')) {
  400.             $sns $source->getNameSpaces();
  401.             foreach ($sns as $ns)
  402.             {
  403.                 if (!isset($this->_registry[$ns])) {
  404.                     // If namespace does not exist, make it and load the data
  405.                     $this->makeNameSpace($ns);
  406.                 }
  407.  
  408.                 // Load the variables into the registry's default namespace.
  409.                 foreach ($source->toArray($nsas $k => $v{
  410.                     $this->_registry[$ns]['data']->$k $v;
  411.                 }
  412.             }
  413.             return true;
  414.         }
  415.         return false;
  416.     }
  417.  
  418.     /**
  419.      * Get a namespace in a given string format
  420.      *
  421.      * @access    public
  422.      * @param    string    $format        Format to return the string in
  423.      * @param    string    $namespace    Namespace to return [optional: null returns the default namespace]
  424.      * @param    mixed    $params        Parameters used by the formatter, see formatters for more info
  425.      * @return    string    Namespace in string format
  426.      * @since    1.5
  427.      */
  428.     function toString($format 'INI'$namespace null$params null)
  429.     {
  430.         jimport('joomla.registry.format');
  431.         // Return a namespace in a given format
  432.         $handler =JRegistryFormat::getInstance($format);
  433.  
  434.         // If namespace is not set, get the default namespace
  435.         if ($namespace == null{
  436.             $namespace $this->_defaultNameSpace;
  437.         }
  438.  
  439.         // Get the namespace
  440.         $ns $this->_registry[$namespace]['data'];
  441.  
  442.         return $handler->objectToString($ns$params);
  443.     }
  444.  
  445.     /**
  446.      * Transforms a namespace to an array
  447.      *
  448.      * @access    public
  449.      * @param    string    $namespace    Namespace to return [optional: null returns the default namespace]
  450.      * @return    array    An associative array holding the namespace data
  451.      * @since    1.5
  452.      */
  453.     function toArray($namespace null)
  454.     {
  455.         // If namespace is not set, get the default namespace
  456.         if ($namespace == null{
  457.             $namespace $this->_defaultNameSpace;
  458.         }
  459.  
  460.         // Get the namespace
  461.         $ns $this->_registry[$namespace]['data'];
  462.  
  463.         $array array();
  464.         foreach (get_object_vars$ns as $k => $v{
  465.             $array[$k$v;
  466.         }
  467.  
  468.         return $array;
  469.     }
  470.  
  471.     /**
  472.      * Transforms a namespace to an object
  473.      *
  474.      * @access    public
  475.      * @param    string    $namespace    Namespace to return [optional: null returns the default namespace]
  476.      * @return    object    An an object holding the namespace data
  477.      * @since    1.5
  478.      */
  479.     function toObject($namespace null)
  480.     {
  481.         // If namespace is not set, get the default namespace
  482.         if ($namespace == null{
  483.             $namespace $this->_defaultNameSpace;
  484.         }
  485.  
  486.         // Get the namespace
  487.         $ns $this->_registry[$namespace]['data'];
  488.  
  489.         return $ns;
  490.     }
  491.  
  492.     /**
  493.      * Return the relevant JRegistryFormat object
  494.      *
  495.      * @access    private
  496.      * @param    string    $format    The format to return
  497.      * @return    object    Formatting object
  498.      */
  499.     function &_loadFormat($format)
  500.     {
  501.         $lformat JString::strtolower($format);
  502.         if(jimport('joomla.registry.format.'.$lformat))
  503.         {
  504.             $return null;
  505.             $class =  'JRegistryFormat'.$format;
  506.             $return new $class();
  507.             return $return;
  508.         else {
  509.             JError::raiseError(500,JText::_('Unable to load format'));
  510.         }
  511.     }
  512. }
  513. ?>

Documentation generated on Mon, 05 Mar 2007 21:20:05 +0000 by phpDocumentor 1.3.1