diff --git a/config.m4 b/config.m4 index fd96ebd..e75478a 100644 --- a/config.m4 +++ b/config.m4 @@ -3,6 +3,6 @@ PHP_ARG_ENABLE(splclassloader, whether to enable splclassloader, if test "$PHP_SPLCLASSLOADER" != "no"; then - PHP_NEW_EXTENSION(splclassloader, splclassloader.c, $ext_shared) + PHP_NEW_EXTENSION(splclassloader, spl_classloader.c, $ext_shared) fi diff --git a/config.w32 b/config.w32 index 834f855..ae8feb0 100644 --- a/config.w32 +++ b/config.w32 @@ -2,5 +2,5 @@ ARG_ENABLE("splclassloader", "enable splclassloader support", "no"); if (PHP_SPLCLASSLOADER != "no") { - EXTENSION("splclassloader", "splclassloader.c"); + EXTENSION("splclassloader", "spl_classloader.c"); } diff --git a/splclassloader.c b/spl_classloader.c similarity index 89% rename from splclassloader.c rename to spl_classloader.c index 6bbfcfc..0b6b1c5 100644 --- a/splclassloader.c +++ b/spl_classloader.c @@ -1,8 +1,8 @@ /* +----------------------------------------------------------------------+ - | PHP Version 5 | + | PHP Version 5.4 | +----------------------------------------------------------------------+ - | Copyright (c) 1997-2009 The PHP Group | + | Copyright (c) 1997-2013 The PHP Group | +----------------------------------------------------------------------+ | This source file is subject to version 3.01 of the PHP license, | | that is bundled with this package in the file LICENSE, and is | @@ -24,8 +24,8 @@ #include "php_ini.h" #include "ext/standard/info.h" #include "zend_interfaces.h" -#include "php_splclassloader.h" +#include "spl_classloader.h" #ifdef COMPILE_DL_SPLCLASSLOADER ZEND_GET_MODULE(splclassloader) @@ -34,11 +34,9 @@ ZEND_GET_MODULE(splclassloader) /* zend_API.h: #define ZEND_NS_NAME(ns, name) ns"\\"name */ #define SPLCLASSLD_NS_SEPARATOR '\\' - static zend_class_entry* splclassloader_ce; zend_object_handlers splclassloader_object_handlers; - typedef struct _splclassloader_object { zend_object std; char* ns; @@ -69,26 +67,36 @@ static void splclassloader_object_dtor(void* object, zend_object_handle handle T efree(obj); } - -zend_object_value splclassloader_create_object(zend_class_entry* class_type TSRMLS_DC) +zend_object_value spl_object_classloader_new_ex(zend_class_entry *class_type, splclassloader_object **obj, zval *orig TSRMLS_DC) /* {{{ */ { zend_object_value retval; + splclassloader_object *intern; - splclassloader_object* obj = (splclassloader_object*)emalloc(sizeof(splclassloader_object)); - memset(obj, 0, sizeof(splclassloader_object)); + intern = emalloc(sizeof(splclassloader_object)); + memset(intern, 0, sizeof(splclassloader_object)); + *obj = intern; - zend_object_std_init(&obj->std, class_type TSRMLS_CC); - zend_hash_copy(obj->std.properties, &class_type->default_properties, (copy_ctor_func_t) zval_add_ref, (void*)0, sizeof(zval*)); + zend_object_std_init(&intern->std, class_type TSRMLS_CC); + object_properties_init(&intern->std, class_type); - obj->file_ext = estrndup(".php", sizeof(".php")-1); - obj->file_ext_len = sizeof(".php")-1; - - retval.handle = zend_objects_store_put(obj, (zend_objects_store_dtor_t)zend_objects_destroy_object, (void*)splclassloader_object_dtor, NULL TSRMLS_CC); + //zend_hash_copy(intern->std.properties, &class_type->default_properties, (copy_ctor_func_t) zval_add_ref, (void*)NULL, sizeof(zval*)); + + intern->file_ext = estrndup(".php", sizeof(".php")-1); + intern->file_ext_len = sizeof(".php")-1; + + retval.handle = zend_objects_store_put(intern, (zend_objects_store_dtor_t)zend_objects_destroy_object, (void*)splclassloader_object_dtor, NULL TSRMLS_CC); retval.handlers = &splclassloader_object_handlers; return retval; -} +} /* }}} */ +/* {{{ splclassloader_create_object */ +static zend_object_value splclassloader_create_object(zend_class_entry *class_type TSRMLS_DC) +{ + splclassloader_object *tmp; + return spl_object_classloader_new_ex(class_type, &tmp, NULL TSRMLS_CC); +} +/* }}} */ /* {{{ proto void SplClassLoader::__construct([string $namespace [, string $include_path]]) Constructor */ @@ -411,6 +419,9 @@ PHP_MINFO_FUNCTION(splclassloader) php_info_print_table_start (); php_info_print_table_header(2, "SplClassLoader support", "enabled"); php_info_print_table_row (2, "Conformance", "PSR-0"); + php_info_print_table_row (2, "Version", "0.2"); + php_info_print_table_row (2, "Author", "David Coallier "); + php_info_print_table_row (2, "Author", "Marcel Araujo "); php_info_print_table_end (); } @@ -424,6 +435,6 @@ zend_module_entry splclassloader_module_entry = { NULL, NULL, PHP_MINFO(splclassloader), - "0.1", + "0.2", STANDARD_MODULE_PROPERTIES }; diff --git a/php_splclassloader.h b/spl_classloader.h similarity index 100% rename from php_splclassloader.h rename to spl_classloader.h diff --git a/tests/bench/SplClassLoader.php b/tests/bench/SplClassLoader.php new file mode 100644 index 0000000..2778340 --- /dev/null +++ b/tests/bench/SplClassLoader.php @@ -0,0 +1,136 @@ +register(); + * + * @author Jonathan H. Wage + * @author Roman S. Borschel + * @author Matthew Weier O'Phinney + * @author Kris Wallsmith + * @author Fabien Potencier + */ +class SplClassLoader +{ + private $_fileExtension = '.php'; + private $_namespace; + private $_includePath; + private $_namespaceSeparator = '\\'; + + /** + * Creates a new SplClassLoader that loads classes of the + * specified namespace. + * + * @param string $ns The namespace to use. + */ + public function __construct($ns = null, $includePath = null) + { + $this->_namespace = $ns; + $this->_includePath = $includePath; + } + + /** + * Sets the namespace separator used by classes in the namespace of this class loader. + * + * @param string $sep The separator to use. + */ + public function setNamespaceSeparator($sep) + { + $this->_namespaceSeparator = $sep; + } + + /** + * Gets the namespace seperator used by classes in the namespace of this class loader. + * + * @return void + */ + public function getNamespaceSeparator() + { + return $this->_namespaceSeparator; + } + + /** + * Sets the base include path for all class files in the namespace of this class loader. + * + * @param string $includePath + */ + public function setIncludePath($includePath) + { + $this->_includePath = $includePath; + } + + /** + * Gets the base include path for all class files in the namespace of this class loader. + * + * @return string $includePath + */ + public function getIncludePath() + { + return $this->_includePath; + } + + /** + * Sets the file extension of class files in the namespace of this class loader. + * + * @param string $fileExtension + */ + public function setFileExtension($fileExtension) + { + $this->_fileExtension = $fileExtension; + } + + /** + * Gets the file extension of class files in the namespace of this class loader. + * + * @return string $fileExtension + */ + public function getFileExtension() + { + return $this->_fileExtension; + } + + /** + * Installs this class loader on the SPL autoload stack. + */ + public function register() + { + spl_autoload_register(array($this, 'loadClass')); + } + + /** + * Uninstalls this class loader from the SPL autoloader stack. + */ + public function unregister() + { + spl_autoload_unregister(array($this, 'loadClass')); + } + + /** + * Loads the given class or interface. + * + * @param string $className The name of the class to load. + * @return void + */ + public function loadClass($className) + { + if (null === $this->_namespace || $this->_namespace.$this->_namespaceSeparator === substr($className, 0, strlen($this->_namespace.$this->_namespaceSeparator))) { + $fileName = ''; + $namespace = ''; + if (false !== ($lastNsPos = strripos($className, $this->_namespaceSeparator))) { + $namespace = substr($className, 0, $lastNsPos); + $className = substr($className, $lastNsPos + 1); + $fileName = str_replace($this->_namespaceSeparator, DIRECTORY_SEPARATOR, $namespace) . DIRECTORY_SEPARATOR; + } + $fileName .= str_replace('_', DIRECTORY_SEPARATOR, $className) . $this->_fileExtension; + + require ($this->_includePath !== null ? $this->_includePath . DIRECTORY_SEPARATOR : '') . $fileName; + } + } +} diff --git a/tests/bench/functions.php b/tests/bench/functions.php new file mode 100644 index 0000000..15d696a --- /dev/null +++ b/tests/bench/functions.php @@ -0,0 +1,25 @@ +register(); + +$piggy = new \Yeap\Piggy(); +$piggy->speak(); + +$cow = new \Yeap\MilkCow; +$cow->speak(); + +$sheep = new \Yeap\BigHornSheep; +$sheep->speak(); + +end_test($t0, "SplClassLoader PHP Class"); +echo "Memory Usage: " . (memory_get_usage() - $startMemory) . " bytes\n"; \ No newline at end of file diff --git a/tests/bench/test-native.php b/tests/bench/test-native.php new file mode 100644 index 0000000..d71d157 --- /dev/null +++ b/tests/bench/test-native.php @@ -0,0 +1,25 @@ +register(); + +$piggy = new \Yeap\Piggy(); +$piggy->speak(); + +$cow = new \Yeap\MilkCow; +$cow->speak(); + +$sheep = new \Yeap\BigHornSheep; +$sheep->speak(); + +end_test($t0, "SplClassLoader PHP Native"); +echo "Memory Usage: " . (memory_get_usage() - $startMemory) . " bytes\n"; \ No newline at end of file diff --git a/tests/bench/vendor/Yeap/Animal.php b/tests/bench/vendor/Yeap/Animal.php new file mode 100644 index 0000000..68e0d97 --- /dev/null +++ b/tests/bench/vendor/Yeap/Animal.php @@ -0,0 +1,9 @@ +register(); -* -* @author Jonathan H. Wage -* @author Roman S. Borschel -* @author Matthew Weier O'Phinney -* @author Kris Wallsmith -* @author Fabien Potencier -*/ -class SplClassLoaderUser + * SplClassLoader implementation that implements the technical interoperability + * standards for PHP 5.3 namespaces and class names. + * + * http://groups.google.com/group/php-standards/web/final-proposal + * + * // Example which loads classes for the Doctrine Common package in the + * // Doctrine\Common namespace. + * $classLoader = new SplClassLoader('Doctrine\Common', '/path/to/doctrine'); + * $classLoader->register(); + * + * @author Jonathan H. Wage + * @author Roman S. Borschel + * @author Matthew Weier O'Phinney + * @author Kris Wallsmith + * @author Fabien Potencier + */ +class SplClassLoader { private $_fileExtension = '.php'; private $_namespace; private $_includePath; private $_namespaceSeparator = '\\'; - + /** -* Creates a new SplClassLoader that loads classes of the -* specified namespace. -* -* @param string $ns The namespace to use. -*/ + * Creates a new SplClassLoader that loads classes of the + * specified namespace. + * + * @param string $ns The namespace to use. + */ public function __construct($ns = null, $includePath = null) { $this->_namespace = $ns; $this->_includePath = $includePath; } - + /** -* Sets the namespace separator used by classes in the namespace of this class loader. -* -* @param string $sep The separator to use. -*/ + * Sets the namespace separator used by classes in the namespace of this class loader. + * + * @param string $sep The separator to use. + */ public function setNamespaceSeparator($sep) { $this->_namespaceSeparator = $sep; } - + /** -* Gets the namespace seperator used by classes in the namespace of this class loader. -* -* @return void -*/ + * Gets the namespace seperator used by classes in the namespace of this class loader. + * + * @return void + */ public function getNamespaceSeparator() { return $this->_namespaceSeparator; } - + /** -* Sets the base include path for all class files in the namespace of this class loader. -* -* @param string $includePath -*/ + * Sets the base include path for all class files in the namespace of this class loader. + * + * @param string $includePath + */ public function setIncludePath($includePath) { $this->_includePath = $includePath; } - + /** -* Gets the base include path for all class files in the namespace of this class loader. -* -* @return string $includePath -*/ + * Gets the base include path for all class files in the namespace of this class loader. + * + * @return string $includePath + */ public function getIncludePath() { return $this->_includePath; } - + /** -* Sets the file extension of class files in the namespace of this class loader. -* -* @param string $fileExtension -*/ + * Sets the file extension of class files in the namespace of this class loader. + * + * @param string $fileExtension + */ public function setFileExtension($fileExtension) { $this->_fileExtension = $fileExtension; } - + /** -* Gets the file extension of class files in the namespace of this class loader. -* -* @return string $fileExtension -*/ + * Gets the file extension of class files in the namespace of this class loader. + * + * @return string $fileExtension + */ public function getFileExtension() { return $this->_fileExtension; } - + /** -* Installs this class loader on the SPL autoload stack. -*/ + * Installs this class loader on the SPL autoload stack. + */ public function register() { spl_autoload_register(array($this, 'loadClass')); } - + /** -* Uninstalls this class loader from the SPL autoloader stack. -*/ + * Uninstalls this class loader from the SPL autoloader stack. + */ public function unregister() { spl_autoload_unregister(array($this, 'loadClass')); } - + /** -* Loads the given class or interface. -* -* @param string $className The name of the class to load. -* @return void -*/ + * Loads the given class or interface. + * + * @param string $className The name of the class to load. + * @return void + */ public function loadClass($className) { if (null === $this->_namespace || $this->_namespace.$this->_namespaceSeparator === substr($className, 0, strlen($this->_namespace.$this->_namespaceSeparator))) { @@ -129,8 +129,8 @@ public function loadClass($className) $fileName = str_replace($this->_namespaceSeparator, DIRECTORY_SEPARATOR, $namespace) . DIRECTORY_SEPARATOR; } $fileName .= str_replace('_', DIRECTORY_SEPARATOR, $className) . $this->_fileExtension; - + require ($this->_includePath !== null ? $this->_includePath . DIRECTORY_SEPARATOR : '') . $fileName; } } -} \ No newline at end of file +}