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

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: @package_version@
  63.  * @link       http://pear.php.net/package/ScriptReorganizer
  64.  */
  65. {
  66.     // {{{ public function __construct( ScriptReorganizer_Strategy $strategy )
  67.     
  68.     /**
  69.      * Constructor
  70.      *
  71.      * @param ScriptReorganizer_Strategy $strategy a
  72.      *         <kbd>ScriptReorganizer_Strategy</kbd> to apply
  73.      */
  74.     public function __constructScriptReorganizer_Strategy $strategy )
  75.     {
  76.         parent::__construct$strategy );
  77.         
  78.         $this->imports = array();
  79.     }
  80.     
  81.     // }}}
  82.     
  83.     // {{{ public function load( $file )
  84.     
  85.     /**
  86.      * Loads the script's content to be reorganized from disk
  87.      *
  88.      * @param  string $file a string representing the file's name to load
  89.      * @return void 
  90.      * @throws {@link ScriptReorganizer_Type_Exception ScriptReorganizer_Type_Exception}
  91.      */
  92.     public function load$file )
  93.     {
  94.         parent::load$file );
  95.         
  96.         $baseDirectory realpathdirname$file ) );
  97.         $this->imports[$this->retrieveRealPath$file$baseDirectory );
  98.         
  99.         $this->_setContent$this->resolveImports$baseDirectory ) );
  100.         
  101.         $importException '"< import of( file [^>]+)>"';
  102.         
  103.         if preg_match_all$importException$this->_getContent()$matches ) ) {
  104.             throw new ScriptReorganizer_Type_Exception(
  105.                 'Import of' . PHP_EOL . '-' implodePHP_EOL . '-'$matches[1) )
  106.             );
  107.         }
  108.     }
  109.     
  110.     // }}}
  111.     
  112.     // {{{ private function resolveImports( $baseDirectory )
  113.     
  114.     /**
  115.      * Adds all imported files' contents to the script currently being reorganized
  116.      *
  117.      * @param  string $baseDirectory a string representing the base path to use for
  118.      *          resolving files' imports
  119.      * @return string a string representing the old script's content with the import
  120.      *          instructions replaced by the respective imported files' contents
  121.      * @see    retrieveContent()
  122.      */
  123.     private function resolveImports$baseDirectory )
  124.     {
  125.         $content $this->_getContent();
  126.         $resolvedContents = array();
  127.         
  128.         $eol $this->getEolIdentifier$content );
  129.         
  130.         $staticImport  '"(;|[' $eol '])(([ \t]*)(include|require)(_once)?';
  131.         $staticImport .= '[ \t]*\(?[ \t' $eol ']*[\'\"]([^\'\"]+)[\'\"]';
  132.         $staticImport .= '[ \t' $eol ']*\)?[ \t]*;)"';
  133.         
  134.         // starting with PHPUnit2-2.3.0: Compilation failed: unmatched parentheses at offset 90?
  135.         
  136.         if @preg_match_all$staticImport$content$matches ) ) {
  137.             $i = 0;
  138.             
  139.             foreach $matches[6as $file {
  140.                 $resolvedContents[$this->resolveImports(
  141.                     $this->retrieveContent(
  142.                         $file'_once' === $matches[5][$i++]$baseDirectory
  143.                     )
  144.                 );
  145.             }
  146.             
  147.             $i = 0;
  148.             
  149.             foreach $matches[2as $staticIdentifier {
  150.                 $indent $matches[3][$i];
  151.                 
  152.                 $resolvedContent str_replace(
  153.                     $eol$eol $indent$resolvedContents[$i++]
  154.                 );
  155.                 
  156.                 $staticIdentifier '!' str_replace(
  157.                     '(''\('str_replace')''\)'$staticIdentifier )
  158.                 '!';
  159.                 
  160.                 $content preg_replace(
  161.                     $staticIdentifier$resolvedContent$content1
  162.                 );
  163.             }
  164.         }
  165.         
  166.         return $content;
  167.     }
  168.     
  169.     // }}}
  170.     // {{{ private function retrieveContent( $file, $importOnce, $baseDirectory )
  171.     
  172.     /**
  173.      * Loads the file's content to be imported
  174.      *
  175.      * Avoids duplication of files being imported with <kbd>include_once</kbd> or
  176.      * <kbd>require_once</kbd>.
  177.      *
  178.      * @param  string $file a string representing the file's name to be added
  179.      * @param  boolean $importOnce boolean true, if the import instruction is either
  180.      *          <kbd>include_once</kbd> or <kbd>require_once</kbd>; otherwise false
  181.      * @param  string $baseDirectory a string representing the base path to use for
  182.      *          resolving the files' imports
  183.      * @return string a string representing the new base path to use for resolving
  184.      *          future files' (relative) imports
  185.      * @see    resolveImports(), retrieveRealPath()
  186.      */
  187.     private function retrieveContent$file$importOnce$baseDirectory )
  188.     {
  189.         $realFile $this->retrieveRealPath$file$baseDirectory );
  190.         
  191.         if $importOnce {
  192.             if in_array$realFile$this->imports ) ) {
  193.                 $this->_setContent'' );
  194.                 return 'will not be used';
  195.             }
  196.             
  197.             $this->imports[$realFile;
  198.         }
  199.         
  200.         try {
  201.             parent::load$realFile );
  202.         catch scriptReorganizer_Type_Exception $e {
  203.             $file $baseDirectory . DIRECTORY_SEPARATOR . $file;
  204.             
  205.             $this->_setContent(
  206.                 '< import of file ' $file ' failed >'
  207.             );
  208.         }
  209.         
  210.         return dirname$realFile );
  211.     }
  212.     
  213.     // }}}
  214.     // {{{ private function retrieveRealPath( $file, $baseDirectory )
  215.     
  216.     /**
  217.      * Delivers the file's realpath to be imported
  218.      *
  219.      * @param  string $file a string representing the file's name to be converted
  220.      *          into a realpath identifier
  221.      * @param  string $baseDirectory a string representing the base path to add to
  222.      *          the current include_path directive, if needed
  223.      * @return string a string representing the file's real path to be imported
  224.      * @see    retrieveContent()
  225.      */
  226.     private function retrieveRealPath$file$baseDirectory )
  227.     {
  228.         if !is_file$file ) ) {
  229.             $includePaths $baseDirectory . PATH_SEPARATOR . get_include_path();
  230.             
  231.             foreach explodePATH_SEPARATOR$includePaths as $includePath {
  232.                 $script $includePath . DIRECTORY_SEPARATOR . $file;
  233.                 
  234.                 if is_file$script ) ) {
  235.                     $file $script;
  236.                     break;
  237.                 }
  238.             }
  239.         }
  240.         
  241.         return realpath$file );
  242.     }
  243.     
  244.     // }}}
  245.     
  246.     // {{{ private properties
  247.     
  248.     /**
  249.      * Holds the list of already imported file names
  250.      *
  251.      * @var array 
  252.      */
  253.     private $imports = null;
  254.     
  255.     // }}}
  256. }
  257.  
  258. /*
  259.  * Local variables:
  260.  * tab-width: 4
  261.  * c-basic-offset: 4
  262.  * c-hanging-comment-ender-p: nil
  263.  * End:
  264.  */
  265.  
  266. ?>

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