Fixed bug #14050: CleanUp - CGL format of t3lib files - t3lib_formprotection_directory
[Packages/TYPO3.CMS.git] / t3lib / formprotection / class.t3lib_formprotection_factory.php
1 <?php
2 /***************************************************************
3 * Copyright notice
4 *
5 * (c) 2010 Oliver Klee <typo3-coding@oliverklee.de>
6 * All rights reserved
7 *
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.
13 *
14 * The GNU General Public License can be found at
15 * http://www.gnu.org/copyleft/gpl.html.
16 *
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.
21 *
22 * This copyright notice MUST APPEAR in all copies of the script!
23 ***************************************************************/
24
25 /**
26 * Class t3lib_formprotection_Factory.
27 *
28 * This class creates and manages instances of the various form protection
29 * classes.
30 *
31 * This class provides only static methods. It can not be instantiated.
32 *
33 * Usage for the back-end form protection:
34 *
35 * <pre>
36 * $formProtection = t3lib_formprotection_Factory::get(
37 * 't3lib_formProtection_BackEnd'
38 * );
39 * </pre>
40 *
41 * Usage for the install tool form protection:
42 *
43 * <pre>
44 * $formProtection = t3lib_formprotection_Factory::get(
45 * 'tx_install_formprotection'
46 * );
47 * $formProtection->injectInstallTool($this);
48 * </pre>
49 *
50 * $Id$
51 *
52 * @package TYPO3
53 * @subpackage t3lib
54 *
55 * @author Oliver Klee <typo3-coding@oliverklee.de>
56 * @author Ernesto Baschny <ernst@cron-it.de>
57 */
58 final class t3lib_formprotection_Factory {
59 /**
60 * created instances of form protections using the type as array key
61 *
62 * @var array<t3lib_formProtectionAbstract>
63 */
64 static protected $instances = array();
65
66 /**
67 * Private constructor to prevent instantiation.
68 */
69 private function __construct() {
70 }
71
72 /**
73 * Gets a form protection instance for the requested class $className.
74 *
75 * If there already is an existing instance of the requested $className, the
76 * existing instance will be returned.
77 *
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"
81 *
82 * @return t3lib_formprotection_Abstract the requested instance
83 */
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 . '".',
90 1285352962
91 );
92 }
93
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 "' .
99 $className . '".',
100 1285353026
101 );
102 }
103 self::$instances[$className] = $instance;
104 }
105 return self::$instances[$className];
106 }
107
108 /**
109 * Sets the instance that will be returned by get() for a specific class
110 * name.
111 *
112 * Note: This function is intended for testing purposes only.
113 *
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
119 *
120 * @return void
121 */
122 static public function set($className, t3lib_formprotection_Abstract $instance) {
123 self::$instances[$className] = $instance;
124 }
125
126 /**
127 * Purges all existing instances.
128 *
129 * This function is particularly useful when cleaning up in unit testing.
130 *
131 * @return void
132 */
133 static public function purgeInstances() {
134 foreach (self::$instances as $key => $instance) {
135 $instance->__destruct();
136 unset(self::$instances[$key]);
137 }
138 }
139 }
140
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']);
143 }
144 ?>