home *** CD-ROM | disk | FTP | other *** search
/ Cricao de Sites - 650 Layouts Prontos / WebMasters.iso / Servidores / xampp-win32-1.6.7-installer.exe / php / PEAR / System / Folders / Cached.php
Encoding:
PHP Script  |  2008-07-02  |  12.2 KB  |  490 lines

  1. <?php
  2. /**
  3. *   Provides a cached version of System_Folders.
  4. *   It also has methods to read folder settings from an ini file.
  5. *
  6. *   Simpe example:
  7. *       require_once 'System/Folders/Cached.php';
  8. *       $sf = new System_Folders_Cached();
  9. *
  10. *       //load the stored settings from last time
  11. *       $sf->loadFromFile();
  12. *       echo $sf->getHome();
  13. *
  14. *       //Set an own documents directory
  15. *       $sf->setDocuments('/home/cweiske/MyDocuments/');
  16. *       //Save the settings for next time
  17. *       $sf->saveToFile();
  18. *
  19. *
  20. *   @category   System
  21. *   @package    System_Folders
  22. *   @author     Christian Weiske <cweiske@php.net>
  23. *   @license    LGPL
  24. *   @version    CVS: $Id: Cached.php,v 1.1 2006/03/10 13:04:26 cweiske Exp $
  25. */
  26.  
  27. require_once 'System/Folders.php';
  28. require_once 'Config.php';
  29. require_once 'Config/Container.php';
  30.  
  31. /**
  32. *   Provides a cached version of System_Folders.
  33. *   It also has methods to read folder settings from an ini file.
  34. *
  35. *   Be very careful when overriding the AppData setting with
  36. *    setAppData()! When loading a config file without specifying the file
  37. *    name, the default app data directory will be used. After loading,
  38. *    the file will be saved to the new app data directory, thus it won't
  39. *    be available the next time, as the app data folder is the old one again.
  40. *
  41. *   @category   System
  42. *   @package    System_Folders
  43. *   @author     Christian Weiske <cweiske@php.net>
  44. *   @license    LGPL
  45. *   @version    CVS: $Id: Cached.php,v 1.1 2006/03/10 13:04:26 cweiske Exp $
  46. */
  47. class System_Folders_Cached extends System_Folders
  48. {
  49.     /**
  50.     *   The cached paths will be hold here.
  51.     *
  52.     *   @access protected
  53.     *   @var array
  54.     */
  55.     var $arCache = array();
  56.  
  57.     /**
  58.     *   The settings that are available.
  59.     *
  60.     *   @access protected
  61.     *   @var array
  62.     */
  63.     var $arSettings = array(
  64.         'AllUsers', 'AppData', 'Desktop', 'Documents', 'Home',
  65.         'Programs', 'Temp', 'SharedDocuments', 'Windows'
  66.     );
  67.  
  68.  
  69.  
  70.     function System_Folders_Cached()
  71.     {
  72.         parent::System_Folders();
  73.     }//function System_Folders_Cached()
  74.  
  75.  
  76.  
  77.     /**
  78.     *   Loads the directories from an ini file.
  79.     *   If you don't specify the config file, it will be determined
  80.     *    automatically.
  81.     *
  82.     *   @access public
  83.     *   @param string   $strFile    The file to load the data from (ini file)
  84.     *   @return mixed   True on success, PEAR_Error on failure
  85.     */
  86.     function loadFromFile($strFile = null)
  87.     {
  88.         $strFile = $this->getDefaultConfigFile();
  89.         if (!file_exists($strFile)) {
  90.             //Not existing config file isn't an error
  91.             return true;
  92.         }
  93.         $conf = new Config();
  94.         $root =& $conf->parseConfig($strFile, 'inifile');
  95.  
  96.         if (PEAR::isError($root)) {
  97.             return $root;
  98.         }
  99.  
  100.         $arSettings = $root->toArray();
  101.         if (!isset($arSettings['root']['paths'])) {
  102.             return true;
  103.         }
  104.  
  105.         foreach ($arSettings['root']['paths'] as $strId => $strValue) {
  106.             if ($strValue != '') {
  107.                 $this->arCache[$strId] = $strValue;
  108.             }
  109.         }
  110.  
  111.         return true;
  112.     }//function loadFromFile($strFile = null)
  113.  
  114.  
  115.  
  116.     /**
  117.     *   Saves the folders into a config file that can be edited by hand.
  118.     *   If you don't specify the config file, it will be determined
  119.     *    automatically.
  120.     *   Values that are NULL won't be saved.
  121.     *
  122.     *   @access public
  123.     *   @param string   $strFile    The file to save the data into (ini file)
  124.     *   @param boolean  $bSaveAllSettings   If all settings shall be saved
  125.     *                           that can be loaded, or only that settings,
  126.     *                           that have been retrieved by the user
  127.     *   @return mixed   True on success, PEAR_Error on failure
  128.     */
  129.     function saveToFile($strFile = null, $bSaveAllSettings = true)
  130.     {
  131.         $conf  =& new Config_Container('section', 'paths');
  132.  
  133.         if ($bSaveAllSettings) {
  134.             foreach ($this->arSettings as $strSetting) {
  135.                 $strFunction = 'get' . $strSetting;
  136.                 $strValue = $this->$strFunction();
  137.                 $conf->createDirective(strtolower($strSetting), $strValue);
  138.             }
  139.         } else {
  140.             foreach ($this->arCache as $strId => $strValue) {
  141.                 $conf->createDirective($strId, $strValue);
  142.             }
  143.         }
  144.  
  145.         $config = new Config();
  146.         $config->setRoot($conf);
  147.         return $config->writeConfig($this->getDefaultConfigFile(), 'inifile');
  148.     }//function saveToFile($strFile = null, $bSaveAllSettings = true)
  149.  
  150.  
  151.  
  152.     /**
  153.     *   Returns the path to the default config file.
  154.     *   It the one that's used if no filename is passed to the
  155.     *    saveToFile/loadFromFile methods.
  156.     *
  157.     *   @access public
  158.     *   @return string  The filename
  159.     */
  160.     function getDefaultConfigFile()
  161.     {
  162.         return $this->getAppData() . '/.net.php.pear.system.folders';
  163.     }//function getDefaultConfigFile()
  164.  
  165.  
  166.  
  167.     /**
  168.     *   Returns a cached value.
  169.     *   If the cache doesn't exist, the cached value is empty or null,
  170.     *   the System_Folders method for the key is called to get the value.
  171.     *
  172.     *   @access protected
  173.     *   @param string $strKey   The id of the value to get
  174.     *   @return string      The directory
  175.     */
  176.     function getCachedValue($strKey)
  177.     {
  178.         $strKeyLower = strtolower($strKey);
  179.         if (!isset($this->arCache[$strKeyLower])) {
  180.             $strFunction = 'get' . $strKey;
  181.             $this->arCache[$strKeyLower] = parent::$strFunction();
  182.         }
  183.  
  184.         return $this->arCache[$strKeyLower];
  185.     }//function getCachedValue($strKey)
  186.  
  187.  
  188.  
  189.     /**
  190.     *   Sets the cache of the given key to the given value.
  191.     *   Passing NULL removes the cache entry.
  192.     *
  193.     *   @access protected
  194.     *   @param string $strKey       Id of the value to get
  195.     *   @param string $strValue     Value to set.
  196.     */
  197.     function setCachedValue($strKey, $strValue)
  198.     {
  199.         if ($strValue === null) {
  200.             unset($this->arCache[strtolower($strKey)]);
  201.         } else {
  202.             $this->arCache[strtolower($strKey)] = $strValue;
  203.         }
  204.     }//function setCachedValue($strKey, $strValue)
  205.  
  206.  
  207.  
  208.     /*
  209.     *   Overriding the parent's methods to cache them
  210.     */
  211.  
  212.  
  213.  
  214.     /**
  215.     *   Cached version of getAllUsers().
  216.     *
  217.     *   @access public
  218.     *   @see System_Folders::getAllUsers()
  219.     *   @return string      The all users directory
  220.     */
  221.     function getAllUsers()
  222.     {
  223.         return $this->getCachedValue('AllUsers');
  224.     }//function getAllUsers()
  225.  
  226.  
  227.  
  228.     /**
  229.     *   Cached version of getAppData().
  230.     *
  231.     *   @access public
  232.     *   @see System_Folders::getAppData()
  233.     *   @return string      The application data directory
  234.     */
  235.     function getAppData()
  236.     {
  237.         return $this->getCachedValue('AppData');
  238.     }//function getAppData()
  239.  
  240.  
  241.  
  242.     /**
  243.     *   Cached version of getDesktop().
  244.     *
  245.     *   @access public
  246.     *   @see System_Folders::getDesktop()
  247.     *   @return string      The desktop directory
  248.     */
  249.     function getDesktop()
  250.     {
  251.         return $this->getCachedValue('Desktop');
  252.     }//function getDesktop()
  253.  
  254.  
  255.  
  256.     /**
  257.     *   Cached version of getDocuments().
  258.     *
  259.     *   @access public
  260.     *   @see System_Folders::getDocuments()
  261.     *   @return string      The documents directory
  262.     */
  263.     function getDocuments()
  264.     {
  265.         return $this->getCachedValue('Documents');
  266.     }//function getDocuments()
  267.  
  268.  
  269.  
  270.     /**
  271.     *   Cached version of getHome().
  272.     *
  273.     *   @access public
  274.     *   @see System_Folders::getHome()
  275.     *   @return string      The home directory
  276.     */
  277.     function getHome()
  278.     {
  279.         return $this->getCachedValue('Home');
  280.     }//function getHome()
  281.  
  282.  
  283.  
  284.     /**
  285.     *   Cached version of getPrograms().
  286.     *
  287.     *   @access public
  288.     *   @see System_Folders::getPrograms()
  289.     *   @return string      The programs directory
  290.     */
  291.     function getPrograms()
  292.     {
  293.         return $this->getCachedValue('Programs');
  294.     }//function getPrograms()
  295.  
  296.  
  297.  
  298.     /**
  299.     *   Cached version of getTemp().
  300.     *
  301.     *   @access public
  302.     *   @see System_Folders::getTemp()
  303.     *   @return string      The temporary directory
  304.     */
  305.     function getTemp()
  306.     {
  307.         return $this->getCachedValue('Temp');
  308.     }//function getTemp()
  309.  
  310.  
  311.  
  312.     /**
  313.     *   Cached version of getSharedDocuments().
  314.     *
  315.     *   @access public
  316.     *   @see System_Folders::getSharedDocuments()
  317.     *   @return string      The shared documents directory
  318.     */
  319.     function getSharedDocuments()
  320.     {
  321.         return $this->getCachedValue('SharedDocuments');
  322.     }//function getSharedDocuments()
  323.  
  324.  
  325.  
  326.     /**
  327.     *   Cached version of getWindows().
  328.     *
  329.     *   @access public
  330.     *   @see System_Folders::getWindows()
  331.     *   @return string      The windows directory
  332.     */
  333.     function getWindows()
  334.     {
  335.         return $this->getCachedValue('Windows');
  336.     }//function getWindows()
  337.  
  338.  
  339.  
  340.     /*
  341.     *   Setter methods
  342.     */
  343.  
  344.  
  345.  
  346.     /**
  347.     *   Sets an own all users directory.
  348.     *   Set it to NULL to deactivate saving and
  349.     *    remove the value from the cache.
  350.     *
  351.     *   @access public
  352.     *   @see getAllUsers
  353.     *   @param string $value    The new all users directory
  354.     */
  355.     function setAllUsers($value)
  356.     {
  357.         $this->setCachedValue('AllUsers', $value);
  358.     }//function setAllUsers($value)
  359.  
  360.  
  361.  
  362.     /**
  363.     *   Sets an own application data directory.
  364.     *   Set it to NULL to deactivate saving and
  365.     *    remove the value from the cache.
  366.     *
  367.     *   @access public
  368.     *   @see getAppData
  369.     *   @param string $value    The new app data directory
  370.     */
  371.     function setAppData($value)
  372.     {
  373.         $this->setCachedValue('AppData', $value);
  374.     }//function setAppData($value)
  375.  
  376.  
  377.  
  378.     /**
  379.     *   Sets an own desktop directory.
  380.     *   Set it to NULL to deactivate saving and
  381.     *    remove the value from the cache.
  382.     *
  383.     *   @access public
  384.     *   @see getDesktop
  385.     *   @param string $value    The new desktop directory
  386.     */
  387.     function setDesktop($value)
  388.     {
  389.         $this->setCachedValue('Desktop', $value);
  390.     }//function setDesktop($value)
  391.  
  392.  
  393.  
  394.     /**
  395.     *   Sets an own documents directory.
  396.     *   Set it to NULL to deactivate saving and
  397.     *    remove the value from the cache.
  398.     *
  399.     *   @access public
  400.     *   @see getDocuments
  401.     *   @param string $value    The new documents directory
  402.     */
  403.     function setDocuments($value)
  404.     {
  405.         $this->setCachedValue('Documents', $value);
  406.     }//function setDocuments($value)
  407.  
  408.  
  409.  
  410.     /**
  411.     *   Sets an own home directory.
  412.     *   Set it to NULL to deactivate saving and
  413.     *    remove the value from the cache.
  414.     *
  415.     *   @access public
  416.     *   @see getHome
  417.     *   @param string $value    The new home directory
  418.     */
  419.     function setHome($value)
  420.     {
  421.         $this->setCachedValue('Home', $value);
  422.     }//function setHome($value)
  423.  
  424.  
  425.  
  426.     /**
  427.     *   Sets an own programs directory.
  428.     *   Set it to NULL to deactivate saving and
  429.     *    remove the value from the cache.
  430.     *
  431.     *   @access public
  432.     *   @see getPrograms
  433.     *   @param string $value    The new programs directory
  434.     */
  435.     function setPrograms($value)
  436.     {
  437.         $this->setCachedValue('Programs', $value);
  438.     }//function setPrograms($value)
  439.  
  440.  
  441.  
  442.     /**
  443.     *   Sets an own temp directory.
  444.     *   Set it to NULL to deactivate saving and
  445.     *    remove the value from the cache.
  446.     *
  447.     *   @access public
  448.     *   @see getTemp
  449.     *   @param string $value    The new temp directory
  450.     */
  451.     function setTemp($value)
  452.     {
  453.         $this->setCachedValue('Temp', $value);
  454.     }//function setTemp($value)
  455.  
  456.  
  457.  
  458.     /**
  459.     *   Sets an own shared documents directory.
  460.     *   Set it to NULL to deactivate saving and
  461.     *    remove the value from the cache.
  462.     *
  463.     *   @access public
  464.     *   @see getSharedDocuments
  465.     *   @param string $value    The new shared documents directory
  466.     */
  467.     function setSharedDocuments($value)
  468.     {
  469.         $this->setCachedValue('SharedDocuments', $value);
  470.     }//function setSharedDocuments($value)
  471.  
  472.  
  473.  
  474.     /**
  475.     *   Sets an own windows directory.
  476.     *   Set it to NULL to deactivate saving and
  477.     *    remove the value from the cache.
  478.     *
  479.     *   @access public
  480.     *   @see getWindows
  481.     *   @param string $value    The new windows directory
  482.     */
  483.     function setWindows($value)
  484.     {
  485.         $this->setCachedValue('Windows', $value);
  486.     }//function setWindows($value)
  487.  
  488. }//class System_Folders_Cached extends System_Folders
  489. ?>
  490.