[TASK] Re-work/simplify copyright header in PHP files - Part 2
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Classes / Cache / CacheFactory.php
1 <?php
2 namespace TYPO3\CMS\Core\Cache;
3
4 /**
5 * This file is part of the TYPO3 CMS project.
6 *
7 * It is free software; you can redistribute it and/or modify it under
8 * the terms of the GNU General Public License, either version 2
9 * of the License, or any later version.
10 *
11 * For the full copyright and license information, please read the
12 * LICENSE.txt file that was distributed with this source code.
13 *
14 * The TYPO3 project - inspiring people to share!
15 */
16 /**
17 * This cache factory takes care of instantiating a cache frontend and injecting
18 * a certain cache backend. After creation of the new cache, the cache object
19 * is registered at the cache manager.
20 *
21 * This file is a backport from FLOW3
22 *
23 * @author Robert Lemke <robert@typo3.org>
24 * @scope singleton
25 * @api
26 */
27 class CacheFactory implements \TYPO3\CMS\Core\SingletonInterface {
28
29 /**
30 * The current FLOW3 context ("production", "development" etc.)
31 *
32 * TYPO3 v4 note: This variable is always set to "production"
33 * in TYPO3 v4 and only kept in v4 to keep v4 and FLOW3 in sync.
34 *
35 * @var string
36 */
37 protected $context;
38
39 /**
40 * A reference to the cache manager
41 *
42 * @var \TYPO3\CMS\Core\Cache\CacheManager
43 */
44 protected $cacheManager;
45
46 /**
47 * Constructs this cache factory
48 *
49 * @param string $context The current FLOW3 context
50 * @param \TYPO3\CMS\Core\Cache\CacheManager $cacheManager The cache manager
51 */
52 public function __construct($context, \TYPO3\CMS\Core\Cache\CacheManager $cacheManager) {
53 $this->context = $context;
54 $this->cacheManager = $cacheManager;
55 $this->cacheManager->injectCacheFactory($this);
56 }
57
58 /**
59 * Factory method which creates the specified cache along with the specified kind of backend.
60 * After creating the cache, it will be registered at the cache manager.
61 *
62 * @param string $cacheIdentifier The name / identifier of the cache to create
63 * @param string $cacheObjectName Object name of the cache frontend
64 * @param string $backendObjectName Object name of the cache backend
65 * @param array $backendOptions (optional) Array of backend options
66 * @return \TYPO3\CMS\Core\Cache\Frontend\FrontendInterface The created cache frontend
67 * @throws \TYPO3\CMS\Core\Cache\Exception\InvalidBackendException if the cache backend is not valid
68 * @throws \TYPO3\CMS\Core\Cache\Exception\InvalidCacheException if the cache frontend is not valid
69 * @api
70 */
71 public function create($cacheIdentifier, $cacheObjectName, $backendObjectName, array $backendOptions = array()) {
72 // New operator used on purpose: This class is required early during
73 // bootstrap before makeInstance() is propely set up
74 $backendObjectName = '\\' . ltrim($backendObjectName, '\\');
75 $backend = new $backendObjectName($this->context, $backendOptions);
76 if (!$backend instanceof \TYPO3\CMS\Core\Cache\Backend\BackendInterface) {
77 throw new \TYPO3\CMS\Core\Cache\Exception\InvalidBackendException('"' . $backendObjectName . '" is not a valid cache backend object.', 1216304301);
78 }
79 if (is_callable(array($backend, 'initializeObject'))) {
80 $backend->initializeObject();
81 }
82 // New used on purpose, see comment above
83 $cache = new $cacheObjectName($cacheIdentifier, $backend);
84 if (!$cache instanceof \TYPO3\CMS\Core\Cache\Frontend\FrontendInterface) {
85 throw new \TYPO3\CMS\Core\Cache\Exception\InvalidCacheException('"' . $cacheObjectName . '" is not a valid cache frontend object.', 1216304300);
86 }
87 if (is_callable(array($cache, 'initializeObject'))) {
88 $cache->initializeObject();
89 }
90 $this->cacheManager->registerCache($cache);
91 return $cache;
92 }
93
94 }