php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Return to Bug #49852
Patch spl_autoload_case_sensitive revision 2010-12-30 00:59 UTC by chris at cmbuckley dot co dot uk

Patch spl_autoload_case_sensitive for SPL related Bug #49852

Patch version 2010-12-30 00:59 UTC

Return to Bug #49852 | Download this patch
Patch Revisions:

Developer: chris@cmbuckley.co.uk

Index: ext/spl/spl.php
===================================================================
--- ext/spl/spl.php	(revision 306824)
+++ ext/spl/spl.php	(working copy)
@@ -185,6 +185,16 @@
 function spl_autoload_extensions($file_extensions) {/**/};
 
 /** @ingroup SPL
+ * @brief Register and returns the use case sensitive file naming flag
+ * @since PHP 5.x
+ *
+ * @param use_case_sensitive_file_naming optional comma separated list of extensions to use in
+ *        default autoload function. If not given just return the current list.
+ * @return bool the value of the case sensitive flag
+ */
+function spl_autoload_case_sensitive($use_case_sensitive_file_naming = null) {/**/};
+
+/** @ingroup SPL
  * @brief Return all registered autoload functionns
  * @since PHP 5.1
  *
Index: ext/spl/tests/TestClassCS.php.inc
===================================================================
--- ext/spl/tests/TestClassCS.php.inc	(revision 0)
+++ ext/spl/tests/TestClassCS.php.inc	(revision 0)
@@ -0,0 +1,12 @@
+<?php
+
+if (1 === preg_match('/TestClassCS.php.inc$/', __FILE__))
+{
+	echo __FILE__ . "\n";
+
+	class TestClassCS
+	{
+	}
+}
+
+?>
\ No newline at end of file
Index: ext/spl/tests/spl_autoload_015.phpt
===================================================================
--- ext/spl/tests/spl_autoload_015.phpt	(revision 0)
+++ ext/spl/tests/spl_autoload_015.phpt	(revision 0)
@@ -0,0 +1,70 @@
+--TEST--
+SPL: spl_autoload_case_sensitive()
+--INI--
+include_path=.
+--FILE--
+<?php
+
+if (spl_autoload_case_sensitive() == false) {
+	echo "return spl_autoload_case_sensitive() == FALSE\n";
+} else { 
+	echo "return spl_autoload_case_sensitive() == TRUE\n";
+}
+$retVal = spl_autoload_case_sensitive(true);
+if ($retVal == true) {
+	echo "Successfully set\n";
+} else {
+	echo "Failed to set\n";
+}
+
+$retVal = spl_autoload_case_sensitive(false);
+if ($retVal == false) {
+	echo "Successfully set\n";
+} else {
+	echo "Failed to set\n";
+}
+
+echo "===AUTOLOAD testclass_lc (cs = false)===\n";
+try
+{
+	spl_autoload_extensions(".php.inc");
+	spl_autoload("testclass_lc");
+} 
+catch(Exception $e) {}
+var_dump(class_exists("testclass_lc", true));
+
+echo "===AUTOLOAD testclasscs (cs = true)===\n";
+spl_autoload_case_sensitive(true);
+try
+{
+	spl_autoload_extensions(".php.inc");
+	spl_autoload("testclasscs");
+}
+catch(Exception $e) {}
+var_dump(class_exists("testclasscs", true));
+
+
+echo "===AUTOLOAD TestClassCS (cs = true)===\n";
+try
+{
+	spl_autoload_extensions(".php.inc");
+	spl_autoload("TestClassCS");
+}
+catch(Exception $e) {}
+var_dump(class_exists("TestClassCS", true));
+
+?>
+===DONE===
+--EXPECTF--
+return spl_autoload_case_sensitive() == FALSE
+Successfully set
+Successfully set
+===AUTOLOAD testclass_lc (cs = false)===
+%stestclass_lc.php.inc
+bool(true)
+===AUTOLOAD testclasscs (cs = true)===
+bool(false)
+===AUTOLOAD TestClassCS (cs = true)===
+%sTestClassCS.php.inc
+bool(true)
+===DONE===
Index: ext/spl/tests/testclass_lc.php.inc
===================================================================
--- ext/spl/tests/testclass_lc.php.inc	(revision 0)
+++ ext/spl/tests/testclass_lc.php.inc	(revision 0)
@@ -0,0 +1,9 @@
+<?php
+
+echo __FILE__ . "\n";
+
+class testclass_lc
+{
+}
+
+?>
\ No newline at end of file
Index: ext/spl/php_spl.c
===================================================================
--- ext/spl/php_spl.c	(revision 306824)
+++ ext/spl/php_spl.c	(working copy)
@@ -62,6 +62,7 @@
 	spl_globals->autoload_extensions_len = 0;
 	spl_globals->autoload_functions      = NULL;
 	spl_globals->autoload_running        = 0;
+	spl_globals->autoload_case_sensitive = 0;
 }
 /* }}} */
 
@@ -210,17 +211,25 @@
 }
 /* }}} */
 
-static int spl_autoload(const char *class_name, const char * lc_name, int class_name_len, const char * file_extension TSRMLS_DC) /* {{{ */
+static int spl_autoload(const char *class_name, int class_name_len, const char * file_extension TSRMLS_DC) /* {{{ */
 {
 	char *class_file;
+	char *ps_name;
 	int class_file_len;
 	int dummy = 1;
 	zend_file_handle file_handle;
 	zend_op_array *new_op_array;
 	zval *result = NULL;
 	int ret;
+	
+	if (SPL_G(autoload_case_sensitive) == 0) {
+		ps_name = zend_str_tolower_dup(class_name, class_name_len);
+	} else {
+		ps_name = safe_emalloc(class_name_len, 1, sizeof(long) + 1);
+		strcpy(ps_name, class_name);
+	}
 
-	class_file_len = spprintf(&class_file, 0, "%s%s", lc_name, file_extension);
+	class_file_len = spprintf(&class_file, 0, "%s%s", ps_name, file_extension);
 
 	ret = php_stream_open_for_zend_ex(class_file, &file_handle, ENFORCE_SAFE_MODE|USE_PATH|STREAM_OPEN_FOR_INCLUDE TSRMLS_CC);
 
@@ -250,10 +259,12 @@
 			}
 
 			efree(class_file);
-			return zend_hash_exists(EG(class_table), (char*)lc_name, class_name_len+1);
+			efree(ps_name);
+			return zend_hash_exists(EG(class_table), (char*)ps_name, class_name_len+1);
 		}
 	}
 	efree(class_file);
+	efree(ps_name);
 	return 0;
 } /* }}} */
 
@@ -261,7 +272,7 @@
  Default implementation for __autoload() */
 PHP_FUNCTION(spl_autoload)
 {
-	char *class_name, *lc_name, *file_exts = SPL_G(autoload_extensions);
+	char *class_name, *file_exts = SPL_G(autoload_extensions);
 	int class_name_len, file_exts_len = SPL_G(autoload_extensions_len), found = 0;
 	char *copy, *pos1, *pos2;
 	zval **original_return_value = EG(return_value_ptr_ptr);
@@ -278,7 +289,6 @@
 	} else {
 		copy = pos1 = estrndup(file_exts, file_exts_len);
 	}
-	lc_name = zend_str_tolower_dup(class_name, class_name_len);
 	while(pos1 && *pos1 && !EG(exception)) {
 		EG(return_value_ptr_ptr) = original_return_value;
 		EG(opline_ptr) = original_opline_ptr;
@@ -286,13 +296,12 @@
 		EG(function_state_ptr) = original_function_state_ptr;
 		pos2 = strchr(pos1, ',');
 		if (pos2) *pos2 = '\0';
-		if (spl_autoload(class_name, lc_name, class_name_len, pos1 TSRMLS_CC)) {
+		if (spl_autoload(class_name, class_name_len, pos1 TSRMLS_CC)) {
 			found = 1;
 			break; /* loaded */
 		}
 		pos1 = pos2 ? pos2 + 1 : NULL;
 	}
-	efree(lc_name);
 	if (copy) {
 		efree(copy);
 	}
@@ -341,6 +350,21 @@
 	}
 } /* }}} */
 
+
+/* {{{ proto bool spl_autoload_case_sensitive([bool use_case_sensitive_file_naming])
+ Register and returns the use case sensitive file naming flag for spl_autoload */
+PHP_FUNCTION(spl_autoload_case_sensitive)
+{
+    zend_bool use_case_sensitive = SPL_G(autoload_case_sensitive);
+    
+    zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|b", &use_case_sensitive);
+
+    SPL_G(autoload_case_sensitive) = use_case_sensitive;
+    
+    RETURN_BOOL(SPL_G(autoload_case_sensitive));    
+} /* }}} */
+
+
 typedef struct {
 	zend_function *func_ptr;
 	zval *obj;
@@ -698,20 +722,21 @@
 /* {{{ spl_functions
  */
 zend_function_entry spl_functions[] = {
-	PHP_FE(spl_classes,             NULL)
-	PHP_FE(spl_autoload,            NULL)
-	PHP_FE(spl_autoload_extensions, NULL)
-	PHP_FE(spl_autoload_register,   NULL)
-	PHP_FE(spl_autoload_unregister, NULL)
-	PHP_FE(spl_autoload_functions,  NULL)
-	PHP_FE(spl_autoload_call,       NULL)
-	PHP_FE(class_parents,           NULL)
-	PHP_FE(class_implements,        NULL)
-	PHP_FE(spl_object_hash,         NULL)
+	PHP_FE(spl_classes,                 NULL)
+	PHP_FE(spl_autoload,                NULL)
+	PHP_FE(spl_autoload_extensions,     NULL)
+	PHP_FE(spl_autoload_case_sensitive, NULL)
+	PHP_FE(spl_autoload_register,       NULL)
+	PHP_FE(spl_autoload_unregister,     NULL)
+	PHP_FE(spl_autoload_functions,      NULL)
+	PHP_FE(spl_autoload_call,           NULL)
+	PHP_FE(class_parents,               NULL)
+	PHP_FE(class_implements,            NULL)
+	PHP_FE(spl_object_hash,             NULL)
 #ifdef SPL_ITERATORS_H
-	PHP_FE(iterator_to_array,       arginfo_iterator_to_array)
-	PHP_FE(iterator_count,          arginfo_iterator)
-	PHP_FE(iterator_apply,          arginfo_iterator_apply)
+	PHP_FE(iterator_to_array,           arginfo_iterator_to_array)
+	PHP_FE(iterator_count,              arginfo_iterator)
+	PHP_FE(iterator_apply,              arginfo_iterator_apply)
 #endif /* SPL_ITERATORS_H */
 	{NULL, NULL, NULL}
 };
Index: ext/spl/php_spl.h
===================================================================
--- ext/spl/php_spl.h	(revision 306824)
+++ ext/spl/php_spl.h	(working copy)
@@ -60,6 +60,7 @@
 	HashTable *  autoload_functions;
 	int          autoload_running;
 	int          autoload_extensions_len;
+	int          autoload_case_sensitive;
 ZEND_END_MODULE_GLOBALS(spl)
 
 #ifdef ZTS
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri May 03 11:01:32 2024 UTC