Source for file Type.php
Documentation is available at Type.php
/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
* ScriptReorganizer :: Type
* 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: Type.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';
* Throws <kbd>ScriptReorganizer_Type_Exception</kbd>
require_once 'ScriptReorganizer/Type/Exception.php';
* Base class to be extended by (reorganizer) types to use
* All types must follow the naming convention
* <kbd>ScriptReorganizer_Type_<Type></kbd>.
* @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->strategy = $strategy;
$this->endOfLineIdentifiers = array (
'win' => "\r\n", 'unix' => "\n", 'mac' => "\r"
// {{{ public function __destruct()
unset ( $this->strategy );
// {{{ 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 )
if ( false === $content ) {
'File ' . $file . ' is not readable'
$this->initializeIdentifiers ( $eol );
if ( $eol != $this->endOfLine ) {
$content = str_replace( $eol, $this->endOfLine, $content );
if ( preg_match( $this->hashBangIdentifier, $content, $match ) ) {
if ( !$this->hashBang ) {
$this->hashBang = $match[1 ];
$result = trim( $content );
// {{{ public function reformat()
* Reorganizes the script's content by applying the chosen
* {@link ScriptReorganizer_Strategy Strategy}
$this->maskHeredocs ( $content );
$content = trim( $this->strategy->reformat( $content, $this->endOfLine ) );
$this->unmaskHeredocs ( $content );
// {{{ public function save( $file )
* Saves the reorganized script's content to disk
* @param string $file a string representing the file's name to save
* @throws {@link ScriptReorganizer_Type_Exception ScriptReorganizer_Type_Exception}
public function save( $file )
$content = $this->hashBang;
$content .= '<?php' . $this->endOfLine . $this->endOfLine . $this->_getContent()
. $this->endOfLine . $this->endOfLine . '?>';
'File ' . $file . ' is not writable'
// {{{ protected function getEolIdentifier( & $content )
* Detects the currently used end-of-line identifier
* @param string &$content a string representing the script's content
* @return string a string representing the end-of-line identifier found in the
* @since Method available sind Release 0.3.0
foreach ( $this->endOfLineIdentifiers as $eol ) {
if ( false !== strpos( $content, $eol ) ) {
// {{{ package function _getContent()
* Gets the script's content currently being reorganized
* @visibility package restricted
* @return string a string representing the script's content
// {{{ package function _setContent( $content )
* Sets the script's content currently being reorganized
* @visibility package restricted
* @param string $content a string representing the content's replacement
$this->content = $content;
// {{{ private function initializeIdentifiers( $eol )
* Sets the values of internal identifiers for future use
* @param string $eol a string representing an end-of-line identifier
* @since Method available sind Release 0.3.0
private function initializeIdentifiers ( $eol )
if ( !$this->endOfLine ) {
$this->hashBangIdentifier = '"^[ \t' . $eol . ']*(\#\![^' . $eol . ']+'
$heredocs = '"([<]{3}[ \t]*(\w+)[' . $eol . ']';
$heredocs .= '(.|[' . $eol . '])+?\2;?)[' . $eol . ']"';
$this->heredocsIdentifier = $heredocs;
// {{{ private function maskHeredocs( & $content )
* Hides Heredoc strings before the reorganization process
* @param string &$content a string representing the script's content
* @see unmaskHeredocs(), reformat()
* @since Method available since Release 0.2.1
private function maskHeredocs ( & $content )
if ( preg_match_all( $this->heredocsIdentifier, $content, $this->heredocs ) ) {
foreach ( $this->heredocs[1 ] as $heredoc ) {
$heredoc, '< Heredoc ' . $i++ . ' >', $content
preg_match( '"^[<]{3}[ \t]*(\w+)"', $heredoc, $identifier );
$heredocIndent = '"[' . $this->endOfLine . ']([ \t]+)' . $identifier[1 ] . ';?$"';
if ( preg_match( $heredocIndent, $heredoc, $indent ) ) {
$this->endOfLine . $indent[1 ], $this->endOfLine, $heredoc
// {{{ private function unmaskHeredocs( & $content )
* Unhides Heredoc strings after the reorganization process
* @param string &$content a string representing the script's content
* @see maskHeredocs(), reformat()
* @since Method available since Release 0.2.1
private function unmaskHeredocs ( & $content )
foreach ( $this->heredocs[1 ] as $heredoc ) {
$hd = '< Heredoc ' . $i++ . ' >';
$trailingSpace = false !== strpos( $content, $hd . ' ' );
$hd . ( $trailingSpace ? ' ' : '' ),
$heredoc . ( $trailingSpace ? $this->endOfLine : '' ), $content
// {{{ private properties
* Holds the script's content currently being reorganized
* Holds the end-of-line identifier currently being used
* Holds the end-of-line identifiers of known OSes
private $endOfLineIdentifiers = null;
* Holds the first found hash-bang directive
* Holds the regular expression for the unices' has-bang directive
private $hashBangIdentifier = '';
* Holds the list of Heredoc strings to un-/mask
private $heredocs = null;
* Holds the regular expression for Heredoc strings
private $heredocsIdentifier = '';
* Holds the strategy to apply
* @var ScriptReorganizer_Strategy
private $strategy = null;
* 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.
|