Source for file Bcompile.php

Documentation is available at Bcompile.php

  1. <?php
  2.  
  3. /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
  4.  
  5. /**
  6. * ScriptReorganizer Type Decorator :: Bcompile
  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: Bcompile.php 33 2005-11-06 22:05:46Z stefanorausch $
  22. * @link http://pear.php.net/package/ScriptReorganizer
  23. * @since File available since Release 0.3.0
  24. * @filesource
  25. */
  26.  
  27. /**
  28. * Depends on <kbd>ScriptReorganizer_Type</kbd>
  29. */
  30. require_once 'ScriptReorganizer/Type.php';
  31.  
  32. /**
  33. * Extends <kbd>ScriptReorganizer_Type_Decorator</kbd>
  34. */
  35. require_once 'ScriptReorganizer/Type/Decorator.php';
  36.  
  37. /**
  38. * Throws <kbd>ScriptReorganizer_Type_Decorator_Exception</kbd>
  39. */
  40. require_once 'ScriptReorganizer/Type/Decorator/Exception.php';
  41.  
  42. /**
  43. * Decorator/Adapter for encoding a PHP source file to byte-code
  44. *
  45. * If a script or a library is bcompiled, a non-ScriptReorganized source code tree
  46. * should be shipped together with the optimized one, to enable third parties to
  47. * track down undiscoverd bugs.
  48. *
  49. * ANN: Decoration of a directly sequencing Bcompile-Decorator or Pharize-Decorator
  50. * is not allowed.
  51. *
  52. * @category Tools
  53. * @package ScriptReorganizer
  54. * @subpackage Type_Decorator
  55. * @author Stefano F. Rausch <stefano@rausch-e.net>
  56. * @copyright 2005 Stefano F. Rausch <stefano@rausch-e.net>
  57. * @license http://www.gnu.org/copyleft/lesser.html LGPL License 2.1
  58. * @version Release: 0.4.0
  59. * @link http://pear.php.net/package/ScriptReorganizer
  60. * @since Class available since Release 0.3.0
  61. */
  62. class ScriptReorganizer_Type_Decorator_Bcompile extends ScriptReorganizer_Type_Decorator
  63. {
  64. // {{{ public function __construct( ScriptReorganizer_Type $type )
  65. /**
  66. * Constructor
  67. *
  68. * @param ScriptReorganizer_Type $type a <kbd>ScriptReorganizer_Type</kbd> to
  69. * decorate
  70. * @throws {@link ScriptReorganizer_Type_Decorator_Exception ScriptReorganizer_Type_Decorator_Exception}
  71. */
  72. public function __construct( ScriptReorganizer_Type $type )
  73. {
  74. $constraint = '';
  75. if ( $type instanceof ScriptReorganizer_Type_Decorator_Bcompile ) {
  76. $constraint = 'Bcompile-Decorator';
  77. } else if ( class_exists( 'ScriptReorganizer_Type_Decorator_Pharize', false ) ) {
  78. if ( $type instanceof ScriptReorganizer_Type_Decorator_Pharize ) {
  79. $constraint = 'Pharize-Decorator';
  80. }
  81. }
  82. if ( $constraint ) {
  83. throw new ScriptReorganizer_Type_Decorator_Exception(
  84. 'Decoration of a directly sequencing ' . $constraint . ' not allowed'
  85. );
  86. }
  87. if ( !extension_loaded( 'bcompiler' ) ) {
  88. throw new ScriptReorganizer_Type_Decorator_Exception(
  89. 'PHP Extension bcompiler not loaded'
  90. );
  91. }
  92. parent::__construct( $type );
  93. }
  94. // }}}
  95. // {{{ public function save( $file )
  96. /**
  97. * Saves the reorganized script's content as encoded byte-code to disk
  98. *
  99. * @param string $file a string representing the file's name to save
  100. * @return void
  101. * @throws {@link ScriptReorganizer_Type_Decorator_Exception ScriptReorganizer_Type_Decorator_Exception}
  102. */
  103. public function save( $file )
  104. {
  105. $source = 'source.' . md5($file);
  106. @file_put_contents( $source, '<?php ' . $this->_getContent() . ' ?>' );
  107. if ( is_file( $source ) ) {
  108. if ( $target = @fopen( $file, 'wb' ) ) {
  109. bcompiler_write_header( $target );
  110. bcompiler_write_file( $target, $source );
  111. bcompiler_write_footer( $target );
  112. @fclose( $target );
  113. }
  114. unlink( $source );
  115. }
  116. if ( !is_file( $file ) ) {
  117. throw new ScriptReorganizer_Type_Decorator_Exception(
  118. 'BCompiler file ' . $file . ' is not writable'
  119. );
  120. }
  121. }
  122. // }}}
  123.  
  124. }
  125.  
  126. /*
  127. * Local variables:
  128. * tab-width: 4
  129. * c-basic-offset: 4
  130. * c-hanging-comment-ender-p: nil
  131. * End:
  132. */
  133.  
  134. ?>

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