]> granicus.if.org Git - php/commitdiff
- Implement basic exception classes
authorMarcus Boerger <helly@php.net>
Mon, 1 Nov 2004 15:50:25 +0000 (15:50 +0000)
committerMarcus Boerger <helly@php.net>
Mon, 1 Nov 2004 15:50:25 +0000 (15:50 +0000)
ext/spl/config.m4
ext/spl/config.w32
ext/spl/php_spl.c
ext/spl/spl.php
ext/spl/spl_exceptions.c [new file with mode: 0755]
ext/spl/spl_exceptions.h [new file with mode: 0755]

index f5b5639ddf17f7cbece7a8e1712d6f16ff5ca297..4760a14203722361abe8bc7b3952ce63bd3d1722 100755 (executable)
@@ -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
index d69471a717e28379bf6b28a956ba423baaa971ee..255a2b4eab5b25f17fac659b47c40e01805880f5 100644 (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);
 }
index 55f4369dd5d137f94717c87658f8beef219ea1f8..9bb4834e49093651329e6e75f71e4363d7194056 100755 (executable)
@@ -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)
index 4cf429d71cd97dd5ae0c3da526bfa0bf7a4de9c3..f3c0ba30479cf68de2c55f09952b91c8ef207eb5 100755 (executable)
  *
  * 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 
diff --git a/ext/spl/spl_exceptions.c b/ext/spl/spl_exceptions.c
new file mode 100755 (executable)
index 0000000..4d45133
--- /dev/null
@@ -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
+ */
diff --git a/ext/spl/spl_exceptions.h b/ext/spl/spl_exceptions.h
new file mode 100755 (executable)
index 0000000..1b6a7fe
--- /dev/null
@@ -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
+ */