Page Menu
Home
In-Portal Phabricator
Search
Configure Global Search
Log In
Files
F847692
D115.id255.diff
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Flag For Later
Subscribers
None
File Metadata
Details
File Info
Storage
Attached
Created
Sat, Apr 19, 11:53 AM
Size
11 KB
Mime Type
text/x-diff
Expires
Sun, Apr 20, 11:53 AM (50 m, 56 s)
Engine
blob
Format
Raw Data
Handle
602495
Attached To
D115: INP-1465 - Don't calculate "profiler enabled" state each time in class factory
D115.id255.diff
View Options
Index: core/kernel/application.php
===================================================================
--- core/kernel/application.php
+++ core/kernel/application.php
@@ -2222,7 +2222,7 @@
* @param Array $arguments
* @return kBase
*/
- public function recallObject($name, $pseudo_class = null, $event_params = Array(), $arguments = Array ())
+ public function recallObject($name, $pseudo_class = null, array $event_params = array(), array $arguments = array())
{
/*if ( !$this->hasObject($name) && $this->isDebugMode() && ($name == '_prefix_here_') ) {
// first time, when object with "_prefix_here_" prefix is accessed
@@ -2280,7 +2280,7 @@
* @return kBase
* @access public
*/
- public function makeClass($pseudo_class, $arguments = Array ())
+ public function makeClass($pseudo_class, array $arguments = array())
{
return $this->Factory->makeClass($pseudo_class, $arguments);
}
Index: core/kernel/utility/factory.php
===================================================================
--- core/kernel/utility/factory.php
+++ core/kernel/utility/factory.php
@@ -16,7 +16,8 @@
defined('FULL_PATH') or die('restricted access!');
-class kFactory extends kBase implements kiCacheable {
+class kFactory extends kBase implements kiCacheable
+{
const TYPE_CLASS = 1;
const TYPE_INTERFACE = 2;
@@ -26,14 +27,13 @@
const MODIFIER_FINAL = 2;
/**
- * Mapping between class name and file name
- * where class definition can be found.
+ * Mapping between class name and file name where class definition can be found.
+ *
* File path absolute!
*
- * @var Array
- * @access protected
+ * @var array
*/
- protected $classMap = Array ();
+ protected $classMap = array();
/**
* Class information.
@@ -57,28 +57,46 @@
protected $namespaceMap = array();
/**
- * Map class names to their pseudo
- * class names, e.g. key=pseudo_class,
- * value=real_class_name
+ * Map class names to their pseudo class names, e.g. key=pseudo_class, value=real_class_name.
*
- * @var Array
- * @access protected
+ * @var array
*/
- protected $realClasses = Array ();
+ protected $realClasses = array();
/**
- * Where all created objects are stored
+ * Where all created objects are stored.
*
- * @var Array|kBase[]
- * @access protected
+ * @var kBase[]
*/
- protected $Storage = Array ();
+ protected $storage = array();
+ /**
+ * Debug factory internal processing.
+ *
+ * @var boolean
+ */
+ private $_debugFactory = false;
+
+ /**
+ * Profile memory usage during class file inclusion.
+ *
+ * @var boolean
+ */
+ private $_profileMemory = false;
+
+ /**
+ * Creates factory instance.
+ */
public function __construct()
{
parent::__construct();
spl_autoload_register(array($this, 'autoload'), true, true);
+
+ if ( defined('DEBUG_MODE') && DEBUG_MODE && $this->Application->isDebugMode() ) {
+ $this->_debugFactory = defined('DBG_FACTORY') && DBG_FACTORY;
+ $this->_profileMemory = defined('DBG_PROFILE_MEMORY') && DBG_PROFILE_MEMORY;
+ }
}
/**
@@ -134,11 +152,11 @@
}
/**
- * Sets data from cache to object
+ * Sets data from cache to object.
+ *
+ * @param array $data Data.
*
- * @param Array $data
* @return void
- * @access public
*/
public function setFromCache(&$data)
{
@@ -150,11 +168,11 @@
}
/**
- * Performs automatic loading of classes registered with the factory
+ * Performs automatic loading of classes registered with the factory.
+ *
+ * @param string $class Class.
*
- * @param string $class
- * @return bool|null
- * @access public
+ * @return boolean|null
*/
public function autoload($class)
{
@@ -172,9 +190,9 @@
/**
* Finds the path to the file where the class is defined.
*
- * @param string $class The name of the class
- * @return string|bool The path if found, false otherwise
- * @access protected
+ * @param string $class The name of the class.
+ *
+ * @return string|boolean The path if found, false otherwise.
*/
protected function findFile($class)
{
@@ -189,12 +207,12 @@
$pos = strrpos($class, '\\');
if ( $pos !== false ) {
- // namespaced class name
+ // Namespaced class name.
$class_path = str_replace('\\', DIRECTORY_SEPARATOR, substr($class, 0, $pos)) . DIRECTORY_SEPARATOR;
$class_name = substr($class, $pos + 1);
}
else {
- // PEAR-like class name
+ // PEAR-like class name.
$class_path = null;
$class_name = $class;
}
@@ -219,10 +237,9 @@
}
/**
- * Gets object data for caching
+ * Gets object data for caching.
*
- * @return Array
- * @access public
+ * @return array
*/
public function getToCache()
{
@@ -232,7 +249,7 @@
ksort($this->namespaceMap);
ksort($this->realClasses);
- return Array (
+ return array(
'Factory.Files' => $this->classMap,
'Factory.ClassInfo' => $this->classInfo,
'Factory.ClassTree' => $this->classTree,
@@ -242,34 +259,29 @@
}
/**
- * Splits any mixing of prefix and
- * special into correct ones
+ * Splits any mixing of prefix and special into correct ones.
*
- * @param string $prefix_special
- * @return Array
- * @access public
+ * @param string $prefix_special Prefix-special.
+ *
+ * @return array
*/
public function processPrefix($prefix_special)
{
- // l.pick, l, m.test_TagProcessor
-
- //preg_match("/(.*)\.*(.*)(_*)(.*)/", $prefix_special, $regs);
- //return Array('prefix'=>$regs[1].$regs[3].$regs[4], 'special'=>$regs[2]);
-
+ // Example: "l.pick", "l", "m.test_TagProcessor".
$tmp = explode('_', $prefix_special, 2);
$tmp[0] = explode('.', $tmp[0]);
$prefix = $tmp[0][0];
- $prefix_special = $prefix; // new1
+ $prefix_special = $prefix;
if ( isset($tmp[1]) ) {
$prefix .= '_' . $tmp[1];
}
$special = isset($tmp[0][1]) ? $tmp[0][1] : '';
- $prefix_special .= '.' . $special; // new2
+ $prefix_special .= '.' . $special;
- return Array ('prefix' => $prefix, 'special' => $special, 'prefix_special' => $prefix_special);
+ return array('prefix' => $prefix, 'special' => $special, 'prefix_special' => $prefix_special);
}
/**
@@ -277,17 +289,17 @@
*
* @param string $name Object name in factory.
* @param string $pseudo_class Pseudo class.
- * @param Array $event_params Event params.
- * @param Array $arguments Constructor arguments.
+ * @param array $event_params Event params.
+ * @param array $arguments Constructor arguments.
*
* @return kBase
*/
- public function getObject($name, $pseudo_class = '', $event_params = Array (), $arguments = Array ())
+ public function getObject($name, $pseudo_class = '', array $event_params = array(), array $arguments = array())
{
$name = rtrim($name, '.');
- if ( isset($this->Storage[$name]) ) {
- return $this->Storage[$name];
+ if ( isset($this->storage[$name]) ) {
+ return $this->storage[$name];
}
$ret = $this->processPrefix($name);
@@ -296,40 +308,42 @@
$pseudo_class = $ret['prefix'];
}
- if ( defined('DEBUG_MODE') && defined('DBG_FACTORY') && DBG_FACTORY && $this->Application->isDebugMode() ) {
- $this->Application->Debugger->appendHTML('<b>Creating object:</b> Pseudo class: ' . $pseudo_class . ' Prefix: ' . $name);
+ if ( $this->_debugFactory ) {
+ $this->Application->Debugger->appendHTML(
+ '<strong>Creating object:</strong> Pseudo class: ' . $pseudo_class . ' Prefix: ' . $name
+ );
$this->Application->Debugger->appendTrace();
}
- $this->Storage[$name] = $this->makeClass($pseudo_class, $arguments);
- $this->Storage[$name]->Init($ret['prefix'], $ret['special']);
+ $this->storage[$name] = $this->makeClass($pseudo_class, $arguments);
+ $this->storage[$name]->Init($ret['prefix'], $ret['special']);
$this->Application->EventManager->runBuildEvent($ret['prefix_special'], $pseudo_class, $event_params);
- return $this->Storage[$name];
+ return $this->storage[$name];
}
/**
- * Removes object from storage, so next time it could be created from scratch
+ * Removes object from storage, so next time it could be created from scratch.
+ *
+ * @param string $name Object's name in the Storage.
*
- * @param string $name Object's name in the Storage
* @return void
- * @access public
*/
public function DestroyObject($name)
{
- unset($this->Storage[$name]);
+ unset($this->storage[$name]);
}
/**
- * Checks if object with prefix passes was already created in factory
+ * Checks if object with prefix passes was already created in factory.
+ *
+ * @param string $name Object pseudo_class, prefix.
*
- * @param string $name object pseudo_class, prefix
- * @return bool
- * @access public
+ * @return boolean
*/
public function hasObject($name)
{
- return isset($this->Storage[$name]);
+ return isset($this->storage[$name]);
}
/**
@@ -343,7 +357,7 @@
* @return kBase
* @throws kFactoryException When class not found.
*/
- public function makeClass($pseudo_class, $arguments = Array ())
+ public function makeClass($pseudo_class, array $arguments = array())
{
if ( !isset($this->realClasses[$pseudo_class]) ) {
$error_msg = 'RealClass not defined for "<strong>' . $pseudo_class . '</strong>" pseudo_class.';
@@ -371,24 +385,27 @@
$arguments = (array)$arguments;
if ( !$arguments ) {
- $class = new $real_class();
+ $object = new $real_class();
}
else {
$reflection = new ReflectionClass($real_class);
- $class = $reflection->newInstanceArgs($arguments);
+ $object = $reflection->newInstanceArgs($arguments);
}
- if ( defined('DEBUG_MODE') && DEBUG_MODE && defined('DBG_PROFILE_MEMORY') && DBG_PROFILE_MEMORY && $this->Application->isDebugMode() ) {
+ if ( $this->_profileMemory ) {
$mem_after = memory_get_usage();
$time_after = microtime(true);
$mem_used = $mem_after - $mem_before;
- $time_used = $time_after - $time_before;
+ $mem_used_formatted = round($mem_used / 1024, 3);
+ $time_used = round($time_after - $time_before, 5);
- $this->Application->Debugger->appendHTML('Factroy created <b>' . $real_class . '</b> - used ' . round($mem_used / 1024, 3) . 'Kb time: ' . round($time_used, 5));
+ $this->Application->Debugger->appendHTML(
+ 'Factory created <b>' . $real_class . '</b> - used ' . $mem_used_formatted . 'Kb time: ' . $time_used
+ );
$this->Application->Debugger->profilerAddTotal('objects', null, $mem_used);
}
- return $class;
+ return $object;
}
/**
@@ -453,11 +470,11 @@
/**
* Registers new class in the factory
*
- * @param string $real_class Real name of class as in class declaration
- * @param string $file Filename in what $real_class is declared
- * @param string $pseudo_class Name under this class object will be accessed using getObject method
+ * @param string $real_class Real name of class as in class declaration.
+ * @param string $file Filename in what $real_class is declared.
+ * @param string $pseudo_class Name under this class object will be accessed using getObject method.
+ *
* @return void
- * @access public
*/
public function registerClass($real_class, $file, $pseudo_class = null)
{
@@ -475,18 +492,19 @@
/**
* Unregisters existing class from factory
*
- * @param string $real_class Real name of class as in class declaration
- * @param string $pseudo_class Name under this class object is accessed using getObject method
+ * @param string $real_class Real name of class as in class declaration.
+ * @param string $pseudo_class Name under this class object is accessed using getObject method.
+ *
* @return void
- * @access public
*/
public function unregisterClass($real_class, $pseudo_class = null)
{
unset($this->classMap[$real_class]);
}
-}
+}
-class kFactoryException extends Exception {
+class kFactoryException extends Exception
+{
}
Event Timeline
Log In to Comment