ScriptReorganizer
[ class tree: ScriptReorganizer ] [ index: ScriptReorganizer ] [ all elements ]

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: @package_version@
  68.  * @link       http://pear.php.net/package/ScriptReorganizer
  69.  * @todo       implement method <kbd>loadDirectory</kbd>
  70.  */
  71. {
  72.     // {{{ public function __construct( ScriptReorganizer_Type $type )
  73.     
  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 __constructScriptReorganizer_Type $type )
  82.     {
  83.         $constraint '';
  84.         
  85.         if $type instanceof ScriptReorganizer_Type_Decorator_Pharize {
  86.             $constraint 'Pharize-Decorator';
  87.         else if class_exists'ScriptReorganizer_Type_Decorator_Bcompile'false ) ) {
  88.             if $type instanceof ScriptReorganizer_Type_Decorator_Bcompile {
  89.                 $constraint 'Bcompile-Decorator';
  90.             }
  91.         }
  92.         
  93.         if $constraint {
  94.             throw new ScriptReorganizer_Type_Decorator_Exception(
  95.                 'Decoration of a directly sequencing ' $constraint ' not allowed'
  96.             );
  97.         }
  98.         
  99.         parent::__construct$type );
  100.         
  101.         $this->files = array();
  102.         $this->magic = array();
  103.     }
  104.     
  105.     // }}}
  106.     
  107.     // {{{ public function load( $source, $target, $magicRequire = false )
  108.     
  109.     /**
  110.      * Loads the script's content to be reorganized from disk
  111.      *
  112.      * @param  string $source a string representing the file's name to load
  113.      * @param  string $target a string representing the file's location in the PHP
  114.      *          Archive
  115.      * @param  boolean $magicRequire boolean true, for phar-stream activation within
  116.      *          the PHP Arcive; otherwise false
  117.      * @return void 
  118.      * @throws {@link ScriptReorganizer_Type_Exception ScriptReorganizer_Type_Exception}
  119.      * @throws {@link ScriptReorganizer_Type_Decorator_Exception ScriptReorganizer_Type_Decorator_Exception}
  120.      */
  121.     public function load$source$target$magicRequire = false )
  122.     {
  123.         parent::load$source );
  124.         
  125.         if !is_string$target || '' == $target {
  126.             throw new ScriptReorganizer_Type_Decorator_Exception(
  127.                 'Argument $target for Pharize-Decorator either not of type string or empty'
  128.             );
  129.         }
  130.         
  131.         $content = parent::_getContent();
  132.         
  133.         $this->loadContent$content$target$magicRequire );
  134.     }
  135.     
  136.     // }}}
  137.     // {{{ public function loadDirectory ( ... )
  138.     
  139.     /*
  140.      * to be implemented
  141.      */
  142.     
  143.     // }}}
  144.     // {{{ public function loadFiles ( $files, $magicRequire = false )
  145.     
  146.     /**
  147.      * Loads the scripts' content to be reorganized from disk
  148.      *
  149.      * @param  array $files an associative array holding all files' name to load and
  150.      *          the corresponding files' locations in the PHP Archive
  151.      * @param  boolean $magicRequire boolean true, for phar-stream activation within
  152.      *          the PHP Archive; otherwise false
  153.      * @return void 
  154.      * @throws {@link ScriptReorganizer_Type_Exception ScriptReorganizer_Type_Exception}
  155.      * @throws {@link ScriptReorganizer_Type_Decorator_Exception ScriptReorganizer_Type_Decorator_Exception}
  156.      */
  157.     public function loadFiles$files$magicRequire = false )
  158.     {
  159.         if !is_array$files || empty$files ) ) {
  160.             throw new ScriptReorganizer_Type_Decorator_Exception(
  161.                 'Argument $files for Pharize-Decorator either not of type array or empty'
  162.             );
  163.         }
  164.         
  165.         foreach $files as $source => $target {
  166.             $this->load$source$target$magicRequire );
  167.         }
  168.     }
  169.     
  170.     // }}}
  171.     // {{{ public function reformat()
  172.     
  173.     /**
  174.      * Reorganizes the script's content by applying the chosen
  175.      * {@link ScriptReorganizer_Strategy Strategy}
  176.      *
  177.      * @return void 
  178.      */
  179.     public function reformat()
  180.     {
  181.         foreach $this->files as $target => $content {
  182.             parent::_setContent$content );
  183.             parent::reformat();
  184.             $this->files[$target= parent::_getContent();
  185.         }
  186.     }
  187.     
  188.     // }}}
  189.     // {{{ public function save( $file, $initFile = 'index.php', $compress = false, $allowDirectAccess = false )
  190.     
  191.     /**
  192.      * Saves the PHP Archive to disk
  193.      *
  194.      * @param  string $file a string representing the PHP Archive file's name to save
  195.      * @param  string $initFile a string representing the file's name called by
  196.      *          default upon PHAR execution
  197.      * @param  boolean $compress boolean true, if the files have to be compressed;
  198.      *          otherwise false
  199.      * @param  mixed $allowDirectAccess boolean true, for unrestricted file access;
  200.      *          boolean false, for access restricted to the init file; a string
  201.      *          representing the restricted file type (extension) access
  202.      * @return void 
  203.      * @throws {@link ScriptReorganizer_Type_Decorator_Exception ScriptReorganizer_Type_Decorator_Exception}
  204.      */
  205.     public function save$file$initFile 'index.php'$compress = false$allowDirectAccess = false )
  206.     {
  207.         $archive = new PHP_Archive_Creator(
  208.             $initFile$compress$allowDirectAccess
  209.         );
  210.         
  211.         $additionErrors = array();
  212.         
  213.         foreach $this->files as $target => $content {
  214.             $content '<?php ' $content ' ?>';
  215.             
  216.             if !$archive->addString$content$target$this->magic[$target) ) {
  217.                 $additionErrors[$target;
  218.             }
  219.         }
  220.         
  221.         if !empty$additionErrors ) ) {
  222.             throw new ScriptReorganizer_Type_Decorator_Exception(
  223.                 'Could not add ' . PHP_EOL . '-'
  224.                 . implodePHP_EOL . '- file '$additionErrors )
  225.                 . PHP_EOL . 'to PHP Archive file ' $file
  226.             );
  227.         }
  228.         
  229.         @$archive->savePhar$file );
  230.         
  231.         if !is_file$file ) ) {
  232.             throw new ScriptReorganizer_Type_Decorator_Exception(
  233.                 'PHP Archive file ' $file ' is not writable'
  234.             );
  235.         }
  236.     }
  237.     
  238.     // }}}
  239.     
  240.     // {{{ package function _getContent()
  241.     
  242.     /**
  243.      * Gets the scripts' contents currently being reorganized
  244.      *
  245.      * @visibility package restricted
  246.      * @return     array an associative array holding all files' locations in the PHP
  247.      *              Archive and the corresponding contents
  248.      * @since      Method available since Release 0.3.0
  249.      */
  250.     public function _getContent()
  251.     {
  252.         return $this->files;
  253.     }
  254.     
  255.     // }}}
  256.     // {{{ package function _setContent( $targets, $magicRequire = false )
  257.     
  258.     /**
  259.      * Sets the scripts' contents currently being reorganized
  260.      *
  261.      * @visibility package restricted
  262.      * @param      array $targets an associative array holding all files' locations in
  263.      *              the PHP Archive and the corresponding contents
  264.      * @param      boolean $magicRequire boolean true, for phar-stream activation within
  265.      *              the PHP Arcive; otherwise false
  266.      * @return     void 
  267.      * @throws     {@link ScriptReorganizer_Type_Decorator_Exception ScriptReorganizer_Type_Decorator_Exception}
  268.      * @since      Method available since Release 0.3.0
  269.      */
  270.     public function _setContent$targets$magicRequire = false )
  271.     {
  272.         if !is_array$targets || empty$targets ) ) {
  273.             throw new ScriptReorganizer_Type_Decorator_Exception(
  274.                 'Argument $targets for Pharize-Decorator either not of type array or empty'
  275.             );
  276.         }
  277.         
  278.         foreach $targets as $target => $content {
  279.             $this->loadContent$content$target$magicRequire );
  280.         }
  281.     }
  282.     
  283.     // }}}
  284.     
  285.     // {{{ private function loadContent( $content, $target, $magicRequire = false )
  286.     
  287.     /**
  288.      * Loads a string as the file's content to add to the PHP Archive
  289.      *
  290.      * @param  string $content a string representing the file's content to add
  291.      * @param  string $target a string representing the file's location in the PHP
  292.      *          Archive
  293.      * @param  boolean $magicRequire boolean true, for phar-stream activation within
  294.      *          the PHP Arcive; otherwise false
  295.      * @return void 
  296.      */
  297.     private function loadContent$content$target$magicRequire = false )
  298.     {
  299.         $this->files[$target$content;
  300.         $this->magic[$target$magicRequire;
  301.     }
  302.     
  303.     // }}}
  304.     
  305.     // {{{ private properties
  306.     
  307.     /*
  308.      * Holds the files to add to the PHP Archive
  309.      *
  310.      * @var array
  311.      */
  312.     private $files = null;
  313.     
  314.     /*
  315.      * Holds the <kbd><import>_once</kbd> replacement directives for the files to add
  316.      *
  317.      * @var array
  318.      */
  319.     private $magic = null;
  320.     
  321.     // }}}
  322. }
  323.  
  324. /*
  325.  * Local variables:
  326.  * tab-width: 4
  327.  * c-basic-offset: 4
  328.  * c-hanging-comment-ender-p: nil
  329.  * End:
  330.  */
  331.  
  332. ?>

Documentation generated on Mon, 11 Mar 2019 14:10:30 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.