## Apache Zeta Components Manual :: File Source for plain.php

## Source for file plain.php

Documentation is available at plain.php

1. <?php
2. /**
3. * File containing the ezcCacheStoragePlain class.
4. *
5. * Licensed to the Apache Software Foundation (ASF) under one
6. * or more contributor license agreements. See the NOTICE file
7. * distributed with this work for additional information
8. * regarding copyright ownership. The ASF licenses this file
9. * to you under the Apache License, Version 2.0 (the
10. * "License"); you may not use this file except in compliance
11. * with the License. You may obtain a copy of the License at
12. *
13. * http://www.apache.org/licenses/LICENSE-2.0
14. *
15. * Unless required by applicable law or agreed to in writing,
16. * software distributed under the License is distributed on an
17. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
18. * KIND, either express or implied. See the License for the
19. * specific language governing permissions and limitations
20. * under the License.
21. *
22. * @package Cache
23. * @version //autogentag//
24. * @license http://www.apache.org/licenses/LICENSE-2.0 Apache License, Version 2.0
25. * @filesource
26. */
28. /**
29. * This class implements a simple storage to cache plain text
30. * on the filesystem. It takes its base methods from the extended
31. * storage base class {@link ezcCacheStorageFile}.
32. *
33. * In contrast to other {@link ezcCacheStorageFile} implementations, the stored
34. * cache data is restored using PHP's file_get_contents() class. This cache
35. * is not capable to store array values. If numeric values are stored the
36. * restored values will be of type string. The same applies to values of the
37. * simple type bool. It is highly recommended that you cast the resulting
38. * value to its correct type, also PHP will automatically perform this cast
39. * when necessary. An explicit cast ensures that type consistent comparisons
40. * (using the === or !== operators) will not fail on restored cache data.
41. *
42. * An even better solution, if you want to store non-string values, are the
43. * usage of {@link ezcCacheStorageFileArray} and
44. * {@link ezcCacheStorageFileEvalArray} storage classes, since those keep the
45. * variable types of you cached data consistent.
46. *
47. * For example code of using a cache storage, see {@link ezcCacheManager}.
48. *
49. * The Cache package contains several other implementations of
50. * {@link ezcCacheStorageFile}. As there are:
51. *
52. * - ezcCacheStorageFileArray
53. * - ezcCacheStorageFilePlain
54. *
55. * @package Cache
56. * @version //autogentag//
57. */
58. class ezcCacheStorageFilePlain extends ezcCacheStorageFile
59. {
60. /**
61. * Fetch data from the cache.
62. * This method does the fetching of the data itself. In this case, the
63. * method simply includes the file and returns the value returned by the
64. * include (or false on failure).
65. *
66. * @param string $filename The file to fetch data from.
67. * @return string The fetched data or false on failure.
68. */
69. protected function fetchData ( $filename )
70. {
71. return file_get_contents ( $filename );
72. }
74. /**
75. * Serialize the data for storing.
76. * Serializes a PHP variable (except type resource and object) to a
77. * executable PHP code representation string.
78. *
79. * @param mixed $data Simple type or array
80. * @return string The serialized data
81. *
82. * @throws ezcCacheInvalidDataException
83. * If the data submitted is an array,object or a resource, since
84. * this implementation of {@link ezcCacheStorageFile} can only deal
85. * with scalar values.
86. */
87. protected function prepareData ( $data )
88. {
89. if ( is_scalar ( $data ) === false )
90. {
91. throw new ezcCacheInvalidDataException ( gettype ( $data ), array( 'simple' ) );
92. }
93. return ( string ) $data;
94. }
96. /**
97. * Restores and returns the meta data struct.
98. *
99. * This method fetches the meta data stored in the storage and returns the
100. * according struct of type {@link ezcCacheStackMetaData}. The meta data
101. * must be stored inside the storage, but should not be visible as normal
102. * cache items to the user.
103. *
104. * @return ezcCacheStackMetaData
105. */
106. public function restoreMetaData ()
107. {
108. // Silence require warnings. It's ok that meta data does not exist.
109. $dataStr = @$this-> fetchData (
110. $this-> properties ['location'] . $this-> properties ['options']->metaDataFile
111. );
112. $dataArr = unserialize ( $dataStr );
115. $result = null;
116. if ( $dataArr !== false )
117. {
118. $result = new $dataArr['class']();
119. $result->setState( $dataArr['data'] );
120. }
121. return $result;
122. }
124. /**
125. * Stores the given meta data struct.
126. *
127. * This method stores the given $metaData inside the storage. The data must
128. * be stored with the same mechanism that the storage itself uses. However,
129. * it should not be stored as a normal cache item, if possible, to avoid
130. * accedental user manipulation.
131. *
132. * @param ezcCacheStackMetaData $metaData
133. * @return void
134. */
135. public function storeMetaData ( ezcCacheStackMetaData $metaData )
136. {
137. $dataArr = array(
138. 'class' => get_class ( $metaData ),
139. 'data' => $metaData->getState(),
140. );
141. // This storage only handles scalar values, so we serialize here.
142. $dataStr = serialize ( $dataArr );
143. $this-> storeRawData (
144. $this-> properties ['location'] . $this-> properties ['options']->metaDataFile,
145. $this-> prepareData ( $dataStr )
146. );
147. }
148. }
149. ?>

Documentation generated by phpDocumentor 1.4.3