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() {}
72 * Gets a form protection instance for the requested class $className.
74 * If there already is an existing instance of the requested $className, the
75 * existing instance will be returned.
77 * @param string $className
78 * the name of the class for which to return an instance, must be
79 * "t3lib_formProtection_BackEnd" or "t3lib_formprotection_InstallToolFormProtection"
81 * @return t3lib_formprotection_Abstract the requested instance
83 static public function get($className) {
84 if (!isset(self
::$instances[$className])) {
85 if (!class_exists($className, TRUE)) {
86 throw new InvalidArgumentException(
87 '$className must be the name of an existing class, but ' .
88 'actually was "' . $className . '".',
93 $instance = t3lib_div
::makeInstance($className);
94 if (!$instance instanceof t3lib_formprotection_Abstract
) {
95 throw new InvalidArgumentException(
96 '$className must be a subclass of ' .
97 't3lib_formprotection_Abstract, but actually was "' .
102 self
::$instances[$className] = $instance;
104 return self
::$instances[$className];
108 * Sets the instance that will be returned by get() for a specific class
111 * Note: This function is intended for testing purposes only.
113 * @param string $className
114 * the name of the class for which to set an instance, must be
115 * "t3lib_formProtection_BackEnd" or "t3lib_formprotection_InstallToolFormProtection"
116 * @param t3lib_formprotection_Abstract $instance
117 * the instance to set
121 static public function set($className, t3lib_formprotection_Abstract
$instance) {
122 self
::$instances[$className] = $instance;
126 * Purges all existing instances.
128 * This function is particularly useful when cleaning up in unit testing.
132 static public function purgeInstances() {
133 foreach (self
::$instances as $key => $instance) {
134 $instance->__destruct();
135 unset(self
::$instances[$key]);
140 if (defined('TYPO3_MODE') && $TYPO3_CONF_VARS[TYPO3_MODE
]['XCLASS']['t3lib/formprotection/class.t3lib_formprotection_factory.php']) {
141 include_once($TYPO3_CONF_VARS[TYPO3_MODE
]['XCLASS']['t3lib/formprotection/class.t3lib_formprotection_factory.php']);