Source for file Type.php

Documentation is available at Type.php

  1. <?php
  2.  
  3. /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
  4.  
  5. /**
  6. * ScriptReorganizer :: Type
  7. *
  8. * PHP version 5
  9. *
  10. * LICENSE: This library is free software; you can redistribute it and/or modify it
  11. * under the terms of the GNU Lesser General Public License as published by the Free
  12. * Software Foundation; either version 2.1 of the License, or (at your option) any
  13. * later version.
  14. *
  15. * @category Tools
  16. * @package ScriptReorganizer
  17. * @author Stefano F. Rausch <stefano@rausch-e.net>
  18. * @copyright 2005 Stefano F. Rausch <stefano@rausch-e.net>
  19. * @license http://www.gnu.org/copyleft/lesser.html LGPL License 2.1
  20. * @version SVN: $Id: Type.php 32 2005-10-30 22:05:19Z stefanorausch $
  21. * @link http://pear.php.net/package/ScriptReorganizer
  22. * @filesource
  23. */
  24.  
  25. /**
  26. * Depends on <kbd>ScriptReorganizer_Strategy</kbd>
  27. */
  28. require_once 'ScriptReorganizer/Strategy.php';
  29.  
  30. /**
  31. * Throws <kbd>ScriptReorganizer_Type_Exception</kbd>
  32. */
  33. require_once 'ScriptReorganizer/Type/Exception.php';
  34.  
  35. /**
  36. * Base class to be extended by (reorganizer) types to use
  37. *
  38. * All types must follow the naming convention
  39. * <kbd>ScriptReorganizer_Type_<Type></kbd>.
  40. *
  41. * @category Tools
  42. * @package ScriptReorganizer
  43. * @author Stefano F. Rausch <stefano@rausch-e.net>
  44. * @copyright 2005 Stefano F. Rausch <stefano@rausch-e.net>
  45. * @license http://www.gnu.org/copyleft/lesser.html LGPL License 2.1
  46. * @version Release: 0.3.0
  47. * @link http://pear.php.net/package/ScriptReorganizer
  48. */
  49. abstract class ScriptReorganizer_Type
  50. {
  51. // {{{ public function __construct( ScriptReorganizer_Strategy $strategy )
  52. /**
  53. * Constructor
  54. *
  55. * @param ScriptReorganizer_Strategy $strategy a
  56. * <kbd>ScriptReorganizer_Strategy</kbd> to apply
  57. */
  58. public function __construct( ScriptReorganizer_Strategy $strategy )
  59. {
  60. $this->strategy = $strategy;
  61. $this->endOfLineIdentifiers = array(
  62. 'win' => "\r\n", 'unix' => "\n", 'mac' => "\r"
  63. );
  64. }
  65. // }}}
  66. // {{{ public function __destruct()
  67. /**
  68. * Destructor
  69. */
  70. public function __destruct()
  71. {
  72. unset( $this->strategy );
  73. }
  74. // }}}
  75. // {{{ public function load( $file )
  76. /**
  77. * Loads the script's content to be reorganized from disk
  78. *
  79. * @param string $file a string representing the file's name to load
  80. * @return void
  81. * @throws {@link ScriptReorganizer_Type_Exception ScriptReorganizer_Type_Exception}
  82. */
  83. public function load( $file )
  84. {
  85. $content = @file_get_contents( $file );
  86. if ( false === $content ) {
  87. throw new ScriptReorganizer_Type_Exception(
  88. 'File ' . $file . ' is not readable'
  89. );
  90. }
  91. $eol = $this->getEolIdentifier( $content );
  92. $this->initializeIdentifiers( $eol );
  93. if ( $eol != $this->endOfLine ) {
  94. $content = str_replace( $eol, $this->endOfLine, $content );
  95. }
  96. if ( preg_match( $this->hashBangIdentifier, $content, $match ) ) {
  97. $content = str_replace( $match[0], '', $content );
  98. if ( !$this->hashBang ) {
  99. $this->hashBang = $match[1];
  100. }
  101. }
  102. $result = trim( $content );
  103. $result = preg_replace( '"^<\?php"', '', $result );
  104. $result = preg_replace( '"\?>$"', '', $result );
  105. $this->_setContent( $result );
  106. }
  107. // }}}
  108. // {{{ public function reformat()
  109. /**
  110. * Reorganizes the script's content by applying the chosen
  111. * {@link ScriptReorganizer_Strategy Strategy}
  112. *
  113. * @return void
  114. */
  115. public function reformat()
  116. {
  117. $content = $this->_getContent();
  118. $this->maskHeredocs( $content );
  119. $content = trim( $this->strategy->reformat( $content, $this->endOfLine ) );
  120. $this->unmaskHeredocs( $content );
  121. $this->_setContent( $content );
  122. }
  123. // }}}
  124. // {{{ public function save( $file )
  125. /**
  126. * Saves the reorganized script's content to disk
  127. *
  128. * @param string $file a string representing the file's name to save
  129. * @return void
  130. * @throws {@link ScriptReorganizer_Type_Exception ScriptReorganizer_Type_Exception}
  131. */
  132. public function save( $file )
  133. {
  134. $content = $this->hashBang;
  135. $content .= '<?php' . $this->endOfLine . $this->endOfLine . $this->_getContent()
  136. . $this->endOfLine . $this->endOfLine . '?>';
  137. if ( false === @file_put_contents( $file, $content ) ) {
  138. throw new ScriptReorganizer_Type_Exception(
  139. 'File ' . $file . ' is not writable'
  140. );
  141. }
  142. $this->endOfLine = '';
  143. }
  144. // }}}
  145. // {{{ protected function getEolIdentifier( & $content )
  146. /**
  147. * Detects the currently used end-of-line identifier
  148. *
  149. * @param string &$content a string representing the script's content
  150. * @return string a string representing the end-of-line identifier found in the
  151. * script's content
  152. * @since Method available sind Release 0.3.0
  153. */
  154. protected function getEolIdentifier( & $content )
  155. {
  156. foreach ( $this->endOfLineIdentifiers as $eol ) {
  157. if ( false !== strpos( $content, $eol ) ) {
  158. return $eol;
  159. }
  160. }
  161. }
  162. // }}}
  163. // {{{ package function _getContent()
  164. /**
  165. * Gets the script's content currently being reorganized
  166. *
  167. * @visibility package restricted
  168. * @return string a string representing the script's content
  169. */
  170. public function _getContent()
  171. {
  172. return $this->content;
  173. }
  174. // }}}
  175. // {{{ package function _setContent( $content )
  176. /**
  177. * Sets the script's content currently being reorganized
  178. *
  179. * @visibility package restricted
  180. * @param string $content a string representing the content's replacement
  181. * @return void
  182. */
  183. public function _setContent( $content )
  184. {
  185. $this->content = $content;
  186. }
  187. // }}}
  188. // {{{ private function initializeIdentifiers( $eol )
  189. /**
  190. * Sets the values of internal identifiers for future use
  191. *
  192. * @param string $eol a string representing an end-of-line identifier
  193. * @return void
  194. * @since Method available sind Release 0.3.0
  195. */
  196. private function initializeIdentifiers( $eol )
  197. {
  198. if ( !$this->endOfLine ) {
  199. $this->endOfLine = $eol;
  200. $this->hashBangIdentifier = '"^[ \t' . $eol . ']*(\#\![^' . $eol . ']+'
  201. . $eol . ')"';
  202. $heredocs = '"([<]{3}[ \t]*(\w+)[' . $eol . ']';
  203. $heredocs .= '(.|[' . $eol . '])+?\2;?)[' . $eol . ']"';
  204. $this->heredocsIdentifier = $heredocs;
  205. }
  206. }
  207. // }}}
  208. // {{{ private function maskHeredocs( & $content )
  209. /**
  210. * Hides Heredoc strings before the reorganization process
  211. *
  212. * @param string &$content a string representing the script's content
  213. * @return void
  214. * @see unmaskHeredocs(), reformat()
  215. * @since Method available since Release 0.2.1
  216. */
  217. private function maskHeredocs( & $content )
  218. {
  219. if ( preg_match_all( $this->heredocsIdentifier, $content, $this->heredocs ) ) {
  220. $i = 0;
  221. foreach ( $this->heredocs[1] as $heredoc ) {
  222. $content = str_replace(
  223. $heredoc, '< Heredoc ' . $i++ . ' >', $content
  224. );
  225. preg_match( '"^[<]{3}[ \t]*(\w+)"', $heredoc, $identifier );
  226. $heredocIndent = '"[' . $this->endOfLine . ']([ \t]+)' . $identifier[1] . ';?$"';
  227. if ( preg_match( $heredocIndent, $heredoc, $indent ) ) {
  228. $this->heredocs[1][$i-1] = str_replace(
  229. $this->endOfLine . $indent[1], $this->endOfLine, $heredoc
  230. );
  231. }
  232. }
  233. }
  234. }
  235. // }}}
  236. // {{{ private function unmaskHeredocs( & $content )
  237. /**
  238. * Unhides Heredoc strings after the reorganization process
  239. *
  240. * @param string &$content a string representing the script's content
  241. * @return void
  242. * @see maskHeredocs(), reformat()
  243. * @since Method available since Release 0.2.1
  244. */
  245. private function unmaskHeredocs( & $content )
  246. {
  247. $i = 0;
  248. foreach ( $this->heredocs[1] as $heredoc ) {
  249. $hd = '< Heredoc ' . $i++ . ' >';
  250. $trailingSpace = false !== strpos( $content, $hd . ' ' );
  251. $content = str_replace(
  252. $hd . ( $trailingSpace ? ' ' : '' ),
  253. $heredoc . ( $trailingSpace ? $this->endOfLine : '' ), $content
  254. );
  255. }
  256. }
  257. // }}}
  258. // {{{ private properties
  259. /**
  260. * Holds the script's content currently being reorganized
  261. *
  262. * @var string
  263. */
  264. private $content = '';
  265. /**
  266. * Holds the end-of-line identifier currently being used
  267. *
  268. * @var string
  269. */
  270. private $endOfLine = '';
  271. /**
  272. * Holds the end-of-line identifiers of known OSes
  273. *
  274. * @var array
  275. */
  276. private $endOfLineIdentifiers = null;
  277. /**
  278. * Holds the first found hash-bang directive
  279. *
  280. * @var string
  281. */
  282. private $hashBang = '';
  283. /**
  284. * Holds the regular expression for the unices' has-bang directive
  285. *
  286. * @var string
  287. */
  288. private $hashBangIdentifier = '';
  289. /**
  290. * Holds the list of Heredoc strings to un-/mask
  291. *
  292. * @var array
  293. */
  294. private $heredocs = null;
  295. /**
  296. * Holds the regular expression for Heredoc strings
  297. *
  298. * @var string
  299. */
  300. private $heredocsIdentifier = '';
  301. /**
  302. * Holds the strategy to apply
  303. *
  304. * @var ScriptReorganizer_Strategy
  305. */
  306. private $strategy = null;
  307. // }}}
  308.  
  309. }
  310.  
  311. /*
  312. * Local variables:
  313. * tab-width: 4
  314. * c-basic-offset: 4
  315. * c-hanging-comment-ender-p: nil
  316. * End:
  317. */
  318.  
  319. ?>

Documentation generated on Sun, 6 Nov 2005 22:48:18 +0100 by phpDocumentor 1.3.0RC3