From fe1909b5919769c4805f8aacc3785f5b7a2a0dba Mon Sep 17 00:00:00 2001 From: Marcus Boerger Date: Mon, 1 Nov 2004 15:50:25 +0000 Subject: [PATCH] - Implement basic exception classes --- ext/spl/config.m4 | 2 +- ext/spl/config.w32 | 2 +- ext/spl/php_spl.c | 12 +++ ext/spl/spl.php | 172 +++++++++++++++++++++++++++++++++++++-- ext/spl/spl_exceptions.c | 75 +++++++++++++++++ ext/spl/spl_exceptions.h | 50 ++++++++++++ 6 files changed, 305 insertions(+), 8 deletions(-) create mode 100755 ext/spl/spl_exceptions.c create mode 100755 ext/spl/spl_exceptions.h diff --git a/ext/spl/config.m4 b/ext/spl/config.m4 index f5b5639ddf..4760a14203 100755 --- a/ext/spl/config.m4 +++ b/ext/spl/config.m4 @@ -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 diff --git a/ext/spl/config.w32 b/ext/spl/config.w32 index d69471a717..255a2b4eab 100644 --- a/ext/spl/config.w32 +++ b/ext/spl/config.w32 @@ -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); } diff --git a/ext/spl/php_spl.c b/ext/spl/php_spl.c index 55f4369dd5..9bb4834e49 100755 --- a/ext/spl/php_spl.c +++ b/ext/spl/php_spl.c @@ -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) diff --git a/ext/spl/spl.php b/ext/spl/spl.php index 4cf429d71c..f3c0ba3047 100755 --- a/ext/spl/spl.php +++ b/ext/spl/spl.php @@ -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 + * here. + * * You can download this documentation as a chm file * here. * * (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 diff --git a/ext/spl/spl_exceptions.c b/ext/spl/spl_exceptions.c new file mode 100755 index 0000000000..4d45133ddb --- /dev/null +++ b/ext/spl/spl_exceptions.c @@ -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 | + +----------------------------------------------------------------------+ + */ + +/* $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 + */ diff --git a/ext/spl/spl_exceptions.h b/ext/spl/spl_exceptions.h new file mode 100755 index 0000000000..1b6a7feb5b --- /dev/null +++ b/ext/spl/spl_exceptions.h @@ -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 | + +----------------------------------------------------------------------+ + */ + +/* $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 + */ -- 2.40.0