2 /***************************************************************
5 * (c) 2010 Oliver Klee <typo3-coding@oliverklee.de>
8 * This script is part of the TYPO3 project. The TYPO3 project is
9 * free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * The GNU General Public License can be found at
15 * http://www.gnu.org/copyleft/gpl.html.
17 * This script is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * This copyright notice MUST APPEAR in all copies of the script!
23 ***************************************************************/
26 * Class t3lib_formprotection_Factory.
28 * This class creates and manages instances of the various form protection
31 * This class provides only static methods. It can not be instantiated.
33 * Usage for the back-end form protection:
36 * $formProtection = t3lib_formprotection_Factory::get(
37 * 't3lib_formProtection_BackEnd'
41 * Usage for the install tool form protection:
44 * $formProtection = t3lib_formprotection_Factory::get(
45 * 'tx_install_formprotection'
47 * $formProtection->injectInstallTool($this);
55 * @author Oliver Klee <typo3-coding@oliverklee.de>
56 * @author Ernesto Baschny <ernst@cron-it.de>
58 final class t3lib_formprotection_Factory
{
60 * created instances of form protections using the type as array key
62 * @var array<t3lib_formProtectionAbstract>
64 static protected $instances = array();
67 * Private constructor to prevent instantiation.
69 private function __construct() {
73 * Gets a form protection instance for the requested class $className.
75 * If there already is an existing instance of the requested $className, the
76 * existing instance will be returned.
78 * @param string $className
79 * the name of the class for which to return an instance, must be
80 * "t3lib_formProtection_BackEnd" or "t3lib_formprotection_InstallToolFormProtection"
82 * @return t3lib_formprotection_Abstract the requested instance
84 static public function get($className) {
85 if (!isset(self
::$instances[$className])) {
86 if (!class_exists($className, TRUE)) {
87 throw new InvalidArgumentException(
88 '$className must be the name of an existing class, but ' .
89 'actually was "' . $className . '".',
94 $instance = t3lib_div
::makeInstance($className);
95 if (!$instance instanceof t3lib_formprotection_Abstract
) {
96 throw new InvalidArgumentException(
97 '$className must be a subclass of ' .
98 't3lib_formprotection_Abstract, but actually was "' .
103 self
::$instances[$className] = $instance;
105 return self
::$instances[$className];
109 * Sets the instance that will be returned by get() for a specific class
112 * Note: This function is intended for testing purposes only.
114 * @param string $className
115 * the name of the class for which to set an instance, must be
116 * "t3lib_formProtection_BackEnd" or "t3lib_formprotection_InstallToolFormProtection"
117 * @param t3lib_formprotection_Abstract $instance
118 * the instance to set
122 static public function set($className, t3lib_formprotection_Abstract
$instance) {
123 self
::$instances[$className] = $instance;
127 * Purges all existing instances.
129 * This function is particularly useful when cleaning up in unit testing.
133 static public function purgeInstances() {
134 foreach (self
::$instances as $key => $instance) {
135 $instance->__destruct();
136 unset(self
::$instances[$key]);
141 if (defined('TYPO3_MODE') && $TYPO3_CONF_VARS[TYPO3_MODE
]['XCLASS']['t3lib/formprotection/class.t3lib_formprotection_factory.php']) {
142 include_once($TYPO3_CONF_VARS[TYPO3_MODE
]['XCLASS']['t3lib/formprotection/class.t3lib_formprotection_factory.php']);