1: <?php
  2: 
  3:   4:   5:   6:   7:   8:   9:  10:  11:  12:  13:  14:  15:  16:  17:  18:  19:  20:  21: 
 22: 
 23: namespace picon;
 24: 
 25:  26:  27:  28:  29:  30: 
 31: class ListenerRequestTarget implements RequestTarget, Identifiable
 32: {
 33:     private $componentPath;
 34:     private $page;
 35:     private $behaviour;
 36:     
 37:      38:  39:  40:  41: 
 42:     public function __construct($page, $componentPath, $behaviour = null)
 43:     {
 44:         $this->page = $page;
 45:         $this->componentPath = $componentPath;
 46:         $this->behaviour = $behaviour;
 47:     }
 48:     
 49:     public function respond(Response $response)
 50:     {
 51:         if($this->page instanceof Identifier)
 52:         {
 53:             $fullClassName = $this->page->getFullyQualifiedName();
 54:             $page = new $fullClassName();
 55:             $page->internalInitialize();
 56:         }
 57:         else
 58:         {
 59:             $page = $this->page;
 60: 
 61:         }
 62:         
 63:         if($page==null)
 64:         {
 65:             $GLOBALS['requestCycle']->addTarget(new PageNotFoundRequestTarget());
 66:             return;
 67:         }
 68:         
 69:         $listener = $this->getListener($page);
 70:         
 71:         if($listener==null)
 72:         {
 73:             $page->beforePageRender();
 74:             $listener = $this->getListener($page);
 75:         } 
 76:         if($listener==null || !($listener instanceof Listener))
 77:         {
 78:             throw new \RuntimeException(sprintf("Listener component %s was not found", $this->componentPath));
 79:         }
 80:         
 81:         if($GLOBALS['requestCycle']->getRequest()->isAjax()==false)
 82:         {
 83:             $url = $GLOBALS['requestCycle']->generateUrl(new PageInstanceRequestTarget($page));
 84:             PageMap::get()->addOrUpdate($page);
 85:             $GLOBALS['requestCycle']->addTarget(new RedirectRequestTarget($url));
 86:         }
 87:         $listener->onEvent();
 88:     }
 89:     
 90:     private function getListener($page)
 91:     {
 92:         $listener = null;
 93:         if($this->behaviour==null)
 94:         {
 95:             $listener = $page->get($this->componentPath);
 96:         }
 97:         else
 98:         {
 99:             $component = $page->get($this->componentPath);
100:             if($component!=null && $component instanceof Component)
101:             {
102:                 $listener = $component->getBehaviourById($this->behaviour);
103:             }
104:         }
105:         return $listener;
106:     }
107:     
108:     public function getComponentPath()
109:     {
110:         return $this->componentPath;
111:     }
112: 
113:     public function getPage()
114:     {
115:         return $this->page;
116:     }
117:     
118:     public function getBehaviour()
119:     {
120:         return $this->behaviour;
121:     }
122:     
123:     public static function getIdentifier()
124:     {
125:         return Identifier::forName(get_called_class());
126:     }
127: }
128: 
129: ?>
130: