1
0
mirror of https://github.com/dg/dibi.git synced 2025-08-06 14:16:39 +02:00

added DibiLazyStorage, simple caching layer

This commit is contained in:
David Grudl
2010-08-03 06:28:17 +02:00
parent 3b87d71a68
commit 84e0f0ecc1
2 changed files with 67 additions and 0 deletions

View File

@@ -107,6 +107,7 @@ class DibiVariable extends DateTime53
// dibi libraries
require_once dirname(__FILE__) . '/libs/interfaces.php';
require_once dirname(__FILE__) . '/libs/DibiObject.php';
require_once dirname(__FILE__) . '/libs/DibiLazyStorage.php';
require_once dirname(__FILE__) . '/libs/DibiException.php';
require_once dirname(__FILE__) . '/libs/DibiConnection.php';
require_once dirname(__FILE__) . '/libs/DibiResult.php';

View File

@@ -0,0 +1,66 @@
<?php
/**
* dibi - tiny'n'smart database abstraction layer
* ----------------------------------------------
*
* @copyright Copyright (c) 2005, 2010 David Grudl
* @license http://dibiphp.com/license dibi license
* @link http://dibiphp.com
* @package dibi
*/
/**#@+
* Lazy cached storage.
*
* @copyright Copyright (c) 2005, 2010 David Grudl
* @package dibi
* @internal
*/
abstract class DibiLazyStorageBase
{
private $callback;
public function __construct($callback)
{
$this->setCallback($callback);
}
public function setCallback($callback)
{
if (!is_callable($callback)) {
$able = is_callable($callback, TRUE, $textual);
throw new InvalidArgumentException("Handler '$textual' is not " . ($able ? 'callable.' : 'valid PHP callback.'));
}
$this->callback = $callback;
}
public function getCallback()
{
return $this->callback;
}
}
final class DibiLazyStorage extends DibiLazyStorageBase
{
public function __get($nm)
{
if (is_array($nm)) { // preg_replace_callback support
$nm = $nm[1];
}
return $this->$nm = call_user_func($this->getCallback(), $nm);
}
}
/**#@-*/