/protected/components/ezcomponents/Template/src/source_code.php

https://github.com/kamarulismail/kamarul-playground · PHP · 301 lines · 111 code · 16 blank · 174 comment · 13 complexity · 88996898cea5c7feab231ad0876a379b MD5 · raw file

  1. <?php
  2. /**
  3. * File containing the ezcTemplateSourceCode class
  4. *
  5. * @package Template
  6. * @version 1.4.2
  7. * @copyright Copyright (C) 2005-2009 eZ Systems AS. All rights reserved.
  8. * @license http://ez.no/licenses/new_bsd New BSD License
  9. * @access private
  10. */
  11. /**
  12. * Encapsulates information on a template file containing source code.
  13. *
  14. * This class is used by the manager when it parses template code and can be
  15. * used by code to read and write template code or remove template files in
  16. * a convenient way.
  17. *
  18. * Instantiate this class with the stream string and the optional arguments,
  19. * then call either load(), save() or delete() to perform the requested action.
  20. * The class will throw exceptions for failures so it is a good idea to call
  21. * isReadable(), isWriteable() or isAvailable() before any of these actions.
  22. *
  23. * For instance to display the content of a template file one can do:
  24. * <code>
  25. * $source = new ezcTemplateSourceCode( "templates/main.tpl" );
  26. * if ( $source->isReadable() )
  27. * {
  28. * $source->load();
  29. * echo $source->code;
  30. * }
  31. * else
  32. * {
  33. * echo "Cannot load template ", $source->stream, "\n";
  34. * }
  35. * </code>
  36. *
  37. * Similarly one can create template files with:
  38. * <code>
  39. * $source = new ezcTemplateSourceCode( "templates/left.tpl" );
  40. * $source->code = '{$left|str_capitalize}';
  41. * if ( !$source->isAvailable() )
  42. * {
  43. * $source->save();
  44. * }
  45. * else if ( $source->isWriteable() )
  46. * {
  47. * $source->save();
  48. * }
  49. * else
  50. * {
  51. * echo "Cannot save template ", $source->stream, "\n";
  52. * }
  53. * </code>
  54. *
  55. * To check if any source code has been loaded or set use hasCode().
  56. *
  57. * @property string $stream
  58. * The PHP stream path for the template source file.
  59. * @property string $resource
  60. * The resource string which requested this template.
  61. * @property string $code
  62. * The original template code taken from the template file or
  63. * other resource. Contains a string with the source code or
  64. * false if no code is read yet.
  65. * @property ezcTemplateOutputContext $context
  66. * The current context for the template code. Will be used for
  67. * parsing and run-time behaviour.
  68. *
  69. * @package Template
  70. * @version 1.4.2
  71. * @access private
  72. */
  73. class ezcTemplateSourceCode
  74. {
  75. /**
  76. * Array that stores the property values.
  77. *
  78. * @var array(string=>mixed)
  79. */
  80. private $properties = array();
  81. /**
  82. * Property get
  83. *
  84. * @param string $name
  85. * @return mixed
  86. */
  87. public function __get( $name )
  88. {
  89. switch ( $name )
  90. {
  91. case 'stream':
  92. case 'resource':
  93. case 'code':
  94. case 'context':
  95. return $this->properties[$name];
  96. default:
  97. throw new ezcBasePropertyNotFoundException( $name );
  98. }
  99. }
  100. /**
  101. * Property set
  102. *
  103. * @param string $name
  104. * @param mixed $value
  105. * @return void
  106. */
  107. public function __set( $name, $value )
  108. {
  109. switch ( $name )
  110. {
  111. case 'stream':
  112. case 'resource':
  113. case 'code':
  114. case 'context':
  115. $this->properties[$name] = $value;
  116. break;
  117. default:
  118. throw new ezcBasePropertyNotFoundException( $name );
  119. }
  120. }
  121. /**
  122. * Property isset
  123. *
  124. * @param string $name
  125. * @return bool
  126. */
  127. public function __isset( $name )
  128. {
  129. switch ( $name )
  130. {
  131. case 'stream':
  132. case 'resource':
  133. case 'code':
  134. case 'context':
  135. return true;
  136. default:
  137. return false;
  138. }
  139. }
  140. /**
  141. * Initialises the source object with the code and output context.
  142. *
  143. * @param string $stream The actual PHP stream path for the template source
  144. * file.
  145. * @param string $resource The requested resource string, if false $stream
  146. * is used as value.
  147. * @param string $code The source code for the template.
  148. * @param ezcTemplateOutputContext $context The context for the parsing and
  149. * run-time behaviour, a value of null
  150. * means to use the current context in
  151. * the template manager.
  152. */
  153. public function __construct( $stream, $resource = false, $code = false, ezcTemplateOutputContext $context = null )
  154. {
  155. $this->stream = $stream;
  156. $this->resource = $resource;
  157. $this->code = $code;
  158. $this->context = $context;
  159. }
  160. /**
  161. * Loads the data from the PHP stream into the $code member variable.
  162. *
  163. * @throws ezcTemplateFileNotFoundException if the file does not exist on disk.
  164. * @throws ezcTemplateFileNotReadableException if the file cannot be read.
  165. *
  166. * @see isAvailable(), isReadable()
  167. *
  168. * Note: Calling this multiple times will re-init the $source variable.
  169. *
  170. * @return void
  171. */
  172. public function load()
  173. {
  174. if ( !file_exists( $this->stream ) )
  175. throw new ezcTemplateFileNotFoundException( $this->stream );
  176. if ( !is_readable( $this->stream ) )
  177. throw new ezcTemplateFileNotReadableException( $this->stream );
  178. $this->code = file_get_contents( $this->stream );
  179. }
  180. /**
  181. * Saves the data from $code member variable to the PHP stream.
  182. *
  183. * This method creates a backup from the exisiting template. The name of the
  184. * backup template appends a tilde (~). If a backup already exists,
  185. * this method overwrites the old backup.
  186. *
  187. * @throws ezcTemplateFileNotWritableException if the file cannot be written to.
  188. *
  189. * @see isWriteable()
  190. * Note: Storing the data will not record the template file in the system or
  191. * signal the changes, call the template manager to perform these tasks.
  192. *
  193. * Note: Calling this multiple times will overwrite the file contents over and
  194. * over again. And the backup contains the same information as the original.
  195. *
  196. * @return void
  197. */
  198. public function save()
  199. {
  200. if ( file_exists( $this->stream ) && !is_writeable( $this->stream ) )
  201. throw new ezcTemplateFileNotWriteableException( $this->stream );
  202. // Store data in a temporary file
  203. $tempName = dirname( $this->stream ) . '/#' . basename( $this->stream ) . '#';
  204. if ( file_put_contents( $tempName, $this->code, LOCK_EX ) === false )
  205. throw new ezcTemplateFileNotWriteableException( $this->stream );
  206. $backupName = $this->stream . '~';
  207. // Remove old backup (if it exists)
  208. if ( file_exists( $backupName ) &&
  209. !unlink( $backupName ) )
  210. {
  211. unlink( $tempName );
  212. throw new ezcTemplateFileFailedUnlinkException( $backupName );
  213. }
  214. // Make the current file (if it exists) a backup
  215. if ( file_exists( $this->stream ) &&
  216. !rename( $this->stream, $backupName ) )
  217. {
  218. unlink( $tempName );
  219. throw new ezcTemplateFileRenameFailedException( $this->stream, $backupName );
  220. }
  221. // Make the temporary file the current one
  222. if ( !rename( $tempName, $this->stream ) )
  223. {
  224. unlink( $tempName );
  225. rename( $backupName, $this->stream );
  226. throw new ezcTemplateFileRenamedFailedException( $tempName, $this->stream );
  227. }
  228. }
  229. /**
  230. * Deletes the file from the file system.
  231. *
  232. * @throws ezcTemplateFileNotFoundException if the file does not exist on disk.
  233. * @throws ezcTemplateFileFailedUnlinkException if the file could not be unlinked.
  234. *
  235. *
  236. * @see isAvailable()
  237. *
  238. * @return void
  239. */
  240. public function delete()
  241. {
  242. if ( !file_exists( $this->stream ) )
  243. throw new ezcTemplateFileNotFoundException( $this->stream );
  244. if ( !unlink( $this->stream ) )
  245. throw new ezcTemplateFileFailedUnlinkException( $this->stream );
  246. }
  247. /**
  248. * Checks if the template file exists on disk and can be read.
  249. *
  250. * @return bool
  251. */
  252. public function isAvailable()
  253. {
  254. return file_exists( $this->stream );
  255. }
  256. /**
  257. * Checks if the template file can be read from.
  258. *
  259. * @return bool
  260. */
  261. public function isReadable()
  262. {
  263. return is_readable( $this->stream );
  264. }
  265. /**
  266. * Checks if the template file can be written to.
  267. *
  268. * @return bool
  269. */
  270. public function isWriteable()
  271. {
  272. return is_writeable( $this->stream );
  273. }
  274. /**
  275. * Checks if source code has been loaded from the template file or set by PHP
  276. * code.
  277. *
  278. * @return bool
  279. */
  280. public function hasCode()
  281. {
  282. return $this->code !== false;
  283. }
  284. }
  285. ?>