@ 2013-11-13T14:08:09Z <?php
<?php
/**
* Object Registry: get instance of requested and optionally registered object.
*
* Object (instance of a class) is generater, or returned from internal cache
* if it was requested and instantiated before.
*
* @author Time.ly Network, Inc.
* @since 2.0
* @package Ai1EC
* @subpackage Ai1EC.Registry
*/
class Ai1ec_Object_Registry {
/**
* @var array The internal objects cache
*/
private $_objects = array();
/**
* @var Ai1ec_Loader The Ai1ec_Loader instance used by the registry
*/
private $_loader = null;
/**
* Get class instance.
*
* Return an instance for the requested key, this method has an internal
* cache.
*
* @param string $key Name of previously registered object or parseable
* class name
*
* @return object Instance of the requested class
*/
public function get( $key ) {
$class_data = $this->_loader->resolve_class_name( $key );
if ( ! $class_data ) {
throw new Ai1ec_Bootstrap_Exception(
'Unable to resolve class for "' . $key . '"'
);
}
$class_name = $class_data['c'];
$instantiator = $class_data['i'];
$args = array_slice( func_get_args(), 1 );
if ( isset ( $class_data['r'] ) ) {
array_unshift( $args, $this );
}
if ( Ai1ec_Loader::NEWINST === $instantiator ) {
return $this->initiate(
$class_name,
$args
);
}
if ( Ai1ec_Loader::GLOBALINST === $instantiator ) {
if ( ! isset( $this->_objects[$class_name] ) ) {
// Ask the loader to load the required files to avoid autoloader
$this->_loader->load( $class_name );
$this->_objects[$class_name] = $this->initiate(
$class_name,
$args
);
}
return $this->_objects[$class_name];
}
// Ok it's a factory.
$factory = explode( '.', $instantiator );
return $this->dispatch(
$factory[0],
$factory[1],
$args
);
}
/**
* Instanciate the class given the class names and arguments.
*
* @param string $class_name The name of the class to instanciate.
* @param array $argv An array of aguments for construction.
*
* @return object A new instance of the requested class
*/
public function initiate( $class_name, array $argv = array() ) {
switch ( count( $argv ) ) {
case 0:
return new $class_name();
case 1:
return new $class_name( $argv[0] );
case 2:
return new $class_name( $argv[0], $argv[1] );
case 3:
return new $class_name( $argv[0], $argv[1], $argv[2] );
case 4:
return new $class_name( $argv[0], $argv[1], $argv[2], $argv[3] );
case 5:
return new $class_name( $argv[0], $argv[1], $argv[2], $argv[3], $argv[4] );
}
$reflected = new ReflectionClass( $class_name );
return $reflected->newInstanceArgs( $argv );
}
/**
* A call_user_func_array alternative.
*
* @param string $class
* @param string $method
* @param array $params
*
* @return mixed
*/
public function dispatch( $class, $method, $params = array() ) {
if ( empty( $class ) ) {
switch ( count( $params) ) {
case 0:
return $method();
case 1:
return $method( $params[0] );
case 2:
return $method( $params[0], $params[1] );
case 3:
return $method( $params[0], $params[1], $params[2] );
default:
return call_user_func_array(
array( $class, $method ),
$params
);
}
} else {
// get an instance of the class
$class = $this->get( $class );
switch ( count( $params) ) {
case 0:
return $class->{$method}();
case 1:
return $class->{$method}( $params[0] );
case 2:
return $class->{$method}( $params[0], $params[1] );
case 3:
return $class->{$method}( $params[0], $params[1], $params[2] );
default:
return call_user_func_array(
array( $class, $method ),
$params
);
}
}
}
/**
* Constructor
*
* Initialize the Registry
*
* @param Ai1ec_Loader $ai1ec_loader Instance of Ai1EC classes loader
*
* @return void Constructor does not return
*/
public function __construct( $ai1ec_loader ) {
$this->_loader = $ai1ec_loader;
}
}
class Ai1ec_Core_Callback {
protected $_registry = null;
protected $_registry_name = null;
protected $_method = null;
public function __construct( Ai1ec_Objects_Registry $registry, $path, $method ) {
$this->_registry = $registry;
$this->_registry_name = $path;
$this->_method = $method;
}
public function execute( array $argv ) {
return $this->_registry->get( $this->_registry_name )->{$this->_method}( $argv );
}
}
$registry = new Ai1ec_Object_Registry(null);
for($i = 0; $i < 200; $i++){
$obj = new Ai1ec_Core_Callback($registry, "bla bla bla", "method name");
}
Enable javascript to submit You have javascript disabled. You will not be able to edit any code.
Here you find the average performance (time & memory) of each version. A grayed out version indicates it didn't complete successfully (based on exit-code).
Version System time (s) User time (s) Memory (MiB) 5.4.21 0.019 0.046 12.38 5.4.20 0.011 0.042 12.38 5.4.19 0.011 0.041 12.37 5.4.18 0.010 0.041 12.37 5.4.17 0.009 0.042 12.38 5.4.16 0.012 0.040 12.37 5.4.15 0.009 0.045 12.37 5.4.14 0.009 0.042 12.06 5.4.13 0.012 0.038 12.04 5.4.12 0.010 0.039 12.01 5.4.11 0.013 0.038 12.00 5.4.10 0.008 0.043 12.00 5.4.9 0.019 0.047 12.01 5.4.8 0.012 0.039 12.01 5.4.7 0.012 0.039 12.00 5.4.6 0.009 0.040 12.00 5.4.5 0.011 0.040 12.00 5.4.4 0.012 0.041 11.99 5.4.3 0.010 0.042 11.99 5.4.2 0.012 0.040 11.98 5.4.1 0.009 0.042 11.98 5.4.0 0.013 0.038 11.47 5.3.27 0.013 0.046 12.72 5.3.26 0.011 0.046 12.72 5.3.25 0.010 0.043 12.72 5.3.24 0.011 0.048 12.72 5.3.23 0.013 0.043 12.71 5.3.22 0.014 0.039 12.68 5.3.21 0.009 0.045 12.68 5.3.20 0.012 0.041 12.68 5.3.19 0.010 0.044 12.68 5.3.18 0.011 0.041 12.67 5.3.17 0.019 0.051 12.67 5.3.16 0.013 0.040 12.67 5.3.15 0.016 0.067 12.67 5.3.14 0.014 0.039 12.66 5.3.13 0.012 0.042 12.66 5.3.12 0.009 0.046 12.66 5.3.11 0.012 0.043 12.66 5.3.10 0.012 0.041 12.12 5.3.9 0.012 0.040 12.08 5.3.8 0.010 0.042 12.08 5.3.7 0.017 0.049 12.07 5.3.6 0.011 0.041 12.06 5.3.5 0.009 0.043 12.00 5.3.4 0.013 0.053 12.00 5.3.3 0.009 0.042 11.94 5.3.2 0.010 0.041 11.72 5.3.1 0.009 0.039 11.69 5.3.0 0.012 0.042 11.67
preferences:dark mode live preview
141.71 ms | 1394 KiB | 7 Q