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

Source for file Type.php

Documentation is available at Type.php

  1. <?php
  2.  
  3. /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
  4.  
  5. /**
  6.  * ScriptReorganizer :: Type
  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.  * @author     Stefano F. Rausch <stefano@rausch-e.net>
  18.  * @copyright  2005 Stefano F. Rausch <stefano@rausch-e.net>
  19.  * @license    http://www.gnu.org/copyleft/lesser.html LGPL License 2.1
  20.  * @version    SVN: $Id: Type.php 32 2005-10-30 22:05:19Z stefanorausch $
  21.  * @link       http://pear.php.net/package/ScriptReorganizer
  22.  * @filesource
  23.  */
  24.  
  25. /**
  26.  * Depends on <kbd>ScriptReorganizer_Strategy</kbd>
  27.  */
  28. require_once 'ScriptReorganizer/Strategy.php';
  29.  
  30. /**
  31.  * Throws <kbd>ScriptReorganizer_Type_Exception</kbd>
  32.  */
  33. require_once 'ScriptReorganizer/Type/Exception.php';
  34.  
  35. /**
  36.  * Base class to be extended by (reorganizer) types to use
  37.  *
  38.  * All types must follow the naming convention
  39.  * <kbd>ScriptReorganizer_Type_<Type></kbd>.
  40.  *
  41.  * @category  Tools
  42.  * @package   ScriptReorganizer
  43.  * @author    Stefano F. Rausch <stefano@rausch-e.net>
  44.  * @copyright 2005 Stefano F. Rausch <stefano@rausch-e.net>
  45.  * @license   http://www.gnu.org/copyleft/lesser.html LGPL License 2.1
  46.  * @version   Release: @package_version@
  47.  * @link      http://pear.php.net/package/ScriptReorganizer
  48.  */
  49. abstract class ScriptReorganizer_Type
  50. {
  51.     // {{{ public function __construct( ScriptReorganizer_Strategy $strategy )
  52.     
  53.     /**
  54.      * Constructor
  55.      *
  56.      * @param ScriptReorganizer_Strategy $strategy a
  57.      *         <kbd>ScriptReorganizer_Strategy</kbd> to apply
  58.      */
  59.     public function __constructScriptReorganizer_Strategy $strategy )
  60.     {
  61.         $this->strategy $strategy;
  62.         
  63.         $this->endOfLineIdentifiers = array(
  64.             'win' => "\r\n"'unix' => "\n"'mac' => "\r"
  65.         );
  66.     }
  67.     
  68.     // }}}
  69.     // {{{ public function __destruct()
  70.     
  71.     /**
  72.      * Destructor
  73.      */
  74.     public function __destruct()
  75.     {
  76.         unset$this->strategy );
  77.     }
  78.     
  79.     // }}}
  80.     
  81.     // {{{ public function load( $file )
  82.     
  83.     /**
  84.      * Loads the script's content to be reorganized from disk
  85.      *
  86.      * @param  string $file a string representing the file's name to load
  87.      * @return void 
  88.      * @throws {@link ScriptReorganizer_Type_Exception ScriptReorganizer_Type_Exception}
  89.      */
  90.     public function load$file )
  91.     {
  92.         $content @file_get_contents$file );
  93.         
  94.         if false === $content {
  95.             throw new ScriptReorganizer_Type_Exception(
  96.                 'File ' $file ' is not readable'
  97.             );
  98.         }
  99.         
  100.         $eol $this->getEolIdentifier$content );
  101.         
  102.         $this->initializeIdentifiers$eol );
  103.         
  104.         if $eol != $this->endOfLine {
  105.             $content str_replace$eol$this->endOfLine$content );
  106.         }
  107.         
  108.         if preg_match$this->hashBangIdentifier$content$match ) ) {
  109.             $content str_replace$match[0]''$content );
  110.             
  111.             if !$this->hashBang {
  112.                 $this->hashBang $match[1];
  113.             }
  114.         }
  115.         
  116.         $result trim$content );
  117.         $result preg_replace'"^<\?php"'''$result );
  118.         $result preg_replace'"\?>$"'''$result );
  119.         
  120.         $this->_setContent$result );
  121.     }
  122.     
  123.     // }}}
  124.     // {{{ public function reformat()
  125.     
  126.     /**
  127.      * Reorganizes the script's content by applying the chosen
  128.      * {@link ScriptReorganizer_Strategy Strategy}
  129.      *
  130.      * @return void 
  131.      */
  132.     public function reformat()
  133.     {
  134.         $content $this->_getContent();
  135.         
  136.         $this->maskHeredocs$content );
  137.         $content trim$this->strategy->reformat$content$this->endOfLine ) );
  138.         $this->unmaskHeredocs$content );
  139.         
  140.         $this->_setContent$content );
  141.     }
  142.     
  143.     // }}}
  144.     // {{{ public function save( $file )
  145.     
  146.     /**
  147.      * Saves the reorganized script's content to disk
  148.      *
  149.      * @param  string $file a string representing the file's name to save
  150.      * @return void 
  151.      * @throws {@link ScriptReorganizer_Type_Exception ScriptReorganizer_Type_Exception}
  152.      */
  153.     public function save$file )
  154.     {
  155.         $content  $this->hashBang;
  156.         $content .= '<?php' $this->endOfLine $this->endOfLine $this->_getContent()
  157.             . $this->endOfLine $this->endOfLine '?>';
  158.         
  159.         if false === @file_put_contents$file$content ) ) {
  160.             throw new ScriptReorganizer_Type_Exception(
  161.                 'File ' $file ' is not writable'
  162.             );
  163.         }
  164.         
  165.         $this->endOfLine '';
  166.     }
  167.     
  168.     // }}}
  169.     
  170.     // {{{ protected function getEolIdentifier( & $content )
  171.     
  172.     /**
  173.      * Detects the currently used end-of-line identifier
  174.      *
  175.      * @param  string &$content a string representing the script's content
  176.      * @return string a string representing the end-of-line identifier found in the
  177.      *          script's content
  178.      * @since  Method available sind Release 0.3.0
  179.      */
  180.     protected function getEolIdentifier$content )
  181.     {
  182.         foreach $this->endOfLineIdentifiers as $eol {
  183.             if false !== strpos$content$eol ) ) {
  184.                 return $eol;
  185.             }
  186.         }
  187.     }
  188.     
  189.     // }}}
  190.     
  191.     // {{{ package function _getContent()
  192.     
  193.     /**
  194.      * Gets the script's content currently being reorganized
  195.      *
  196.      * @visibility package restricted
  197.      * @return     string a string representing the script's content
  198.      */
  199.     public function _getContent()
  200.     {
  201.         return $this->content;
  202.     }
  203.     
  204.     // }}}
  205.     // {{{ package function _setContent( $content )
  206.     
  207.     /**
  208.      * Sets the script's content currently being reorganized
  209.      *
  210.      * @visibility package restricted
  211.      * @param      string $content a string representing the content's replacement
  212.      * @return     void 
  213.      */
  214.     public function _setContent$content )
  215.     {
  216.         $this->content $content;
  217.     }
  218.     
  219.     // }}}
  220.     
  221.     // {{{ private function initializeIdentifiers( $eol )
  222.     
  223.     /**
  224.      * Sets the values of internal identifiers for future use
  225.      *
  226.      * @param  string $eol a string representing an end-of-line identifier
  227.      * @return void 
  228.      * @since  Method available sind Release 0.3.0
  229.      */
  230.     private function initializeIdentifiers$eol )
  231.     {
  232.         if !$this->endOfLine {
  233.             $this->endOfLine $eol;
  234.             
  235.             $this->hashBangIdentifier '"^[ \t' $eol ']*(\#\![^' $eol ']+'
  236.                  . $eol ')"';
  237.             
  238.             $heredocs  '"([<]{3}[ \t]*(\w+)[' $eol ']';
  239.             $heredocs .= '(.|[' $eol '])+?\2;?)[' $eol ']"';
  240.             
  241.             $this->heredocsIdentifier $heredocs;
  242.         }
  243.     }
  244.     
  245.     // }}}
  246.     // {{{ private function maskHeredocs( & $content )
  247.     
  248.     /**
  249.      * Hides Heredoc strings before the reorganization process
  250.      *
  251.      * @param  string &$content a string representing the script's content
  252.      * @return void 
  253.      * @see    unmaskHeredocs(), reformat()
  254.      * @since  Method available since Release 0.2.1
  255.      */
  256.     private function maskHeredocs$content )
  257.     {
  258.         if preg_match_all$this->heredocsIdentifier$content$this->heredocs ) ) {
  259.             $i = 0;
  260.             
  261.             foreach $this->heredocs[1as $heredoc {
  262.                 $content str_replace(
  263.                     $heredoc'< Heredoc ' $i++ . ' >'$content
  264.                 );
  265.                 
  266.                 preg_match'"^[<]{3}[ \t]*(\w+)"'$heredoc$identifier );
  267.                 $heredocIndent '"[' $this->endOfLine ']([ \t]+)' $identifier[1';?$"';
  268.                 
  269.                 if preg_match$heredocIndent$heredoc$indent ) ) {
  270.                     $this->heredocs[1][$i-1str_replace(
  271.                         $this->endOfLine $indent[1]$this->endOfLine$heredoc
  272.                     );
  273.                 }
  274.             }
  275.         }
  276.     }
  277.     
  278.     // }}}
  279.     // {{{ private function unmaskHeredocs( & $content )
  280.     
  281.     /**
  282.      * Unhides Heredoc strings after the reorganization process
  283.      *
  284.      * @param  string &$content a string representing the script's content
  285.      * @return void 
  286.      * @see    maskHeredocs(), reformat()
  287.      * @since  Method available since Release 0.2.1
  288.      */
  289.     private function unmaskHeredocs$content )
  290.     {
  291.         $i = 0;
  292.         
  293.         foreach $this->heredocs[1as $heredoc {
  294.             $hd '< Heredoc ' $i++ . ' >';
  295.             $trailingSpace = false !== strpos$content$hd ' ' );
  296.             
  297.             $content str_replace(
  298.                 $hd $trailingSpace ' ' '' ),
  299.                 $heredoc $trailingSpace $this->endOfLine '' )$content
  300.             );
  301.         }
  302.     }
  303.     
  304.     // }}}
  305.     
  306.     // {{{ private properties
  307.     
  308.     /**
  309.      * Holds the script's content currently being reorganized
  310.      *
  311.      * @var string 
  312.      */
  313.     private $content '';
  314.     
  315.     /**
  316.      * Holds the end-of-line identifier currently being used
  317.      *
  318.      * @var string 
  319.      */
  320.     private $endOfLine '';
  321.     
  322.     /**
  323.      * Holds the end-of-line identifiers of known OSes
  324.      *
  325.      * @var array 
  326.      */
  327.     private $endOfLineIdentifiers = null;
  328.     
  329.     /**
  330.      * Holds the first found hash-bang directive
  331.      *
  332.      * @var string 
  333.      */
  334.     private $hashBang '';
  335.     
  336.     /**
  337.      * Holds the regular expression for the unices' has-bang directive
  338.      *
  339.      * @var string 
  340.      */
  341.     private $hashBangIdentifier '';
  342.     
  343.     /**
  344.      * Holds the list of Heredoc strings to un-/mask
  345.      *
  346.      * @var array 
  347.      */
  348.     private $heredocs = null;
  349.     
  350.     /**
  351.      * Holds the regular expression for Heredoc strings
  352.      *
  353.      * @var string 
  354.      */
  355.     private $heredocsIdentifier '';
  356.     
  357.     /**
  358.      * Holds the strategy to apply
  359.      *
  360.      * @var ScriptReorganizer_Strategy 
  361.      */
  362.     private $strategy = null;
  363.     
  364.     // }}}
  365. }
  366.  
  367. /*
  368.  * Local variables:
  369.  * tab-width: 4
  370.  * c-basic-offset: 4
  371.  * c-hanging-comment-ender-p: nil
  372.  * End:
  373.  */
  374.  
  375. ?>

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