Source for file Library.php
Documentation is available at Library.php
/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
* ScriptReorganizer Type :: Library
* LICENSE: This library is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by the Free
* Software Foundation; either version 2.1 of the License, or (at your option) any
* @package ScriptReorganizer
* @author Stefano F. Rausch <stefano@rausch-e.net>
* @copyright 2005 Stefano F. Rausch <stefano@rausch-e.net>
* @license http://www.gnu.org/copyleft/lesser.html LGPL License 2.1
* @version SVN: $Id: Library.php 32 2005-10-30 22:05:19Z stefanorausch $
* @link http://pear.php.net/package/ScriptReorganizer
* Depends on <kbd>ScriptReorganizer_Strategy</kbd>
require_once 'ScriptReorganizer/Strategy.php';
* Extends <kbd>ScriptReorganizer_Type</kbd>
require_once 'ScriptReorganizer/Type.php';
* Throws <kbd>ScriptReorganizer_Type_Exception</kbd>
require_once 'ScriptReorganizer/Type/Exception.php';
* Many-to-one reorganization
* Converts a script file and all included/required files to a single library file
* according to the {@link ScriptReorganizer_Strategy Strategy} to apply.
* To avoid the processing of files' imports, which can change independently from the
* code base at any time, transform the respective statement from a static to a
* dynamic one, e.g. from <kbd>require_once 'configuration.php';</kbd> to
* <kbd>require_once 'configuration' . '.php';</kbd>.
* If the advanced pack mode strategy is used for packaging, a non-ScriptReorganized
* source code tree should be shipped together with the optimized one, to enable
* third parties to track down undiscoverd bugs.
* @package ScriptReorganizer
* @author Stefano F. Rausch <stefano@rausch-e.net>
* @copyright 2005 Stefano F. Rausch <stefano@rausch-e.net>
* @license http://www.gnu.org/copyleft/lesser.html LGPL License 2.1
* @version Release: @package_version@
* @link http://pear.php.net/package/ScriptReorganizer
// {{{ public function __construct( ScriptReorganizer_Strategy $strategy )
* @param ScriptReorganizer_Strategy $strategy a
* <kbd>ScriptReorganizer_Strategy</kbd> to apply
public function __construct( ScriptReorganizer_Strategy $strategy )
$this->imports = array ();
// {{{ public function load( $file )
* Loads the script's content to be reorganized from disk
* @param string $file a string representing the file's name to load
* @throws {@link ScriptReorganizer_Type_Exception ScriptReorganizer_Type_Exception}
public function load( $file )
$this->imports[] = $this->retrieveRealPath ( $file, $baseDirectory );
$this->_setContent( $this->resolveImports ( $baseDirectory ) );
$importException = '"< import of( file [^>]+)>"';
'Import of' . PHP_EOL . '-' . ( implode( PHP_EOL . '-', $matches[1 ] ) )
// {{{ private function resolveImports( $baseDirectory )
* Adds all imported files' contents to the script currently being reorganized
* @param string $baseDirectory a string representing the base path to use for
* resolving files' imports
* @return string a string representing the old script's content with the import
* instructions replaced by the respective imported files' contents
private function resolveImports ( $baseDirectory )
$resolvedContents = array ();
$staticImport = '"(;|[' . $eol . '])(([ \t]*)(include|require)(_once)?';
$staticImport .= '[ \t]*\(?[ \t' . $eol . ']*[\'\"]([^\'\"]+)[\'\"]';
$staticImport .= '[ \t' . $eol . ']*\)?[ \t]*;)"';
// starting with PHPUnit2-2.3.0: Compilation failed: unmatched parentheses at offset 90?
foreach ( $matches[6 ] as $file ) {
$resolvedContents[] = $this->resolveImports (
$file, '_once' === $matches[5 ][$i++ ], $baseDirectory
foreach ( $matches[2 ] as $staticIdentifier ) {
$indent = $matches[3 ][$i];
$eol, $eol . $indent, $resolvedContents[$i++ ]
$staticIdentifier, $resolvedContent, $content, 1
// {{{ private function retrieveContent( $file, $importOnce, $baseDirectory )
* Loads the file's content to be imported
* Avoids duplication of files being imported with <kbd>include_once</kbd> or
* <kbd>require_once</kbd>.
* @param string $file a string representing the file's name to be added
* @param boolean $importOnce boolean true, if the import instruction is either
* <kbd>include_once</kbd> or <kbd>require_once</kbd>; otherwise false
* @param string $baseDirectory a string representing the base path to use for
* resolving the files' imports
* @return string a string representing the new base path to use for resolving
* future files' (relative) imports
* @see resolveImports(), retrieveRealPath()
private function retrieveContent ( $file, $importOnce, $baseDirectory )
$realFile = $this->retrieveRealPath ( $file, $baseDirectory );
if ( in_array( $realFile, $this->imports ) ) {
return 'will not be used';
$this->imports[] = $realFile;
parent ::load( $realFile );
} catch ( scriptReorganizer_Type_Exception $e ) {
$file = $baseDirectory . DIRECTORY_SEPARATOR . $file;
'< import of file ' . $file . ' failed >'
// {{{ private function retrieveRealPath( $file, $baseDirectory )
* Delivers the file's realpath to be imported
* @param string $file a string representing the file's name to be converted
* into a realpath identifier
* @param string $baseDirectory a string representing the base path to add to
* the current include_path directive, if needed
* @return string a string representing the file's real path to be imported
private function retrieveRealPath ( $file, $baseDirectory )
foreach ( explode( PATH_SEPARATOR , $includePaths ) as $includePath ) {
$script = $includePath . DIRECTORY_SEPARATOR . $file;
// {{{ private properties
* Holds the list of already imported file names
* c-hanging-comment-ender-p: nil
Documentation generated on Mon, 11 Mar 2019 14:10:30 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.
|