Permalink
Browse files

Merging the profiler changes.

  • Loading branch information...
2 parents f587e45 + c5c654b commit 0a5b6aa1a7bdb3936f69786d45ee53a754dcde72 @zyxist zyxist committed Dec 21, 2010
@@ -13,7 +13,7 @@ public function doSomething()
$event = $this->_profiler->getEvent('doSomething');
$event->notify('oplClass.start', microtime(true));
$class = new Opl_Class();
- $this->_profiler->notify('doSomething', 'oplClass.end', microtime(true));
+ $this->_profiler->notifyEvent('doSomething', 'oplClass.end', microtime(true));
$event->notify('oplClass', 'dupa');
for($i = 0; $i<100000; $i++)
{
@@ -30,7 +30,7 @@ public function doAnything()
{
$i = $i + 5;
}
- $this->_profiler->notify('doAnything', 'template.name', 'name.tpl');
- $this->_profiler->notify('doAnything', 'end', microtime(true));
+ $this->_profiler->notifyEvent('doAnything', 'template.name', 'name.tpl');
+ $this->_profiler->notifyEvent('doAnything', 'end', microtime(true));
} // end doAnything();
} // end Test_Class;
@@ -5,16 +5,17 @@
$loader->addLibrary('Test', './');
$loader->register();
-$profiler = new Opl_Profiler();
+$test_profiler = new Opl_Profiler('TestClass');
$class = new Test_Class();
-$class->setProfiler($profiler->getModule('Test'));
+$class->setProfiler($test_profiler);
$class->doSomething();
+$another_profiler = new Opl_Profiler('AnotherInstance');
$anotherInstance = new Test_Class();
-$anotherInstance->setProfiler($profiler->getModule('AnotherInstance'));
+$anotherInstance->setProfiler($another_profiler);
$anotherInstance->doAnything();
// Iterate through all modules.
-foreach($profiler as $module)
+foreach(array($test_profiler, $another_profiler) as $module)
{
// Show module name.
echo $module->getName().PHP_EOL;
@@ -32,9 +33,7 @@
}
}
}
-// Returns all modules in array
-$modules = $profiler->getModules();
// Returns all events in array
-$events = $profiler->getModule('Test')->getEvents();
+$events = $test_profiler->getEvents();
// Return all event data in array
-$eventData = $profiler->getModule('Test')->getEvent('doAnything')->getData();
+$eventData = $test_profiler->getEvent('doAnything')->getData();
View
@@ -25,64 +25,126 @@ class Opl_Profiler extends Opl_Profiler_Abstract implements Opl_Profiler_Interfa
* The name of the default class for new modules.
* @var string
*/
- $_moduleClassName = 'Opl_Profiler_Module';
+ $_eventClassName = 'Opl_Profiler_Event',
+ /**
+ * Profiler name.
+ * @var string
+ */
+ $_name;
+
+ /**
+ * Creates an object. Sets its name.
+ *
+ * @param string $name Name of module.
+ */
+ public function __construct($name)
+ {
+ $this->_name = (string)$name;
+ } // end __construct();
/**
- * Returns the registered module. If the module doesn't exist, it creates
- * a new one using the module class name.
- *
- * @param string $name Module name.
- * @return Opl_Profiler_Module_Interface
+ * Returns the name of this profiler.
+ *
+ * @return string
+ */
+ public function getName()
+ {
+ return $this->_name;
+ } // end getName();
+
+ /**
+ * Adds an event to this profiler.
+ *
+ * @param Opl_Profiler_Event_Interface $event Event object.
*/
- public function getModule($name)
+ public function addEvent(Opl_Profiler_Event_Interface $event)
{
- if(!isset($this->_modules[(string)$name]))
+ $this->_events[$event->getName()] = $event;
+ $this->_positions[] = $event->getName();
+ } // end addEvent();
+
+ /**
+ * Returns the event object.
+ *
+ * @param string $eventName Event name.
+ * @throws Opl_Profiler_Exception
+ */
+ public function getEvent($eventName)
+ {
+ $eventName = (string)$eventName;
+ if(!isset($this->_events[$eventName]))
{
- $this->_modules[(string)$name] = new $this->_moduleClassName((string)$name);
- $this->_positions[] = (string)$name;
+ $this->_events[$eventName] = new $this->_eventClassName($eventName);
+ if(!$this->_events[$eventName] instanceof Opl_Profiler_Event_Interface)
+ {
+ throw new Opl_Profiler_Exception('Event "'.$eventName.'" is not an instance of Opl_Profiler_Event_Interface!');
+ }
+ $this->_positions[] = $eventName;
}
- return $this->_modules[(string)$name];
- } // end getModule();
+ return $this->_events[$eventName];
+ } // end getEvent();
/**
- * Adds the module to the profiler.
- *
- * @param string $name Module name.
- * @param Opl_Profiler_Module_Interface $module Module.
+ * Creates an event with the specified name.
+ *
+ * @param string $eventName The event name.
+ * @throws Opl_Profiler_Exception
*/
- public function addModule(Opl_Profiler_Module_Interface $module)
+ public function createEvent($eventName)
{
- $this->_modules[$module->getName()] = $module;
- $this->_positions[] = $module->getName();
- } // end addModule();
+ $eventName = (string)$eventName;
+ $this->_events[$eventName] = new $this->_eventClassName($eventName);
+ if(!$this->_events[$eventName] instanceof Opl_Profiler_Event_Interface)
+ {
+ throw new Opl_Profiler_Exception('Event "'.$eventName.'" is not an instance of Opl_Profiler_Event_Interface!');
+ }
+ $this->_positions[] = $eventName;
+ } // end createEvent();
/**
- * Sets the default class name for new modules.
+ * Notifies the event about the action.
+ *
+ * @param string $eventName Event name.
+ * @param mixed $paramName The parameter name or array of the parameters and its values.
+ * @param mixed $paramValue Optional parameter value used when a single parameter is passed.
+ * @throws Opl_Profiler_Exception
+ */
+ public function notifyEvent($eventName, $paramName, $paramValue = null)
+ {
+ if(!isset($this->_events[$eventName]))
+ {
+ throw new Opl_Profiler_Exception('There\'s no event with name "'.$eventName.'"!');
+ }
+ $this->_events[$eventName]->notify($paramName, $paramValue);
+ } // end notify();
+
+ /**
+ * Sets the default class name for new events.
*
* @param string $classname Module class name.
*/
- public function setModuleClassName($classname)
+ public function setEventClassName($classname)
{
- $this->_moduleClassName = (string)$classname;
- } // end setModuleClassName();
+ $this->_eventClassName = (string)$classname;
+ } // end setEventClassName();
/**
- * Returns the default class name for new modules.
- *
+ * Returns the default class name for new events.
+ *
* @return string
*/
- public function getModuleClassName()
+ public function getEventClassName()
{
- return $this->_moduleClassName;
- } // end getModuleClassName();
+ return $this->_eventClassName;
+ } // end getEventClassName();
/**
- * Returns an array of registered modules.
+ * Returns an array of registered events.
*
* @return array
*/
- public function getModules()
+ public function getEvents()
{
- return $this->_modules;
- } // end getModules();
+ return $this->_events;
+ } // end getEvents();
} // end Opl_Profiler;
@@ -13,8 +13,8 @@
/**
* The base class for the Open Power Libs profilers. It provides the base
- * module storage, as well as iteration over the collection of profiler
- * modules.
+ * event storage, as well as iteration over the collection of profiler
+ * events.
*
* @author Amadeusz "megawebmaster" Starzykiewicz
* @copyright Invenzzia Group <http://www.invenzzia.org/> and contributors.
@@ -34,19 +34,19 @@
*/
$_positions = array(),
/**
- * Contains created modules.
+ * Contains events data.
* @var array
- */
- $_modules = array();
+ */
+ $_events = array();
/**
- * Returns the number of profiler modules.
+ * Returns the number of profiler events.
*
* @return integer
*/
public function count()
{
- return count($this->_modules);
+ return count($this->_events);
} // end count();
/**
@@ -58,17 +58,17 @@ public function rewind()
} // end rewind();
/**
- * Returns module in current position.
+ * Returns the current event.
*
- * @return Opl_Profiler_Module_Interface
+ * @return Opl_Profiler_Event_Interface
*/
public function current()
{
- return $this->_modules[$this->_positions[$this->_position]];
+ return $this->_events[$this->_positions[$this->_position]];
} // end current();
/**
- * Returns current position key name.
+ * Returns the index of the current event.
*
* @return string
*/
@@ -78,32 +78,32 @@ public function key()
} // end key();
/**
- * Moves current position to next element.
+ * Updates the internal iterator to the next events.
*/
public function next()
{
++$this->_position;
} // end next();
/**
- * Returns if current position is valid.
+ * Checks if the internal iterator is in a valid position.
*
* @return boolean
*/
public function valid()
{
if(isset($this->_positions[$this->_position]))
{
- return isset($this->_modules[$this->_positions[$this->_position]]);
+ return isset($this->_events[$this->_positions[$this->_position]]);
}
return false;
} // end valid();
/**
- * Moves current position to selected place.
+ * Moves the internal iterator to the specified position.
*
* @throws OutOfBoundsException
- * @param integer $position Needed position
+ * @param integer $position The new iterator position.
*/
public function seek($position)
{
@@ -113,4 +113,4 @@ public function seek($position)
throw new OutOfBoundsException('Invalid seek position ('.$position.')');
}
} // end seek();
-} // end Opl_Profiler;
+} // end Opl_Profiler_Abstract;
@@ -12,7 +12,7 @@
*/
/**
- * The standard profiler module for Open-Power-Libs.
+ * The standard event for the Opl_Profiler.
*
* @author Amadeusz "megawebmaster" Starzykiewicz
* @copyright Invenzzia Group <http://www.invenzzia.org/> and contributors.
@@ -68,6 +68,7 @@ public function getName()
*
* @param mixed $paramName Param name or array of params and its values.
* @param mixed $paramValue optional Param value.
+ * @throws Opl_Profiler_Event_Exception
*/
public function notify($paramName, $paramValue = null)
{
@@ -103,6 +104,12 @@ public function notify($paramName, $paramValue = null)
}
} // end notify();
+ /**
+ * Sets the value to the param.
+ *
+ * @param string $name Name of param.
+ * @param mixed $value Value.
+ */
public function set($name, $value)
{
$name = explode('.', $name);
@@ -129,6 +136,13 @@ public function set($name, $value)
}
} // end set();
+ /**
+ * Returns the param value.
+ * If param name is not found returns its parent.
+ *
+ * @param string $name Param name.
+ * @return mixed
+ */
public function get($name)
{
$name = explode('.', $name);
@@ -156,11 +170,23 @@ public function getData()
// Magical methods
+ /**
+ * Magical method for get().
+ *
+ * @param string $name Param name.
+ * @return mixed
+ */
public function __get($name)
{
return $this->get($name);
} // end __get();
+ /**
+ * Magical method for set().
+ *
+ * @param string $name Param name.
+ * @param mixed $value Value
+ */
public function __set($name, $value)
{
$this->set($name, $value);
Oops, something went wrong.

0 comments on commit 0a5b6aa

Please sign in to comment.