Added feature #14666: Caching framework: Backport PhpFrontend from FLOW3 and refactor...
[Packages/TYPO3.CMS.git] / t3lib / cache / frontend / class.t3lib_cache_frontend_phpfrontend.php
1 <?php
2 /***************************************************************
3 * Copyright notice
4 *
5 * (c) 2010 Christian Kuhn <lolli@schwarzbu.ch>
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 * A cache frontend tailored to PHP code.
28 *
29 * This file is a backport from FLOW3
30 *
31 * @package TYPO3
32 * @subpackage t3lib_cache
33 * @api
34 * @scope prototype
35 * @version $Id$
36 */
37 class t3lib_cache_frontend_PhpFrontend extends t3lib_cache_frontend_StringFrontend {
38 /**
39 * Constructs the cache
40 *
41 * @param string $identifier A identifier which describes this cache
42 * @param t3lib_cache_backend_PhpCapableBackend $backend Backend to be used for this cache
43 * @author Robert Lemke <robert@typo3.org>
44 */
45 public function __construct($identifier, t3lib_cache_backend_PhpCapableBackend $backend) {
46 parent::__construct($identifier, $backend);
47 }
48
49 /**
50 * Saves the PHP source code in the cache.
51 *
52 * @param string $entryIdentifier An identifier used for this cache entry, for example the class name
53 * @param string $sourceCode PHP source code
54 * @param array $tags Tags to associate with this cache entry
55 * @param integer $lifetime Lifetime of this cache entry in seconds. If NULL is specified, the default lifetime is used. "0" means unlimited liftime.
56 * @return void
57 * @author Robert Lemke <robert@typo3.org>
58 * @api
59 */
60 public function set($entryIdentifier, $sourceCode, $tags = array(), $lifetime = NULL) {
61 if (!$this->isValidEntryIdentifier($entryIdentifier)) {
62 throw new InvalidArgumentException(
63 '"' . $entryIdentifier . '" is not a valid cache entry identifier.',
64 1264023823
65 );
66 }
67 if (!is_string($sourceCode)) {
68 throw new t3lib_cache_exception_InvalidData(
69 'The given source code is not a valid string.',
70 1264023824
71 );
72 }
73 foreach ($tags as $tag) {
74 if (!$this->isValidTag($tag)) {
75 throw new InvalidArgumentException(
76 '"' . $tag . '" is not a valid tag for a cache entry.',
77 1264023825
78 );
79 }
80 }
81 $sourceCode = '<?php' . chr(10) . $sourceCode . chr(10) . '__halt_compiler();';
82 $this->backend->set($entryIdentifier, $sourceCode, $tags, $lifetime);
83 }
84
85 /**
86 * Loads PHP code from the cache and require_onces it right away.
87 *
88 * @param string $entryIdentifier An identifier which describes the cache entry to load
89 * @return mixed Potential return value from the include operation
90 * @author Robert Lemke <robert@typo3.org>
91 * @api
92 */
93 public function requireOnce($entryIdentifier) {
94 return $this->backend->requireOnce($entryIdentifier);
95 }
96 }
97
98 if (defined('TYPO3_MODE') && $GLOBALS['TYPO3_CONF_VARS'][TYPO3_MODE]['XCLASS']['t3lib/cache/class.t3lib_cache_frontend_phpfrontend.php']) {
99 include_once($GLOBALS['TYPO3_CONF_VARS'][TYPO3_MODE]['XCLASS']['t3lib/cache/class.t3lib_cache_frontend_phpfrontend.php']);
100 }
101
102 ?>