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 / SharedMemory / File.php < prev    next >
Encoding:
PHP Script  |  2008-07-02  |  4.4 KB  |  181 lines

  1. <?php
  2. /**
  3. *
  4. * The Plain File driver for SharedMemory
  5. *
  6. * PHP versions 4 and 5
  7. *
  8. * LICENSE: This source file is subject to version 3.0 of the PHP license
  9. * that is available through the world-wide-web at the following URI:
  10. * http://www.php.net/license/3_0.txt.  If you did not receive a copy of
  11. * the PHP License and are unable to obtain it through the web, please
  12. * send a note to license@php.net so we can mail you a copy immediately.
  13. *
  14. * @category   System
  15. * @package    System_Sharedmemory
  16. * @author     Evgeny Stepanischev <bolk@lixil.ru>
  17. * @copyright  2005 Evgeny Stepanischev
  18. * @license    http://www.php.net/license/3_0.txt  PHP License 3.0
  19. * @version    CVS: $Id:$
  20. * @link       http://pear.php.net/package/System_SharedMemory
  21. */
  22.  
  23. /**
  24. *
  25. * The methods PEAR SharedMemory uses to interact with plain file
  26. * for interacting with shared memory via plain files
  27. *
  28. * These methods overload the ones declared System_SharedMemory_Common
  29. *
  30. * @category   System
  31. * @package    System_Sharedmemory
  32. * @package    System_Sharedmemory
  33. * @author     Evgeny Stepanischev <bolk@lixil.ru>
  34. * @copyright  2005 Evgeny Stepanischev
  35. * @license    http://www.php.net/license/3_0.txt  PHP License 3.0
  36. * @version    CVS: $Id:$
  37. * @link       http://pear.php.net/package/System_SharedMemory
  38. */
  39.  
  40.  
  41. require_once 'System/SharedMemory/Common.php';
  42. require_once "PEAR.php";
  43.  
  44. // {{{ class System_SharedMemory
  45.  
  46. class System_SharedMemory_File extends System_SharedMemory_Common
  47. {
  48.     // {{{ properties
  49.     /**
  50.     * Contains internal options
  51.     *
  52.     * @var string
  53.     *
  54.     * @access private
  55.     */
  56.     var $_options;
  57.  
  58.     /**
  59.     * true if plugin was connected to backend
  60.     *
  61.     * @var bool
  62.     *
  63.     * @access private
  64.     */
  65.     var $_connected;
  66.     // }}}
  67.     // {{{ constructor
  68.  
  69.     /**
  70.      * Constructor. Init all variables.
  71.      *
  72.      * @param array $options
  73.      *
  74.      * @access public
  75.      */
  76.     function System_SharedMemory_File($options)
  77.     {
  78.         $this->_options = $this->_default($options, array
  79.         (
  80.             'tmp'  => '/tmp',
  81.         ));
  82.  
  83.         $this->_connected = is_writeable($this->_options['tmp']) && is_dir($this->_options['tmp']);
  84.     }
  85.     // }}}
  86.     // {{{ isConnected()
  87.  
  88.     /**
  89.      * returns true if plugin was 
  90.      * successfully connected to backend
  91.      *
  92.      * @return bool true if connected
  93.      * @access public
  94.      */
  95.     function isConnected()
  96.     {
  97.         return $this->_connected;
  98.     }
  99.     // }}}
  100.     // {{{ get()
  101.  
  102.     /**
  103.      * returns value of variable in shared mem
  104.      *
  105.      * @param string $name  name of the variable
  106.      * @param string $value value of the variable
  107.      *
  108.      * @return mixed true on success or PEAR_error on fail
  109.      * @access public
  110.      */
  111.     function get($name)
  112.     {
  113.         $name = $this->_options['tmp'].'/smf_'.md5($name);
  114.  
  115.         if (!file_exists($name)) {
  116.             return array();
  117.         }
  118.  
  119.         $fp = fopen($name, 'rb');
  120.         if (is_resource($fp)) {
  121.             flock ($fp, LOCK_SH);
  122.  
  123.             $str = fread($fp, filesize($name));
  124.             fclose($fp);
  125.             return $str == '' ? array() : unserialize($str);
  126.         }
  127.  
  128.         return PEAR::raiseError('Cannot open file.', 1);
  129.     }
  130.     // }}}
  131.     // {{{ set()
  132.  
  133.     /**
  134.      * set value of variable in shared mem
  135.      *
  136.      * @param string $name  name of the variable
  137.      * @param string $value value of the variable
  138.      *
  139.      * @return mixed true on success or PEAR_error on fail
  140.      * @access public
  141.      */
  142.     function set($name, $value)
  143.     {
  144.         $fp = fopen($this->_options['tmp'].'/smf_'.md5($name), 'ab');
  145.         if (is_resource($fp)) {
  146.             flock ($fp, LOCK_EX);
  147.             ftruncate($fp, 0);
  148.             fseek($fp, 0);
  149.  
  150.             fwrite($fp, serialize($value));
  151.             fclose($fp);
  152.             clearstatcache();
  153.             return true;
  154.         }
  155.  
  156.         return PEAR::raiseError('Cannot write to file.', 2);
  157.     }
  158.     // }}}
  159.     // {{{ rm()
  160.  
  161.     /**
  162.      * remove variable from memory
  163.      *
  164.      * @param string $name  name of the variable
  165.      *
  166.      * @return mixed true on success or PEAR_error on fail
  167.      * @access public
  168.      */
  169.     function rm($name)
  170.     {
  171.         $name = $this->_options['tmp'].'/smf_'.md5($name);
  172.  
  173.         if (file_exists($name)) {
  174.             unlink($name);
  175.         }
  176.     }
  177.     // }}}
  178.  
  179. }
  180. // }}}
  181. ?>