- Implement basic exception classes

This commit is contained in:
Marcus Boerger 2004-11-01 15:50:25 +00:00
parent db47e478c5
commit fe1909b591
6 changed files with 305 additions and 8 deletions

View file

@ -9,6 +9,6 @@ if test "$PHP_SPL" != "no"; then
AC_MSG_ERROR(Cannot build SPL as a shared module)
fi
AC_DEFINE(HAVE_SPL, 1, [Whether you want SPL (Standard PHP Library) support])
PHP_NEW_EXTENSION(spl, php_spl.c spl_functions.c spl_engine.c spl_iterators.c spl_array.c spl_directory.c spl_sxe.c, $ext_shared)
PHP_NEW_EXTENSION(spl, php_spl.c spl_functions.c spl_engine.c spl_iterators.c spl_array.c spl_directory.c spl_sxe.c spl_exceptions.c, $ext_shared)
PHP_ADD_EXTENSION_DEP(spl, simplexml)
fi

View file

@ -7,6 +7,6 @@ if (PHP_SPL != "no") {
if (PHP_SPL_SHARED) {
ERROR("SPL cannot be compiled as a shared ext");
}
EXTENSION("spl", "php_spl.c spl_functions.c spl_engine.c spl_iterators.c spl_array.c spl_directory.c spl_sxe.c");
EXTENSION("spl", "php_spl.c spl_functions.c spl_engine.c spl_iterators.c spl_array.c spl_directory.c spl_sxe.c spl_exceptions.c");
AC_DEFINE('HAVE_SPL', 1);
}

View file

@ -32,6 +32,7 @@
#include "spl_directory.h"
#include "spl_iterators.h"
#include "spl_sxe.h"
#include "spl_exceptions.h"
#ifdef COMPILE_DL_SPL
ZEND_GET_MODULE(spl)
@ -93,6 +94,7 @@ PHP_MINIT_FUNCTION(spl)
PHP_MINIT(spl_array)(INIT_FUNC_ARGS_PASSTHRU);
PHP_MINIT(spl_directory)(INIT_FUNC_ARGS_PASSTHRU);
PHP_MINIT(spl_sxe)(INIT_FUNC_ARGS_PASSTHRU);
PHP_MINIT(spl_exceptions)(INIT_FUNC_ARGS_PASSTHRU);
return SUCCESS;
}
@ -168,19 +170,29 @@ PHP_FUNCTION(class_implements)
SPL_ADD_CLASS(CachingRecursiveIterator, z_list, sub, allow, ce_flags); \
SPL_ADD_CLASS(Countable, z_list, sub, allow, ce_flags); \
SPL_ADD_CLASS(DirectoryIterator, z_list, sub, allow, ce_flags); \
SPL_ADD_CLASS(DomainException, z_list, sub, allow, ce_flags); \
SPL_ADD_CLASS(EmptyIterator, z_list, sub, allow, ce_flags); \
SPL_ADD_CLASS(FilterIterator, z_list, sub, allow, ce_flags); \
SPL_ADD_CLASS(InfiniteIterator, z_list, sub, allow, ce_flags); \
SPL_ADD_CLASS(InvalidArgumentException, z_list, sub, allow, ce_flags); \
SPL_ADD_CLASS(IteratorIterator, z_list, sub, allow, ce_flags); \
SPL_ADD_CLASS(LengthException, z_list, sub, allow, ce_flags); \
SPL_ADD_CLASS(LimitIterator, z_list, sub, allow, ce_flags); \
SPL_ADD_CLASS(LogicException, z_list, sub, allow, ce_flags); \
SPL_ADD_CLASS(NoRewindIterator, z_list, sub, allow, ce_flags); \
SPL_ADD_CLASS(OuterIterator, z_list, sub, allow, ce_flags); \
SPL_ADD_CLASS(OutOfRangeException, z_list, sub, allow, ce_flags); \
SPL_ADD_CLASS(OutOfBoundsException, z_list, sub, allow, ce_flags); \
SPL_ADD_CLASS(OverflowException, z_list, sub, allow, ce_flags); \
SPL_ADD_CLASS(ParentIterator, z_list, sub, allow, ce_flags); \
SPL_ADD_CLASS(RangeException, z_list, sub, allow, ce_flags); \
SPL_ADD_CLASS(RecursiveDirectoryIterator, z_list, sub, allow, ce_flags); \
SPL_ADD_CLASS(RecursiveIterator, z_list, sub, allow, ce_flags); \
SPL_ADD_CLASS(RecursiveIteratorIterator, z_list, sub, allow, ce_flags); \
SPL_ADD_CLASS(RuntimeException, z_list, sub, allow, ce_flags); \
SPL_ADD_CLASS(SeekableIterator, z_list, sub, allow, ce_flags); \
SPL_ADD_CLASS(SimpleXMLIterator, z_list, sub, allow, ce_flags); \
SPL_ADD_CLASS(UnderflowException, z_list, sub, allow, ce_flags); \
/* {{{ spl_classes */
PHP_FUNCTION(spl_classes)

View file

@ -13,26 +13,186 @@
*
* SPL - Standard PHP Library
*
* SPL is a collection of interfaces and classes that are meant to solve
* standard problems.
*
* A nice article about it can be found
* <a href="http://www.sitepoint.com/article/php5-standard-library/1">here</a>.
*
* You can download this documentation as a chm file
* <a href="http://php.net/~helly/php/ext/spl/spl.chm">here</a>.
*
* (c) Marcus Boerger, 2003 - 2004
*/
/** \defgroup SPL Internal classes
/** @defgroup ZendEngine Zend engine classes
*
* The classes and interfaces in this group are contained in the c-code of
* PHP's Zend engine.
*/
/** @defgroup SPL Internal classes
*
* The classes and interfaces in this group are contained in the c-code of
* ext/SPL.
*/
/** \defgroup Examples Example classes
/** @defgroup Examples Example classes
*
* The classes and interfaces in this group are contained as PHP code in the
* examples subdirectory of ext/SPL. Sooner or later they will be moved to
* c-code.
*/
/** \ingroup SPL
/** @ingroup ZendEngine
* @brief Basic Exception class.
*/
class Exception
{
/** The exception message */
protected $message;
/** The string represenations as generated during construction */
private $string;
/** The code passed to the constructor */
protected $code;
/** The file name where the exception was instantiated */
protected $file;
/** The line number where the exception was instantiated */
protected $line;
/** The stack trace */
private $trace;
/** Prevent clone
*/
final private function __clone();
/**
*/
public function __construct($message, $code);
/** @return the message passed to the constructor
*/
final public function getMessage();
/** @return the code passed to the constructor
*/
final public function getCode();
/** @return the name of the file where the exception was thrown
*/
final public function getFile();
/** @return the line number where the exception was thrown
*/
final public function getLine();
/** @return the stack trace as array
*/
final public function getTrace();
/** @return the stack trace as string
*/
final public function getTraceAsString();
/** @return string represenation of exception
*/
public function __toString();
}
/** @ingroup SPL
* @brief Exception that represents error in the program logic.
*
* This kind of exceptions should directly leed to a fix in your code.
*/
class LogicException extends Exception
{
}
/** @ingroup SPL
* @brief Exception that denotes a value not in the valid domain was used.
*
* This kind of exception should be used to inform about domain erors in
* mathematical sense.
*/
class DomainException extends LogicException
{
}
/** @ingroup SPL
* @brief Exception that denotes invalid arguments were passed.
*/
class InvalidArgumentException extends LogicException
{
}
/** @ingroup SPL
* @brief Exception thrown when a parameter exceeds the allowed length.
*
* This can be used for strings length, array size, file size, number of
* elements read from an Iterator and so on.
*/
class LengthException extends LogicException
{
}
/** @ingroup SPL
* @brief Exception thrown when an illegal index was requested.
*
* This represents errors that should be detected at compile time.
*
* @see OutOfBoundsException
*/
class OutOfRangeException extends LogicException
{
}
/** @ingroup SPL
* @brief Exception thrown for errors that are only detectable at runtime.
*/
class RuntimeException extends Exception
{
}
/** @ingroup SPL
* @brief Exception thrown when an illegal index was requested.
*
* This represents errors that cannot be detected at compile time.
*
* @see OutOfRangeException
*/
class OutOfBoundsException extends LogicException
{
}
/** @ingroup SPL
* @brief Exception thrown to indicate arithmetic/buffer overflow.
*/
class OverflowException extends RuntimeException
{
}
/** @ingroup SPL
* @brief Exception thrown to indicate range errors during program execution.
*
* Normally this means there was an arithmetic error other than under/overflow.
*/
class RangeException extends RuntimeException
{
}
/** @ingroup SPL
* @brief Exception Exception thrown to indicate arithmetic/buffer underflow.
*/
class UnderflowException extends RuntimeException
{
}
/** \ingroup ZendEngine
* \brief Interface to override array access of objects.
*/
interface ArrayAccess
@ -57,7 +217,7 @@ interface ArrayAccess
function offsetExists($offset);
}
/** \ingroup SPL
/** \ingroup ZendEngine
* \brief Interface to detect a class is traversable using foreach.
*
* Abstract base interface that cannot be implemented alone. Instead it
@ -75,7 +235,7 @@ interface Traversable
{
}
/** \ingroup SPL
/** \ingroup ZendEngine
* \brief Interface to create an external Iterator.
*
* \note This is an engine internal interface.
@ -87,7 +247,7 @@ interface IteratorAggregate extends Traversable
function getIterator();
}
/** \ingroup SPL
/** \ingroup ZendEngine
* \brief Basic iterator
*
* Interface for external iterators or objects that can be iterated

75
ext/spl/spl_exceptions.c Executable file
View file

@ -0,0 +1,75 @@
/*
+----------------------------------------------------------------------+
| PHP Version 5 |
+----------------------------------------------------------------------+
| Copyright (c) 1997-2004 The PHP Group |
+----------------------------------------------------------------------+
| This source file is subject to version 3.0 of the PHP license, |
| that is bundled with this package in the file LICENSE, and is |
| available through the world-wide-web at the following url: |
| http://www.php.net/license/3_0.txt. |
| If you did not receive a copy of the PHP license and are unable to |
| obtain it through the world-wide-web, please send a note to |
| license@php.net so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
| Authors: Marcus Boerger <helly@php.net> |
+----------------------------------------------------------------------+
*/
/* $Id$ */
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#include "php.h"
#include "php_ini.h"
#include "ext/standard/info.h"
#include "zend_interfaces.h"
#include "zend_exceptions.h"
#include "php_spl.h"
#include "spl_functions.h"
#include "spl_engine.h"
#include "spl_exceptions.h"
zend_class_entry *spl_ce_LogicException;
zend_class_entry *spl_ce_DomainException;
zend_class_entry *spl_ce_InvalidArgumentException;
zend_class_entry *spl_ce_LengthException;
zend_class_entry *spl_ce_OutOfRangeException;
zend_class_entry *spl_ce_RuntimeException;
zend_class_entry *spl_ce_OutOfBoundsException;
zend_class_entry *spl_ce_OverflowException;
zend_class_entry *spl_ce_RangeException;
zend_class_entry *spl_ce_UnderflowException;
#define spl_ce_Exception zend_exception_get_default()
/* {{{ PHP_MINIT_FUNCTION(spl_exceptions) */
PHP_MINIT_FUNCTION(spl_exceptions)
{
REGISTER_SPL_SUB_CLASS_EX(LogicException, Exception, NULL, NULL);
REGISTER_SPL_SUB_CLASS_EX(DomainException, LogicException, NULL, NULL);
REGISTER_SPL_SUB_CLASS_EX(InvalidArgumentException, LogicException, NULL, NULL);
REGISTER_SPL_SUB_CLASS_EX(LengthException, LogicException, NULL, NULL);
REGISTER_SPL_SUB_CLASS_EX(OutOfRangeException, LogicException, NULL, NULL);
REGISTER_SPL_SUB_CLASS_EX(RuntimeException, Exception, NULL, NULL);
REGISTER_SPL_SUB_CLASS_EX(OutOfBoundsException RuntimeException, NULL, NULL);
REGISTER_SPL_SUB_CLASS_EX(OverflowException, RuntimeException, NULL, NULL);
REGISTER_SPL_SUB_CLASS_EX(RangeException, RuntimeException, NULL, NULL);
REGISTER_SPL_SUB_CLASS_EX(UnderflowException, RuntimeException, NULL, NULL);
return SUCCESS;
}
/* }}} */
/*
* Local variables:
* tab-width: 4
* c-basic-offset: 4
* End:
* vim600: fdm=marker
* vim: noet sw=4 ts=4
*/

50
ext/spl/spl_exceptions.h Executable file
View file

@ -0,0 +1,50 @@
/*
+----------------------------------------------------------------------+
| PHP Version 5 |
+----------------------------------------------------------------------+
| Copyright (c) 1997-2004 The PHP Group |
+----------------------------------------------------------------------+
| This source file is subject to version 3.0 of the PHP license, |
| that is bundled with this package in the file LICENSE, and is |
| available through the world-wide-web at the following url: |
| http://www.php.net/license/3_0.txt. |
| If you did not receive a copy of the PHP license and are unable to |
| obtain it through the world-wide-web, please send a note to |
| license@php.net so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
| Authors: Marcus Boerger <helly@php.net> |
+----------------------------------------------------------------------+
*/
/* $Id$ */
#ifndef SPL_EXCEPTIONS_H
#define SPL_EXCEPTIONS_H
#include "php.h"
#include "php_spl.h"
extern zend_class_entry *spl_ce_LogicException;
extern zend_class_entry *spl_ce_DomainException;
extern zend_class_entry *spl_ce_InvalidArgumentException;
extern zend_class_entry *spl_ce_LengthException;
extern zend_class_entry *spl_ce_OutOfRangeException;
extern zend_class_entry *spl_ce_RuntimeException;
extern zend_class_entry *spl_ce_OutOfBoundsException;
extern zend_class_entry *spl_ce_OverflowException;
extern zend_class_entry *spl_ce_RangeException;
extern zend_class_entry *spl_ce_UnderflowException;
PHP_MINIT_FUNCTION(spl_exceptions);
#endif /* SPL_EXCEPTIONS_H */
/*
* Local Variables:
* c-basic-offset: 4
* tab-width: 4
* End:
* vim600: fdm=marker
* vim: noet sw=4 ts=4
*/