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

Source for file Container.php

Documentation is available at Container.php

  1. <?php
  2. require_once('Auth/PrefManager2.php');
  3.  
  4. class Auth_PrefManager2_Container {
  5.  
  6.     /**
  7.      * An array of options to use.
  8.      *
  9.      * @var array 
  10.      * @access protected
  11.      * @since 0.1.0
  12.      */
  13.     var $_options = array();
  14.     
  15.     /**
  16.      * The PEAR_ErrorStack object to use for error handling.
  17.      *
  18.      * @var PEAR_ErrorStack 
  19.      * @access protected
  20.      * @since 0.1.0
  21.      */
  22.     var $_errorStack = null;
  23.     
  24.     /**
  25.      * Constructor
  26.      *
  27.      * Applications should never call this constructor directly, instead
  28.      * create a container with the factory method.
  29.      *
  30.      * @access protected
  31.      * @param array $options An associative array of options.
  32.      * @return void 
  33.      * @see Auth_PrefManager2::&factory()
  34.      */
  35.     function Auth_PrefManager2_Container($options = array())
  36.     {
  37.         $this->_errorStack =PEAR_ErrorStack::singleton('Auth_PrefManager2');
  38.         $this->_parseOptions($options);
  39.     }
  40.     
  41.     /**
  42.      * Gets a preference for the specified owner and application.
  43.      *
  44.      * @param string $preference The name of the preference to retrieve.
  45.      * @param string $owner The owner to retrieve the preference for.
  46.      * @param string $application The application to retrieve from, if left as
  47.      *                             null the default application will be used.
  48.      * @param bool $returnDefaults Should a default value be returned if no
  49.      *                              user preference is available?
  50.      * @return mixed|nullThe value, or null of no value was available.
  51.      * @access public
  52.      */
  53.     function getPref($preference$owner = null$application = null$returnDefaults = true)
  54.     {
  55.         if (is_null($owner)) {
  56.             $owner $this->_options['default_owner'];
  57.         }
  58.         
  59.         if (is_null($application)) {
  60.             $application $this->_options['default_app'];
  61.         }
  62.         
  63.         if (!is_null($value $this->_get($owner$preference$application))) {
  64.             return $this->_decodeValue($value);
  65.         else {
  66.             if ($returnDefaults && $this->_options['return_defaults'&& ($owner != $this->_options['default_app'])) {
  67.                 return $this->getPref($preferencenull$application);
  68.             }
  69.         }
  70.     }
  71.     
  72.     /**
  73.      * Sets a preference for the specified owner and application.
  74.      *
  75.      * @param string $preference The name of the preference to retrieve.
  76.      * @param mixed $value The value to set the preference to.
  77.      * @param string $owner The owner to retrieve the preference for.
  78.      * @param string $application The application to retrieve from, if left as
  79.      *                             null the default application will be used.
  80.      * @return bool Success/failure
  81.      * @access public
  82.      */
  83.     function setPref($preference$value$owner = null$application = null)
  84.     {
  85.         if (is_null($owner)) {
  86.             $owner $this->_options['default_owner'];
  87.         }
  88.         
  89.         if (is_null($application)) {
  90.             $application $this->_options['default_app'];
  91.         }
  92.         
  93.         return $this->_set($owner$preference$this->_encodeValue($value)$application);
  94.     }
  95.     
  96.     /**
  97.      * Deletes a preference for the specified owner and application.
  98.      *
  99.      * @param string $preference The name of the preference to delete.
  100.      * @param string $owner The owner to delete the preference for.
  101.      * @param string $application The application to delete from, if left as
  102.      *                             null the default application will be used.
  103.      * @return bool Success/failure
  104.      * @access public
  105.      */
  106.     function deletePref($preference$owner = null$application = null)
  107.     {
  108.         if (is_null($owner)) {
  109.             $owner $this->_options['default_owner'];
  110.         }
  111.         
  112.         if (is_null($application)) {
  113.             $application $this->_options['default_app'];
  114.         }
  115.         
  116.         return $this->_delete($owner$preference$application);
  117.     }
  118.     
  119.     /**
  120.      * Sets a value with the container.
  121.      * This method should be overridden by container classes to do whatever
  122.      * needs doing.
  123.      *
  124.      * @param string $owner The owner to set the preference for.
  125.      * @param string $preference The name of the preference to set.
  126.      * @param string $application The application to set for.
  127.      * @return bool Success/failure
  128.      * @access protected
  129.      * @abstract
  130.      */
  131.     function _set($owner$preference$value$application)
  132.     {
  133.         $this->_throwError(AUTH_PREFMANAGER2_NOT_IMPLEMENTED);
  134.         return false;
  135.     }
  136.     
  137.     /**
  138.      * Gets a value from the container.
  139.      * This method should be overridden by container classes to do whatever
  140.      * needs doing.
  141.      *
  142.      * @param string $owner The owner to set the preference for.
  143.      * @param string $preference The name of the preference to set.
  144.      * @param mixed $value The value to set the preference to.
  145.      * @param string $application The application to set for.
  146.      * @return bool Success/failure
  147.      * @access protected
  148.      * @abstract
  149.      */
  150.     function _get($owner$preference$application)
  151.     {
  152.         $this->_throwError(AUTH_PREFMANAGER2_NOT_IMPLEMENTED);
  153.         return false;
  154.     }
  155.     
  156.     /**
  157.      * Deletes a value from the container.
  158.      * This method should be overridden by container classes to do whatever
  159.      * needs doing.
  160.      *
  161.      * @param string $owner The owner to delete the preference for.
  162.      * @param string $preference The name of the preference to delete.
  163.      * @param string $application The application to delete from.
  164.      * @return bool Success/failure
  165.      * @access protected
  166.      * @abstract
  167.      */
  168.     function _delete($owner$preference$application)
  169.     {
  170.         $this->_throwError(AUTH_PREFMANAGER2_NOT_IMPLEMENTED);
  171.         return false;
  172.     }
  173.     
  174.     /**
  175.      * Checks if the specified preference exists in the data container.
  176.      * This method should be overridden by container classes to do whatever
  177.      * needs doing.
  178.      *
  179.      * Returns null if an error occurs.
  180.      *
  181.      * @param string $owner The owner to delete the preference for.
  182.      * @param string $preference The name of the preference to delete.
  183.      * @param string $application The application to delete from.
  184.      * @return bool Does the pref exist?
  185.      * @access protected
  186.      * @abstract
  187.      */
  188.     function _exists($owner$preference$application)
  189.     {
  190.         $this->_throwError(AUTH_PREFMANAGER2_NOT_IMPLEMENTED);
  191.         return null;
  192.     }
  193.     
  194.     /**
  195.      * Prepares a value for saving in the data container.
  196.      * Containers that override this method should always call
  197.      * parent::_encodeValue() to do serialization.
  198.      *
  199.      * @param mixed $value The value to prepare.
  200.      * @return mixed The prepared value.
  201.      * @access protected
  202.      */
  203.     function _encodeValue($value)
  204.     {
  205.         if ($this->_options['serialize']{
  206.             return serialize($value);
  207.         }
  208.         
  209.         return $value;
  210.     }
  211.     
  212.     /**
  213.      * Reverts any preparation that was done to store the value.
  214.      * Containers that override this method should always call
  215.      * parent::_decodeValue() to do unserialization.
  216.      * 
  217.      * @param mixed $value The value to decode.
  218.      * @return mixed The unprepared value.
  219.      * @access protected
  220.      */
  221.     function _decodeValue($value)
  222.     {
  223.         if ($this->_options['serialize']{
  224.             return unserialize($value);
  225.         }
  226.     
  227.         return $value;
  228.     }
  229.     
  230.     /**
  231.      * Reads the options array, and sets default values for anything
  232.      * which isn't set.
  233.      *
  234.      * Container classes should override this method, set any defaults
  235.      * that they need, and then pass the options to parent::_parseOptions().
  236.      *
  237.      * @param array $options An array of options.
  238.      * @return void 
  239.      * @access protected
  240.      */
  241.     function _parseOptions($options)
  242.     {
  243.         if (!isset($options['default_owner'])) {
  244.             $options['default_owner''default';
  245.         }
  246.         
  247.         if (!isset($options['default_app'])) {
  248.             $options['default_app''default';
  249.         }
  250.         
  251.         if (!isset($options['serialize'])) {
  252.             $options['serialize'= true;
  253.         }
  254.         
  255.         if (!isset($options['cache'])) {
  256.             $options['cache'= true;
  257.         }
  258.         
  259.         if (!isset($options['cache_key'])) {
  260.             $options['cache_key''_prefmanager2';
  261.         }
  262.         
  263.         if (!isset($options['debug'])) {
  264.             $options['debug'= false;
  265.         }
  266.         
  267.         if (!isset($options['locale'])) {
  268.             $options['locale''en';
  269.         }
  270.         
  271.         if (!isset($options['return_defaults'])) {
  272.             $options['return_defaults'= true;
  273.         }
  274.         
  275.         $this->_options $options;
  276.     }
  277.     
  278.     /**
  279.      * Throws an error, using the current locale if it exists, or en if it doesn't.
  280.      * 
  281.      * @param int $code The error code.
  282.      * @param string $level The level of the error.
  283.      * @param array $params Any other information to include with the error.
  284.      * @return void 
  285.      * @access protected
  286.      */
  287.     function _throwError($code$level 'error'$params = array()$repackage = null)
  288.     {
  289.         $locale = isset($this->_errorMessages['_Auth_PrefManager2'][$this->_options['locale']])
  290.             ? $this->_options['locale']
  291.             : 'en';
  292.             
  293.         var_dump($this->_errorStack->push($code
  294.                                  $level,
  295.                                  $params,
  296.                                  $GLOBALS['_Auth_PrefManager2']['err'][$locale][$code],
  297.                                  $repackage));
  298.     }
  299.  
  300. }
  301.  
  302. ?>

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