PageRenderTime 45ms CodeModel.GetById 12ms app.highlight 27ms RepoModel.GetById 1ms app.codeStats 1ms

/mapscript/php/error.c

https://github.com/smcmurray/mapserver
C | 198 lines | 126 code | 33 blank | 39 comment | 17 complexity | 5c35dd4352b4f78f29bb15ffad7544ad MD5 | raw file
  1/**********************************************************************
  2 * $Id: php_mapscript.c 9765 2010-01-28 15:32:10Z aboudreault $
  3 *
  4 * Project:  MapServer
  5 * Purpose:  PHP/MapScript extension for MapServer.  External interface
  6 *           functions
  7 * Author:   Daniel Morissette, DM Solutions Group (dmorissette@dmsolutions.ca)
  8 *           Alan Boudreault, Mapgears
  9 *
 10 **********************************************************************
 11 * Copyright (c) 2000-2010, Daniel Morissette, DM Solutions Group Inc.
 12 *
 13 * Permission is hereby granted, free of charge, to any person obtaining a
 14 * copy of this software and associated documentation files (the "Software"),
 15 * to deal in the Software without restriction, including without limitation
 16 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 17 * and/or sell copies of the Software, and to permit persons to whom the
 18 * Software is furnished to do so, subject to the following conditions:
 19 *
 20 * The above copyright notice and this permission notice shall be included in
 21 * all copies of this Software or works derived from this Software.
 22 *
 23 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 24 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 25 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 26 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 27 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 28 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 29 * DEALINGS IN THE SOFTWARE.
 30 **********************************************************************/
 31
 32#include "php_mapscript.h"
 33
 34zend_class_entry *mapscript_ce_error;
 35
 36ZEND_BEGIN_ARG_INFO_EX(error___get_args, 0, 0, 1)
 37ZEND_ARG_INFO(0, property)
 38ZEND_END_ARG_INFO()
 39
 40ZEND_BEGIN_ARG_INFO_EX(error___set_args, 0, 0, 2)
 41ZEND_ARG_INFO(0, property)
 42ZEND_ARG_INFO(0, value)
 43ZEND_END_ARG_INFO()
 44
 45/* {{{ proto error __construct()
 46   errorObj CANNOT be instanciated, this will throw an exception on use */
 47PHP_METHOD(errorObj, __construct)
 48{
 49  mapscript_throw_exception("errorObj cannot be constructed" TSRMLS_CC);
 50}
 51/* }}} */
 52
 53PHP_METHOD(errorObj, __get)
 54{
 55  char *property;
 56  long property_len = 0;
 57  zval *zobj = getThis();
 58  php_error_object *php_error;
 59
 60  PHP_MAPSCRIPT_ERROR_HANDLING(TRUE);
 61  if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s",
 62                            &property, &property_len) == FAILURE) {
 63    PHP_MAPSCRIPT_RESTORE_ERRORS(TRUE);
 64    return;
 65  }
 66  PHP_MAPSCRIPT_RESTORE_ERRORS(TRUE);
 67
 68  php_error = (php_error_object *) zend_object_store_get_object(zobj TSRMLS_CC);
 69
 70  IF_GET_LONG("code", php_error->error->code)
 71  else IF_GET_STRING("routine", php_error->error->routine)
 72    else IF_GET_STRING("message", php_error->error->message)
 73      else IF_GET_LONG("isreported", php_error->error->isreported)
 74        else {
 75          mapscript_throw_exception("Property '%s' does not exist in this object." TSRMLS_CC, property);
 76        }
 77}
 78
 79PHP_METHOD(errorObj, __set)
 80{
 81  char *property;
 82  long property_len = 0;
 83  zval *value;
 84  zval *zobj = getThis();
 85  php_error_object *php_error;
 86
 87  PHP_MAPSCRIPT_ERROR_HANDLING(TRUE);
 88  if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sz",
 89                            &property, &property_len, &value) == FAILURE) {
 90    PHP_MAPSCRIPT_RESTORE_ERRORS(TRUE);
 91    return;
 92  }
 93  PHP_MAPSCRIPT_RESTORE_ERRORS(TRUE);
 94
 95  php_error = (php_error_object *) zend_object_store_get_object(zobj TSRMLS_CC);
 96
 97  if ( (STRING_EQUAL("code", property)) ||
 98       (STRING_EQUAL("routine", property)) ||
 99       (STRING_EQUAL("isreported", property)) ||
100       (STRING_EQUAL("message", property))) {
101    mapscript_throw_exception("Property '%s' is read-only and cannot be set." TSRMLS_CC, property);
102  } else {
103    mapscript_throw_exception("Property '%s' does not exist in this object." TSRMLS_CC, property);
104  }
105}
106
107/* {{{ proto int error.next()
108   Returns a ref to the next errorObj in the list, or NULL if we reached the last one */
109PHP_METHOD(errorObj, next)
110{
111  zval *zobj = getThis();
112  php_error_object *php_error;
113  errorObj *error = NULL;
114
115  PHP_MAPSCRIPT_ERROR_HANDLING(TRUE);
116  if (zend_parse_parameters_none() == FAILURE) {
117    PHP_MAPSCRIPT_RESTORE_ERRORS(TRUE);
118    return;
119  }
120  PHP_MAPSCRIPT_RESTORE_ERRORS(TRUE);
121
122  php_error = (php_error_object *) zend_object_store_get_object(zobj TSRMLS_CC);
123
124  if (php_error->error->next == NULL)
125    RETURN_NULL();
126
127  /* Make sure 'self' is still valid.  It may have been deleted by
128   * msResetErrorList() */
129  error = msGetErrorObj();
130  while(error != php_error->error) {
131    if (error->next == NULL) {
132      mapscript_throw_exception("Trying to access an errorObj that has expired." TSRMLS_CC);
133      return;
134    }
135    error = error->next;
136  }
137
138  php_error->error = php_error->error->next;
139  *return_value = *zobj;
140  zval_copy_ctor(return_value);
141  INIT_PZVAL(return_value);
142}
143/* }}} */
144
145zend_function_entry error_functions[] = {
146  PHP_ME(errorObj, __construct, NULL, ZEND_ACC_PUBLIC|ZEND_ACC_CTOR)
147  PHP_ME(errorObj, __get, error___get_args, ZEND_ACC_PUBLIC)
148  PHP_ME(errorObj, __set, error___set_args, ZEND_ACC_PUBLIC)
149  PHP_ME(errorObj, next, NULL, ZEND_ACC_PUBLIC) {
150    NULL, NULL, NULL
151  }
152};
153
154void mapscript_create_error(errorObj *error, zval *return_value TSRMLS_DC)
155{
156  php_error_object * php_error;
157  object_init_ex(return_value, mapscript_ce_error);
158  php_error = (php_error_object *)zend_object_store_get_object(return_value TSRMLS_CC);
159  php_error->error = error;
160}
161
162static void mapscript_error_object_destroy(void *object TSRMLS_DC)
163{
164  php_error_object *php_error = (php_error_object *)object;
165
166  MAPSCRIPT_FREE_OBJECT(php_error);
167
168  /* We don't need to free the errorObj */
169
170  efree(object);
171}
172
173static zend_object_value mapscript_error_object_new(zend_class_entry *ce TSRMLS_DC)
174{
175  zend_object_value retval;
176  php_error_object *php_error;
177
178  MAPSCRIPT_ALLOC_OBJECT(php_error, php_error_object);
179
180  retval = mapscript_object_new(&php_error->std, ce,
181                                &mapscript_error_object_destroy TSRMLS_CC);
182
183  return retval;
184}
185
186PHP_MINIT_FUNCTION(error)
187{
188  zend_class_entry ce;
189
190  MAPSCRIPT_REGISTER_CLASS("errorObj",
191                           error_functions,
192                           mapscript_ce_error,
193                           mapscript_error_object_new);
194
195  mapscript_ce_error->ce_flags |= ZEND_ACC_FINAL_CLASS;
196
197  return SUCCESS;
198}