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: 0.4.0
  45. * @link http://pear.php.net/package/ScriptReorganizer
  46. * @since Class available since Release 0.4.0
  47. */
  48. class ScriptReorganizer_Factory
  49. {
  50. // {{{ public static function create( $type, $strategy [, $decorator ...] )
  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. $argument = is_array( $strategy ) && isset( $strategy[1] ) ? $strategy[1] : null;
  77. $strategy = new $classes[1]( $argument );
  78. $type = new $classes[0]( $strategy );
  79. for ( $i = count( $arguments ) - 1; $i > 1; $i-- ) {
  80. $argument = is_array( $arguments[$i] ) && isset( $arguments[$i][1] ) ? $arguments[$i][1] : null;
  81. $type = new $classes[$i]( $type, $argument );
  82. }
  83. return $type;
  84. }
  85. // }}}
  86. // {{{ private static function & importAndCheck( & $arguments )
  87. /**
  88. * Checks that the required classes are available after having imported them
  89. *
  90. * @param array &$arguments an array holding the arguments to process
  91. * @return array an array holding the classes' names that have been imported
  92. * @throws {@link ScriptReorganizer_Factory_Exception ScriptReorganizer_Factory_Exception}
  93. */
  94. private static function & importAndCheck( & $arguments )
  95. {
  96. $classes = array();
  97. foreach ( $arguments as $argument ) {
  98. if ( !is_string( $argument ) && !is_array( $argument ) || empty( $argument ) ) {
  99. throw new ScriptReorganizer_Factory_Exception(
  100. 'Argument(s) either not of type string/array or empty'
  101. );
  102. }
  103. if ( is_array( $argument ) ) {
  104. if ( !isset( $argument[0] ) || !is_string( $argument[0] ) || empty( $argument[0] ) ) {
  105. throw new ScriptReorganizer_Factory_Exception(
  106. 'Array argument(s) either not of type string or empty'
  107. );
  108. }
  109. $argument = $argument[0];
  110. }
  111. $classes[] = $argument;
  112. }
  113. $classes[0] = 'ScriptReorganizer_Type_' . $classes[0];
  114. $classes[1] = 'ScriptReorganizer_Strategy_' . $classes[1];
  115. for ( $i = 2, $j = count( $classes ); $i < $j; $i++ ) {
  116. $classes[$i] = 'ScriptReorganizer_Type_Decorator_' . $classes[$i];
  117. }
  118. $root = realpath( dirname( __FILE__ ) . '/..' ) . DIRECTORY_SEPARATOR;
  119. foreach ( $classes as $class ) {
  120. @include_once $root . str_replace( '_', DIRECTORY_SEPARATOR, $class ) . '.php';
  121. if ( !class_exists( $class ) ) {
  122. throw new ScriptReorganizer_Factory_Exception(
  123. 'Class ' . $class . ' not found'
  124. );
  125. }
  126. }
  127. return $classes;
  128. }
  129. // }}}
  130.  
  131. }
  132.  
  133. /*
  134. * Local variables:
  135. * tab-width: 4
  136. * c-basic-offset: 4
  137. * c-hanging-comment-ender-p: nil
  138. * End:
  139. */
  140.  
  141. ?>

Documentation generated on Tue, 22 Nov 2005 01:57:09 +0100 by phpDocumentor 1.3.0RC3