30 | Thanks Navicat for supporting Open Source projects. 31 |
32 | 33 | 36 | 37 |41 | A great amount of time has been spent creating, crafting and maintaining this software, please consider donating. 42 |
43 | 44 |Donating helps ensure continued support, development and availability.
45 | 48 |'; 33 | print_r($rs); 34 | echo ''; 35 | echo $db->isCli(1); 36 | 37 | /** 38 | * delete the REF stored on DB or Cache 39 | */ 40 | $rs = $sessions->delSessionRef(1); 41 | echo $db->isCli(1); 42 | @$_SESSION['test']++; 43 | echo $_SESSION['test']; 44 | echo $db->isCli(1); 45 | 46 | # ----------------------------------------------------------------------------------------------------------------- 47 | echo PHP_EOL,str_repeat('-', 80),PHP_EOL,'Time: ',$timer->getPageLoadTime(),' - Memory: ',$timer->getMemoryUsage(1),PHP_EOL,str_repeat('-', 80),PHP_EOL; 48 | -------------------------------------------------------------------------------- /examples/sessions/curl.php: -------------------------------------------------------------------------------- 1 | =200 && $httpcode<300) ? true :false; 29 | } 30 | } 31 | 32 | /** 33 | * benchmark for sessions 34 | */ 35 | for ($i=0; $i < 100; $i++) { 36 | for ($j=0; $j < 30; $j++) { 37 | echo checkSite($url,1).PHP_EOL; 38 | } 39 | @unlink('/tmp/cookie.txt'); 40 | } 41 | 42 | # ----------------------------------------------------------------------------------------------------------------- 43 | echo PHP_EOL,str_repeat('-', 80),PHP_EOL,'Time: ',$timer->getPageLoadTime(),' - Memory: ',$timer->getMemoryUsage(1),PHP_EOL,str_repeat('-', 80),PHP_EOL; 44 | -------------------------------------------------------------------------------- /examples/sessions/sessions-redis.php: -------------------------------------------------------------------------------- 1 | getSessionRef($GLOBALS['UID'])) { 23 | // user is online 24 | exit('user already logged'); 25 | } else { 26 | $sessions->regenerate_id(true); 27 | } 28 | 29 | /** 30 | * You can use $_SESSIONS like always 31 | */ 32 | $_SESSIONS['foo'] = 'bar'; 33 | 34 | echo session_id(); 35 | # ----------------------------------------------------------------------------------------------------------------- 36 | echo PHP_EOL, str_repeat('-', 80), PHP_EOL, 'Time: ',$timer->getPageLoadTime(),' - Memory: ',$timer->getMemoryUsage(1),PHP_EOL,str_repeat('-', 80),PHP_EOL; 37 | -------------------------------------------------------------------------------- /examples/sessions/sessions.php: -------------------------------------------------------------------------------- 1 | regenerate_id(4); 35 | } 36 | 37 | $_SESSION['test'] = 1 + @$_SESSION['test']; 38 | 39 | $rs = $db->FetchMode('ASSOC')->PGetRow('SELECT * FROM dalmp_sessions WHERE ref=?', $GLOBALS['UID']); 40 | print_r($rs); 41 | 42 | echo $db->isCli(1),session_id().$db->isCli(1),$_SESSION['test']; 43 | 44 | # ----------------------------------------------------------------------------------------------------------------- 45 | echo PHP_EOL,str_repeat('-', 80),PHP_EOL,'Time: ',$timer->getPageLoadTime(),' - Memory: ',$timer->getMemoryUsage(1),PHP_EOL,str_repeat('-', 80),PHP_EOL; 46 | -------------------------------------------------------------------------------- /examples/sessions/sessions_1.php: -------------------------------------------------------------------------------- 1 | regenerate_id(4); 16 | } 17 | 18 | $_SESSION['test'] = 1 + @$_SESSION['test']; 19 | 20 | echo $_SESSION['test']; 21 | 22 | echo session_id(); 23 | 24 | echo '
'; 25 | # ----------------------------------------------------------------------------------------------------------------- 26 | echo PHP_EOL, str_repeat('-', 80), PHP_EOL, 'Time: ',$timer->getPageLoadTime(),' - Memory: ',$timer->getMemoryUsage(1),PHP_EOL,str_repeat('-', 80),PHP_EOL; 27 | -------------------------------------------------------------------------------- /examples/sessions/sqlite-encryption.php: -------------------------------------------------------------------------------- 1 | getPageLoadTime(),' - Memory: ',$timer->getMemoryUsage(1),PHP_EOL,str_repeat('-', 80),PHP_EOL; 65 | -------------------------------------------------------------------------------- /examples/sessions/sqliteSessions.php: -------------------------------------------------------------------------------- 1 | getPageLoadTime(),' - Memory: ',$timer->getMemoryUsage(1),PHP_EOL,str_repeat('-', 80),PHP_EOL; 30 | -------------------------------------------------------------------------------- /examples/world.sql.gz: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nbari/DALMP/49f74eb5b61a79d396c10ff58a9311f3e6f2f0c5/examples/world.sql.gz -------------------------------------------------------------------------------- /phpunit.xml.dist: -------------------------------------------------------------------------------- 1 | 2 |3 | 39 | -------------------------------------------------------------------------------- /src/DALMP/Cache.php: -------------------------------------------------------------------------------- 1 | 8 | * @package DALMP 9 | * @license BSD License 10 | * @version 3.0.3 11 | */ 12 | class Cache 13 | { 14 | 15 | /** 16 | * CacheInterface instance 17 | * 18 | * @var CacheInterface 19 | * @access private 20 | */ 21 | private $cache_object; 22 | 23 | public function __construct(Cache\CacheInterface $object) 24 | { 25 | $this->cache_object = $object; 26 | } 27 | 28 | public function __call($method, $args) 29 | { 30 | if (!method_exists($this->cache_object, $method)) { 31 | throw new \Exception("Undefined method {$method}"); 32 | } 33 | 34 | return call_user_func_array(array($this->cache_object, $method), $args); 35 | } 36 | 37 | } 38 | -------------------------------------------------------------------------------- /src/DALMP/Cache/APC.php: -------------------------------------------------------------------------------- 1 | 8 | * @package DALMP 9 | * @license BSD License 10 | * @version 3.0.3 11 | */ 12 | class APC implements CacheInterface 13 | { 14 | protected $cache; 15 | 16 | /** 17 | * Constructor 18 | * 19 | * @param string $host 20 | * @param int $port 21 | * @param int $timeout 22 | * @param int $compress 23 | */ 24 | public function __construct() 25 | { 26 | if (!extension_loaded('apc') && !ini_get('apc.enabled')) { 27 | throw new \Exception(__CLASS__ . ': APC PECL extension not loaded or enabled!'); 28 | } 29 | } 30 | 31 | /** 32 | * Store data at the server 33 | * 34 | * @param string $key 35 | * @param string $value 36 | * @param int $expire time in seconds(default is 0 meaning unlimited) 37 | */ 38 | public function set($key, $value, $expire = 0) 39 | { 40 | return apc_store($key, $value, $expire); 41 | } 42 | 43 | /** 44 | * Retrieve item from the server 45 | * 46 | * @param string $key 47 | */ 48 | public function Get($key) 49 | { 50 | return apc_fetch($key); 51 | } 52 | 53 | /** 54 | * Delete item from the server 55 | * 56 | * @param string $key 57 | */ 58 | public function Delete($key) 59 | { 60 | return apc_delete($key); 61 | } 62 | 63 | /** 64 | * Flush cache 65 | */ 66 | public function Flush() 67 | { 68 | return apc_clear_cache('user'); 69 | } 70 | 71 | /** 72 | * Get cache stats 73 | */ 74 | public function Stats() 75 | { 76 | return apc_cache_info(); 77 | } 78 | 79 | /** 80 | * X execute/call custom methods 81 | * 82 | * @return cache object 83 | */ 84 | public function X() 85 | { 86 | return $this; 87 | } 88 | 89 | } 90 | -------------------------------------------------------------------------------- /src/DALMP/Cache/CacheInterface.php: -------------------------------------------------------------------------------- 1 | 8 | * @package DALMP 9 | * @license BSD License 10 | * @version 3.0.3 11 | */ 12 | interface CacheInterface 13 | { 14 | /** 15 | * Store data at the server 16 | * 17 | * @param string $key 18 | * @param string $value 19 | * @param int $ttl time in seconds(default is 0 meaning unlimited) 20 | * @return this 21 | */ 22 | public function Set($key, $value, $ttl = 0); 23 | 24 | /** 25 | * Retrieve item from the server 26 | * 27 | * @param string $key 28 | * @return mixed 29 | */ 30 | public function Get($key); 31 | 32 | /* 33 | * Delete item from the server 34 | * 35 | * @param string $key 36 | * @return bool 37 | */ 38 | public function Delete($key); 39 | 40 | /** 41 | * Flush cache 42 | * 43 | * @return bool 44 | */ 45 | public function Flush(); 46 | 47 | /** 48 | * Get cache stats 49 | * 50 | * @return bool 51 | */ 52 | public function Stats(); 53 | 54 | /** 55 | * X execute/call custom methods 56 | * 57 | * @return cache object 58 | */ 59 | public function X(); 60 | 61 | } 62 | -------------------------------------------------------------------------------- /src/DALMP/Cache/Memcache.php: -------------------------------------------------------------------------------- 1 | 8 | * @package DALMP 9 | * @license BSD License 10 | * @version 3.0.3 11 | */ 12 | class Memcache implements CacheInterface 13 | { 14 | private $host = '127.0.0.1'; 15 | private $port = 11211; 16 | private $timeout = 1; 17 | private $compress = false; 18 | protected $cache; 19 | 20 | /** 21 | * Constructor 22 | * 23 | * @param string $host 24 | * @param int $port 25 | * @param int $timeout 26 | * @param int $compress 27 | */ 28 | public function __construct() 29 | { 30 | $args = func_get_args(); 31 | 32 | if ($args) { 33 | $this->host = isset($args[0]) ? $args[0] : '127.0.0.1'; 34 | $this->port = isset($args[1]) ? (int) $args[1] : 11211; 35 | $this->timeout = isset($args[2]) ? (int) $args[2] : 1; 36 | if (isset($args[3])) { 37 | $this->compress = MEMCACHE_COMPRESSED; 38 | } 39 | } 40 | } 41 | 42 | /** 43 | * Store data at the server 44 | * 45 | * @param string $key 46 | * @param string $value 47 | * @param int $expire time in seconds(default is 0 meaning unlimited) 48 | */ 49 | public function Set($key, $value, $expire = 0) 50 | { 51 | if ($this->connect()) { 52 | ($this->compress === false) && $this->cache->setCompressThreshold(0); 53 | 54 | return $this->cache->set($key, $value, $this->compress, $expire); 55 | } else { 56 | return false; 57 | } 58 | } 59 | 60 | /** 61 | * Retrieve item from the server 62 | * 63 | * @param string $key 64 | */ 65 | public function Get($key) 66 | { 67 | return $this->connect() ? $this->cache->get($key) : false; 68 | } 69 | 70 | /** 71 | * Delete item from the server 72 | * 73 | * @param string $key 74 | */ 75 | public function Delete($key) 76 | { 77 | return $this->connect() ? $this->cache->delete($key) : false; 78 | } 79 | 80 | /** 81 | * Flush cache 82 | */ 83 | public function Flush() 84 | { 85 | return $this->connect() ? $this->cache->flush() : false; 86 | } 87 | 88 | /** 89 | * Get cache stats 90 | */ 91 | public function Stats() 92 | { 93 | return $this->connect() ? $this->cache->getStats() : false; 94 | } 95 | 96 | /** 97 | * X execute/call custom methods 98 | * 99 | * @return cache object 100 | */ 101 | public function X() 102 | { 103 | return $this->connect() ? $this->cache : false; 104 | } 105 | 106 | /** 107 | * try to establish a connection 108 | */ 109 | private function connect() 110 | { 111 | if ($this->cache instanceof MemCache) { 112 | return true; 113 | } else { 114 | if (!extension_loaded('memcache')) { 115 | throw new \Exception(__CLASS__ . 'Memcache PECL extension not loaded! - http://pecl.php.net/package/memcache'); 116 | } 117 | 118 | $memcache = new \Memcache(); 119 | 120 | /** 121 | * if a / found try to connect via socket 122 | */ 123 | if (strpos($this->host, '/') !== false) { 124 | return $this->cache = $memcache->connect($this->host) ? $memcache : false; 125 | } else { 126 | return $this->cache = $memcache->connect($this->host, $this->port, $this->timeout) ? $memcache : false; 127 | } 128 | } 129 | } 130 | 131 | } 132 | -------------------------------------------------------------------------------- /src/DALMP/Cache/Redis.php: -------------------------------------------------------------------------------- 1 | 8 | * @package DALMP 9 | * @license BSD License 10 | * @version 3.0.3 11 | */ 12 | class Redis implements CacheInterface 13 | { 14 | private $host = '127.0.0.1'; 15 | private $port = 6379; 16 | private $timeout = 1; 17 | protected $cache; 18 | 19 | /** 20 | * Constructor 21 | * 22 | * @param string $host 23 | * @param int $port 24 | * @param int $timeout 25 | */ 26 | public function __construct() 27 | { 28 | $args = func_get_args(); 29 | 30 | if ($args) { 31 | $this->host = isset($args[0]) ? $args[0] : '127.0.0.1'; 32 | $this->port = isset($args[1]) ? (int) $args[1] : 6379; 33 | $this->timeout = isset($args[2]) ? (int) $args[2] : 1; 34 | } 35 | } 36 | 37 | /** 38 | * Store data at the server 39 | * 40 | * @param string $key 41 | * @param string $value 42 | * @param int $expire time in seconds(default is 0 meaning unlimited) 43 | */ 44 | public function set($key, $value, $expire = 0) 45 | { 46 | if ($this->connect()) { 47 | if ($expire == 0 || $expire == -1) { 48 | return (bool) $this->cache->set($key, serialize($value)); 49 | } else { 50 | return (bool) $this->cache->setex($key, $expire, serialize($value)); 51 | } 52 | } else { 53 | return false; 54 | } 55 | } 56 | 57 | /** 58 | * Retrieve item from the server 59 | * 60 | * @param string $key 61 | */ 62 | public function Get($key) 63 | { 64 | return $this->connect() ? unserialize($this->cache->get($key)) : false; 65 | } 66 | 67 | /** 68 | * Delete item from the server 69 | * 70 | * @param string $key 71 | */ 72 | public function Delete($key) 73 | { 74 | return $this->connect() ? (bool) $this->cache->delete($key) : false; 75 | } 76 | 77 | /** 78 | * Flush cache 79 | */ 80 | public function Flush() 81 | { 82 | return $this->connect() ? (bool) $this->cache->flushDB() : false; 83 | } 84 | 85 | /** 86 | * Get cache stats 87 | */ 88 | public function Stats() 89 | { 90 | return $this->connect() ? $this->cache->info() : false; 91 | } 92 | 93 | /** 94 | * X execute/call custom methods 95 | * 96 | * @return cache object 97 | */ 98 | public function X() 99 | { 100 | return $this->connect() ? $this->cache : false; 101 | } 102 | 103 | /** 104 | * try to establish a connection 105 | */ 106 | private function connect() 107 | { 108 | if ($this->cache instanceof Redis) { 109 | return true; 110 | } else { 111 | if (!extension_loaded('redis')) { 112 | throw new \Exception(__CLASS__ . 'redis extension not loaded! - http://github.com/nicolasff/phpredis'); 113 | } 114 | 115 | $redis = new \Redis(); 116 | try { 117 | /** 118 | * if a / found try to connect via socket 119 | */ 120 | if (strpos($this->host, '/') !== false) { 121 | return $this->cache = $redis->connect($this->host) ? $redis : false; 122 | } else { 123 | return $this->cache = $redis->connect($this->host, $this->port, $this->timeout) ? $redis : false; 124 | } 125 | } catch (\RedisException $e) { 126 | trigger_error('ERROR ->' . __METHOD__ . $e->getMessage(), E_USER_NOTICE); 127 | 128 | return false; 129 | } 130 | } 131 | } 132 | 133 | } 134 | -------------------------------------------------------------------------------- /src/DALMP/DI.php: -------------------------------------------------------------------------------- 1 | 8 | * @package DALMP 9 | * @license BSD License 10 | * @version 3.0.3 11 | */ 12 | class DI extends abstractDI 13 | { 14 | public function __construct() 15 | { 16 | $this->c['database'] = $this->share(function() { 17 | $obj = new \ReflectionClass('DALMP\Database'); 18 | 19 | return $obj->newInstanceArgs(func_get_args()); 20 | }); 21 | 22 | $this->c['cache_memcache'] = $this->share(function() { 23 | $obj = new \ReflectionClass('DALMP\Cache\Memcache'); 24 | 25 | return $obj->newInstanceArgs(func_get_args()); 26 | }); 27 | 28 | $this->c['cache_redis'] = $this->share(function() { 29 | $obj = new \ReflectionClass('DALMP\Cache\Redis'); 30 | 31 | return $obj->newInstanceArgs(func_get_args()); 32 | }); 33 | 34 | $this->c['cache_disk'] = $this->share(function() { 35 | $obj = new \ReflectionClass('DALMP\Cache\Disk'); 36 | 37 | return $obj->newInstanceArgs(func_get_args()); 38 | }); 39 | 40 | $this->c['cache'] = $this->share(function($backend) { 41 | return new Cache($backend); 42 | }); 43 | 44 | $this->c['sessions_memcache'] = $this->share(function() { 45 | $obj = new \ReflectionClass('DALMP\Sessions\Memcache'); 46 | 47 | return $obj->newInstanceArgs(func_get_args()); 48 | }); 49 | 50 | $this->c['sessions_redis'] = $this->share(function() { 51 | $obj = new \ReflectionClass('DALMP\Sessions\Redis'); 52 | 53 | return $obj->newInstanceArgs(func_get_args()); 54 | }); 55 | 56 | $this->c['sessions_mysql'] = $this->share(function() { 57 | $obj = new \ReflectionClass('DALMP\Sessions\MySQL'); 58 | 59 | return $obj->newInstanceArgs(func_get_args()); 60 | }); 61 | 62 | $this->c['sessions'] = $this->share(function($backend) { 63 | return new Sessions($backend); 64 | }); 65 | 66 | } 67 | 68 | } 69 | -------------------------------------------------------------------------------- /src/DALMP/Loader.php: -------------------------------------------------------------------------------- 1 | 8 | * @package DALMP 9 | * @license BSD License 10 | * @version 3.0.3 11 | */ 12 | class Loader 13 | { 14 | public static function autoload($className) 15 | { 16 | $className = ltrim($className, '\\'); 17 | $fileName = DALMP_DIR . DIRECTORY_SEPARATOR . str_replace('\\', DIRECTORY_SEPARATOR, $className) . '.php'; 18 | if (is_readable($fileName)) { 19 | require $fileName; 20 | } 21 | } 22 | 23 | public static function register() 24 | { 25 | spl_autoload_register(array('DALMP\Loader', 'autoload')); 26 | } 27 | 28 | } 29 | -------------------------------------------------------------------------------- /src/DALMP/Queue.php: -------------------------------------------------------------------------------- 1 | 8 | * @package DALMP 9 | * @license BSD License 10 | * @version 3.0.3 11 | */ 12 | class Queue 13 | { 14 | /** 15 | * QueueInterface instance 16 | * 17 | * @var QueueInterface 18 | * @access private 19 | */ 20 | private $cache_object; 21 | 22 | public function __construct(Queue\QueueInterface $object) 23 | { 24 | $this->cache_object = $object; 25 | } 26 | 27 | public function __call($method, $args) 28 | { 29 | if (!method_exists($this->cache_object, $method)) { 30 | throw new \Exception("Undefined method {$method}"); 31 | } 32 | 33 | return call_user_func_array(array($this->cache_object, $method), $args); 34 | } 35 | 36 | } 37 | -------------------------------------------------------------------------------- /src/DALMP/Queue/Gearman.php: -------------------------------------------------------------------------------- 1 | 8 | * @package DALMP 9 | * @license BSD License 10 | * @version 3.0.3 11 | */ 12 | class Gearman implements QueueInterface 13 | { 14 | /** 15 | * queue name 16 | * 17 | * @var string 18 | */ 19 | private $queue_name; 20 | 21 | /** 22 | * gearman host 23 | * @var string 24 | */ 25 | private $host; 26 | 27 | /** 28 | * gearman port 29 | * 30 | * @var int 31 | */ 32 | private $port; 33 | 34 | /** 35 | * Constructor 36 | * 37 | * @param string $filename 38 | * @param string $queue 39 | * @param string $enc_key 40 | */ 41 | public function __construct($queue_name = 'dalmp_queue', $host = '127.0.0.1', $port = 4730) 42 | { 43 | $this->host = $host; 44 | $this->port = $port; 45 | $this->queue_name = $queue_name; 46 | } 47 | 48 | /** 49 | * enqueue 50 | * 51 | * @param string $value 52 | * @return boolean 53 | */ 54 | public function enqueue($value) 55 | { 56 | $gm = new \GearmanClient(); 57 | $gm->addServer($this->host, $this->port); 58 | $gm->queue_name = $this->queue_name; 59 | 60 | if ($gm->ping('ping')) { 61 | $job_handle = $gmclient->doBackground($this->queue_name, json_encode($value), md5($value)); 62 | 63 | return ($this->gmclient->returnCode() != GEARMAN_SUCCESS) ? false : true; 64 | } else { 65 | return false; 66 | } 67 | } 68 | 69 | /** 70 | * dequeue 71 | * 72 | * @param string $key 73 | */ 74 | public function dequeue($limit = false) 75 | { 76 | $gm= new \GermanWorker(); 77 | $gm->addServer($this->host, $this->port); 78 | $gm->addFunction($this->queue_name, XXX); 79 | } 80 | 81 | /** 82 | * delete element from queue 83 | * 84 | * @param string $value 85 | */ 86 | public function delete($key) 87 | { 88 | } 89 | 90 | /** 91 | * 92 | * X execute/call custom methods 93 | * 94 | * @return queue object 95 | */ 96 | public function X() 97 | { 98 | return $this->gmclient; 99 | } 100 | 101 | } 102 | -------------------------------------------------------------------------------- /src/DALMP/Queue/QueueInterface.php: -------------------------------------------------------------------------------- 1 | 8 | * @package DALMP 9 | * @license BSD License 10 | * @version 3.0.3 11 | */ 12 | interface QueueInterface 13 | { 14 | /** 15 | * enqueue 16 | * 17 | * @param string $value 18 | * @return boolean 19 | */ 20 | public function enqueue($value); 21 | 22 | /** 23 | * dequeue 24 | * 25 | * @param int $limit returns {$limit} entries on the queue in ASC order 26 | * @return array 27 | */ 28 | public function dequeue($limit = false); 29 | 30 | /** 31 | * delete element from queue 32 | * 33 | * @param string $key 34 | * @return boolean 35 | */ 36 | public function delete($key); 37 | 38 | /** 39 | * X execute/call custom methods 40 | * 41 | * @return queue object 42 | */ 43 | public function X(); 44 | 45 | } 46 | -------------------------------------------------------------------------------- /src/DALMP/Queue/SQLite.php: -------------------------------------------------------------------------------- 1 | 8 | * @package DALMP 9 | * @license BSD License 10 | * @version 3.0.3 11 | */ 12 | class SQLite implements QueueInterface 13 | { 14 | /** 15 | * filename - Path to the SQLite database, or :memory: to use in-memory 16 | * database. 17 | * 18 | * @var string 19 | */ 20 | private $filename; 21 | 22 | /** 23 | * queue name 24 | * 25 | * @var string 26 | */ 27 | private $queue_name; 28 | 29 | /** 30 | * enc_key 31 | * 32 | * @var string 33 | */ 34 | private $enc_key = false; 35 | 36 | /** 37 | * Constructor 38 | * 39 | * @param string $filename 40 | * @param string $queue 41 | * @param string $enc_key 42 | */ 43 | public function __construct($filename, $queue_name = 'default', $enc_key = null) 44 | { 45 | $sdb = new \SQLite3($filename); 46 | $sdb->busyTimeout(2000); 47 | 48 | $this->filename = $filename; 49 | $this->queue_name = $queue_name; 50 | 51 | if ($enc_key) { 52 | if ($this->sdb->exec(sprintf("PRAGMA key='%s'", $enc_key))) { 53 | $this->enc_key = $enc_key; 54 | } 55 | } 56 | 57 | $sdb->exec('PRAGMA synchronous=OFF; PRAGMA temp_store=MEMORY; PRAGMA journal_mode=MEMORY'); 58 | $sdb->exec('CREATE TABLE IF NOT EXISTS queues (id INTEGER PRIMARY KEY, queue VARCHAR (64) NOT null, data TEXT, cdate DATE)'); 59 | $sdb->busyTimeout(0); 60 | $sdb->close(); 61 | } 62 | 63 | /** 64 | * enqueue 65 | * 66 | * @param string $value 67 | * @return boolean 68 | */ 69 | public function enqueue($value) 70 | { 71 | $sdb = new \SQLite3($this->filename); 72 | $sdb->busyTimeout(2000); 73 | if ($this->enc_key) { 74 | $sdb->exec(sprintf("PRAGMA key='%s'", $enc_key)); 75 | } 76 | 77 | $stmt = $sdb->prepare('INSERT INTO queues VALUES (null, ?, ?, ?)'); 78 | $stmt->bindValue(1, $this->queue_name, SQLITE3_TEXT); 79 | $stmt->bindValue(2, base64_encode($value), SQLITE3_TEXT); 80 | $stmt->bindValue(3, @date('Y-m-d H:i:s'), SQLITE3_BLOB); 81 | 82 | if (!$stmt->execute()) { 83 | throw new \ErrorException(sprintf('Could not save: [ %s ] on queue [ %s ] in [ %s ]', $value, $this->queue_name, $this->filename)); 84 | } 85 | 86 | $sdb->busyTimeout(0); 87 | $sdb->close(); 88 | 89 | return true; 90 | } 91 | 92 | /** 93 | * dequeue 94 | * 95 | * @param string $key 96 | */ 97 | public function dequeue($limit = false) 98 | { 99 | $sdb = new \SQLite3($this->filename); 100 | $sdb->busyTimeout(2000); 101 | if ($this->enc_key) { 102 | $sdb->exec(sprintf("PRAGMA key='%s'", $enc_key)); 103 | } 104 | 105 | if ($limit) { 106 | $stmt = $sdb->prepare('SELECT * FROM queues WHERE queue = ? LIMIT ?'); 107 | $stmt->bindValue(1, $this->queue_name, SQLITE3_TEXT); 108 | $stmt->bindValue(2, $limit, SQLITE3_INTEGER); 109 | } else { 110 | $stmt = $sdb->prepare('SELECT * FROM queues WHERE queue = ?'); 111 | $stmt->bindValue(1, $this->queue_name, SQLITE3_TEXT); 112 | } 113 | 114 | $rs = $stmt->execute(); 115 | 116 | $rows = array(); 117 | 118 | if ($rs) { 119 | while ($row = $rs->fetchArray(SQLITE3_ASSOC)) { 120 | $rows[$row['id']] = array('id' => $row['id'], 'queue' => $row['queue'], 'data' => $row['data'], 'cdate' => $row['cdate']); 121 | } 122 | } 123 | 124 | return $rows; 125 | } 126 | 127 | /** 128 | * delete element from queue 129 | * 130 | * @param string $value 131 | */ 132 | public function delete($key) 133 | { 134 | $sdb = new \SQLite3($this->filename); 135 | $sdb->busyTimeout(2000); 136 | if ($this->enc_key) { 137 | $sdb->exec(sprintf("PRAGMA key='%s'", $enc_key)); 138 | } 139 | 140 | $stmt = $sdb->prepare('DELETE FROM queues WHERE queue = ? AND id = ?'); 141 | $stmt->bindValue(1, $this->queue_name, SQLITE3_TEXT); 142 | $stmt->bindValue(2, $key, SQLITE3_INTEGER); 143 | 144 | $sdb->busyTimeout(0); 145 | 146 | return (bool) $stmt->execute(); 147 | } 148 | 149 | /** 150 | * 151 | * X execute/call custom methods 152 | * 153 | * @return queue object 154 | */ 155 | public function X() {} 156 | 157 | } 158 | -------------------------------------------------------------------------------- /src/DALMP/Sessions.php: -------------------------------------------------------------------------------- 1 | 8 | * @package DALMP 9 | * @license BSD License 10 | * @version 3.0.3 11 | */ 12 | class Sessions 13 | { 14 | /** 15 | * session_handler 16 | * 17 | * @access private 18 | * @SessionHandlerInterface 19 | */ 20 | private $session_handler; 21 | 22 | /** 23 | * construct - set the sesion save handler 24 | * 25 | * @param SessionHandlerInterface $session_handler 26 | * @param hash_algo $session_hash 27 | */ 28 | public function __construct($session_handler = false, $hash_algo = 'sha256') 29 | { 30 | if (!$session_handler) { 31 | $this->session_handler = new Sessions\SQLite(); 32 | } else { 33 | if ($session_handler instanceof \SessionHandlerInterface) { 34 | $this->session_handler = $session_handler; 35 | } else { 36 | throw new \InvalidArgumentException((string) $session_handler . ' is not an instance of SessionHandlerInterface'); 37 | } 38 | } 39 | 40 | ini_set('session.gc_maxlifetime', defined('DALMP_SESSIONS_MAXLIFETIME') ? DALMP_SESSIONS_MAXLIFETIME : get_cfg_var('session.gc_maxlifetime')); 41 | ini_set('session.use_cookies', 1); 42 | ini_set('session.use_only_cookies', 1); 43 | ini_set('session.cookie_httponly', 1); 44 | ini_set('session.use_trans_sid', 0); 45 | ini_set('session.hash_bits_per_character', 5); 46 | ini_set('session.hash_function', in_array($hash_algo, hash_algos()) ? $hash_algo : 1); 47 | ini_set('session.name', 'DALMP'); 48 | 49 | session_set_save_handler($this->session_handler, true); 50 | session_start(); 51 | } 52 | 53 | /** 54 | * regenerate id - regenerate sessions and create a fingerprint, helps to 55 | * prevent HTTP session hijacking attacks. 56 | * 57 | * @param boolean $use_IP 58 | * @return boolean 59 | */ 60 | public function regenerate_id($use_IP = true) 61 | { 62 | $fingerprint = @$_SERVER['HTTP_ACCEPT'] . @$_SERVER['HTTP_USER_AGENT'] . @$_SERVER['HTTP_ACCEPT_ENCODING'] . @$_SERVER['HTTP_ACCEPT_LANGUAGE']; 63 | 64 | if ($use_IP) { 65 | $fingerprint .= @$_SERVER['REMOTE_ADDR']; 66 | } 67 | 68 | $fingerprint = sha1('DALMP' . $fingerprint); 69 | 70 | if ((isset($_SESSION['fingerprint']) && $_SESSION['fingerprint'] != $fingerprint)) { 71 | $_SESSION = array(); 72 | session_destroy(); 73 | } 74 | 75 | if (session_regenerate_id(true)) { 76 | $_SESSION['fingerprint'] = $fingerprint; 77 | 78 | return true; 79 | } else { 80 | return false; 81 | } 82 | } 83 | 84 | /** 85 | * to handle session Refs per session_handler 86 | */ 87 | public function __call($method, $args) 88 | { 89 | if (!method_exists($this->session_handler, $method)) { 90 | throw new \Exception("Undefined method {$method}"); 91 | } 92 | 93 | return call_user_func_array(array($this->session_handler, $method), $args); 94 | } 95 | 96 | } 97 | -------------------------------------------------------------------------------- /src/DALMP/Sessions/Files.php: -------------------------------------------------------------------------------- 1 | 8 | * @package DALMP 9 | * @license BSD License 10 | * @version 3.0.3 11 | */ 12 | class Files implements \SessionHandlerInterface 13 | { 14 | /** 15 | * path to store sessions 16 | * 17 | * @access private 18 | * @var string 19 | */ 20 | private $sessions_dir; 21 | 22 | /** 23 | * constructor 24 | * 25 | * @param string $dir 26 | */ 27 | public function __construct($sessions_dir = false) 28 | { 29 | if (!$sessions_dir) { 30 | $sessions_dir = defined('DALMP_SESSIONS_DIR') ? DALMP_SESSIONS_DIR : '/tmp/dalmp_sessions'; 31 | } 32 | 33 | if (!is_writable($sessions_dir)) { 34 | if (!is_dir($sessions_dir) && !mkdir($sessions_dir, 0700, true)) { 35 | throw new \InvalidArgumentException($sessions_dir . ' not accessible'); 36 | } 37 | } 38 | 39 | $this->sessions_dir = $dir; 40 | } 41 | 42 | public function close() 43 | { 44 | return true; 45 | } 46 | 47 | public function destroy($session_id) 48 | { 49 | $sess_path = sprintf('%s/%s/%s/%s', $this->sessions_dir, substr($session_id, 0, 2), substr($session_id, 2, 2), substr($session_id, 4, 2)); 50 | $sess_file = sprintf('%s/%s', $sess_path , "{$session_id}.sess"); 51 | 52 | if (file_exists($sess_file)) { 53 | unlink($sess_file); 54 | } 55 | 56 | return true; 57 | } 58 | 59 | public function gc($maxlifetime) 60 | { 61 | $session_files = $this->rsearch($this->sessions_dir, '#^.*\.sess$#'); 62 | 63 | foreach ($session_files as $file) { 64 | if (filemtime($file) + $maxlifetime < time() && file_exists($file)) { 65 | unlink($file); 66 | } 67 | } 68 | 69 | return true; 70 | } 71 | 72 | public function open($save_path, $name) 73 | { 74 | return true; 75 | } 76 | 77 | public function read($session_id) 78 | { 79 | $sess_path = sprintf('%s/%s/%s/%s', $this->sessions_dir, substr($session_id, 0, 2), substr($session_id, 2, 2), substr($session_id, 4, 2)); 80 | 81 | if (!is_dir($sess_path) && !mkdir($sess_path, 0700, true)) { 82 | throw new \Exception("$sess_path not accessible"); 83 | } 84 | 85 | $sess_file = sprintf('%s/%s', $sess_path , "{$session_id}.sess"); 86 | 87 | return (string) @file_get_contents($sess_file); 88 | } 89 | 90 | public function write($session_id, $session_data) 91 | { 92 | $sess_path = sprintf('%s/%s/%s/%s', $this->sessions_dir, substr($session_id, 0, 2), substr($session_id, 2, 2), substr($session_id, 4, 2)); 93 | 94 | if (!is_dir($sess_path) && !mkdir($sess_path, 0700, true)) { 95 | throw new \Exception("$sess_path not accessible"); 96 | } 97 | 98 | $sess_file = sprintf('%s/%s', $sess_path , "{$session_id}.sess"); 99 | 100 | return file_put_contents($sess_file, $session_data) === false ? false : true; 101 | } 102 | 103 | /** 104 | * getSessionsRefs 105 | * 106 | * @param int $expiry 107 | * @return array of sessions containing any reference 108 | */ 109 | public function getSessionsRefs($expired_sessions = false) 110 | { 111 | $refs = array(); 112 | 113 | return false; 114 | } 115 | 116 | /** 117 | * getSessionRef 118 | * 119 | * @param string $ref 120 | * @return array of session containing a specific reference 121 | */ 122 | public function getSessionRef($ref) 123 | { 124 | return false; 125 | } 126 | 127 | /** 128 | * delSessionsRef - delete sessions containing a specific reference 129 | * 130 | * @param string $ref 131 | * @return boolean 132 | */ 133 | public function delSessionRef($ref) 134 | { 135 | return false; 136 | } 137 | 138 | /** 139 | * recursive dir search 140 | * 141 | * @param string $folder 142 | * @param string $pattern example: '#^.*\.sess$#' 143 | */ 144 | public function rsearch($folder, $pattern) 145 | { 146 | $dir = new RecursiveDirectoryIterator($folder); 147 | $ite = new RecursiveIteratorIterator($dir); 148 | $files = new RegexIterator($ite, $pattern, RegexIterator::GET_MATCH); 149 | $fileList = array(); 150 | foreach ($files as $file) { 151 | $fileList = array_merge($fileList, $file); 152 | } 153 | 154 | return $fileList; 155 | } 156 | 157 | } 158 | -------------------------------------------------------------------------------- /src/DALMP/Sessions/MySQL.php: -------------------------------------------------------------------------------- 1 | 8 | * @package DALMP 9 | * @license BSD License 10 | * @version 3.0.3 11 | */ 12 | class MySQL implements \SessionHandlerInterface 13 | { 14 | /** 15 | * DALMP\Database instance 16 | * 17 | * @access protected 18 | * @var DALMP\Database 19 | */ 20 | protected $DB; 21 | 22 | /** 23 | * REF - field used for storing references 24 | * 25 | * @access private 26 | * @var mixed 27 | */ 28 | private $dalmp_sessions_ref; 29 | 30 | /** 31 | * table to use for sessions 32 | * @access private 33 | * @var mixed 34 | */ 35 | private $dalmp_sessions_table = 'dalmp_sessions'; 36 | 37 | /** 38 | * constructor 39 | * 40 | * @param DALMP\Database $db instance 41 | * @param string $sessions_ref global variable to be stored as reference 42 | */ 43 | public function __construct(\DALMP\Database $DB, $sessions_ref = 'UID') 44 | { 45 | $this->DB = $DB; 46 | $this->dalmp_sessions_ref = defined('DALMP_SESSIONS_REF') ? DALMP_SESSIONS_REF : $sessions_ref; 47 | 48 | if (defined('DALMP_SESSIONS_TABLE')) { 49 | $this->dalmp_sessions_table = DALMP_SESSIONS_TABLE; 50 | } 51 | } 52 | 53 | public function close() 54 | { 55 | return true; 56 | } 57 | 58 | public function destroy($session_id) 59 | { 60 | $sql = 'DELETE FROM ' . $this->dalmp_sessions_table . ' WHERE sid=?'; 61 | 62 | return $this->DB->PExecute($sql, $session_id); 63 | } 64 | 65 | public function gc($maxlifetime) 66 | { 67 | $sql = 'DELETE FROM ' . $this->dalmp_sessions_table . ' WHERE expiry < UNIX_TIMESTAMP()'; 68 | $this->DB->Execute($sql); 69 | 70 | $sql = 'OPTIMIZE TABLE ' . $this->dalmp_sessions_table; 71 | $this->DB->Execute($sql); 72 | 73 | return true; 74 | } 75 | 76 | public function open($save_path, $name) 77 | { 78 | return true; 79 | } 80 | 81 | public function read($session_id) 82 | { 83 | return ($rs = $this->DB->PGetOne('SELECT data FROM ' . $this->dalmp_sessions_table . ' WHERE sid=? AND expiry >=?', $session_id, time())) ? $rs : ''; 84 | } 85 | 86 | public function write($session_id, $session_data) 87 | { 88 | $ref = (isset($GLOBALS[$this->dalmp_sessions_ref]) && !empty($GLOBALS[$this->dalmp_sessions_ref])) ? $GLOBALS[$this->dalmp_sessions_ref] : null; 89 | 90 | $expiry = time() + ini_get('session.gc_maxlifetime'); 91 | 92 | $sql = "REPLACE INTO $this->dalmp_sessions_table (sid, expiry, data, ref) VALUES(?,?,?,?)"; 93 | 94 | return $this->DB->PExecute($sql, $session_id, $expiry, $session_data, $ref); 95 | } 96 | 97 | /** 98 | * getSessionsRefs 99 | * 100 | * @return array of sessions containing any reference 101 | */ 102 | public function getSessionsRefs() 103 | { 104 | $refs = array(); 105 | 106 | $db_refs = $this->DB->FetchMode('ASSOC')->GetAll("SELECT sid, ref, expiry FROM $this->dalmp_sessions_table WHERE ref IS NOT null"); 107 | 108 | if ($db_refs) { 109 | foreach ($db_refs as $value) { 110 | $refs[$value['sid']] = array($value['ref'] => $value['expiry']); 111 | } 112 | } 113 | 114 | return $refs; 115 | } 116 | 117 | /** 118 | * getSessionRef 119 | * 120 | * @param string $ref 121 | * @return array of session containing a specific reference 122 | */ 123 | public function getSessionRef($ref) 124 | { 125 | $refs = array(); 126 | 127 | $db_refs = $this->DB->PGetall('SELECT sid, ref, expiry FROM dalmp_sessions WHERE ref=?', $ref); 128 | 129 | if ($db_refs) { 130 | foreach ($db_refs as $value) { 131 | $refs[$value['sid']] = array($value['ref'] => $value['expiry']); 132 | } 133 | } 134 | 135 | return $refs; 136 | } 137 | 138 | /** 139 | * delSessionRef - delete sessions containing a specific reference 140 | * 141 | * @param string $ref 142 | * @return boolean 143 | */ 144 | public function delSessionRef($ref) 145 | { 146 | return $this->DB->PExecute('DELETE FROM ' . $this->dalmp_sessions_table . ' WHERE ref=?', $ref); 147 | } 148 | 149 | } 150 | -------------------------------------------------------------------------------- /src/DALMP/abstractDI.php: -------------------------------------------------------------------------------- 1 | 8 | * @package DALMP 9 | * @license BSD License 10 | * @version 3.0.3 11 | */ 12 | abstract class abstractDI 13 | { 14 | 15 | /** 16 | * Container for the classes 17 | * 18 | * @var array 19 | */ 20 | protected $c = array(); 21 | 22 | /** 23 | * Share the classes 24 | * 25 | * @param Closure $callable A closure to wrap for uniqueness 26 | * @return Closure The wrapped closure 27 | */ 28 | public function share(\Closure $callable) 29 | { 30 | return function () use ($callable) { 31 | static $object = null; 32 | if (is_null($object)) { 33 | $object = call_user_func_array($callable, func_get_args()); 34 | } 35 | 36 | return $object; 37 | }; 38 | } 39 | 40 | /** 41 | * Dispatch the classes 42 | * 43 | * @param string $name 44 | * @param string $args 45 | * @return chainable object 46 | */ 47 | public function __call($name, $args) 48 | { 49 | if (!isset($this->c[$name])) { 50 | throw new \InvalidArgumentException(sprintf('Class "%s" does not exist.', $name)); 51 | } 52 | 53 | $c = $this->c[$name]; 54 | 55 | return ($c instanceof \Closure) ? call_user_func_array($c, $args) : $c; 56 | } 57 | 58 | /** 59 | * return the Closures 60 | * 61 | * @param string Closure name 62 | * @return closure 63 | */ 64 | public function __get($key) 65 | { 66 | if (array_key_exists($key, $this->c)) { 67 | return $this->c[$key]; 68 | } 69 | } 70 | 71 | /** 72 | * Add an object to the container 73 | * 74 | * To share (load and later use) this can be used: 75 | * $di->addObject('name', $di->share(function () { 76 | * return new Foo(); 77 | * })); 78 | * 79 | * @param string $name 80 | * @param object $object 81 | * @return true on success exception on failure 82 | */ 83 | public function addObject($name, $object) 84 | { 85 | if (isset($this->c[$name])) { 86 | throw new \InvalidArgumentException(sprintf('Class "%s" already defined.', $name)); 87 | } 88 | 89 | return $this->c[$name] = $object; 90 | } 91 | 92 | } 93 | -------------------------------------------------------------------------------- /src/dalmp.php: -------------------------------------------------------------------------------- 1 | 8 | * @package DALMP 9 | * @license BSD License 10 | * @version 3.0.3 11 | */ 12 | 13 | if (defined('DALMP_DIR')) { 14 | return; 15 | } else { 16 | define('DALMP_DIR', dirname(__FILE__)); 17 | } 18 | 19 | require_once DALMP_DIR . '/DALMP/Loader.php'; 20 | 21 | Loader::register(); 22 | -------------------------------------------------------------------------------- /tests/test_cache_base.php: -------------------------------------------------------------------------------- 1 | 7 | * @package DALMP 8 | * @license BSD License 9 | * @version 3.0 10 | */ 11 | abstract class test_cache_base extends PHPUnit_Framework_TestCase 12 | { 13 | abstract public function testAttributes(); 14 | 15 | public function testSet() 16 | { 17 | $tmp_string = ''; 18 | foreach (range(1,100) as $id) { 19 | $this->assertTrue($this->cache->set("test_dalmp_key_{$id}", "val_{$id}")); 20 | /** 21 | * test expiry TTL 22 | */ 23 | $this->assertTrue($this->cache->set("test_dalmp_key_ttl_{$id}", "val_{$id}", 2)); 24 | $tmp_string .= sha1($id); 25 | } 26 | 27 | $this->assertTrue($this->cache->set("test_dalmp_key_tmp_string", $tmp_string)); 28 | } 29 | 30 | /** 31 | * @medium 32 | */ 33 | public function testGet() 34 | { 35 | /** 36 | * wait 2 seconds to let keys expire 37 | */ 38 | sleep(2); 39 | $tmp_string = ''; 40 | foreach (range(1,100) as $id) { 41 | $this->assertEquals("val_{$id}", $this->cache->get("test_dalmp_key_{$id}")); 42 | $this->assertEquals(false, $this->cache->get("test_dalmp_key_ttl_{$id}")); 43 | $tmp_string .= sha1($id); 44 | } 45 | 46 | $this->assertEquals($tmp_string, $this->cache->get("test_dalmp_key_tmp_string")); 47 | } 48 | 49 | public function testDelete() 50 | { 51 | foreach (range(1,100) as $id) { 52 | $this->assertEquals(true, $this->cache->delete("test_dalmp_key_{$id}")); 53 | } 54 | 55 | $this->assertEquals(true, $this->cache->delete("test_dalmp_key_tmp_string")); 56 | } 57 | 58 | public function testFlush() 59 | { 60 | $this->cache->set('x', 'y'); 61 | $this->asserttrue($this->cache->Flush()); 62 | $this->assertEquals(false, $this->cache->get('x')); 63 | } 64 | 65 | } 66 | -------------------------------------------------------------------------------- /tests/test_cache_disk.php: -------------------------------------------------------------------------------- 1 | 9 | * @package DALMP 10 | * @license BSD License 11 | * @version 3.0 12 | */ 13 | class test_cache_disk extends test_cache_base 14 | { 15 | /** 16 | * Cache instance 17 | * 18 | * @var Cache 19 | */ 20 | protected $cache; 21 | 22 | public function setUp() 23 | { 24 | $this->cache = new DALMP\Cache\Disk('/tmp/test_dalmp_disk'); 25 | } 26 | 27 | public function testAttributes() 28 | { 29 | $this->assertClassHasAttribute('cache_dir', 'DALMP\Cache\Disk'); 30 | } 31 | 32 | public function testX() 33 | { 34 | $this->assertContainsOnlyInstancesOf('DALMP\Cache\Disk', array($this->cache)); 35 | } 36 | 37 | } 38 | -------------------------------------------------------------------------------- /tests/test_cache_memcache.php: -------------------------------------------------------------------------------- 1 | 9 | * @package DALMP 10 | * @license BSD License 11 | * @version 3.0 12 | */ 13 | class test_cache_memcache extends test_cache_base 14 | { 15 | /** 16 | * Cache instance 17 | * 18 | * @var Cache 19 | */ 20 | protected $cache; 21 | 22 | public function setUp() 23 | { 24 | if (!extension_loaded('memcache')) { 25 | $this->markTestSkipped('The memcache extension is not available.'); 26 | } 27 | $this->cache = new DALMP\Cache\Memcache; 28 | } 29 | 30 | public function testAttributes() 31 | { 32 | $this->assertClassHasAttribute('host', 'DALMP\Cache\Memcache'); 33 | $this->assertClassHasAttribute('port', 'DALMP\Cache\Memcache'); 34 | $this->assertClassHasAttribute('timeout', 'DALMP\Cache\Memcache'); 35 | $this->assertClassHasAttribute('compress', 'DALMP\Cache\Memcache'); 36 | $this->assertClassHasAttribute('cache', 'DALMP\Cache\Memcache'); 37 | } 38 | 39 | public function testX() 40 | { 41 | $this->assertContainsOnlyInstancesOf('DALMP\Cache\Memcache', array($this->cache)); 42 | } 43 | 44 | } 45 | -------------------------------------------------------------------------------- /tests/test_cache_redis.php: -------------------------------------------------------------------------------- 1 | 9 | * @package DALMP 10 | * @license BSD License 11 | * @version 3.0 12 | */ 13 | class test_cache_redis extends test_cache_base 14 | { 15 | /** 16 | * Cache instance 17 | * 18 | * @var Cache 19 | */ 20 | protected $cache; 21 | 22 | public function setUp() 23 | { 24 | if (!extension_loaded('redis')) { 25 | $this->markTestSkipped('The redis extension is not available.'); 26 | } 27 | $this->cache = new DALMP\Cache\Redis; 28 | } 29 | 30 | public function testAttributes() 31 | { 32 | $this->assertClassHasAttribute('host', 'DALMP\Cache\Redis'); 33 | $this->assertClassHasAttribute('port', 'DALMP\Cache\Redis'); 34 | $this->assertClassHasAttribute('timeout', 'DALMP\Cache\Redis'); 35 | $this->assertClassHasAttribute('cache', 'DALMP\Cache\Redis'); 36 | } 37 | 38 | public function testX() 39 | { 40 | $this->assertContainsOnlyInstancesOf('DALMP\Cache\Redis', array($this->cache)); 41 | } 42 | 43 | public function testPing() 44 | { 45 | $this->assertEquals('+PONG', $this->cache->X()->ping()); 46 | 47 | $count = 1000; 48 | while ($count --) { 49 | $this->assertEquals('+PONG', $this->cache->X()->ping()); 50 | } 51 | } 52 | 53 | } 54 | -------------------------------------------------------------------------------- /tests/test_dalmp_cache_disk.php: -------------------------------------------------------------------------------- 1 | 9 | * @package DALMP 10 | * @license BSD License 11 | * @version 3.0 12 | */ 13 | class test_dalmp_cache_disk extends test_dalmp_cache_base 14 | { 15 | public function setUp() 16 | { 17 | if (!extension_loaded('mysqli')) { 18 | $this->markTestSkipped('The mysqli extension is not available.'); 19 | } 20 | 21 | /** 22 | * read DSN from phpunit.xml 23 | */ 24 | $this->db = new DALMP\Database($GLOBALS['DSN']); 25 | $this->db->useCache(new DALMP\Cache(new DALMP\Cache\Disk('/tmp/test_dalmp_disk'))); 26 | } 27 | 28 | } 29 | -------------------------------------------------------------------------------- /tests/test_dalmp_cache_memcache.php: -------------------------------------------------------------------------------- 1 | 9 | * @package DALMP 10 | * @license BSD License 11 | * @version 3.0 12 | */ 13 | class test_dalmp_cache_memcache extends test_dalmp_cache_base 14 | { 15 | public function setUp() 16 | { 17 | if (!extension_loaded('mysqli')) { 18 | $this->markTestSkipped('The mysqli extension is not available.'); 19 | } 20 | 21 | /** 22 | * read DSN from phpunit.xml 23 | */ 24 | $this->db = new DALMP\Database($GLOBALS['DSN']); 25 | $this->db->useCache(new DALMP\Cache(new DALMP\Cache\Memcache())); 26 | } 27 | 28 | } 29 | -------------------------------------------------------------------------------- /tests/test_dalmp_cache_redis.php: -------------------------------------------------------------------------------- 1 | 9 | * @package DALMP 10 | * @license BSD License 11 | * @version 3.0 12 | */ 13 | class test_dalmp_cache_redis extends test_dalmp_cache_base 14 | { 15 | public function setUp() 16 | { 17 | if (!extension_loaded('mysqli')) { 18 | $this->markTestSkipped('The mysqli extension is not available.'); 19 | } 20 | 21 | /** 22 | * read DSN from phpunit.xml 23 | */ 24 | $this->db = new DALMP\Database($GLOBALS['DSN']); 25 | $this->db->useCache(new DALMP\Cache(new DALMP\Cache\Redis())); 26 | } 27 | 28 | } 29 | -------------------------------------------------------------------------------- /tests/test_sessions_base.php: -------------------------------------------------------------------------------- 1 | 7 | * @package DALMP 8 | * @license BSD License 9 | * @version 3.0 10 | */ 11 | abstract class test_sessions_base extends PHPUnit_Framework_TestCase 12 | { 13 | abstract public function testAttributes(); 14 | 15 | public function getSessionData($i) 16 | { 17 | return "{$i}. Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum."; 18 | } 19 | 20 | public function testOpen() 21 | { 22 | $this->asserttrue($this->sess->open(true, true)); 23 | } 24 | 25 | /** 26 | * @depends testOpen 27 | */ 28 | public function testWrite() 29 | { 30 | for ($i = 0; $i < 100; $i++) { 31 | $this->asserttrue($this->sess->write(sha1("sid_{$i}"), $this->getSessionData($i))); 32 | } 33 | } 34 | 35 | /** 36 | * @depends testWrite 37 | */ 38 | public function testRead() 39 | { 40 | for ($i = 0; $i < 100; $i++) { 41 | $this->assertEquals($this->getSessionData($i), $this->sess->read(sha1("sid_{$i}"))); 42 | } 43 | } 44 | 45 | /** 46 | * @depends testWrite 47 | */ 48 | public function testDestroy() 49 | { 50 | for ($i = 0; $i < 100; $i++) { 51 | $this->asserttrue($this->sess->destroy(sha1("sid_{$i}"))); 52 | } 53 | } 54 | 55 | /** 56 | * @depends testOpen 57 | */ 58 | public function testClose() 59 | { 60 | $this->asserttrue($this->sess->close()); 61 | } 62 | 63 | /** 64 | * @depends testClose 65 | */ 66 | public function testGC() 67 | { 68 | $this->asserttrue($this->sess->gc(true)); 69 | } 70 | 71 | public function testWriteRef() 72 | { 73 | for ($i = 0; $i < 100; $i++) { 74 | $GLOBALS['UID'] = sha1($i); 75 | $this->asserttrue($this->sess->write(sha1("sid_{$i}"), $this->getSessionData($i))); 76 | } 77 | } 78 | 79 | public function testGetSessionsRefs() 80 | { 81 | $this->assertEquals(100, count($this->sess->getSessionsRefs())); 82 | } 83 | 84 | public function testGetSessionRef() 85 | { 86 | for ($i = 0; $i < 100; $i++) { 87 | $this->assertEquals(1, count($this->sess->getSessionRef(sha1($i)))); 88 | } 89 | } 90 | 91 | public function testDelSessionRef() 92 | { 93 | for ($i = 0; $i < 100; $i++) { 94 | $this->asserttrue($this->sess->delSessionRef(sha1($i))); 95 | $this->assertEquals(array(), $this->sess->getSessionRef(sha1($i))); 96 | } 97 | } 98 | 99 | } 100 | -------------------------------------------------------------------------------- /tests/test_sessions_memcache.php: -------------------------------------------------------------------------------- 1 | 9 | * @package DALMP 10 | * @license BSD License 11 | * @version 3.0 12 | */ 13 | class test_sessions_memcache extends test_sessions_base 14 | { 15 | /** 16 | * SessionHandler instance 17 | * 18 | * @var sess 19 | */ 20 | protected $sess; 21 | 22 | public function setUp() 23 | { 24 | if (!extension_loaded('memcache')) { 25 | $this->markTestSkipped('The memcache extension is not available.'); 26 | } 27 | $this->sess = new DALMP\Sessions\Memcache(new DALMP\Cache\Memcache); 28 | } 29 | 30 | public function testAttributes() 31 | { 32 | $this->assertClassHasAttribute('cache', 'DALMP\Sessions\Memcache'); 33 | $this->assertClassHasAttribute('cache_ref_key', 'DALMP\Sessions\Memcache'); 34 | $this->assertClassHasAttribute('dalmp_sessions_ref', 'DALMP\Sessions\Memcache'); 35 | $this->assertClassHasAttribute('dalmp_sessions_key', 'DALMP\Sessions\Memcache'); 36 | } 37 | 38 | } 39 | -------------------------------------------------------------------------------- /tests/test_sessions_mysql.php: -------------------------------------------------------------------------------- 1 | 9 | * @package DALMP 10 | * @license BSD License 11 | * @version 3.0 12 | */ 13 | class test_sessions_mysql extends test_sessions_base 14 | { 15 | /** 16 | * SessionHandler instance 17 | * 18 | * @var sess 19 | */ 20 | protected $sess; 21 | 22 | public function setUp() 23 | { 24 | if (!extension_loaded('mysqli')) { 25 | $this->markTestSkipped('The mysqli extension is not available.'); 26 | } 27 | 28 | /** 29 | * read DSN from phpunit.xml 30 | */ 31 | $db = new DALMP\Database($GLOBALS['DSN']); 32 | $this->sess = new DALMP\Sessions\MySQL($db); 33 | } 34 | 35 | public function testAttributes() 36 | { 37 | $this->assertClassHasAttribute('DB', 'DALMP\Sessions\MySQL'); 38 | $this->assertClassHasAttribute('dalmp_sessions_ref', 'DALMP\Sessions\MySQL'); 39 | $this->assertClassHasAttribute('dalmp_sessions_table', 'DALMP\Sessions\MySQL'); 40 | } 41 | 42 | } 43 | -------------------------------------------------------------------------------- /tests/test_sessions_redis.php: -------------------------------------------------------------------------------- 1 | 9 | * @package DALMP 10 | * @license BSD License 11 | * @version 3.0 12 | */ 13 | class test_sessions_redis extends test_sessions_base 14 | { 15 | /** 16 | * SessionHandler instance 17 | * 18 | * @var sess 19 | */ 20 | protected $sess; 21 | 22 | public function setUp() 23 | { 24 | if (!extension_loaded('redis')) { 25 | $this->markTestSkipped('The redis extension is not available.'); 26 | } 27 | $this->sess = new DALMP\Sessions\Redis(new DALMP\Cache\Redis); 28 | } 29 | 30 | public function testAttributes() 31 | { 32 | $this->assertClassHasAttribute('cache', 'DALMP\Sessions\Redis'); 33 | $this->assertClassHasAttribute('cache_ref_key', 'DALMP\Sessions\Redis'); 34 | $this->assertClassHasAttribute('dalmp_sessions_ref', 'DALMP\Sessions\Redis'); 35 | $this->assertClassHasAttribute('dalmp_sessions_key', 'DALMP\Sessions\Redis'); 36 | } 37 | 38 | } 39 | --------------------------------------------------------------------------------4 | 5 | 6 |7 | 38 |8 | 10 |tests/test_cache_disk.php 9 |11 | 13 |tests/test_cache_memcache.php 12 |14 | 16 |tests/test_cache_redis.php 15 |17 | 19 |tests/test_sessions_memcache.php 18 |20 | 22 |tests/test_sessions_redis.php 21 |23 | 25 |tests/test_sessions_mysql.php 24 |26 | 28 |tests/test_dalmp.php 27 |29 | 31 |tests/test_dalmp_cache_memcache.php 30 |32 | 34 |tests/test_dalmp_cache_redis.php 33 |35 | 37 |tests/test_dalmp_cache_disk.php 36 |