Source for file Pharize.php

Documentation is available at Pharize.php

  1. <?php
  2.  
  3. /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
  4.  
  5. /**
  6. * ScriptReorganizer Type Decorator :: Pharize
  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_Decorator
  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: Pharize.php 32 2005-10-30 22:05:19Z stefanorausch $
  22. * @link http://pear.php.net/package/ScriptReorganizer
  23. * @filesource
  24. */
  25.  
  26. /**
  27. * Uses <kbd>PHP_Archive_Creator</kbd>
  28. */
  29. require_once 'PHP/Archive/' . 'Creator.php';
  30.  
  31. /**
  32. * Depends on <kbd>ScriptReorganizer_Type</kbd>
  33. */
  34. require_once 'ScriptReorganizer/Type.php';
  35.  
  36. /**
  37. * Extends <kbd>ScriptReorganizer_Type_Decorator</kbd>
  38. */
  39. require_once 'ScriptReorganizer/Type/Decorator.php';
  40.  
  41. /**
  42. * Throws <kbd>ScriptReorganizer_Type_Exception</kbd>
  43. */
  44. require_once 'ScriptReorganizer/Type/Exception.php';
  45.  
  46. /**
  47. * Throws <kbd>ScriptReorganizer_Type_Decorator_Exception</kbd>
  48. */
  49. require_once 'ScriptReorganizer/Type/Decorator/Exception.php';
  50.  
  51. /**
  52. * Decorator/Adapter for creating a <kbd>PHP_Archive</kbd>
  53. *
  54. * If a (complex) application is pharized, a non-ScriptReorganized source code tree
  55. * should be shipped together with the optimized one, to enable third parties to
  56. * track down undiscoverd bugs.
  57. *
  58. * ANN: Decoration of a directly sequencing Pharize-Decorator or Bcompile-Decorator
  59. * is not allowed.
  60. *
  61. * @category Tools
  62. * @package ScriptReorganizer
  63. * @subpackage Type_Decorator
  64. * @author Stefano F. Rausch <stefano@rausch-e.net>
  65. * @copyright 2005 Stefano F. Rausch <stefano@rausch-e.net>
  66. * @license http://www.gnu.org/copyleft/lesser.html LGPL License 2.1
  67. * @version Release: 0.4.0
  68. * @link http://pear.php.net/package/ScriptReorganizer
  69. * @todo implement method <kbd>loadDirectory</kbd>
  70. */
  71. class ScriptReorganizer_Type_Decorator_Pharize extends ScriptReorganizer_Type_Decorator
  72. {
  73. // {{{ public function __construct( ScriptReorganizer_Type $type )
  74. /**
  75. * Constructor
  76. *
  77. * @param ScriptReorganizer_Type $type a <kbd>ScriptReorganizer_Type</kbd> to
  78. * decorate
  79. * @throws {@link ScriptReorganizer_Type_Decorator_Exception ScriptReorganizer_Type_Decorator_Exception}
  80. */
  81. public function __construct( ScriptReorganizer_Type $type )
  82. {
  83. $constraint = '';
  84. if ( $type instanceof ScriptReorganizer_Type_Decorator_Pharize ) {
  85. $constraint = 'Pharize-Decorator';
  86. } else if ( class_exists( 'ScriptReorganizer_Type_Decorator_Bcompile', false ) ) {
  87. if ( $type instanceof ScriptReorganizer_Type_Decorator_Bcompile ) {
  88. $constraint = 'Bcompile-Decorator';
  89. }
  90. }
  91. if ( $constraint ) {
  92. throw new ScriptReorganizer_Type_Decorator_Exception(
  93. 'Decoration of a directly sequencing ' . $constraint . ' not allowed'
  94. );
  95. }
  96. parent::__construct( $type );
  97. $this->files = array();
  98. $this->magic = array();
  99. }
  100. // }}}
  101. // {{{ public function load( $source, $target, $magicRequire = false )
  102. /**
  103. * Loads the script's content to be reorganized from disk
  104. *
  105. * @param string $source a string representing the file's name to load
  106. * @param string $target a string representing the file's location in the PHP
  107. * Archive
  108. * @param boolean $magicRequire boolean true, for phar-stream activation within
  109. * the PHP Arcive; otherwise false
  110. * @return void
  111. * @throws {@link ScriptReorganizer_Type_Exception ScriptReorganizer_Type_Exception}
  112. * @throws {@link ScriptReorganizer_Type_Decorator_Exception ScriptReorganizer_Type_Decorator_Exception}
  113. */
  114. public function load( $source, $target, $magicRequire = false )
  115. {
  116. parent::load( $source );
  117. if ( !is_string( $target ) || '' == $target ) {
  118. throw new ScriptReorganizer_Type_Decorator_Exception(
  119. 'Argument $target for Pharize-Decorator either not of type string or empty'
  120. );
  121. }
  122. $content = parent::_getContent();
  123. $this->loadContent( $content, $target, $magicRequire );
  124. }
  125. // }}}
  126. // {{{ public function loadDirectory ( ... )
  127. /*
  128. * to be implemented
  129. */
  130. // }}}
  131. // {{{ public function loadFiles ( $files, $magicRequire = false )
  132. /**
  133. * Loads the scripts' content to be reorganized from disk
  134. *
  135. * @param array $files an associative array holding all files' name to load and
  136. * the corresponding files' locations in the PHP Archive
  137. * @param boolean $magicRequire boolean true, for phar-stream activation within
  138. * the PHP Archive; otherwise false
  139. * @return void
  140. * @throws {@link ScriptReorganizer_Type_Exception ScriptReorganizer_Type_Exception}
  141. * @throws {@link ScriptReorganizer_Type_Decorator_Exception ScriptReorganizer_Type_Decorator_Exception}
  142. */
  143. public function loadFiles( $files, $magicRequire = false )
  144. {
  145. if ( !is_array( $files ) || empty( $files ) ) {
  146. throw new ScriptReorganizer_Type_Decorator_Exception(
  147. 'Argument $files for Pharize-Decorator either not of type array or empty'
  148. );
  149. }
  150. foreach ( $files as $source => $target ) {
  151. $this->load( $source, $target, $magicRequire );
  152. }
  153. }
  154. // }}}
  155. // {{{ public function reformat()
  156. /**
  157. * Reorganizes the script's content by applying the chosen
  158. * {@link ScriptReorganizer_Strategy Strategy}
  159. *
  160. * @return void
  161. */
  162. public function reformat()
  163. {
  164. foreach ( $this->files as $target => $content ) {
  165. parent::_setContent( $content );
  166. parent::reformat();
  167. $this->files[$target] = parent::_getContent();
  168. }
  169. }
  170. // }}}
  171. // {{{ public function save( $file, $initFile = 'index.php', $compress = false, $allowDirectAccess = false )
  172. /**
  173. * Saves the PHP Archive to disk
  174. *
  175. * @param string $file a string representing the PHP Archive file's name to save
  176. * @param string $initFile a string representing the file's name called by
  177. * default upon PHAR execution
  178. * @param boolean $compress boolean true, if the files have to be compressed;
  179. * otherwise false
  180. * @param mixed $allowDirectAccess boolean true, for unrestricted file access;
  181. * boolean false, for access restricted to the init file; a string
  182. * representing the restricted file type (extension) access
  183. * @return void
  184. * @throws {@link ScriptReorganizer_Type_Decorator_Exception ScriptReorganizer_Type_Decorator_Exception}
  185. */
  186. public function save( $file, $initFile = 'index.php', $compress = false, $allowDirectAccess = false )
  187. {
  188. $archive = new PHP_Archive_Creator(
  189. $initFile, $compress, $allowDirectAccess
  190. );
  191. $additionErrors = array();
  192. foreach ( $this->files as $target => $content ) {
  193. $content = '<?php ' . $content . ' ?>';
  194. if ( !$archive->addString( $content, $target, $this->magic[$target] ) ) {
  195. $additionErrors[] = $target;
  196. }
  197. }
  198. if ( !empty( $additionErrors ) ) {
  199. throw new ScriptReorganizer_Type_Decorator_Exception(
  200. 'Could not add ' . PHP_EOL . '-'
  201. . implode( PHP_EOL . '- file ', $additionErrors )
  202. . PHP_EOL . 'to PHP Archive file ' . $file
  203. );
  204. }
  205. @$archive->savePhar( $file );
  206. if ( !is_file( $file ) ) {
  207. throw new ScriptReorganizer_Type_Decorator_Exception(
  208. 'PHP Archive file ' . $file . ' is not writable'
  209. );
  210. }
  211. }
  212. // }}}
  213. // {{{ package function _getContent()
  214. /**
  215. * Gets the scripts' contents currently being reorganized
  216. *
  217. * @visibility package restricted
  218. * @return array an associative array holding all files' locations in the PHP
  219. * Archive and the corresponding contents
  220. * @since Method available since Release 0.3.0
  221. */
  222. public function _getContent()
  223. {
  224. return $this->files;
  225. }
  226. // }}}
  227. // {{{ package function _setContent( $targets, $magicRequire = false )
  228. /**
  229. * Sets the scripts' contents currently being reorganized
  230. *
  231. * @visibility package restricted
  232. * @param array $targets an associative array holding all files' locations in
  233. * the PHP Archive and the corresponding contents
  234. * @param boolean $magicRequire boolean true, for phar-stream activation within
  235. * the PHP Arcive; otherwise false
  236. * @return void
  237. * @throws {@link ScriptReorganizer_Type_Decorator_Exception ScriptReorganizer_Type_Decorator_Exception}
  238. * @since Method available since Release 0.3.0
  239. */
  240. public function _setContent( $targets, $magicRequire = false )
  241. {
  242. if ( !is_array( $targets ) || empty( $targets ) ) {
  243. throw new ScriptReorganizer_Type_Decorator_Exception(
  244. 'Argument $targets for Pharize-Decorator either not of type array or empty'
  245. );
  246. }
  247. foreach ( $targets as $target => $content ) {
  248. $this->loadContent( $content, $target, $magicRequire );
  249. }
  250. }
  251. // }}}
  252. // {{{ private function loadContent( $content, $target, $magicRequire = false )
  253. /**
  254. * Loads a string as the file's content to add to the PHP Archive
  255. *
  256. * @param string $content a string representing the file's content to add
  257. * @param string $target a string representing the file's location in the PHP
  258. * Archive
  259. * @param boolean $magicRequire boolean true, for phar-stream activation within
  260. * the PHP Arcive; otherwise false
  261. * @return void
  262. */
  263. private function loadContent( $content, $target, $magicRequire = false )
  264. {
  265. $this->files[$target] = $content;
  266. $this->magic[$target] = $magicRequire;
  267. }
  268. // }}}
  269. // {{{ private properties
  270. /*
  271. * Holds the files to add to the PHP Archive
  272. *
  273. * @var array
  274. */
  275. private $files = null;
  276. /*
  277. * Holds the <kbd><import>_once</kbd> replacement directives for the files to add
  278. *
  279. * @var array
  280. */
  281. private $magic = null;
  282. // }}}
  283.  
  284. }
  285.  
  286. /*
  287. * Local variables:
  288. * tab-width: 4
  289. * c-basic-offset: 4
  290. * c-hanging-comment-ender-p: nil
  291. * End:
  292. */
  293.  
  294. ?>

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