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 / Shmop.php < prev    next >
Encoding:
PHP Script  |  2008-07-02  |  5.9 KB  |  248 lines

  1. <?php
  2. /**
  3. *
  4. * The Shmop 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 PHP's Shmop extension
  26. * for interacting with Shmop shared memory
  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. require_once 'System/SharedMemory/Common.php';
  41. require_once "PEAR.php";
  42.  
  43. // {{{ class System_SharedMemory_Shmop
  44.  
  45. class System_SharedMemory_Shmop extends System_SharedMemory_Common
  46. {
  47.     // {{{ properties
  48.     /**
  49.     * handler for shmop_* functions
  50.     *
  51.     * @var string
  52.     *
  53.     * @access private
  54.     */
  55.     var $_h;
  56.  
  57.     /**
  58.     * Contains internal options
  59.     *
  60.     * @var string
  61.     *
  62.     * @access private
  63.     */
  64.     var $_options;
  65.     // }}}
  66.     // {{{ constructor
  67.  
  68.     /**
  69.      * Constructor. Init all variables.
  70.      *
  71.      * @param array $options
  72.      *
  73.      * @access public
  74.      */
  75.     function System_SharedMemory_Shmop($options)
  76.     {
  77.         $this->_options = $this->_default($options, array
  78.         (
  79.             'size' => 1048576,
  80.             'tmp'  => '/tmp',
  81.             'project' => 's'
  82.         ));
  83.  
  84.        $this->_h = $this->_ftok($this->_options['project']);
  85.     }
  86.     // }}}
  87.     // {{{ get()
  88.  
  89.     /**
  90.     * returns value of variable in shared mem
  91.     *
  92.     * @param mixed $name name of variable or false if all variables needs
  93.     *
  94.     * @return mixed PEAR_error or value of the variable
  95.     * @access public
  96.     */
  97.     function get($name = false)
  98.     {
  99.         $id = shmop_open($this->_h, 'c', 0600, $this->_options['size']);
  100.  
  101.         if ($id !== false) {
  102.             $ret = unserialize(shmop_read($id, 0, shmop_size($id)));
  103.             shmop_close($id);
  104.  
  105.             if ($name === false) {
  106.                 return $ret;
  107.             }
  108.             return isset($ret[$name]) ? $ret[$name] : null;
  109.         }
  110.  
  111.         return PEAR::raiseError('Cannot open shmop.', 1);
  112.     }
  113.     // }}}
  114.     // {{{ set()
  115.  
  116.     /**
  117.     * set value of variable in shared mem
  118.     *
  119.     * @param string $name  name of the variable
  120.     * @param string $value value of the variable
  121.     *
  122.     * @return bool true on success
  123.     * @access public
  124.     */
  125.     function set($name, $value)
  126.     {
  127.         $lh = $this->_lock();
  128.         $val = $this->get();
  129.         if (!is_array($val)) {
  130.             $val = array();
  131.         }
  132.  
  133.         $val[$name] = $value;
  134.         $val = serialize($val);
  135.         return $this->_write($val, $lh);
  136.     }
  137.     // }}}
  138.     // {{{ rm()
  139.  
  140.     /**
  141.     * remove variable from memory
  142.     *
  143.     * @param string $name  name of the variable
  144.     *
  145.     * @return bool true on success
  146.     * @access public
  147.     */
  148.     function rm($name)
  149.     {
  150.         $lh = $this->_lock();
  151.  
  152.         $val = $this->get();
  153.         if (!is_array($val)) {
  154.             $val = array();
  155.         }
  156.         unset($val[$name]);
  157.         $val = serialize($val);
  158.  
  159.         return $this->_write($val, $lh);
  160.     }
  161.     // }}}
  162.     // {{{ _ftok()
  163.  
  164.     /**
  165.      * ftok emulation for Windows
  166.      *
  167.      * @param string $project project ID
  168.      *
  169.      * @access private
  170.      */
  171.     function _ftok($project)
  172.     {
  173.         if (function_exists('ftok')) {
  174.             return ftok(__FILE__, $project);
  175.         }
  176.  
  177.         $s = stat(__FILE__);
  178.         return sprintf("%u", (($s['ino'] & 0xffff) | (($s['dev'] & 0xff) << 16) |
  179.         (($project & 0xff) << 24)));
  180.     }
  181.     // }}}
  182.     // {{{ _write
  183.  
  184.     /**
  185.      * write to the shared memory
  186.      *
  187.      * @param string $val values of all variables
  188.      * @param resource $lh lock handler
  189.      *
  190.      * @return mixed PEAR_error or true on success
  191.      * @access private
  192.      */
  193.     function _write(&$val, &$lh)
  194.     {
  195.         $id  = shmop_open($this->_h, 'c', 0600, $this->_options['size']);
  196.         if ($id) {
  197.            $ret = shmop_write($id, $val, 0) == strlen($val);
  198.            shmop_close($id);
  199.            $this->_unlock($lh);
  200.            return $ret;
  201.         }
  202.  
  203.         $this->_unlock($lh);
  204.         return PEAR::raiseError('Cannot write to shmop.', 2);
  205.     }
  206.     // }}}
  207.     // {{{ &_lock()
  208.  
  209.     /**
  210.      * access locking function
  211.      *
  212.      * @return resource lock handler
  213.      * @access private
  214.      */
  215.     function &_lock()
  216.     {
  217.         if (function_exists('sem_get')) {
  218.             $fp = PHP_VERSION < 4.3 ? sem_get($this->_h, 1, 0600) : sem_get($this->_h, 1, 0600, 1);
  219.             sem_acquire ($fp);
  220.         } else {
  221.             $fp = fopen($this->_options['tmp'].'/sm_'.md5($this->_h), 'w');
  222.             flock($fp, LOCK_EX);
  223.         }
  224.  
  225.         return $fp;
  226.     }
  227.     // }}}
  228.     // {{{ _unlock()
  229.  
  230.     /**
  231.      * access unlocking function
  232.      *
  233.      * @param resource $fp lock handler
  234.      *
  235.      * @access private
  236.      */
  237.     function _unlock(&$fp)
  238.     {
  239.         if (function_exists('sem_get')) {
  240.             sem_release($fp);
  241.         } else {
  242.             fclose($fp);
  243.         }
  244.     }
  245.     // }}}
  246. }
  247. // }}}
  248. ?>