Skip to content

Commit fe1909b

Browse files
committed
- Implement basic exception classes
1 parent db47e47 commit fe1909b

File tree

6 files changed

+305
-8
lines changed

6 files changed

+305
-8
lines changed

ext/spl/config.m4

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,6 @@ if test "$PHP_SPL" != "no"; then
99
AC_MSG_ERROR(Cannot build SPL as a shared module)
1010
fi
1111
AC_DEFINE(HAVE_SPL, 1, [Whether you want SPL (Standard PHP Library) support])
12-
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)
12+
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)
1313
PHP_ADD_EXTENSION_DEP(spl, simplexml)
1414
fi

ext/spl/config.w32

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,6 @@ if (PHP_SPL != "no") {
77
if (PHP_SPL_SHARED) {
88
ERROR("SPL cannot be compiled as a shared ext");
99
}
10-
EXTENSION("spl", "php_spl.c spl_functions.c spl_engine.c spl_iterators.c spl_array.c spl_directory.c spl_sxe.c");
10+
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");
1111
AC_DEFINE('HAVE_SPL', 1);
1212
}

ext/spl/php_spl.c

+12
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
#include "spl_directory.h"
3333
#include "spl_iterators.h"
3434
#include "spl_sxe.h"
35+
#include "spl_exceptions.h"
3536

3637
#ifdef COMPILE_DL_SPL
3738
ZEND_GET_MODULE(spl)
@@ -93,6 +94,7 @@ PHP_MINIT_FUNCTION(spl)
9394
PHP_MINIT(spl_array)(INIT_FUNC_ARGS_PASSTHRU);
9495
PHP_MINIT(spl_directory)(INIT_FUNC_ARGS_PASSTHRU);
9596
PHP_MINIT(spl_sxe)(INIT_FUNC_ARGS_PASSTHRU);
97+
PHP_MINIT(spl_exceptions)(INIT_FUNC_ARGS_PASSTHRU);
9698

9799
return SUCCESS;
98100
}
@@ -168,19 +170,29 @@ PHP_FUNCTION(class_implements)
168170
SPL_ADD_CLASS(CachingRecursiveIterator, z_list, sub, allow, ce_flags); \
169171
SPL_ADD_CLASS(Countable, z_list, sub, allow, ce_flags); \
170172
SPL_ADD_CLASS(DirectoryIterator, z_list, sub, allow, ce_flags); \
173+
SPL_ADD_CLASS(DomainException, z_list, sub, allow, ce_flags); \
171174
SPL_ADD_CLASS(EmptyIterator, z_list, sub, allow, ce_flags); \
172175
SPL_ADD_CLASS(FilterIterator, z_list, sub, allow, ce_flags); \
173176
SPL_ADD_CLASS(InfiniteIterator, z_list, sub, allow, ce_flags); \
177+
SPL_ADD_CLASS(InvalidArgumentException, z_list, sub, allow, ce_flags); \
174178
SPL_ADD_CLASS(IteratorIterator, z_list, sub, allow, ce_flags); \
179+
SPL_ADD_CLASS(LengthException, z_list, sub, allow, ce_flags); \
175180
SPL_ADD_CLASS(LimitIterator, z_list, sub, allow, ce_flags); \
181+
SPL_ADD_CLASS(LogicException, z_list, sub, allow, ce_flags); \
176182
SPL_ADD_CLASS(NoRewindIterator, z_list, sub, allow, ce_flags); \
177183
SPL_ADD_CLASS(OuterIterator, z_list, sub, allow, ce_flags); \
184+
SPL_ADD_CLASS(OutOfRangeException, z_list, sub, allow, ce_flags); \
185+
SPL_ADD_CLASS(OutOfBoundsException, z_list, sub, allow, ce_flags); \
186+
SPL_ADD_CLASS(OverflowException, z_list, sub, allow, ce_flags); \
178187
SPL_ADD_CLASS(ParentIterator, z_list, sub, allow, ce_flags); \
188+
SPL_ADD_CLASS(RangeException, z_list, sub, allow, ce_flags); \
179189
SPL_ADD_CLASS(RecursiveDirectoryIterator, z_list, sub, allow, ce_flags); \
180190
SPL_ADD_CLASS(RecursiveIterator, z_list, sub, allow, ce_flags); \
181191
SPL_ADD_CLASS(RecursiveIteratorIterator, z_list, sub, allow, ce_flags); \
192+
SPL_ADD_CLASS(RuntimeException, z_list, sub, allow, ce_flags); \
182193
SPL_ADD_CLASS(SeekableIterator, z_list, sub, allow, ce_flags); \
183194
SPL_ADD_CLASS(SimpleXMLIterator, z_list, sub, allow, ce_flags); \
195+
SPL_ADD_CLASS(UnderflowException, z_list, sub, allow, ce_flags); \
184196

185197
/* {{{ spl_classes */
186198
PHP_FUNCTION(spl_classes)

ext/spl/spl.php

+166-6
Original file line numberDiff line numberDiff line change
@@ -13,26 +13,186 @@
1313
*
1414
* SPL - Standard PHP Library
1515
*
16+
* SPL is a collection of interfaces and classes that are meant to solve
17+
* standard problems.
18+
*
19+
* A nice article about it can be found
20+
* <a href="http://www.sitepoint.com/article/php5-standard-library/1">here</a>.
21+
*
1622
* You can download this documentation as a chm file
1723
* <a href="http://php.net/~helly/php/ext/spl/spl.chm">here</a>.
1824
*
1925
* (c) Marcus Boerger, 2003 - 2004
2026
*/
2127

22-
/** \defgroup SPL Internal classes
28+
/** @defgroup ZendEngine Zend engine classes
29+
*
30+
* The classes and interfaces in this group are contained in the c-code of
31+
* PHP's Zend engine.
32+
*/
33+
34+
/** @defgroup SPL Internal classes
2335
*
2436
* The classes and interfaces in this group are contained in the c-code of
2537
* ext/SPL.
2638
*/
2739

28-
/** \defgroup Examples Example classes
40+
/** @defgroup Examples Example classes
2941
*
3042
* The classes and interfaces in this group are contained as PHP code in the
3143
* examples subdirectory of ext/SPL. Sooner or later they will be moved to
3244
* c-code.
3345
*/
3446

35-
/** \ingroup SPL
47+
/** @ingroup ZendEngine
48+
* @brief Basic Exception class.
49+
*/
50+
class Exception
51+
{
52+
/** The exception message */
53+
protected $message;
54+
55+
/** The string represenations as generated during construction */
56+
private $string;
57+
58+
/** The code passed to the constructor */
59+
protected $code;
60+
61+
/** The file name where the exception was instantiated */
62+
protected $file;
63+
64+
/** The line number where the exception was instantiated */
65+
protected $line;
66+
67+
/** The stack trace */
68+
private $trace;
69+
70+
/** Prevent clone
71+
*/
72+
final private function __clone();
73+
74+
/**
75+
*/
76+
public function __construct($message, $code);
77+
78+
/** @return the message passed to the constructor
79+
*/
80+
final public function getMessage();
81+
82+
/** @return the code passed to the constructor
83+
*/
84+
final public function getCode();
85+
86+
/** @return the name of the file where the exception was thrown
87+
*/
88+
final public function getFile();
89+
90+
/** @return the line number where the exception was thrown
91+
*/
92+
final public function getLine();
93+
94+
/** @return the stack trace as array
95+
*/
96+
final public function getTrace();
97+
98+
/** @return the stack trace as string
99+
*/
100+
final public function getTraceAsString();
101+
102+
/** @return string represenation of exception
103+
*/
104+
public function __toString();
105+
}
106+
107+
/** @ingroup SPL
108+
* @brief Exception that represents error in the program logic.
109+
*
110+
* This kind of exceptions should directly leed to a fix in your code.
111+
*/
112+
class LogicException extends Exception
113+
{
114+
}
115+
116+
/** @ingroup SPL
117+
* @brief Exception that denotes a value not in the valid domain was used.
118+
*
119+
* This kind of exception should be used to inform about domain erors in
120+
* mathematical sense.
121+
*/
122+
class DomainException extends LogicException
123+
{
124+
}
125+
126+
/** @ingroup SPL
127+
* @brief Exception that denotes invalid arguments were passed.
128+
*/
129+
class InvalidArgumentException extends LogicException
130+
{
131+
}
132+
133+
/** @ingroup SPL
134+
* @brief Exception thrown when a parameter exceeds the allowed length.
135+
*
136+
* This can be used for strings length, array size, file size, number of
137+
* elements read from an Iterator and so on.
138+
*/
139+
class LengthException extends LogicException
140+
{
141+
}
142+
143+
/** @ingroup SPL
144+
* @brief Exception thrown when an illegal index was requested.
145+
*
146+
* This represents errors that should be detected at compile time.
147+
*
148+
* @see OutOfBoundsException
149+
*/
150+
class OutOfRangeException extends LogicException
151+
{
152+
}
153+
154+
/** @ingroup SPL
155+
* @brief Exception thrown for errors that are only detectable at runtime.
156+
*/
157+
class RuntimeException extends Exception
158+
{
159+
}
160+
161+
/** @ingroup SPL
162+
* @brief Exception thrown when an illegal index was requested.
163+
*
164+
* This represents errors that cannot be detected at compile time.
165+
*
166+
* @see OutOfRangeException
167+
*/
168+
class OutOfBoundsException extends LogicException
169+
{
170+
}
171+
172+
/** @ingroup SPL
173+
* @brief Exception thrown to indicate arithmetic/buffer overflow.
174+
*/
175+
class OverflowException extends RuntimeException
176+
{
177+
}
178+
179+
/** @ingroup SPL
180+
* @brief Exception thrown to indicate range errors during program execution.
181+
*
182+
* Normally this means there was an arithmetic error other than under/overflow.
183+
*/
184+
class RangeException extends RuntimeException
185+
{
186+
}
187+
188+
/** @ingroup SPL
189+
* @brief Exception Exception thrown to indicate arithmetic/buffer underflow.
190+
*/
191+
class UnderflowException extends RuntimeException
192+
{
193+
}
194+
195+
/** \ingroup ZendEngine
36196
* \brief Interface to override array access of objects.
37197
*/
38198
interface ArrayAccess
@@ -57,7 +217,7 @@ function offsetUnset($offset);
57217
function offsetExists($offset);
58218
}
59219

60-
/** \ingroup SPL
220+
/** \ingroup ZendEngine
61221
* \brief Interface to detect a class is traversable using foreach.
62222
*
63223
* Abstract base interface that cannot be implemented alone. Instead it
@@ -75,7 +235,7 @@ interface Traversable
75235
{
76236
}
77237

78-
/** \ingroup SPL
238+
/** \ingroup ZendEngine
79239
* \brief Interface to create an external Iterator.
80240
*
81241
* \note This is an engine internal interface.
@@ -87,7 +247,7 @@ interface IteratorAggregate extends Traversable
87247
function getIterator();
88248
}
89249

90-
/** \ingroup SPL
250+
/** \ingroup ZendEngine
91251
* \brief Basic iterator
92252
*
93253
* Interface for external iterators or objects that can be iterated

ext/spl/spl_exceptions.c

+75
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
/*
2+
+----------------------------------------------------------------------+
3+
| PHP Version 5 |
4+
+----------------------------------------------------------------------+
5+
| Copyright (c) 1997-2004 The PHP Group |
6+
+----------------------------------------------------------------------+
7+
| This source file is subject to version 3.0 of the PHP license, |
8+
| that is bundled with this package in the file LICENSE, and is |
9+
| available through the world-wide-web at the following url: |
10+
| http://www.php.net/license/3_0.txt. |
11+
| If you did not receive a copy of the PHP license and are unable to |
12+
| obtain it through the world-wide-web, please send a note to |
13+
| license@php.net so we can mail you a copy immediately. |
14+
+----------------------------------------------------------------------+
15+
| Authors: Marcus Boerger <helly@php.net> |
16+
+----------------------------------------------------------------------+
17+
*/
18+
19+
/* $Id$ */
20+
21+
#ifdef HAVE_CONFIG_H
22+
# include "config.h"
23+
#endif
24+
25+
#include "php.h"
26+
#include "php_ini.h"
27+
#include "ext/standard/info.h"
28+
#include "zend_interfaces.h"
29+
#include "zend_exceptions.h"
30+
31+
#include "php_spl.h"
32+
#include "spl_functions.h"
33+
#include "spl_engine.h"
34+
#include "spl_exceptions.h"
35+
36+
zend_class_entry *spl_ce_LogicException;
37+
zend_class_entry *spl_ce_DomainException;
38+
zend_class_entry *spl_ce_InvalidArgumentException;
39+
zend_class_entry *spl_ce_LengthException;
40+
zend_class_entry *spl_ce_OutOfRangeException;
41+
zend_class_entry *spl_ce_RuntimeException;
42+
zend_class_entry *spl_ce_OutOfBoundsException;
43+
zend_class_entry *spl_ce_OverflowException;
44+
zend_class_entry *spl_ce_RangeException;
45+
zend_class_entry *spl_ce_UnderflowException;
46+
47+
#define spl_ce_Exception zend_exception_get_default()
48+
49+
/* {{{ PHP_MINIT_FUNCTION(spl_exceptions) */
50+
PHP_MINIT_FUNCTION(spl_exceptions)
51+
{
52+
REGISTER_SPL_SUB_CLASS_EX(LogicException, Exception, NULL, NULL);
53+
REGISTER_SPL_SUB_CLASS_EX(DomainException, LogicException, NULL, NULL);
54+
REGISTER_SPL_SUB_CLASS_EX(InvalidArgumentException, LogicException, NULL, NULL);
55+
REGISTER_SPL_SUB_CLASS_EX(LengthException, LogicException, NULL, NULL);
56+
REGISTER_SPL_SUB_CLASS_EX(OutOfRangeException, LogicException, NULL, NULL);
57+
58+
REGISTER_SPL_SUB_CLASS_EX(RuntimeException, Exception, NULL, NULL);
59+
REGISTER_SPL_SUB_CLASS_EX(OutOfBoundsException RuntimeException, NULL, NULL);
60+
REGISTER_SPL_SUB_CLASS_EX(OverflowException, RuntimeException, NULL, NULL);
61+
REGISTER_SPL_SUB_CLASS_EX(RangeException, RuntimeException, NULL, NULL);
62+
REGISTER_SPL_SUB_CLASS_EX(UnderflowException, RuntimeException, NULL, NULL);
63+
64+
return SUCCESS;
65+
}
66+
/* }}} */
67+
68+
/*
69+
* Local variables:
70+
* tab-width: 4
71+
* c-basic-offset: 4
72+
* End:
73+
* vim600: fdm=marker
74+
* vim: noet sw=4 ts=4
75+
*/

0 commit comments

Comments
 (0)