/**
  * registers listeners and binds them to events
  *
  * @param Delegator $d
  * @param array $listeners
  */
 public static function registerEventListeners(Delegator $d, array $listeners = null)
 {
     if (!$listeners) {
         $listeners = array();
         $ls = Application::getInstance()->listeners;
         if ($ls->listener) {
             foreach ($ls->listener as $l) {
                 $listeners[(string) $l->attributes()->listenFor] = (string) $l->attributes()->listener;
             }
         }
     }
     foreach ($listeners as $event => $listener) {
         if ($listener instanceof Closure) {
             Event::bind($event, $listener);
         } else {
             $split = explode("::", $event);
             $model = $split[0];
             $split2 = explode("::", $listener);
             $lclass = $split2[0];
             $lmethod = $split2[1];
             $x = $d->{$model} instanceof Aspect ? $d->{$model}->getObject() : $d->{$model};
             $l = new $lclass($x);
             Event::bind($event, $lmethod, $l);
         }
     }
 }
 /**
  * the master method.  delegate the request
  * to the right class method with parameters.
  * returns the result of the method call
  *
  * @param Request $request
  * @return mixed
  */
 public function delegate(Request $request)
 {
     $args = $request->request->all();
     $uriParts = explode('?', $request->getRequestUri());
     $path = $uriParts[0];
     $path = trim($path, '/');
     $path ? $pathParts = explode('/', urldecode($path)) : ($pathParts = array());
     // access routing overrides from application.xml
     $pathParts = $this->implementRoutingOverrides($pathParts);
     $method = array_pop($pathParts);
     $thing = implode('', $pathParts);
     // convert classname
     $classname_parts = explode('-', $thing);
     foreach ($classname_parts as &$part) {
         $part = ucfirst($part);
     }
     $classname = implode('', $classname_parts);
     // convert method name
     $x = explode("-", $method);
     for ($i = 1; $i < count($x); $i++) {
         $x[$i] = ucfirst($x[$i]);
     }
     $method = implode('', $x);
     Event::run($classname . '::onBefore' . ucfirst($method));
     $result = call_user_func_array(array($this->{$classname}, $method), $args);
     Event::run($classname . '::onAfter' . ucfirst($method));
     return $result;
 }