Updated the caching framework to the according FLOW3 revision 2616
[Packages/TYPO3.CMS.git] / t3lib / cache / class.t3lib_cache_factory.php
1 <?php
2 /***************************************************************
3 * Copyright notice
4 *
5 * (c) 2008-2009 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 * Injects the cache manager.
48 *
49 * This is called by the cache manager itself
50 *
51 * @param t3lib_cache_Manager The cache manager
52 * @return void
53 * @author Robert Lemke <robert@typo3.org>
54 * @internal
55 */
56 public function setCacheManager(t3lib_cache_Manager $cacheManager) {
57 $this->cacheManager = $cacheManager;
58 }
59
60 /**
61 * Factory method which creates the specified cache along with the specified kind of backend.
62 * After creating the cache, it will be registered at the cache manager.
63 *
64 * @param string The name / identifier of the cache to create
65 * @param string Name of the cache frontend
66 * @param string Name of the cache backend
67 * @param array (optional) Array of backend options
68 * @return t3lib_cache_frontend_Frontend The created cache frontend
69 * @author Robert Lemke <robert@typo3.org>
70 */
71 public function create($cacheIdentifier, $cacheName, $backendName, array $backendOptions = array()) {
72
73 // loading the cache backend file and class
74 list($backendFile, $backendClassReference) = explode(
75 ':',
76 $GLOBALS['TYPO3_CONF_VARS']['SYS']['caching']['cacheBackends'][$backendName]
77 );
78
79 $backendRequireFile = t3lib_div::getFileAbsFileName($backendFile);
80 if ($backendRequireFile) {
81 t3lib_div::requireOnce($backendRequireFile);
82 }
83
84 $backend = t3lib_div::makeInstance($backendClassReference, $backendOptions);
85
86 if (!$backend instanceof t3lib_cache_backend_Backend) {
87 throw new t3lib_cache_exception_InvalidCache(
88 '"' .$backendName . '" is not a valid cache backend.',
89 1216304301
90 );
91 }
92
93 // loading the cache frontend file and class
94 list($cacheFile, $cacheClassReference) = explode(
95 ':',
96 $GLOBALS['TYPO3_CONF_VARS']['SYS']['caching']['cacheFrontends'][$cacheName]
97 );
98
99 $cacheRequireFile = t3lib_div::getFileAbsFileName($cacheFile);
100 if ($cacheRequireFile) {
101 t3lib_div::requireOnce($cacheRequireFile);
102 }
103
104 $cache = t3lib_div::makeInstance($cacheClassReference, $cacheIdentifier, $backend);
105
106
107 if (!$cache instanceof t3lib_cache_frontend_Frontend) {
108 throw new t3lib_cache_exception_InvalidCache(
109 '"' .$cacheName . '" is not a valid cache.',
110 1216304300
111 );
112 }
113
114 $this->cacheManager->registerCache($cache);
115
116 return $cache;
117 }
118
119 }
120
121
122 if (defined('TYPO3_MODE') && $TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['t3lib/cache/class.t3lib_cache_factory.php']) {
123 include_once($TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['t3lib/cache/class.t3lib_cache_factory.php']);
124 }
125
126 ?>