Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion config.m4
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion config.w32
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@
ARG_ENABLE("splclassloader", "enable splclassloader support", "no");

if (PHP_SPLCLASSLOADER != "no") {
EXTENSION("splclassloader", "splclassloader.c");
EXTENSION("splclassloader", "spl_classloader.c");
}
45 changes: 28 additions & 17 deletions splclassloader.c → spl_classloader.c
Original file line number Diff line number Diff line change
@@ -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 |
Expand All @@ -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)
Expand All @@ -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;
Expand Down Expand Up @@ -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 */
Expand Down Expand Up @@ -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 <davidc@php.net>");
php_info_print_table_row (2, "Author", "Marcel Araujo <admin@marcelaraujo.me>");
php_info_print_table_end ();
}

Expand All @@ -424,6 +435,6 @@ zend_module_entry splclassloader_module_entry = {
NULL,
NULL,
PHP_MINFO(splclassloader),
"0.1",
"0.2",
STANDARD_MODULE_PROPERTIES
};
File renamed without changes.
136 changes: 136 additions & 0 deletions tests/bench/SplClassLoader.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
<?php

/**
* 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 <jonwage@gmail.com>
* @author Roman S. Borschel <roman@code-factory.org>
* @author Matthew Weier O'Phinney <matthew@zend.com>
* @author Kris Wallsmith <kris.wallsmith@gmail.com>
* @author Fabien Potencier <fabien.potencier@symfony-project.org>
*/
class SplClassLoader
{
private $_fileExtension = '.php';
private $_namespace;
private $_includePath;
private $_namespaceSeparator = '\\';

/**
* Creates a new <tt>SplClassLoader</tt> 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;
}
}
}
25 changes: 25 additions & 0 deletions tests/bench/functions.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

function getmicrotime() {
$t = gettimeofday();
return ($t['sec'] + $t['usec'] / 1000000);
}

function start_test() {
ob_start();
return getmicrotime();
}

function end_test($start, $name) {
global $total;
$end = getmicrotime();
ob_end_clean();
$total += $end-$start;
$num = number_format($end-$start,3);
$len = (24 - strlen($name) - strlen($num));
$pad = str_repeat(" ", ($len < 0 ? 0 : $len) );

echo $name.' '.$pad.$num."\n";
ob_start();
return getmicrotime();
}
12 changes: 12 additions & 0 deletions tests/bench/index.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

ini_set('display_errors', 1);
error_reporting(E_ALL);

if (function_exists("date_default_timezone_set")) {
date_default_timezone_set("America/Sao_Paulo");
}

echo shell_exec('php test-class.php');
echo "\n";
echo shell_exec('php test-native.php');
7 changes: 7 additions & 0 deletions tests/bench/results.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Running on a PHP 5.4.11 enviroment

SplClassLoader PHP Class 0.001
Memory Usage: 33456 bytes

SplClassLoader PHP Native 0.001
Memory Usage: 13536 bytes
24 changes: 24 additions & 0 deletions tests/bench/test-class.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

require_once 'functions.php';

$t0 = start_test();
$startMemory = memory_get_usage();

require_once 'functions.php';
require_once 'SplClassLoader.php';

$classLoader = new SplClassLoader('Yeap', realpath('vendor'));
$classLoader->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";
25 changes: 25 additions & 0 deletions tests/bench/test-native.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

require_once 'functions.php';

if (!extension_loaded('splclassloader')) {
dl('splclassloader.so');
}

$t0 = start_test();
$startMemory = memory_get_usage();

$classLoader = new SplClassLoader('Yeap', realpath('vendor'));
$classLoader->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";
9 changes: 9 additions & 0 deletions tests/bench/vendor/Yeap/Animal.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

namespace Yeap;

class Animal {
public function speak() {
echo 'Animal sounds...';
}
}
7 changes: 7 additions & 0 deletions tests/bench/vendor/Yeap/BigHornSheep.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

namespace Yeap;

class BigHornSheep extends Animal {
use Sheep;
}
9 changes: 9 additions & 0 deletions tests/bench/vendor/Yeap/Cow.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

namespace Yeap;

trait Cow {
public function speak() {
echo 'moo!';
}
}
7 changes: 7 additions & 0 deletions tests/bench/vendor/Yeap/MilkCow.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

namespace Yeap;

class MilkCow extends Animal {
use Cow;
}
9 changes: 9 additions & 0 deletions tests/bench/vendor/Yeap/Pig.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

namespace Yeap;

trait Pig {
public function speak() {
echo 'oink!';
}
}
Loading