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

Source for file Factory.php

Documentation is available at Factory.php

  1. <?php
  2.  
  3. /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
  4.  
  5. /**
  6.  * ScriptReorganizer :: Factory
  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$
  21.  * @link       http://pear.php.net/package/ScriptReorganizer
  22.  * @since      File available since Release 0.4.0
  23.  * @filesource
  24.  */
  25.  
  26. /**
  27.  * Throws <kbd>ScriptReorganizer_Factory_Exception</kbd>
  28.  */
  29. require_once 'ScriptReorganizer/Factory/Exception.php';
  30.  
  31. /**
  32.  * Throws <kbd>ScriptReorganizer_Type_Decorator_Exception</kbd>
  33.  */
  34. require_once 'ScriptReorganizer/Type/Decorator/Exception.php';
  35.  
  36. /**
  37.  * Factory/Facade for easy <kbd>ScriptReorganizer_Type</kbd> object creation
  38.  *
  39.  * @category  Tools
  40.  * @package   ScriptReorganizer
  41.  * @author    Stefano F. Rausch <stefano@rausch-e.net>
  42.  * @copyright 2005 Stefano F. Rausch <stefano@rausch-e.net>
  43.  * @license   http://www.gnu.org/copyleft/lesser.html LGPL License 2.1
  44.  * @version   Release: @package_version@
  45.  * @link      http://pear.php.net/package/ScriptReorganizer
  46.  * @since     Class available since Release 0.4.0
  47.  */
  48. {
  49.     // {{{ public static function create( $type, $strategy [, $decorator ...] )
  50.     
  51.     /**
  52.      * Creates on object according to the specifications supplied classes-wise
  53.      *
  54.      * The names of the classes declared - the last differentiating part - are
  55.      * case-sensitive. E.g., <kbd>ScriptReorganizer_Type_Script</kbd> has to be
  56.      * denoted as 'Script' - on Windows boxes the case does not matter.
  57.      *
  58.      * @param  mixed $type a string or an array representing the name of the
  59.      *          <kbd>ScriptReorganizer_Type</kbd> to instantiate
  60.      * @param  mixed $strategy a string representing the name or an array
  61.      *          representing the name and the optional argument of the
  62.      *          <kbd>ScriptReorganizer_Strategy</kbd> to instantiate
  63.      * @param  mixed $decorator a variable-length argument list of strings
  64.      *          representing the names and/or of arrays representing the names and the
  65.      *          optional arguments of the <kbd>ScriptReorganizer_Type_Decorator</kbd>s
  66.      *          to instantiate
  67.      * @return ScriptReorganizer_Type the <kbd>ScriptReorganizer_Type</kbd> object
  68.      *          created
  69.      * @throws {@link ScriptReorganizer_Factory_Exception ScriptReorganizer_Factory_Exception}
  70.      * @throws {@link ScriptReorganizer_Type_Decorator_Exception ScriptReorganizer_Type_Decorator_Exception}
  71.      */
  72.     public static function create$type$strategy )
  73.     {
  74.         $arguments func_get_args();
  75.         $classes self::importAndCheck$arguments );
  76.         
  77.         $argument is_array$strategy && isset$strategy[1$strategy[1: null;
  78.         $strategy = new $classes[1]$argument );
  79.         
  80.         $type = new $classes[0]$strategy );
  81.         
  82.         for $i count$arguments - 1; $i > 1; $i-- {
  83.             $argument is_array$arguments[$i&& isset$arguments[$i][1$arguments[$i][1: null;
  84.             $type = new $classes[$i]$type$argument );
  85.         }
  86.         
  87.         return $type;
  88.     }
  89.     
  90.     // }}}
  91.     
  92.     // {{{ private static function & importAndCheck( & $arguments )
  93.     
  94.     /**
  95.      * Checks that the required classes are available after having imported them
  96.      *
  97.      * @param  array &$arguments an array holding the arguments to process
  98.      * @return array an array holding the classes' names that have been imported
  99.      * @throws {@link ScriptReorganizer_Factory_Exception ScriptReorganizer_Factory_Exception}
  100.      */
  101.     private static function importAndCheck$arguments )
  102.     {
  103.         $classes = array();
  104.         
  105.         foreach $arguments as $argument {
  106.             if !is_string$argument && !is_array$argument || empty$argument ) ) {
  107.                 throw new ScriptReorganizer_Factory_Exception(
  108.                     'Argument(s) either not of type string/array or empty'
  109.                 );
  110.             }
  111.             
  112.             if is_array$argument ) ) {
  113.                 if !isset$argument[0|| !is_string$argument[0|| empty$argument[0) ) {
  114.                     throw new ScriptReorganizer_Factory_Exception(
  115.                         'Array argument(s) either not of type string or empty'
  116.                     );
  117.                 }
  118.                 
  119.                 $argument $argument[0];
  120.             }
  121.             
  122.             $classes[$argument;
  123.         }
  124.         
  125.         $classes[0'ScriptReorganizer_Type_' $classes[0];
  126.         $classes[1'ScriptReorganizer_Strategy_' $classes[1];
  127.         
  128.         for $i = 2$j count$classes )$i $j$i++ {
  129.             $classes[$i'ScriptReorganizer_Type_Decorator_' $classes[$i];
  130.         }
  131.         
  132.         $root realpathdirname__FILE__ '/..' . DIRECTORY_SEPARATOR;
  133.         
  134.         foreach $classes as $class {
  135.             @include_once $root str_replace'_'DIRECTORY_SEPARATOR$class '.php';
  136.             
  137.             if !class_exists$class ) ) {
  138.                 throw new ScriptReorganizer_Factory_Exception(
  139.                     'Class ' $class ' not found'
  140.                 );
  141.             }
  142.         }
  143.         
  144.         return $classes;
  145.     }
  146.     
  147.     // }}}
  148. }
  149.  
  150. /*
  151.  * Local variables:
  152.  * tab-width: 4
  153.  * c-basic-offset: 4
  154.  * c-hanging-comment-ender-p: nil
  155.  * End:
  156.  */
  157.  
  158. ?>

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