1
0
mirror of https://github.com/dg/dibi.git synced 2025-08-12 00:54:11 +02:00

added new Nette exceptions

This commit is contained in:
David Grudl
2008-02-01 02:12:36 +00:00
parent 89dfa9f772
commit 8da9e778a6
22 changed files with 473 additions and 352 deletions

View File

@@ -20,7 +20,7 @@
/**
* dibi connection
* dibi connection.
*
* @author David Grudl
* @copyright Copyright (c) 2005, 2008 David Grudl
@@ -30,13 +30,13 @@
class DibiConnection extends NObject
{
/**
* Current connection configuration
* Current connection configuration.
* @var array
*/
private $config;
/**
* IDibiDriver
* IDibiDriver.
* @var array
*/
private $driver;
@@ -56,7 +56,7 @@ class DibiConnection extends NObject
/**
* Creates object and (optionally) connects to a database
* Creates object and (optionally) connects to a database.
*
* @param array|string connection parameters
* @throws DibiException
@@ -93,7 +93,7 @@ class DibiConnection extends NObject
/**
* Automatically frees the resources allocated for this result set
* Automatically frees the resources allocated for this result set.
*
* @return void
*/
@@ -106,7 +106,7 @@ class DibiConnection extends NObject
/**
* Connects to a database
* Connects to a database.
*
* @return void
*/
@@ -122,7 +122,7 @@ class DibiConnection extends NObject
/**
* Disconnects from a database
* Disconnects from a database.
*
* @return void
*/
@@ -164,7 +164,7 @@ class DibiConnection extends NObject
/**
* Apply configuration alias or default values
* Apply configuration alias or default values.
*
* @param array connect configuration
* @param string key
@@ -186,7 +186,7 @@ class DibiConnection extends NObject
/**
* Returns the connection resource
* Returns the connection resource.
*
* @return resource
*/
@@ -198,7 +198,7 @@ class DibiConnection extends NObject
/**
* Generates (translates) and executes SQL query
* Generates (translates) and executes SQL query.
*
* @param array|mixed one or more arguments
* @return DibiResult Result set object (if any)
@@ -219,7 +219,7 @@ class DibiConnection extends NObject
/**
* Generates and prints SQL query
* Generates and prints SQL query.
*
* @param array|mixed one or more arguments
* @return bool
@@ -227,6 +227,7 @@ class DibiConnection extends NObject
final public function test($args)
{
$args = func_get_args();
$this->connect();
$trans = new DibiTranslator($this->driver);
$ok = $trans->translate($args);
dibi::dump($trans->sql);
@@ -236,7 +237,7 @@ class DibiConnection extends NObject
/**
* Executes the SQL query
* Executes the SQL query.
*
* @param string SQL statement.
* @return DibiResult Result set object (if any)
@@ -265,7 +266,7 @@ class DibiConnection extends NObject
/**
* Gets the number of affected rows by the last INSERT, UPDATE or DELETE query
* Gets the number of affected rows by the last INSERT, UPDATE or DELETE query.
*
* @return int number of rows
* @throws DibiException
@@ -273,14 +274,14 @@ class DibiConnection extends NObject
public function affectedRows()
{
$rows = $this->driver->affectedRows();
if (!is_int($rows) || $rows < 0) throw new DibiException('Cannot retrieve number of affected rows');
if (!is_int($rows) || $rows < 0) throw new DibiException('Cannot retrieve number of affected rows.');
return $rows;
}
/**
* Retrieves the ID generated for an AUTO_INCREMENT column by the previous INSERT query
* Retrieves the ID generated for an AUTO_INCREMENT column by the previous INSERT query.
*
* @param string optional sequence name
* @return int
@@ -289,7 +290,7 @@ class DibiConnection extends NObject
public function insertId($sequence = NULL)
{
$id = $this->driver->insertId($sequence);
if ($id < 1) throw new DibiException('Cannot retrieve last generated ID');
if ($id < 1) throw new DibiException('Cannot retrieve last generated ID.');
return (int) $id;
}
@@ -303,7 +304,7 @@ class DibiConnection extends NObject
{
$this->connect();
if ($this->inTxn) {
throw new DibiException('There is already an active transaction');
throw new DibiException('There is already an active transaction.');
}
$this->driver->begin();
$this->inTxn = TRUE;
@@ -319,7 +320,7 @@ class DibiConnection extends NObject
public function commit()
{
if (!$this->inTxn) {
throw new DibiException('There is no active transaction');
throw new DibiException('There is no active transaction.');
}
$this->driver->commit();
$this->inTxn = FALSE;
@@ -335,7 +336,7 @@ class DibiConnection extends NObject
public function rollback()
{
if (!$this->inTxn) {
throw new DibiException('There is no active transaction');
throw new DibiException('There is no active transaction.');
}
$this->driver->rollback();
$this->inTxn = FALSE;
@@ -345,7 +346,7 @@ class DibiConnection extends NObject
/**
* Escapes the string
* Escapes the string.
*
* @param string unescaped string
* @return string escaped and optionally quoted string
@@ -359,7 +360,7 @@ class DibiConnection extends NObject
/**
* Delimites identifier (table's or column's name, etc.)
* Delimites identifier (table's or column's name, etc.).
*
* @param string identifier
* @return string delimited identifier
@@ -372,7 +373,7 @@ class DibiConnection extends NObject
/**
* Injects LIMIT/OFFSET to the SQL query
* Injects LIMIT/OFFSET to the SQL query.
*
* @param string &$sql The SQL query that will be modified.
* @param int $limit
@@ -400,7 +401,7 @@ class DibiConnection extends NObject
$handle = @fopen($file, 'r');
if (!$handle) {
throw new DibiException("Cannot open file '$file'");
throw new FileNotFoundException("Cannot open file '$file'.");
}
$count = 0;
@@ -427,27 +428,27 @@ class DibiConnection extends NObject
*/
public function getDibiReflection()
{
throw new BadMethodCallException(__METHOD__ . ' is not implemented');
throw new NotImplementedException;
}
/**
* Prevents unserialization
* Prevents unserialization.
*/
public function __wakeup()
{
throw new DibiException('You cannot serialize or unserialize ' . $this->getClass() . ' instances');
throw new NotSupportedException('You cannot serialize or unserialize ' . $this->getClass() . ' instances.');
}
/**
* Prevents serialization
* Prevents serialization.
*/
public function __sleep()
{
throw new DibiException('You cannot serialize or unserialize ' . $this->getClass() . ' instances');
throw new NotSupportedException('You cannot serialize or unserialize ' . $this->getClass() . ' instances.');
}
}

View File

@@ -20,7 +20,7 @@
/**
* Default implementation of IDataSource for dibi
* Default implementation of IDataSource for dibi.
*
* @author David Grudl
* @copyright Copyright (c) 2005, 2008 David Grudl

View File

@@ -20,7 +20,7 @@
/**
* dibi common exception
* dibi common exception.
*
* @author David Grudl
* @copyright Copyright (c) 2005, 2008 David Grudl
@@ -35,7 +35,7 @@ class DibiException extends NException
/**
* database server exception
* database server exception.
*
* @author David Grudl
* @copyright Copyright (c) 2005, 2008 David Grudl
@@ -49,7 +49,7 @@ class DibiDriverException extends DibiException
/**
* Construct an dibi driver exception
* Construct an dibi driver exception.
*
* @param string Message describing the exception
* @param int Some code

View File

@@ -20,7 +20,7 @@
/**
* dibi basic logger & profiler (experimental)
* dibi basic logger & profiler (experimental).
*
* @author David Grudl
* @copyright Copyright (c) 2005, 2008 David Grudl
@@ -51,7 +51,7 @@ final class DibiLogger extends NObject
/**
* Event handler (events: exception, connected, beforeQuery, afterQuery, begin, commit, rollback)
* Event handler (events: exception, connected, beforeQuery, afterQuery, begin, commit, rollback).
*
* @param DibiConnection
* @param string event name

View File

@@ -20,7 +20,7 @@
/**
* dibi result-set abstract class
* dibi result-set abstract class.
*
* <code>
* $result = dibi::query('SELECT * FROM [table]');
@@ -44,25 +44,25 @@
class DibiResult extends NObject implements IDataSource
{
/**
* IDibiDriver
* IDibiDriver.
* @var array
*/
private $driver;
/**
* Translate table
* Translate table.
* @var array
*/
private $xlat;
/**
* Cache for $driver->getColumnsMeta()
* Cache for $driver->getColumnsMeta().
* @var array
*/
private $metaCache;
/**
* Already fetched? Used for allowance for first seek(0)
* Already fetched? Used for allowance for first seek(0).
* @var bool
*/
private $fetched = FALSE;
@@ -105,7 +105,7 @@ class DibiResult extends NObject implements IDataSource
/**
* Automatically frees the resources allocated for this result set
* Automatically frees the resources allocated for this result set.
*
* @return void
*/
@@ -117,7 +117,7 @@ class DibiResult extends NObject implements IDataSource
/**
* Returns the resultset resource
* Returns the resultset resource.
*
* @return mixed
*/
@@ -129,7 +129,7 @@ class DibiResult extends NObject implements IDataSource
/**
* Moves cursor position without fetching row
* Moves cursor position without fetching row.
*
* @param int the 0-based cursor pos to seek to
* @return boolean TRUE on success, FALSE if unable to seek to specified record
@@ -143,7 +143,7 @@ class DibiResult extends NObject implements IDataSource
/**
* Returns the number of rows in a result set
* Returns the number of rows in a result set.
*
* @return int
*/
@@ -155,7 +155,7 @@ class DibiResult extends NObject implements IDataSource
/**
* Frees the resources allocated for this result set
* Frees the resources allocated for this result set.
*
* @return void
*/
@@ -216,7 +216,7 @@ class DibiResult extends NObject implements IDataSource
/**
* Fetches the row at current position, process optional type conversion
* Fetches the row at current position, process optional type conversion.
* and moves the internal cursor to the next position
*
* @param bool fetch as object? Overrides $this->asObjects
@@ -255,7 +255,7 @@ class DibiResult extends NObject implements IDataSource
/**
* Like fetch(), but returns only first field
* Like fetch(), but returns only first field.
*
* @return mixed value on success, FALSE if no next record
*/
@@ -308,7 +308,7 @@ class DibiResult extends NObject implements IDataSource
/**
* Fetches all records from table and returns associative tree
* Fetches all records from table and returns associative tree.
* Associative descriptor: assoc1,#,assoc2,=,assoc3,@
* builds a tree: $data[assoc1][index][assoc2]['assoc3']->value = {record}
*
@@ -328,7 +328,7 @@ class DibiResult extends NObject implements IDataSource
// check columns
foreach ($assoc as $as) {
if ($as !== '#' && $as !== '=' && $as !== '@' && !array_key_exists($as, $row)) {
throw new InvalidArgumentException("Unknown column '$as' in associative descriptor");
throw new InvalidArgumentException("Unknown column '$as' in associative descriptor.");
}
}
@@ -392,7 +392,7 @@ class DibiResult extends NObject implements IDataSource
/**
* Fetches all records from table like $key => $value pairs
* Fetches all records from table like $key => $value pairs.
*
* @param string associative key
* @param string value
@@ -409,11 +409,11 @@ class DibiResult extends NObject implements IDataSource
if ($value === NULL) {
if ($key !== NULL) {
throw new InvalidArgumentException("Either none or both columns must be specified");
throw new InvalidArgumentException("Either none or both columns must be specified.");
}
if (count($row) < 2) {
throw new LoginException("Result must have at least two columns");
throw new UnexpectedValueException("Result must have at least two columns.");
}
// autodetect
@@ -423,7 +423,7 @@ class DibiResult extends NObject implements IDataSource
} else {
if (!array_key_exists($value, $row)) {
throw new InvalidArgumentException("Unknown value column '$value'");
throw new InvalidArgumentException("Unknown value column '$value'.");
}
if ($key === NULL) { // indexed-array
@@ -434,7 +434,7 @@ class DibiResult extends NObject implements IDataSource
}
if (!array_key_exists($key, $row)) {
throw new InvalidArgumentException("Unknown key column '$key'");
throw new InvalidArgumentException("Unknown key column '$key'.");
}
}
@@ -491,7 +491,7 @@ class DibiResult extends NObject implements IDataSource
/**
* Gets an array of meta informations about column
* Gets an array of meta informations about column.
*
* @return array
*/
@@ -512,7 +512,7 @@ class DibiResult extends NObject implements IDataSource
/**
* Displays complete result-set as HTML table for debug purposes
* Displays complete result-set as HTML table for debug purposes.
*
* @return void
*/
@@ -549,7 +549,7 @@ class DibiResult extends NObject implements IDataSource
/**
* Required by the IteratorAggregate interface
* Required by the IteratorAggregate interface.
* @param int offset
* @param int limit
* @return ArrayIterator
@@ -562,7 +562,7 @@ class DibiResult extends NObject implements IDataSource
/**
* Required by the Countable interface
* Required by the Countable interface.
* @return int
*/
final public function count()
@@ -573,15 +573,15 @@ class DibiResult extends NObject implements IDataSource
/**
* Safe access to property $driver
* Safe access to property $driver.
*
* @return IDibiDriver
* @throws DibiException
* @throws InvalidStateException
*/
private function getDriver()
{
if ($this->driver === NULL) {
throw new DibiException('Resultset was released from memory');
throw new InvalidStateException('Resultset was released from memory.');
}
return $this->driver;

View File

@@ -20,7 +20,7 @@
/**
* External result set iterator
* External result set iterator.
*
* This can be returned by DibiResult::getIterator() method or using foreach
* <code>
@@ -62,7 +62,7 @@ final class DibiResultIterator implements Iterator
/**
* Required by the Iterator interface
* Required by the Iterator interface.
* @param int offset
* @param int limit
*/
@@ -76,7 +76,7 @@ final class DibiResultIterator implements Iterator
/**
* Rewinds the Iterator to the first element
* Rewinds the Iterator to the first element.
* @return void
*/
public function rewind()

View File

@@ -54,7 +54,7 @@ abstract class DibiTable extends NObject
/**
* Table constructor
* Table constructor.
* @param array
* @return void
*/
@@ -72,7 +72,7 @@ abstract class DibiTable extends NObject
/**
* Returns the table name
* Returns the table name.
* @return string
*/
public function getName()
@@ -83,7 +83,7 @@ abstract class DibiTable extends NObject
/**
* Returns the primary key name
* Returns the primary key name.
* @return string
*/
public function getPrimary()
@@ -94,7 +94,7 @@ abstract class DibiTable extends NObject
/**
* Returns the dibi connection
* Returns the dibi connection.
* @return DibiConnection
*/
public function getConnection()
@@ -105,7 +105,7 @@ abstract class DibiTable extends NObject
/**
* Setup object
* Setup object.
* @return void
*/
protected function setup()
@@ -135,7 +135,7 @@ abstract class DibiTable extends NObject
/**
* Inserts row into a table
* Inserts row into a table.
* @param array|object
* @return int new primary key
*/
@@ -150,7 +150,7 @@ abstract class DibiTable extends NObject
/**
* Updates rows in a table
* Updates rows in a table.
* @param mixed primary key value(s)
* @param array|object
* @return int number of updated rows
@@ -168,7 +168,7 @@ abstract class DibiTable extends NObject
/**
* Deletes rows from a table by primary key
* Deletes rows from a table by primary key.
* @param mixed primary key value(s)
* @return int number of deleted rows
*/
@@ -184,7 +184,7 @@ abstract class DibiTable extends NObject
/**
* Finds rows by primary key
* Finds rows by primary key.
* @param mixed primary key value(s)
* @return DibiResult
*/
@@ -202,7 +202,7 @@ abstract class DibiTable extends NObject
/**
* Selects all rows
* Selects all rows.
* @param string column to order by
* @return DibiResult
*/
@@ -224,7 +224,7 @@ abstract class DibiTable extends NObject
/**
* Fetches single row
* Fetches single row.
* @param scalar primary key value
* @return array|object row
*/
@@ -239,7 +239,7 @@ abstract class DibiTable extends NObject
/**
* Returns a blank row (not fetched from database)
* Returns a blank row (not fetched from database).
* @return array|object
*/
public function createBlank()
@@ -255,7 +255,7 @@ abstract class DibiTable extends NObject
/**
* User data pre-processing
* User data pre-processing.
* @param array|object
* @return array
*/
@@ -267,13 +267,13 @@ abstract class DibiTable extends NObject
return $data;
}
throw new DibiException('Dataset must be array or anonymous object');
throw new InvalidArgumentException('Dataset must be array or anonymous object.');
}
/**
* User DibiResult post-processing
* User DibiResult post-processing.
* @param DibiResult
* @return DibiResult
*/

View File

@@ -20,7 +20,7 @@
/**
* dibi SQL translator
* dibi SQL translator.
*
* @author David Grudl
* @copyright Copyright (c) 2005, 2008 David Grudl
@@ -69,7 +69,7 @@ final class DibiTranslator extends NObject
/**
* return IDibiDriver
* return IDibiDriver.
*/
public function getDriver()
{
@@ -79,7 +79,7 @@ final class DibiTranslator extends NObject
/**
* Generates SQL
* Generates SQL.
*
* @param array
* @return bool
@@ -187,7 +187,7 @@ final class DibiTranslator extends NObject
/**
* Apply modifier to single value
* Apply modifier to single value.
* @param mixed
* @param string
* @return string
@@ -342,7 +342,7 @@ final class DibiTranslator extends NObject
/**
* PREG callback from translate() or formatValue()
* PREG callback from translate() or formatValue().
* @param array
* @return string
*/
@@ -426,7 +426,7 @@ final class DibiTranslator extends NObject
if ($matches[2]) // SQL identifiers: [ident]
return $this->delimite($matches[2]);
if ($matches[3]) // SQL strings: '....'
if ($matches[3]) // SQL strings: '...'
return $this->driver->format( str_replace("''", "'", $matches[4]), dibi::FIELD_TEXT);
if ($matches[5]) // SQL strings: "..."
@@ -443,7 +443,7 @@ final class DibiTranslator extends NObject
/**
* Apply substitutions to indentifier and delimites it
* Apply substitutions to indentifier and delimites it.
*
* @param string indentifier
* @return string

View File

@@ -20,7 +20,7 @@
/**
* Default implemenation of IDibiVariable
* Default implemenation of IDibiVariable.
* @package dibi
*/
class DibiVariable extends NObject implements IDibiVariable

View File

@@ -1,132 +0,0 @@
<?php
/**
* dibi - tiny'n'smart database abstraction layer
* ----------------------------------------------
*
* Copyright (c) 2005, 2008 David Grudl aka -dgx- (http://www.dgx.cz)
*
* This source file is subject to the "dibi license" that is bundled
* with this package in the file license.txt.
*
* For more information please see http://dibiphp.com/
*
* @copyright Copyright (c) 2004, 2008 David Grudl
* @license http://nettephp.com/license Nette license
* @link http://nettephp.com/
* @package Nette
*/
// namespace Nette;
/**
* Nette Exception base class
*
* @author David Grudl
* @copyright Copyright (c) 2004, 2008 David Grudl
* @license http://nettephp.com/license Nette license
* @link http://nettephp.com/
* @package Nette
*/
class NException extends Exception
{
/** @var Exception */
private $cause;
/** @var callback */
private static $oldHandler;
/** @var string */
private static $handlerClass;
/**
* Initializes the cause of this throwable to the specified value
*
* @param Exception
* @return void
*/
public function initCause(Exception $cause)
{
if ($this->cause === NULL) {
$this->cause = $cause;
} else {
throw new BadMethodCallException('Cause was already assigned');
}
}
/**
* Gets the Exception instance that caused the current exception
*
* @return Exception
*/
public function getCause()
{
return $this->cause;
}
/**
* Returns string represenation of exception
*
* @return string
*/
public function __toString()
{
return parent::__toString() . ($this->cause === NULL ? '' : "\nCaused by " . $this->cause->__toString());
}
/**
* Enables converting all PHP errors to exceptions
*
* @param Exception class to be thrown
* @return void
*/
public static function catchError($class = __CLASS__)
{
self::$oldHandler = set_error_handler(array(__CLASS__, '_errorHandler'), E_ALL);
self::$handlerClass = $class;
}
/**
* Disables converting errors to exceptions
*
* @return void
*/
public static function restore()
{
if (self::$oldHandler !== NULL) {
set_error_handler(self::$oldHandler);
self::$oldHandler = NULL;
} else {
restore_error_handler();
}
}
/**
* Internal error handler
*/
public static function _errorHandler($code, $message, $file, $line, $context)
{
self::restore();
if (ini_get('html_errors')) {
$message = strip_tags($message);
}
throw new self::$handlerClass($message, $code);
}
}

View File

@@ -1,261 +0,0 @@
<?php
/**
* dibi - tiny'n'smart database abstraction layer
* ----------------------------------------------
*
* Copyright (c) 2005, 2008 David Grudl aka -dgx- (http://www.dgx.cz)
*
* This source file is subject to the "dibi license" that is bundled
* with this package in the file license.txt.
*
* For more information please see http://dibiphp.com/
*
* @copyright Copyright (c) 2004, 2008 David Grudl
* @license http://nettephp.com/license Nette license
* @link http://nettephp.com/
* @package Nette
*/
// namespace Nette;
/**
* NObject is the ultimate ancestor of all instantiable classes.
*
* It defines some handful methods and enhances object core of PHP:
* - access to undeclared members throws exceptions
* - support for conventional properties with getters and setters
* - support for event raising functionality
* - ability to add new methods to class (extension methods)
*
* Properties is a syntactic sugar which allows access public getter and setter
* methods as normal object variables. A property is defined by a getter method
* and optional setter method (no setter method means read-only property).
* <code>
* $val = $obj->label; // equivalent to $val = $obj->getLabel();
* $obj->label = 'Nette'; // equivalent to $obj->setLabel('Nette');
* </code>
* Property names are case-sensitive, and they are written in the camelCaps
* or PascalCaps.
*
* Event functionality is provided by declaration of property named 'on{Something}'
* Multiple handlers are allowed.
* <code>
* public $onClick; // declaration in class
* $this->onClick[] = 'callback'; // attaching event handler
* if (!empty($this->onClick)) ... // are there any handlers?
* $this->onClick($sender, $arg); // raises the event with arguments
* </code>
*
* Adding method to class (i.e. to all instances) works similar to JavaScript
* prototype property. The syntax for adding a new method is:
* <code>
* function MyClass_prototype_newMethod(MyClass $obj, $arg, ...) { ... }
* $obj = new MyClass;
* $obj->newMethod($x); // equivalent to MyClass_prototype_newMethod($obj, $x);
* </code>
*
* @author David Grudl
* @copyright Copyright (c) 2004, 2008 David Grudl
* @license http://nettephp.com/license Nette license
* @link http://nettephp.com/
* @package Nette
*/
abstract class NObject
{
/**
* Returns the name of the class of this object
*
* @return string
*/
final public function getClass()
{
return get_class($this);
}
/**
* Access to reflection
*
* @return ReflectionObject
*/
final public function getReflection()
{
return new ReflectionObject($this);
}
/**
* Call to undefined method
*
* @param string method name
* @param array arguments
* @return mixed
* @throws BadMethodCallException
*/
protected function __call($name, $args)
{
if ($name === '') {
throw new BadMethodCallException("Call to method without name");
}
$class = get_class($this);
// event functionality
if (self::hasEvent($class, $name)) {
$list = $this->$name;
if (is_array($list) || $list instanceof Traversable) {
foreach ($list as $handler) {
call_user_func_array($handler, $args);
}
}
return;
}
// object prototypes support Class__method()
// (or use class Class__method { static function ... } with autoloading?)
$cl = $class;
do {
if (function_exists($nm = $cl . '_prototype_' . $name)) {
array_unshift($args, $this);
return call_user_func_array($nm, $args);
}
} while ($cl = get_parent_class($cl));
throw new BadMethodCallException("Call to undefined method $class::$name()");
}
/**
* Returns property value. Do not call directly.
*
* @param string property name
* @return mixed property value
* @throws LogicException if the property is not defined.
*/
protected function &__get($name)
{
if ($name === '') {
throw new LogicException("Cannot read an property without name");
}
// property getter support
$class = get_class($this);
$m = 'get' . $name;
if (self::hasAccessor($class, $m)) {
// ampersands:
// - using &__get() because declaration should be forward compatible (e.g. with NHtml)
// - not using &$this->$m because user could bypass property setter by: $x = & $obj->property; $x = 'new value';
$val = $this->$m();
return $val;
} else {
throw new LogicException("Cannot read an undeclared property $class::\$$name");
}
}
/**
* Sets value of a property. Do not call directly.
*
* @param string property name
* @param mixed property value
* @return void
* @throws LogicException if the property is not defined or is read-only
*/
protected function __set($name, $value)
{
if ($name === '') {
throw new LogicException('Cannot assign to an property without name');
}
// property setter support
$class = get_class($this);
if (self::hasAccessor($class, 'get' . $name)) {
$m = 'set' . $name;
if (self::hasAccessor($class, $m)) {
$this->$m($value);
} else {
throw new LogicException("Cannot assign to a read-only property $class::\$$name");
}
} else {
throw new LogicException("Cannot assign to an undeclared property $class::\$$name");
}
}
/**
* Is property defined?
*
* @param string property name
* @return bool
*/
protected function __isset($name)
{
return $name !== '' && self::hasAccessor(get_class($this), 'get' . $name);
}
/**
* Access to undeclared property
*
* @param string property name
* @return void
* @throws LogicException
*/
protected function __unset($name)
{
$class = get_class($this);
throw new LogicException("Cannot unset an property $class::\$$name");
}
/**
* Has property an accessor?
*
* @param string class name
* @param string method name
* @return bool
*/
private static function hasAccessor($c, $m)
{
static $cache;
if (!isset($cache[$c])) {
// get_class_methods returns private, protected and public methods of NObject (doesn't matter)
// and ONLY PUBLIC methods of descendants (perfect!)
// but returns static methods too (nothing doing...)
// and is much faster than reflection
// (works good since 5.0.4)
$cache[$c] = array_flip(get_class_methods($c));
}
// case-sensitive checking, capitalize the fourth character
$m[3] = $m[3] & "\xDF";
return isset($cache[$c][$m]);
}
/**
* Is property an event?
*
* @param string class name
* @param string method name
* @return bool
*/
private static function hasEvent($c, $m)
{
return preg_match('#^on[A-Z]#', $m) && property_exists($c, $m);
}
}

View File

@@ -20,13 +20,13 @@
/**
* Interface for user variable, used for generating SQL
* Interface for user variable, used for generating SQL.
* @package dibi
*/
interface IDibiVariable
{
/**
* Format for SQL
* Format for SQL.
*
* @param object DibiTranslator
* @param string optional modifier
@@ -40,7 +40,7 @@ interface IDibiVariable
/**
* Provides an interface between a dataset and data-aware components
* Provides an interface between a dataset and data-aware components.
* @package dibi
*/
interface IDataSource extends Countable, IteratorAggregate
@@ -54,7 +54,7 @@ interface IDataSource extends Countable, IteratorAggregate
/**
* dibi driver interface
* dibi driver interface.
*
* @author David Grudl
* @copyright Copyright (c) 2005, 2008 David Grudl
@@ -65,7 +65,7 @@ interface IDibiDriver
{
/**
* Internal: Connects to a database
* Internal: Connects to a database.
*
* @param array
* @return void
@@ -76,7 +76,7 @@ interface IDibiDriver
/**
* Internal: Disconnects from a database
* Internal: Disconnects from a database.
*
* @return void
* @throws DibiException
@@ -86,7 +86,7 @@ interface IDibiDriver
/**
* Internal: Executes the SQL query
* Internal: Executes the SQL query.
*
* @param string SQL statement.
* @return bool have resultset?
@@ -97,7 +97,7 @@ interface IDibiDriver
/**
* Gets the number of affected rows by the last INSERT, UPDATE or DELETE query
* Gets the number of affected rows by the last INSERT, UPDATE or DELETE query.
*
* @return int|FALSE number of rows or FALSE on error
*/
@@ -106,7 +106,7 @@ interface IDibiDriver
/**
* Retrieves the ID generated for an AUTO_INCREMENT column by the previous INSERT query
* Retrieves the ID generated for an AUTO_INCREMENT column by the previous INSERT query.
*
* @return int|FALSE int on success or FALSE on failure
*/
@@ -142,7 +142,7 @@ interface IDibiDriver
/**
* Format to SQL command
* Format to SQL command.
*
* @param string value
* @param string type (dibi::FIELD_TEXT, dibi::FIELD_BOOL, dibi::FIELD_DATE, dibi::FIELD_DATETIME, dibi::IDENTIFIER)
@@ -152,7 +152,7 @@ interface IDibiDriver
/**
* Injects LIMIT/OFFSET to the SQL query
* Injects LIMIT/OFFSET to the SQL query.
*
* @param string &$sql The SQL query that will be modified.
* @param int $limit
@@ -164,7 +164,7 @@ interface IDibiDriver
/**
* Returns the number of rows in a result set
* Returns the number of rows in a result set.
*
* @return int
*/
@@ -173,7 +173,7 @@ interface IDibiDriver
/**
* Moves cursor position without fetching row
* Moves cursor position without fetching row.
*
* @param int the 0-based cursor pos to seek to
* @return boolean TRUE on success, FALSE if unable to seek to specified record
@@ -184,7 +184,7 @@ interface IDibiDriver
/**
* Fetches the row at current position and moves the internal cursor to the next position
* Fetches the row at current position and moves the internal cursor to the next position.
* internal usage only
*
* @param bool TRUE for associative array, FALSE for numeric
@@ -195,7 +195,7 @@ interface IDibiDriver
/**
* Frees the resources allocated for this result set
* Frees the resources allocated for this result set.
*
* @param resource resultset resource
* @return void
@@ -205,7 +205,7 @@ interface IDibiDriver
/**
* Returns metadata for all columns in a result set
* Returns metadata for all columns in a result set.
*
* @return array
* @throws DibiException
@@ -215,7 +215,7 @@ interface IDibiDriver
/**
* Returns the connection resource
* Returns the connection resource.
*
* @return mixed
*/
@@ -224,7 +224,7 @@ interface IDibiDriver
/**
* Returns the resultset resource
* Returns the resultset resource.
*
* @return mixed
*/