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 37 2005-11-15 05:00:00Z 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.4.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. }
  62. // }}}
  63. // {{{ public function __destruct()
  64. /**
  65. * Destructor
  66. */
  67. public function __destruct()
  68. {
  69. unset( $this->strategy );
  70. }
  71. // }}}
  72. // {{{ public function load( $file )
  73. /**
  74. * Loads the script's content to be reorganized from disk
  75. *
  76. * @param string $file a string representing the file's name to load
  77. * @return void
  78. * @throws {@link ScriptReorganizer_Type_Exception ScriptReorganizer_Type_Exception}
  79. */
  80. public function load( $file )
  81. {
  82. $content = @file_get_contents( $file );
  83. if ( false === $content ) {
  84. throw new ScriptReorganizer_Type_Exception(
  85. 'File ' . $file . ' is not readable'
  86. );
  87. }
  88. $eol = $this->getEolIdentifier( $content );
  89. $this->initializeIdentifiers( $eol );
  90. if ( $eol != $this->endOfLine ) {
  91. $content = str_replace( $eol, $this->endOfLine, $content );
  92. }
  93. if ( preg_match( $this->hashBangIdentifier, $content, $match ) ) {
  94. $content = str_replace( $match[0], '', $content );
  95. if ( !$this->hashBang ) {
  96. $this->hashBang = $match[1];
  97. }
  98. }
  99. $result = trim( $content );
  100. $result = preg_replace( '"^<\?php"', '', $result );
  101. $result = preg_replace( '"\?>$"', '', $result );
  102. $this->_setContent( $result );
  103. }
  104. // }}}
  105. // {{{ public function reformat()
  106. /**
  107. * Reorganizes the script's content by applying the chosen
  108. * {@link ScriptReorganizer_Strategy Strategy}
  109. *
  110. * @return void
  111. */
  112. public function reformat()
  113. {
  114. $content = $this->_getContent();
  115. $this->maskHeredocs( $content );
  116. $content = trim( $this->strategy->reformat( $content, $this->endOfLine ) );
  117. $this->unmaskHeredocs( $content );
  118. $this->_setContent( $content );
  119. }
  120. // }}}
  121. // {{{ public function save( $file )
  122. /**
  123. * Saves the reorganized script's content to disk
  124. *
  125. * @param string $file a string representing the file's name to save
  126. * @return void
  127. * @throws {@link ScriptReorganizer_Type_Exception ScriptReorganizer_Type_Exception}
  128. */
  129. public function save( $file )
  130. {
  131. $content = $this->hashBang;
  132. $content .= '<?php' . $this->endOfLine . $this->endOfLine . $this->_getContent()
  133. . $this->endOfLine . $this->endOfLine . '?>';
  134. if ( false === @file_put_contents( $file, $content ) ) {
  135. throw new ScriptReorganizer_Type_Exception(
  136. 'File ' . $file . ' is not writable'
  137. );
  138. }
  139. $this->endOfLine = '';
  140. }
  141. // }}}
  142. // {{{ protected function getEolIdentifier( & $content )
  143. /**
  144. * Detects the currently used end-of-line identifier
  145. *
  146. * @param string &$content a string representing the script's content
  147. * @return string a string representing the end-of-line identifier found in the
  148. * script's content
  149. * @since Method available sind Release 0.3.0
  150. */
  151. protected function getEolIdentifier( & $content )
  152. {
  153. static $endOfLineIdentifiers = array(
  154. 'win' => "\r\n", 'unix' => "\n", 'mac' => "\r"
  155. );
  156. foreach ( $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 first found hash-bang directive
  273. *
  274. * @var string
  275. */
  276. private $hashBang = '';
  277. /**
  278. * Holds the regular expression for the unices' has-bang directive
  279. *
  280. * @var string
  281. */
  282. private $hashBangIdentifier = '';
  283. /**
  284. * Holds the list of Heredoc strings to un-/mask
  285. *
  286. * @var array
  287. */
  288. private $heredocs = null;
  289. /**
  290. * Holds the regular expression for Heredoc strings
  291. *
  292. * @var string
  293. */
  294. private $heredocsIdentifier = '';
  295. /**
  296. * Holds the strategy to apply
  297. *
  298. * @var ScriptReorganizer_Strategy
  299. */
  300. private $strategy = null;
  301. // }}}
  302. }
  303.  
  304. /*
  305. * Local variables:
  306. * tab-width: 4
  307. * c-basic-offset: 4
  308. * c-hanging-comment-ender-p: nil
  309. * End:
  310. */
  311.  
  312. ?>

Documentation generated on Tue, 22 Nov 2005 01:57:15 +0100 by phpDocumentor 1.3.0RC3