mirror of
				https://github.com/Mibew/mibew.git
				synced 2025-10-31 10:31:07 +03:00 
			
		
		
		
	Reduce code duplication in Handlebars powered styles
This commit is contained in:
		
							parent
							
								
									7021cb983e
								
							
						
					
					
						commit
						eba2eadaca
					
				| @ -0,0 +1,42 @@ | ||||
| <?php | ||||
| /* | ||||
|  * This file is a part of Mibew Messenger. | ||||
|  * | ||||
|  * Copyright 2005-2014 the original author or authors. | ||||
|  * | ||||
|  * Licensed under the Apache License, Version 2.0 (the "License"); | ||||
|  * you may not use this file except in compliance with the License. | ||||
|  * You may obtain a copy of the License at | ||||
|  * | ||||
|  *     http://www.apache.org/licenses/LICENSE-2.0 | ||||
|  * | ||||
|  * Unless required by applicable law or agreed to in writing, software | ||||
|  * distributed under the License is distributed on an "AS IS" BASIS, | ||||
|  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||||
|  * See the License for the specific language governing permissions and | ||||
|  * limitations under the License. | ||||
|  */ | ||||
| 
 | ||||
| namespace Mibew\Handlebars; | ||||
| 
 | ||||
| use Handlebars\Handlebars as HandlebarsEngine; | ||||
| 
 | ||||
| /** | ||||
|  * An interface for all classes that deal with Handlebars template engine. | ||||
|  */ | ||||
| interface HandlebarsAwareInterface | ||||
| { | ||||
|     /** | ||||
|      * Gets an instance of Handlebars template engine. | ||||
|      * | ||||
|      * @return HandlebarsEngine | ||||
|      */ | ||||
|     public function getHandlebars(); | ||||
| 
 | ||||
|     /** | ||||
|      * Sets an instace of Handlebars template engine. | ||||
|      * | ||||
|      * @param HandlebarsEngine $engine An instance of Handlebars engine. | ||||
|      */ | ||||
|     public function setHandlebars(HandlebarsEngine $engine); | ||||
| } | ||||
| @ -0,0 +1,69 @@ | ||||
| <?php | ||||
| /* | ||||
|  * This file is a part of Mibew Messenger. | ||||
|  * | ||||
|  * Copyright 2005-2014 the original author or authors. | ||||
|  * | ||||
|  * Licensed under the Apache License, Version 2.0 (the "License"); | ||||
|  * you may not use this file except in compliance with the License. | ||||
|  * You may obtain a copy of the License at | ||||
|  * | ||||
|  *     http://www.apache.org/licenses/LICENSE-2.0 | ||||
|  * | ||||
|  * Unless required by applicable law or agreed to in writing, software | ||||
|  * distributed under the License is distributed on an "AS IS" BASIS, | ||||
|  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||||
|  * See the License for the specific language governing permissions and | ||||
|  * limitations under the License. | ||||
|  */ | ||||
| 
 | ||||
| namespace Mibew\Style; | ||||
| 
 | ||||
| use Handlebars\Handlebars as HandlebarsEngine; | ||||
| use Mibew\Handlebars\HelpersSet; | ||||
| use Mibew\Handlebars\HandlebarsAwareInterface; | ||||
| 
 | ||||
| /** | ||||
|  * A base class for all styles that use Handlebars templates engine. | ||||
|  */ | ||||
| abstract class AbstractHandlebarsPoweredStyle extends AbstractStyle implements HandlebarsAwareInterface | ||||
| { | ||||
|     /** | ||||
|      * An instance of Handlebars template engine. | ||||
|      * | ||||
|      * @var HandlebarsEngine | ||||
|      */ | ||||
|     protected $templateEngine = null; | ||||
| 
 | ||||
|     /** | ||||
|      * {@inheritdoc} | ||||
|      */ | ||||
|     public function getHandlebars() | ||||
|     { | ||||
|         if (is_null($this->templateEngine)) { | ||||
|             $templates_loader = new \Handlebars\Loader\FilesystemLoader( | ||||
|                 MIBEW_FS_ROOT . '/' . $this->getFilesPath() . '/templates_src/server_side/' | ||||
|             ); | ||||
| 
 | ||||
|             $this->templateEngine = new \Handlebars\Handlebars(array( | ||||
|                 'loader' => $templates_loader, | ||||
|                 'partials_loader' => $templates_loader, | ||||
|                 'helpers' => new \Handlebars\Helpers(HelpersSet::getHelpers()) | ||||
|             )); | ||||
| 
 | ||||
|             // Use custom function to escape strings
 | ||||
|             $this->templateEngine->setEscape('safe_htmlspecialchars'); | ||||
|             $this->templateEngine->setEscapeArgs(array()); | ||||
|         } | ||||
| 
 | ||||
|         return $this->templateEngine; | ||||
|     } | ||||
| 
 | ||||
|     /** | ||||
|      * {@inheritdoc} | ||||
|      */ | ||||
|     public function setHandlebars(HandlebarsEngine $engine) | ||||
|     { | ||||
|         $this->templateEngine = $engine; | ||||
|     } | ||||
| } | ||||
| @ -21,44 +21,12 @@ namespace Mibew\Style; | ||||
| 
 | ||||
| // Import namespaces and classes of the core
 | ||||
| use Mibew\Settings; | ||||
| use Mibew\Handlebars\HelpersSet; | ||||
| 
 | ||||
| /** | ||||
|  * Represents a chat style | ||||
|  */ | ||||
| class ChatStyle extends AbstractStyle implements StyleInterface | ||||
| class ChatStyle extends AbstractHandlebarsPoweredStyle implements StyleInterface | ||||
| { | ||||
|     /** | ||||
|      * Template engine for chat templates. | ||||
|      * | ||||
|      * @var \Handlebars\Handlebars | ||||
|      */ | ||||
|     protected $templateEngine; | ||||
| 
 | ||||
|     /** | ||||
|      * Object constructor | ||||
|      * | ||||
|      * @param string $style_name Name of the style | ||||
|      */ | ||||
|     public function __construct($style_name) | ||||
|     { | ||||
|         parent::__construct($style_name); | ||||
| 
 | ||||
|         $templates_loader = new \Handlebars\Loader\FilesystemLoader( | ||||
|             MIBEW_FS_ROOT . '/' . $this->getFilesPath() . '/templates_src/server_side/' | ||||
|         ); | ||||
| 
 | ||||
|         $this->templateEngine = new \Handlebars\Handlebars(array( | ||||
|             'loader' => $templates_loader, | ||||
|             'partials_loader' => $templates_loader, | ||||
|             'helpers' => new \Handlebars\Helpers(HelpersSet::getHelpers()) | ||||
|         )); | ||||
| 
 | ||||
|         // Use custom function to escape strings
 | ||||
|         $this->templateEngine->setEscape('safe_htmlspecialchars'); | ||||
|         $this->templateEngine->setEscapeArgs(array()); | ||||
|     } | ||||
| 
 | ||||
|     /** | ||||
|      * Builds base path for style files. This path is relative Mibew root and | ||||
|      * does not contain neither leading nor trailing slash. | ||||
| @ -92,7 +60,7 @@ class ChatStyle extends AbstractStyle implements StyleInterface | ||||
|         $data['stylePath'] = MIBEW_WEB_ROOT . '/' . $this->getFilesPath(); | ||||
|         $data['styleName'] = $this->getName(); | ||||
| 
 | ||||
|         return $this->templateEngine->render($template_name, $data); | ||||
|         return $this->getHandlebars()->render($template_name, $data); | ||||
|     } | ||||
| 
 | ||||
|     /** | ||||
|  | ||||
| @ -21,44 +21,12 @@ namespace Mibew\Style; | ||||
| 
 | ||||
| // Import namespaces and classes of the core
 | ||||
| use Mibew\Settings; | ||||
| use Mibew\Handlebars\HelpersSet; | ||||
| 
 | ||||
| /** | ||||
|  * Represents a style for operator pages | ||||
|  */ | ||||
| class PageStyle extends AbstractStyle implements StyleInterface | ||||
| class PageStyle extends AbstractHandlebarsPoweredStyle implements StyleInterface | ||||
| { | ||||
|     /** | ||||
|      * Template engine for chat templates. | ||||
|      * | ||||
|      * @var \Handlebars\Handlebars | ||||
|      */ | ||||
|     protected $templateEngine; | ||||
| 
 | ||||
|     /** | ||||
|      * Object constructor | ||||
|      * | ||||
|      * @param string $style_name Name of the style | ||||
|      */ | ||||
|     public function __construct($style_name) | ||||
|     { | ||||
|         parent::__construct($style_name); | ||||
| 
 | ||||
|         $templates_loader = new \Handlebars\Loader\FilesystemLoader( | ||||
|             MIBEW_FS_ROOT . '/' . $this->getFilesPath() . '/templates_src/server_side/' | ||||
|         ); | ||||
| 
 | ||||
|         $this->templateEngine = new \Handlebars\Handlebars(array( | ||||
|             'loader' => $templates_loader, | ||||
|             'partials_loader' => $templates_loader, | ||||
|             'helpers' => new \Handlebars\Helpers(HelpersSet::getHelpers()) | ||||
|         )); | ||||
| 
 | ||||
|         // Use custom function to escape strings
 | ||||
|         $this->templateEngine->setEscape('safe_htmlspecialchars'); | ||||
|         $this->templateEngine->setEscapeArgs(array()); | ||||
|     } | ||||
| 
 | ||||
|     /** | ||||
|      * Builds base path for style files. This path is relative Mibew root and | ||||
|      * does not contain neither leading nor trailing slash. | ||||
| @ -92,7 +60,7 @@ class PageStyle extends AbstractStyle implements StyleInterface | ||||
|         $data['stylePath'] = MIBEW_WEB_ROOT . '/' . $this->getFilesPath(); | ||||
|         $data['styleName'] = $this->getName(); | ||||
| 
 | ||||
|         return $this->templateEngine->render($template_name, $data); | ||||
|         return $this->getHandlebars()->render($template_name, $data); | ||||
|     } | ||||
| 
 | ||||
|     /** | ||||
|  | ||||
		Loading…
	
		Reference in New Issue
	
	Block a user