Schnellzugriff » 
Home 
» Kategorien

» Seiten

» Suche
« Module ‘json’ already loaded in Unknown on line 0 OS X kernel_task CPU-Usage »

TYPO3: Frontend Renderer als Singleton

Renderer sind stets einmalig. Das Singleton Design Pattern bilded genau dies ab: Es wird lediglich eine einzige Instanz der Klasse erzeugt und zurückgegeben. Die folgende Klasse kann als Basis für die View-Schicht der eigenen TYPO3 Extension verwendet werden. Sie ist von pi_base abgeleitet und bietet so den gewohnten Funktionsumfang. Der Codeschnipsel stammt aus einer mini TYPO3 Extension, an der ich gerade privat bastel.

<?php
/***************************************************************
*  Copyright notice
*
*  (c) 2009 Thorsten Boock <thorsten (at) nerdcenter.de>
*  All rights reserved
*
*  This script is part of the TYPO3 project. The TYPO3 project is
*  free software; you can redistribute it and/or modify
*  it under the terms of the GNU General Public License as published by
*  the Free Software Foundation; either version 2 of the License, or
*  (at your option) any later version.
*
*  The GNU General Public License can be found at
*  http://www.gnu.org/copyleft/gpl.html.
*
*  This script is distributed in the hope that it will be useful,
*  but WITHOUT ANY WARRANTY; without even the implied warranty of
*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
*  GNU General Public License for more details.
*
*  This copyright notice MUST APPEAR in all copies of the script!
***************************************************************/

require_once(PATH_tslib . 'class.tslib_pibase.php');

/**
 * Frontend Renderer
 * Till PHP 5.3 this class has to be declared as final because
 * we don't know the names of the classes extending this one.
 *
 * @author Thorsten Boock <thorsten (at) nerdcenter.de>
 */
final class tx_tbwhoisonline_renderer_fe extends tslib_pibase {
	/**
	 * Instance of this class (Singleton Design Pattern)
	 *
	 * @var tx_whoisonline_renderer_fe
	 */
	private static		$instance					=	null;
	/**
	 * Path to the template file used for rendering
	 *
	 * @var string
	 */
	private				$templateFile				=	'';
	/**
	 * The contents of the file $this->templateFile
	 *
	 * @var string
	 */
	private				$templateHTML				=	'';

	/**
	 * Empty constructor (Singleton Design Pattern)
	 *
	 */
	private function __construct() {

	}

	/**
	 * Empty clone function (Singleton Design Pattern)
	 *
	 */
	private function __clone() {

	}

	/**
	 * Instantiating this object (Singleton Design Pattern)
	 *
	 * @return tx_tbwhoisonline_renderer_fe
	 */
	public static function getInstance() {
		if(self::$instance == null) {
			self::$instance = new tx_tbwhoisonline_renderer_fe();
		}
		return self::$instance;
	}

	/**
	 * Initializing this object
	 *
	 * @param array $conf
	 */
	public function init($conf) {
		// init tslib_pibase
		$this->conf = $conf;
		$this->pi_setPiVarDefaults();
		$this->pi_loadLL();
		// get cObj instance and load template
 		$this->cObj = t3lib_div::makeInstance('tslib_cObj');
 		$this->templateHTML = $this->cObj->fileResource($this->templateFile);
		// override defaults with typoscript configuration
		foreach($this->conf as $key => $value) {
			$key = str_replace('.', '', $key);
			if(isset($this->$key)) {
				$this->$key = $value;
			}
		}
	}
}
?>

Mittwoch, Februar 11th, 2009 and is filed under PHP, Programmierung, TYPO3. You can follow any responses to this entry through the RSS 2.0 feed. Both comments and pings are currently closed.

Comments are closed.


© 2007 - 2009 Thorsten Boock