[TASK] Re-work/simplify copyright header in PHP files - Part 2
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Classes / Cache / Backend / ClassLoaderBackend.php
1 <?php
2 namespace TYPO3\CMS\Core\Cache\Backend;
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 use TYPO3\CMS\Core\Utility\PathUtility;
18
19 /**
20 * A caching backend customized explicitly for the class loader.
21 * This backend is NOT public API!
22 *
23 * @internal
24 */
25 class ClassLoaderBackend extends SimpleFileBackend {
26
27 /**
28 * This string will be used for writing the require statement in the
29 * cache file and for getting the required path via regex.
30 *
31 * @var string
32 */
33 protected $requireFileTemplate = '<?php require \'%s\';';
34
35 /**
36 * Set a class loader cache content
37 *
38 * @TODO: Rename method
39 * @param string $entryIdentifier
40 * @param string $filePath
41 * @throws \InvalidArgumentException
42 * @internal This is not an API method
43 */
44 public function setLinkToPhpFile($entryIdentifier, $filePath) {
45 if ($entryIdentifier === '') {
46 throw new \InvalidArgumentException('The specified entry identifier must not be empty.', 1364205170);
47 }
48 if (!PathUtility::isAbsolutePath($filePath)) {
49 throw new \InvalidArgumentException('Only absolute paths are allowed for the class loader, given path was: ' . $filePath, 1381923089);
50 }
51 if (!@file_exists($filePath)) {
52 throw new \InvalidArgumentException('The specified file path (' . $filePath . ') must exist.', 1364205235);
53 }
54 if (strtolower(substr($filePath, -4)) !== '.php') {
55 throw new \InvalidArgumentException('The specified file (' . $filePath . ') must be a php file.', 1364205377);
56 }
57 if ($entryIdentifier !== basename($entryIdentifier)) {
58 throw new \InvalidArgumentException('The specified entry identifier (' . $entryIdentifier . ') must not contain a path segment.', 1364205166);
59 }
60
61 $this->set($entryIdentifier, sprintf($this->requireFileTemplate, $filePath));
62 }
63
64 /**
65 * Used to set alias for class
66 *
67 * @TODO: Rename method
68 * @param string $entryIdentifier
69 * @param string $otherEntryIdentifier
70 * @internal
71 */
72 public function setLinkToOtherCacheEntry($entryIdentifier, $otherEntryIdentifier) {
73 $otherCacheEntryPathAndFilename = $this->cacheDirectory . $otherEntryIdentifier . $this->cacheEntryFileExtension;
74 $this->setLinkToPhpFile($entryIdentifier, $otherCacheEntryPathAndFilename);
75 }
76
77 /**
78 * Loads data from a cache file.
79 *
80 * @param string $entryIdentifier An identifier which describes the cache entry to load
81 * @return mixed The cache entry's content as a string or FALSE if the cache entry could not be loaded
82 * @throws \InvalidArgumentException If identifier is invalid
83 * @internal
84 */
85 public function get($entryIdentifier) {
86 if ($entryIdentifier !== basename($entryIdentifier)) {
87 throw new \InvalidArgumentException('The specified entry identifier must not contain a path segment.', 1334756880);
88 }
89 $pathAndFilename = $this->cacheDirectory . $entryIdentifier . $this->cacheEntryFileExtension;
90 if (!@file_exists($pathAndFilename)) {
91 return FALSE;
92 }
93 return file_get_contents($pathAndFilename);
94 }
95
96 /**
97 * Retrieves the path and filename that is passed to the require
98 * command in the cache entry with the given identifier.
99 *
100 * @param string $entryIdentifier
101 * @return boolean|string FALSE if required path can not be retrieved or the required file path on success
102 * @internal
103 */
104 public function getPathOfRequiredFileInCacheEntry($entryIdentifier) {
105 $result = FALSE;
106
107 $fileContent = $this->get($entryIdentifier);
108 if ($fileContent !== FALSE) {
109 $pattern = '!^' . sprintf(preg_quote($this->requireFileTemplate), '(.+)') . '!i';
110 $matches = array();
111 if (preg_match($pattern, $fileContent, $matches) === 1) {
112 $requireString = $matches[1];
113 $result = $requireString;
114 }
115 }
116 return $result;
117 }
118
119 /**
120 * Checks if a cache entry with the specified identifier exists.
121 *
122 * @param string $entryIdentifier
123 * @return boolean TRUE if such an entry exists, FALSE if not
124 * @throws \InvalidArgumentException
125 * @internal
126 */
127 public function has($entryIdentifier) {
128 if ($entryIdentifier !== basename($entryIdentifier)) {
129 throw new \InvalidArgumentException('The specified entry identifier must not contain a path segment.', 1334756879);
130 }
131 $pathAndFilename = $this->cacheDirectory . $entryIdentifier . $this->cacheEntryFileExtension;
132 return @file_exists($pathAndFilename);
133 }
134
135 /**
136 * Checks if the given cache entry files are still valid or if their
137 * lifetime has exceeded.
138 *
139 * @param string $cacheEntryPathAndFilename
140 * @return boolean
141 * @internal
142 */
143 protected function isCacheFileExpired($cacheEntryPathAndFilename) {
144 return @file_exists($cacheEntryPathAndFilename) === FALSE;
145 }
146
147 /**
148 * Tries to find the cache entry for the specified identifier.
149 *
150 * @TODO: This methods is implemented in simple, file and this backend, but never called?
151 * @param string $entryIdentifier The cache entry identifier
152 * @return mixed The file names (including path) as an array if one or more entries could be found, otherwise FALSE
153 */
154 protected function findCacheFilesByIdentifier($entryIdentifier) {
155 $pathAndFilename = $this->cacheDirectory . $entryIdentifier . $this->cacheEntryFileExtension;
156 return @file_exists($pathAndFilename) ? array($pathAndFilename) : FALSE;
157 }
158
159 /**
160 * Loads PHP code from the cache and require_onces it right away.
161 *
162 * @param string $entryIdentifier An identifier which describes the cache entry to load
163 * @return mixed Potential return value from the include operation
164 * @throws \InvalidArgumentException
165 * @internal
166 */
167 public function requireOnce($entryIdentifier) {
168 $pathAndFilename = $this->cacheDirectory . $entryIdentifier . $this->cacheEntryFileExtension;
169 return @file_exists($pathAndFilename) ? require_once $pathAndFilename : FALSE;
170 }
171
172 }