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 / Systemv.php < prev   
Encoding:
PHP Script  |  2008-07-02  |  4.5 KB  |  192 lines

  1. <?php
  2. /**
  3. *
  4. * The System V 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 System V extension
  26. * for interacting with System V 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_Systemv
  44.  
  45. class System_SharedMemory_Systemv extends System_SharedMemory_Common
  46. {
  47.     // {{{ properties
  48.     /**
  49.     * true if plugin was connected to backend
  50.     *
  51.     * @var bool
  52.     *
  53.     * @access private
  54.     */
  55.     var $_connected;
  56.  
  57.     /**
  58.     * handler for shmop_* functions
  59.     *
  60.     * @var string
  61.     *
  62.     * @access private
  63.     */
  64.     var $_h;
  65.     // }}}
  66.     // {{{ constructor
  67.  
  68.     /**
  69.      * Constructor. Init all variables.
  70.      *
  71.      * @param array $options
  72.      *
  73.      * @access public
  74.      */
  75.     function System_SharedMemory_Systemv($options)
  76.     {
  77.         extract($this->_default($options, array
  78.         (
  79.             'size'    => false,
  80.             'project' => 's',
  81.         )));
  82.  
  83.        if ($size === false) {
  84.            $this->_h = shm_attach($this->_ftok($project));
  85.        } else {
  86.            if ($size < SHMMIN || $size > SHMMAX) {
  87.                return $this->_connection = false;
  88.            }
  89.  
  90.            $this->_h = shm_attach($this->_ftok($project), $size);
  91.        }
  92.  
  93.        $this->_connection = true;
  94.     }
  95.     // }}}
  96.     // {{{ isConnected()
  97.  
  98.     /**
  99.      * returns true if plugin was 
  100.      * successfully connected to backend
  101.      *
  102.      * @return bool true if connected
  103.      * @access public
  104.      */
  105.     function isConnected()
  106.     {
  107.         return $this->_connected;
  108.     }
  109.     // }}}
  110.     // {{{ get()
  111.  
  112.     /**
  113.      * returns value of variable in shared mem
  114.      *
  115.      * @param int $name name of variable
  116.      *
  117.      * @return mixed value of the variable
  118.      * @access public
  119.      */
  120.     function get($name)
  121.     {
  122.         return shm_get_var($this->_h, $this->_s2i($name));
  123.     }
  124.     // }}}
  125.     // {{{ set()
  126.  
  127.     /**
  128.      * set value of variable in shared mem
  129.      *
  130.      * @param string $name  name of the variable
  131.      * @param string $value value of the variable
  132.      *
  133.      * @return bool true on success, false on fail
  134.      * @access public
  135.      */
  136.     function set($name, $value)
  137.     {
  138.         return shm_put_var($this->_h, $this->_s2i($name), $value);
  139.     }
  140.     // }}}
  141.     // {{{ rm()
  142.  
  143.     /**
  144.      * remove variable from memory
  145.      *
  146.      * @param string $name  name of the variable
  147.      *
  148.      * @return bool true on success, false on fail
  149.      * @access public
  150.      */
  151.     function rm($name)
  152.     {
  153.         return shm_remove_var($this->_h, $this->_s2i($name));
  154.     }
  155.     // }}}
  156.     // {{{ _ftok()
  157.  
  158.     /**
  159.      * ftok emulation for Windows
  160.      *
  161.      * @param string $project project ID
  162.      *
  163.      * @access private
  164.      */
  165.     function _ftok($project)
  166.     {
  167.         if (function_exists('ftok')) {
  168.             return ftok(__FILE__, $project);
  169.         }
  170.  
  171.         $s = stat(__FILE__);
  172.         return sprintf("%u", (($s['ino'] & 0xffff) | (($s['dev'] & 0xff) << 16) |
  173.         (($project & 0xff) << 24)));
  174.     }
  175.     // }}}
  176.     // {{{ _s2i()
  177.  
  178.     /**
  179.      * convert string to int
  180.      *
  181.      * @param string $name string to conversion
  182.      *
  183.      * @access private
  184.      */
  185.     function _s2i($name)
  186.     {
  187.         return unpack('N', str_pad($name, 4, "\0", STR_PAD_LEFT));
  188.     }
  189.     // }}}
  190. }
  191. // }}}
  192. ?>