Support Joomla!

Packages

Package: Joomla-Framework

License

Content on this site is copyright © 2005 - 2008 Open Source Matters Inc and can be used in accordance with the Joomla! Electronic Documentation License. Some parts of this website may be subject to other licenses.
Source code for file /joomla/registry/registry.php

Documentation is available at registry.php

  1. <?php
  2. /**
  3.  * @version        $Id: registry.php 10815 2008-08-27 01:15:56Z tcp $
  4.  * @package        Joomla.Framework
  5.  * @subpackage    Registry
  6.  * @copyright    Copyright (C) 2005 - 2008 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. //Register the session storage class with the loader
  19. JLoader::register('JRegistryFormat'dirname(__FILE__).DS.'format.php');
  20.  
  21. /**
  22.  * JRegistry class
  23.  *
  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.             $count count($nodes);
  130.             if ($count 2{
  131.                 $namespace    $this->_defaultNameSpace;
  132.                 $nodes[1]    $nodes[0];
  133.             else {
  134.                 $namespace $nodes[0];
  135.             }
  136.  
  137.             if (isset($this->_registry[$namespace])) {
  138.                 $ns $this->_registry[$namespace]['data'];
  139.                 $pathNodes $count 1;
  140.  
  141.                 //for ($i = 0; $i < $pathNodes; $i ++) {
  142.                 for ($i 1$i $pathNodes$i ++{
  143.                     if((isset($ns->$nodes[$i]))) $ns =$ns->$nodes[$i];
  144.                 }
  145.  
  146.                 if(isset($ns->$nodes[$i])) {
  147.                     $result $ns->$nodes[$i];
  148.                 }
  149.             }
  150.         }
  151.         return $result;
  152.     }
  153.  
  154.     /**
  155.      * Set a registry value
  156.      *
  157.      * @access    public
  158.      * @param    string    $regpath     Registry Path (e.g. joomla.content.showauthor)
  159.      * @param     mixed    $value        Value of entry
  160.      * @return     mixed    Value of old value or boolean false if operation failed
  161.      * @since    1.5
  162.      */
  163.     function setValue($regpath$value)
  164.     {
  165.         // Explode the registry path into an array
  166.         $nodes explode('.'$regpath);
  167.  
  168.         // Get the namespace
  169.         $count count($nodes);
  170.  
  171.         if ($count 2{
  172.             $namespace $this->_defaultNameSpace;
  173.         else {
  174.             $namespace array_shift($nodes);
  175.             $count--;
  176.         }
  177.  
  178.         if (!isset($this->_registry[$namespace])) {
  179.             $this->makeNameSpace($namespace);
  180.         }
  181.  
  182.         $ns $this->_registry[$namespace]['data'];
  183.  
  184.         $pathNodes $count 1;
  185.  
  186.         if ($pathNodes 0{
  187.             $pathNodes 0;
  188.         }
  189.  
  190.         for ($i 0$i $pathNodes$i ++)
  191.         {
  192.             // If any node along the registry path does not exist, create it
  193.             if (!isset($ns->$nodes[$i])) {
  194.                 $ns->$nodes[$inew stdClass();
  195.             }
  196.             $ns =$ns->$nodes[$i];
  197.         }
  198.  
  199.         // Get the old value if exists so we can return it
  200.         $ns->$nodes[$i=$value;
  201.  
  202.         return $ns->$nodes[$i];
  203.     }
  204.  
  205.     /**
  206.      * Load a associative array of values into the default namespace
  207.      *
  208.      * @access    public
  209.      * @param    array    $array        Associative array of value to load
  210.      * @param    string    $namepsace     The name of the namespace
  211.      * @return    boolean    True on success
  212.      * @since    1.5
  213.      */
  214.     function loadArray($array$namespace null)
  215.     {
  216.         // If namespace is not set, get the default namespace
  217.         if ($namespace == null{
  218.             $namespace $this->_defaultNameSpace;
  219.         }
  220.  
  221.         if (!isset($this->_registry[$namespace])) {
  222.             // If namespace does not exist, make it and load the data
  223.             $this->makeNameSpace($namespace);
  224.         }
  225.  
  226.         // Load the variables into the registry's default namespace.
  227.         foreach ($array as $k => $v)
  228.         {
  229.             $this->_registry[$namespace]['data']->$k $v;
  230.         }
  231.  
  232.         return true;
  233.     }
  234.  
  235.     /**
  236.      * Load the public variables of the object into the default namespace.
  237.      *
  238.      * @access    public
  239.      * @param    object    $object        The object holding the public vars to load
  240.      * @param    string    $namespace     Namespace to load the INI string into [optional]
  241.      * @return    boolean    True on success
  242.      * @since    1.5
  243.      */
  244.     function loadObject(&$object$namespace null)
  245.     {
  246.         // If namespace is not set, get the default namespace
  247.         if ($namespace == null{
  248.             $namespace $this->_defaultNameSpace;
  249.         }
  250.  
  251.         if (!isset($this->_registry[$namespace])) {
  252.             // If namespace does not exist, make it and load the data
  253.             $this->makeNameSpace($namespace);
  254.         }
  255.  
  256.         /*
  257.          * We want to leave groups that are already in the namespace and add the
  258.          * groups loaded into the namespace.  This overwrites any existing group
  259.          * with the same name
  260.          */
  261.         if (is_object$object ))
  262.         {
  263.             foreach (get_object_vars($objectas $k => $v{
  264.                 if (substr($k0,1!= '_' || $k == '_name'{
  265.                     $this->_registry[$namespace]['data']->$k $v;
  266.                 }
  267.             }
  268.         }
  269.  
  270.         return true;
  271.     }
  272.  
  273.     /**
  274.      * Load the contents of a file into the registry
  275.      *
  276.      * @access    public
  277.      * @param    string    $file        Path to file to load
  278.      * @param    string    $format        Format of the file [optional: defaults to INI]
  279.      * @param    string    $namespace    Namespace to load the INI string into [optional]
  280.      * @return    boolean    True on success
  281.      * @since    1.5
  282.      */
  283.     function loadFile($file$format 'INI'$namespace null)
  284.     {
  285.         // Load a file into the given namespace [or default namespace if not given]
  286.         $handler =JRegistryFormat::getInstance($format);
  287.  
  288.         // If namespace is not set, get the default namespace
  289.         if ($namespace == null{
  290.             $namespace $this->_defaultNameSpace;
  291.         }
  292.  
  293.         // Get the contents of the file
  294.         jimport('joomla.filesystem.file');
  295.         $data JFile::read($file);
  296.  
  297.         if (!isset($this->_registry[$namespace]))
  298.         {
  299.             // If namespace does not exist, make it and load the data
  300.             $this->makeNameSpace($namespace);
  301.             $this->_registry[$namespace]['data'$handler->stringToObject($data);
  302.         }
  303.         else
  304.         {
  305.             // Get the data in object format
  306.             $ns $handler->stringToObject($data);
  307.  
  308.             /*
  309.              * We want to leave groups that are already in the namespace and add the
  310.              * groups loaded into the namespace.  This overwrites any existing group
  311.              * with the same name
  312.              */
  313.             foreach (get_object_vars($nsas $k => $v{
  314.                 $this->_registry[$namespace]['data']->$k $v;
  315.             }
  316.         }
  317.  
  318.         return true;
  319.     }
  320.  
  321.     /**
  322.      * Load an XML string into the registry into the given namespace [or default if a namespace is not given]
  323.      *
  324.      * @access    public
  325.      * @param    string    $data        XML formatted string to load into the registry
  326.      * @param    string    $namespace    Namespace to load the XML string into [optional]
  327.      * @return    boolean    True on success
  328.      * @since    1.5
  329.      */
  330.     function loadXML($data$namespace null)
  331.     {
  332.         // Load a string into the given namespace [or default namespace if not given]
  333.         $handler =JRegistryFormat::getInstance('XML');
  334.  
  335.         // If namespace is not set, get the default namespace
  336.         if ($namespace == null{
  337.             $namespace $this->_defaultNameSpace;
  338.         }
  339.  
  340.         if (!isset($this->_registry[$namespace])) {
  341.             // If namespace does not exist, make it and load the data
  342.             $this->makeNameSpace($namespace);
  343.             $this->_registry[$namespace]['data'=$handler->stringToObject($data);
  344.         else {
  345.             // Get the data in object format
  346.             $ns =$handler->stringToObject($data);
  347.  
  348.             /*
  349.              * We want to leave groups that are already in the namespace and add the
  350.              * groups loaded into the namespace.  This overwrites any existing group
  351.              * with the same name
  352.              */
  353.             foreach (get_object_vars($nsas $k => $v{
  354.                 $this->_registry[$namespace]['data']->$k $v;
  355.             }
  356.         }
  357.  
  358.         return true;
  359.     }
  360.  
  361.     /**
  362.      * Load an INI string into the registry into the given namespace [or default if a namespace is not given]
  363.      *
  364.      * @access    public
  365.      * @param    string    $data        INI formatted string to load into the registry
  366.      * @param    string    $namespace    Namespace to load the INI string into [optional]
  367.      * @return    boolean True on success
  368.      * @since    1.5
  369.      */
  370.     function loadINI($data$namespace null)
  371.     {
  372.         // Load a string into the given namespace [or default namespace if not given]
  373.         $handler =JRegistryFormat::getInstance('INI');
  374.  
  375.         // If namespace is not set, get the default namespace
  376.         if ($namespace == null{
  377.             $namespace $this->_defaultNameSpace;
  378.         }
  379.  
  380.         if (!isset($this->_registry[$namespace])) {
  381.             // If namespace does not exist, make it and load the data
  382.             $this->makeNameSpace($namespace);
  383.             $this->_registry[$namespace]['data'=$handler->stringToObject($data);
  384.         else {
  385.             // Get the data in object format
  386.             $ns $handler->stringToObject($data);
  387.  
  388.             /*
  389.              * We want to leave groups that are already in the namespace and add the
  390.              * groups loaded into the namespace.  This overwrites any existing group
  391.              * with the same name
  392.              */
  393.             foreach (get_object_vars($nsas $k => $v{
  394.                 $this->_registry[$namespace]['data']->$k $v;
  395.             }
  396.         }
  397.  
  398.         return true;
  399.     }
  400.  
  401.     /**
  402.      * Merge a JRegistry object into this one
  403.      *
  404.      * @access    public
  405.      * @param    object    $source    Source JRegistry object ot merge
  406.      * @return    boolean    True on success
  407.      * @since    1.5
  408.      */
  409.     function merge(&$source)
  410.     {
  411.         if (is_a($source'JRegistry'))
  412.         {
  413.             $sns $source->getNameSpaces();
  414.             foreach ($sns as $ns)
  415.             {
  416.                 if (!isset($this->_registry[$ns]))
  417.                 {
  418.                     // If namespace does not exist, make it and load the data
  419.                     $this->makeNameSpace($ns);
  420.                 }
  421.  
  422.                 // Load the variables into the registry's default namespace.
  423.                 foreach ($source->toArray($nsas $k => $v)
  424.                 {
  425.                     if ($v != null{
  426.                         $this->_registry[$ns]['data']->$k $v;
  427.                     }
  428.                 }
  429.             }
  430.             return true;
  431.         }
  432.         return false;
  433.     }
  434.  
  435.     /**
  436.      * Get a namespace in a given string format
  437.      *
  438.      * @access    public
  439.      * @param    string    $format        Format to return the string in
  440.      * @param    string    $namespace    Namespace to return [optional: null returns the default namespace]
  441.      * @param    mixed    $params        Parameters used by the formatter, see formatters for more info
  442.      * @return    string    Namespace in string format
  443.      * @since    1.5
  444.      */
  445.     function toString($format 'INI'$namespace null$params null)
  446.     {
  447.         // Return a namespace in a given format
  448.         $handler =JRegistryFormat::getInstance($format);
  449.  
  450.         // If namespace is not set, get the default namespace
  451.         if ($namespace == null{
  452.             $namespace $this->_defaultNameSpace;
  453.         }
  454.  
  455.         // Get the namespace
  456.         $ns $this->_registry[$namespace]['data'];
  457.  
  458.         return $handler->objectToString($ns$params);
  459.     }
  460.  
  461.     /**
  462.      * Transforms a namespace to an array
  463.      *
  464.      * @access    public
  465.      * @param    string    $namespace    Namespace to return [optional: null returns the default namespace]
  466.      * @return    array    An associative array holding the namespace data
  467.      * @since    1.5
  468.      */
  469.     function toArray($namespace null)
  470.     {
  471.         // If namespace is not set, get the default namespace
  472.         if ($namespace == null{
  473.             $namespace $this->_defaultNameSpace;
  474.         }
  475.  
  476.         // Get the namespace
  477.         $ns $this->_registry[$namespace]['data'];
  478.  
  479.         $array array();
  480.         foreach (get_object_vars$ns as $k => $v{
  481.             $array[$k$v;
  482.         }
  483.  
  484.         return $array;
  485.     }
  486.  
  487.     /**
  488.      * Transforms a namespace to an object
  489.      *
  490.      * @access    public
  491.      * @param    string    $namespace    Namespace to return [optional: null returns the default namespace]
  492.      * @return    object    An an object holding the namespace data
  493.      * @since    1.5
  494.      */
  495.     function toObject($namespace null)
  496.     {
  497.         // If namespace is not set, get the default namespace
  498.         if ($namespace == null{
  499.             $namespace $this->_defaultNameSpace;
  500.         }
  501.  
  502.         // Get the namespace
  503.         $ns $this->_registry[$namespace]['data'];
  504.  
  505.         return $ns;
  506.     }
  507.  
  508.     function __clone()
  509.     {
  510.         $this->_registry = unserialize(serialize($this->_registry));
  511.     }
  512. }

Documentation generated on Sat, 14 Nov 2009 11:18:26 +0000 by phpDocumentor 1.3.1