Source for file Library.php

Documentation is available at Library.php

  1. <?php
  2.  
  3. /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
  4.  
  5. /**
  6. * ScriptReorganizer Type :: Library
  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. * @subpackage Type
  18. * @author Stefano F. Rausch <stefano@rausch-e.net>
  19. * @copyright 2005 Stefano F. Rausch <stefano@rausch-e.net>
  20. * @license http://www.gnu.org/copyleft/lesser.html LGPL License 2.1
  21. * @version SVN: $Id: Library.php 32 2005-10-30 22:05:19Z stefanorausch $
  22. * @link http://pear.php.net/package/ScriptReorganizer
  23. * @filesource
  24. */
  25.  
  26. /**
  27. * Depends on <kbd>ScriptReorganizer_Strategy</kbd>
  28. */
  29. require_once 'ScriptReorganizer/Strategy.php';
  30.  
  31. /**
  32. * Extends <kbd>ScriptReorganizer_Type</kbd>
  33. */
  34. require_once 'ScriptReorganizer/Type.php';
  35.  
  36. /**
  37. * Throws <kbd>ScriptReorganizer_Type_Exception</kbd>
  38. */
  39. require_once 'ScriptReorganizer/Type/Exception.php';
  40.  
  41. /**
  42. * Many-to-one reorganization
  43. *
  44. * Converts a script file and all included/required files to a single library file
  45. * according to the {@link ScriptReorganizer_Strategy Strategy} to apply.
  46. *
  47. * To avoid the processing of files' imports, which can change independently from the
  48. * code base at any time, transform the respective statement from a static to a
  49. * dynamic one, e.g. from <kbd>require_once 'configuration.php';</kbd> to
  50. * <kbd>require_once 'configuration' . '.php';</kbd>.
  51. *
  52. * If the advanced pack mode strategy is used for packaging, a non-ScriptReorganized
  53. * source code tree should be shipped together with the optimized one, to enable
  54. * third parties to track down undiscoverd bugs.
  55. *
  56. * @category Tools
  57. * @package ScriptReorganizer
  58. * @subpackage Type
  59. * @author Stefano F. Rausch <stefano@rausch-e.net>
  60. * @copyright 2005 Stefano F. Rausch <stefano@rausch-e.net>
  61. * @license http://www.gnu.org/copyleft/lesser.html LGPL License 2.1
  62. * @version Release: 0.4.0
  63. * @link http://pear.php.net/package/ScriptReorganizer
  64. */
  65. class ScriptReorganizer_Type_Library extends ScriptReorganizer_Type
  66. {
  67. // {{{ public function __construct( ScriptReorganizer_Strategy $strategy )
  68. /**
  69. * Constructor
  70. *
  71. * @param ScriptReorganizer_Strategy $strategy a
  72. * <kbd>ScriptReorganizer_Strategy</kbd> to apply
  73. */
  74. public function __construct( ScriptReorganizer_Strategy $strategy )
  75. {
  76. parent::__construct( $strategy );
  77. $this->imports = array();
  78. }
  79. // }}}
  80. // {{{ public function load( $file )
  81. /**
  82. * Loads the script's content to be reorganized from disk
  83. *
  84. * @param string $file a string representing the file's name to load
  85. * @return void
  86. * @throws {@link ScriptReorganizer_Type_Exception ScriptReorganizer_Type_Exception}
  87. */
  88. public function load( $file )
  89. {
  90. parent::load( $file );
  91. $baseDirectory = realpath( dirname( $file ) );
  92. $this->imports[] = $this->retrieveRealPath( $file, $baseDirectory );
  93. $this->_setContent( $this->resolveImports( $baseDirectory ) );
  94. $importException = '"< import of( file [^>]+)>"';
  95. if ( preg_match_all( $importException, $this->_getContent(), $matches ) ) {
  96. throw new ScriptReorganizer_Type_Exception(
  97. 'Import of' . PHP_EOL . '-' . ( implode( PHP_EOL . '-', $matches[1] ) )
  98. );
  99. }
  100. }
  101. // }}}
  102. // {{{ private function resolveImports( $baseDirectory )
  103. /**
  104. * Adds all imported files' contents to the script currently being reorganized
  105. *
  106. * @param string $baseDirectory a string representing the base path to use for
  107. * resolving files' imports
  108. * @return string a string representing the old script's content with the import
  109. * instructions replaced by the respective imported files' contents
  110. * @see retrieveContent()
  111. */
  112. private function resolveImports( $baseDirectory )
  113. {
  114. $content = $this->_getContent();
  115. $resolvedContents = array();
  116. $eol = $this->getEolIdentifier( $content );
  117. $staticImport = '"(;|[' . $eol . '])(([ \t]*)(include|require)(_once)?';
  118. $staticImport .= '[ \t]*\(?[ \t' . $eol . ']*[\'\"]([^\'\"]+)[\'\"]';
  119. $staticImport .= '[ \t' . $eol . ']*\)?[ \t]*;)"';
  120. // starting with PHPUnit2-2.3.0: Compilation failed: unmatched parentheses at offset 90?
  121. if ( @preg_match_all( $staticImport, $content, $matches ) ) {
  122. $i = 0;
  123. foreach ( $matches[6] as $file ) {
  124. $resolvedContents[] = $this->resolveImports(
  125. $this->retrieveContent(
  126. $file, '_once' === $matches[5][$i++], $baseDirectory
  127. )
  128. );
  129. }
  130. $i = 0;
  131. foreach ( $matches[2] as $staticIdentifier ) {
  132. $indent = $matches[3][$i];
  133. $resolvedContent = str_replace(
  134. $eol, $eol . $indent, $resolvedContents[$i++]
  135. );
  136. $staticIdentifier = '!' . str_replace(
  137. '(', '\(', str_replace( ')', '\)', $staticIdentifier )
  138. ) . '!';
  139. $content = preg_replace(
  140. $staticIdentifier, $resolvedContent, $content, 1
  141. );
  142. }
  143. }
  144. return $content;
  145. }
  146. // }}}
  147. // {{{ private function retrieveContent( $file, $importOnce, $baseDirectory )
  148. /**
  149. * Loads the file's content to be imported
  150. *
  151. * Avoids duplication of files being imported with <kbd>include_once</kbd> or
  152. * <kbd>require_once</kbd>.
  153. *
  154. * @param string $file a string representing the file's name to be added
  155. * @param boolean $importOnce boolean true, if the import instruction is either
  156. * <kbd>include_once</kbd> or <kbd>require_once</kbd>; otherwise false
  157. * @param string $baseDirectory a string representing the base path to use for
  158. * resolving the files' imports
  159. * @return string a string representing the new base path to use for resolving
  160. * future files' (relative) imports
  161. * @see resolveImports(), retrieveRealPath()
  162. */
  163. private function retrieveContent( $file, $importOnce, $baseDirectory )
  164. {
  165. $realFile = $this->retrieveRealPath( $file, $baseDirectory );
  166. if ( $importOnce ) {
  167. if ( in_array( $realFile, $this->imports ) ) {
  168. $this->_setContent( '' );
  169. return 'will not be used';
  170. }
  171. $this->imports[] = $realFile;
  172. }
  173. try {
  174. parent::load( $realFile );
  175. } catch ( scriptReorganizer_Type_Exception $e ) {
  176. $file = $baseDirectory . DIRECTORY_SEPARATOR . $file;
  177. $this->_setContent(
  178. '< import of file ' . $file . ' failed >'
  179. );
  180. }
  181. return dirname( $realFile );
  182. }
  183. // }}}
  184. // {{{ private function retrieveRealPath( $file, $baseDirectory )
  185. /**
  186. * Delivers the file's realpath to be imported
  187. *
  188. * @param string $file a string representing the file's name to be converted
  189. * into a realpath identifier
  190. * @param string $baseDirectory a string representing the base path to add to
  191. * the current include_path directive, if needed
  192. * @return string a string representing the file's real path to be imported
  193. * @see retrieveContent()
  194. */
  195. private function retrieveRealPath( $file, $baseDirectory )
  196. {
  197. if ( !is_file( $file ) ) {
  198. $includePaths = $baseDirectory . PATH_SEPARATOR . get_include_path();
  199. foreach ( explode( PATH_SEPARATOR, $includePaths ) as $includePath ) {
  200. $script = $includePath . DIRECTORY_SEPARATOR . $file;
  201. if ( is_file( $script ) ) {
  202. $file = $script;
  203. break;
  204. }
  205. }
  206. }
  207. return realpath( $file );
  208. }
  209. // }}}
  210. // {{{ private properties
  211. /**
  212. * Holds the list of already imported file names
  213. *
  214. * @var array
  215. */
  216. private $imports = null;
  217. // }}}
  218.  
  219. }
  220.  
  221. /*
  222. * Local variables:
  223. * tab-width: 4
  224. * c-basic-offset: 4
  225. * c-hanging-comment-ender-p: nil
  226. * End:
  227. */
  228.  
  229. ?>

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