[TASK] Use class imports in Cache Frontend classes
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Classes / Cache / Frontend / PhpFrontend.php
1 <?php
2 namespace TYPO3\CMS\Core\Cache\Frontend;
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 use TYPO3\CMS\Core\Cache\Backend\PhpCapableBackendInterface;
17 use TYPO3\CMS\Core\Cache\Exception\InvalidDataException;
18
19 /**
20 * A cache frontend tailored to PHP code.
21 *
22 * This file is a backport from FLOW3
23 *
24 * @author Robert Lemke <robert@typo3.org>
25 * @api
26 */
27 class PhpFrontend extends StringFrontend {
28
29 /**
30 * Constructs the cache
31 *
32 * @param string $identifier A identifier which describes this cache
33 * @param PhpCapableBackendInterface $backend Backend to be used for this cache
34 */
35 public function __construct($identifier, PhpCapableBackendInterface $backend) {
36 parent::__construct($identifier, $backend);
37 }
38
39 /**
40 * Saves the PHP source code in the cache.
41 *
42 * @param string $entryIdentifier An identifier used for this cache entry, for example the class name
43 * @param string $sourceCode PHP source code
44 * @param array $tags Tags to associate with this cache entry
45 * @param int $lifetime Lifetime of this cache entry in seconds. If NULL is specified, the default lifetime is used. "0" means unlimited liftime.
46 * @return void
47 * @throws \InvalidArgumentException If $entryIdentifier or $tags is invalid
48 * @throws InvalidDataException If $sourceCode is not a string
49 * @api
50 */
51 public function set($entryIdentifier, $sourceCode, array $tags = array(), $lifetime = NULL) {
52 if (!$this->isValidEntryIdentifier($entryIdentifier)) {
53 throw new \InvalidArgumentException('"' . $entryIdentifier . '" is not a valid cache entry identifier.', 1264023823);
54 }
55 if (!is_string($sourceCode)) {
56 throw new InvalidDataException('The given source code is not a valid string.', 1264023824);
57 }
58 foreach ($tags as $tag) {
59 if (!$this->isValidTag($tag)) {
60 throw new \InvalidArgumentException('"' . $tag . '" is not a valid tag for a cache entry.', 1264023825);
61 }
62 }
63 $sourceCode = '<?php' . chr(10) . $sourceCode . chr(10) . '#';
64 $this->backend->set($entryIdentifier, $sourceCode, $tags, $lifetime);
65 }
66
67 /**
68 * Loads PHP code from the cache and require_onces it right away.
69 *
70 * @param string $entryIdentifier An identifier which describes the cache entry to load
71 * @return mixed Potential return value from the include operation
72 * @api
73 */
74 public function requireOnce($entryIdentifier) {
75 return $this->backend->requireOnce($entryIdentifier);
76 }
77
78 }