| 
									
										
										
										
											2010-02-26 14:49:12 +00:00
										 |  |  | <?php | 
					
						
							|  |  |  | /** | 
					
						
							|  |  |  |  * @copyright NetMonsters <team@netmonsters.ru> | 
					
						
							|  |  |  |  * @link http://netmonsters.ru | 
					
						
							|  |  |  |  * @package Majestic | 
					
						
							|  |  |  |  * @subpackage View | 
					
						
							|  |  |  |  * @since 2010-02-25 | 
					
						
							|  |  |  |  * @version SVN: $Id$ | 
					
						
							|  |  |  |  * @filesource $URL$ | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-03-13 23:33:46 +00:00
										 |  |  | /** | 
					
						
							|  |  |  |  * @method ViewHelperGet get() | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2010-02-26 14:49:12 +00:00
										 |  |  | class PHPView implements iView | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |      | 
					
						
							|  |  |  |     protected $path      = ''; | 
					
						
							|  |  |  |     protected $variables = array(); | 
					
						
							| 
									
										
										
										
											2010-03-13 23:33:46 +00:00
										 |  |  |     protected $helpers   = array(); | 
					
						
							| 
									
										
										
										
											2010-02-26 14:49:12 +00:00
										 |  |  |     protected $extension = '.phtml'; | 
					
						
							|  |  |  |     protected $template  = ''; | 
					
						
							|  |  |  |      | 
					
						
							| 
									
										
										
										
											2010-03-07 17:26:44 +00:00
										 |  |  |     public function __construct($config) | 
					
						
							| 
									
										
										
										
											2010-02-26 14:49:12 +00:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2010-03-07 17:26:44 +00:00
										 |  |  |         if (!isset($config['path'])) { | 
					
						
							| 
									
										
										
										
											2011-11-24 16:10:06 +04:00
										 |  |  |             throw new InitializationException('Configuration must have a "path" set.'); | 
					
						
							| 
									
										
										
										
											2010-03-07 17:26:44 +00:00
										 |  |  |         } | 
					
						
							|  |  |  |         $this->setPath($config['path']); | 
					
						
							| 
									
										
										
										
											2010-02-26 14:49:12 +00:00
										 |  |  |     } | 
					
						
							|  |  |  |      | 
					
						
							|  |  |  |     public function getPath() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         return $this->path; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |      | 
					
						
							|  |  |  |     public function setPath($path) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $this->path = $path; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |      | 
					
						
							|  |  |  |     /** | 
					
						
							| 
									
										
										
										
											2010-03-14 12:12:17 +00:00
										 |  |  |      * @param Action $object | 
					
						
							| 
									
										
										
										
											2010-02-26 14:49:12 +00:00
										 |  |  |      */ | 
					
						
							|  |  |  |     public function assignObject($object) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         foreach (get_object_vars($object) as $name => $value) { | 
					
						
							|  |  |  |             $this->assign($name, $value); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |      | 
					
						
							| 
									
										
										
										
											2010-03-26 08:49:33 +00:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * @param string $name | 
					
						
							|  |  |  |      * @param mixed $value | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2010-02-26 14:49:12 +00:00
										 |  |  |     public function assign($name, $value = null) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $this->variables[$name] = $value; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |      | 
					
						
							| 
									
										
										
										
											2010-03-26 08:49:33 +00:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * @param string $name | 
					
						
							|  |  |  |      * @param mixed $value | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function prepend($name, $value) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         if (isset($this->variables[$name])) { | 
					
						
							|  |  |  |             $this->variables[$name] = $value . $this->variables[$name]; | 
					
						
							|  |  |  |         } else { | 
					
						
							|  |  |  |             $this->variables[$name] = $value; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |      | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * @param string $name | 
					
						
							|  |  |  |      * @param mixed $value | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function append($name, $value) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         if (isset($this->variables[$name])) { | 
					
						
							|  |  |  |             $this->variables[$name] .= $value; | 
					
						
							|  |  |  |         } else { | 
					
						
							|  |  |  |             $this->variables[$name] = $value; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |      | 
					
						
							| 
									
										
										
										
											2010-02-26 14:49:12 +00:00
										 |  |  |     public function fetch($template) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $this->template = $this->getTemplatePath($template); | 
					
						
							|  |  |  |         unset($template); | 
					
						
							|  |  |  |         extract($this->variables); | 
					
						
							|  |  |  |         ob_start(); | 
					
						
							| 
									
										
										
										
											2010-03-07 17:26:44 +00:00
										 |  |  |         if (!is_readable($this->template)) { | 
					
						
							| 
									
										
										
										
											2010-02-26 14:49:12 +00:00
										 |  |  |             ob_clean(); | 
					
						
							| 
									
										
										
										
											2011-11-24 17:57:40 +04:00
										 |  |  |             throw new GeneralException('Template "' . $this->template .'" not found.'); | 
					
						
							| 
									
										
										
										
											2010-02-26 14:49:12 +00:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2010-03-07 17:26:44 +00:00
										 |  |  |         include($this->template); | 
					
						
							| 
									
										
										
										
											2010-02-26 14:49:12 +00:00
										 |  |  |         return ob_get_clean(); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |      | 
					
						
							|  |  |  |     public function escape($var) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         return htmlentities($var, ENT_QUOTES, 'UTF-8'); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |      | 
					
						
							| 
									
										
										
										
											2010-03-13 23:33:46 +00:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * Helpers call | 
					
						
							|  |  |  |      *  | 
					
						
							|  |  |  |      * @param mixed $name | 
					
						
							|  |  |  |      * @param mixed $args | 
					
						
							|  |  |  |      * @return ViewHelper | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function __call($name, $args) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $helper = $this->getHelper($name); | 
					
						
							|  |  |  |         return call_user_func_array(array($helper, $name), $args); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |      | 
					
						
							|  |  |  |     protected function getHelper($name) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         if (!isset($this->helpers[$name])) { | 
					
						
							| 
									
										
										
										
											2010-04-26 19:56:01 +00:00
										 |  |  |             $class = ucfirst($name) . 'ViewHelper'; | 
					
						
							| 
									
										
										
										
											2010-03-13 23:33:46 +00:00
										 |  |  |             if (!class_exists($class)) { | 
					
						
							|  |  |  |                 throw new GeneralException('View helper "' . $class . '" not found.'); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |             $this->helpers[$name] = new $class($this); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         return $this->helpers[$name]; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |      | 
					
						
							| 
									
										
										
										
											2010-02-26 14:49:12 +00:00
										 |  |  |     protected function getTemplatePath($template) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         return $this->path . $template . $this->extension; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } |