Fixed bug #10454: t3lib_div::makeInstanceClassName() allows to bypass checks for...
[Packages/TYPO3.CMS.git] / t3lib / cache / class.t3lib_cache_factory.php
1 <?php
2 /***************************************************************
3 * Copyright notice
4 *
5 * (c) 2008 Ingo Renner <ingo@typo3.org>
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 /**
27 * This cache factory takes care of instantiating a cache frontend and injecting
28 * a certain cache backend. After creation of the new cache, the cache object
29 * is registered at the cache manager.
30 *
31 * This file is a backport from FLOW3
32 *
33 * @package TYPO3
34 * @subpackage t3lib_cache
35 * @version $Id$
36 */
37 class t3lib_cache_Factory implements t3lib_Singleton {
38
39 /**
40 * A reference to the cache manager
41 *
42 * @var t3lib_cache_Manager
43 */
44 protected $cacheManager;
45
46 /**
47 * Constructs this cache factory
48 *
49 * @param t3lib_cache_Manager A reference to the cache manager
50 * @author Robert Lemke <robert@typo3.org>
51 * @author Ingo Renner <ingo@typo3.org>
52 */
53 public function __construct(t3lib_cache_Manager $cacheManager) {
54 $this->cacheManager = $cacheManager;
55 }
56
57 /**
58 * Factory method which creates the specified cache along with the specified kind of backend.
59 * After creating the cache, it will be registered at the cache manager.
60 *
61 * @param string The name / identifier of the cache to create
62 * @param string Name of the cache frontend
63 * @param string Name of the cache backend
64 * @param array (optional) Array of backend options
65 * @return t3lib_cache_AbstractCache The created cache frontend
66 * @author Robert Lemke <robert@typo3.org>
67 */
68 public function create($cacheIdentifier, $cacheName, $backendName, array $backendOptions = array()) {
69
70 // loading the cache backend file and class
71 list($backendFile, $backendClassReference) = explode(
72 ':',
73 $GLOBALS['TYPO3_CONF_VARS']['SYS']['caching']['cacheBackends'][$backendName]
74 );
75
76 $backendRequireFile = t3lib_div::getFileAbsFileName($backendFile);
77 if ($backendRequireFile) {
78 t3lib_div::requireOnce($backendRequireFile);
79 }
80
81 $backendClassName = t3lib_div::makeInstanceClassName($backendClassReference);
82 $backend = new $backendClassName($backendOptions);
83
84 if (!$backend instanceof t3lib_cache_AbstractBackend) {
85 throw new t3lib_cache_exception_InvalidCache(
86 '"' .$backendName . '" is not a valid cache backend.',
87 1216304301
88 );
89 }
90
91 // loading the cache frontend file and class
92 list($cacheFile, $cacheClassReference) = explode(
93 ':',
94 $GLOBALS['TYPO3_CONF_VARS']['SYS']['caching']['cacheFrontends'][$cacheName]
95 );
96
97 $cacheRequireFile = t3lib_div::getFileAbsFileName($cacheFile);
98 if ($cacheRequireFile) {
99 t3lib_div::requireOnce($cacheRequireFile);
100 }
101
102 $cacheClassName = t3lib_div::makeInstanceClassName($cacheClassReference);
103 $cache = new $cacheClassName($cacheIdentifier, $backend);
104
105
106 if (!$cache instanceof t3lib_cache_AbstractCache) {
107 throw new t3lib_cache_exception_InvalidCache(
108 '"' .$cacheName . '" is not a valid cache.',
109 1216304300
110 );
111 }
112
113 $this->cacheManager->registerCache($cache);
114
115 return $cache;
116 }
117
118 }
119
120
121 if (defined('TYPO3_MODE') && $TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['t3lib/cache/class.t3lib_cache_factory.php']) {
122 include_once($TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['t3lib/cache/class.t3lib_cache_factory.php']);
123 }
124
125 ?>