LimeAutoloader.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. <?php
  2. /*
  3. * This file is part of the Lime framework.
  4. *
  5. * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
  6. * (c) Bernhard Schussek <bernhard.schussek@symfony-project.com>
  7. *
  8. * This source file is subject to the MIT license that is bundled
  9. * with this source code in the file LICENSE.
  10. */
  11. // PHP_VERSION_ID is available as of PHP 5.2.7, if our
  12. // version is lower than that, then emulate it
  13. if(!defined('PHP_VERSION_ID'))
  14. {
  15. $version = explode('.',PHP_VERSION);
  16. define('PHP_VERSION_ID', ($version[0] * 10000 + $version[1] * 100 + $version[2]));
  17. }
  18. /**
  19. * LimeAutoloader is an autoloader for the lime test framework classes.
  20. *
  21. * Use the method register() to activate autoloading for all classes of this
  22. * component.
  23. *
  24. * <code>
  25. * include 'path/to/LimeAutoloader.php';
  26. * LimeAutoloader::register();
  27. * </code>
  28. *
  29. * Bundled with this component comes a backwards compatibility layer that
  30. * offers class and method signatures of lime 1.0 (lime_test, lime_harness etc.).
  31. * To activate this layer, call the method LimeAutoloader::enableLegacyMode()
  32. * anytime before using any of the old class names in your code.
  33. *
  34. * <code>
  35. * include 'path/to/LimeAutoloader.php';
  36. * LimeAutoloader::register();
  37. * LimeAutoloader::enableLegacyMode();
  38. * </code>
  39. *
  40. * @package symfony
  41. * @subpackage lime
  42. * @author Fabien Potencier <fabien.potencier@symfony-project.com>
  43. * @author Bernhard Schussek <bernhard.schussek@symfony-project.com>
  44. * @version SVN: $Id: LimeAutoloader.php 24189 2009-11-20 11:29:03Z bschussek $
  45. */
  46. class LimeAutoloader
  47. {
  48. static protected
  49. $isLegacyMode = false,
  50. $isRegistered = false;
  51. /**
  52. * Enables a backwards compatibility layer to allow use of old class names
  53. * such as lime_test, lime_output etc.
  54. */
  55. static public function enableLegacyMode()
  56. {
  57. self::$isLegacyMode = true;
  58. }
  59. /**
  60. * Registers LimeAutoloader as an SPL autoloader.
  61. */
  62. static public function register()
  63. {
  64. if (!self::$isRegistered)
  65. {
  66. ini_set('unserialize_callback_func', 'spl_autoload_call');
  67. spl_autoload_register(array(new self, 'autoload'));
  68. self::$isRegistered = true;
  69. }
  70. }
  71. /**
  72. * Handles autoloading of classes.
  73. *
  74. * @param string $class A class name.
  75. *
  76. * @return boolean Returns true if the class has been loaded
  77. */
  78. public function autoload($class)
  79. {
  80. // backwards compatibility
  81. if (0 === strpos($class, 'lime_') && self::$isLegacyMode)
  82. {
  83. require_once dirname(__FILE__).'/lime.php';
  84. return true;
  85. }
  86. if (0 === strpos($class, 'Lime'))
  87. {
  88. $file = dirname(__FILE__).'/';
  89. if (0 === strpos($class, 'LimeExpectation'))
  90. {
  91. $file .= 'expectation/';
  92. }
  93. else if (0 === strpos($class, 'LimeLexer'))
  94. {
  95. $file .= 'lexer/';
  96. }
  97. else if (0 === strpos($class, 'LimeParser'))
  98. {
  99. $file .= 'parser/';
  100. }
  101. else if (0 === strpos($class, 'LimeOutput'))
  102. {
  103. $file .= 'output/';
  104. }
  105. else if (0 === strpos($class, 'LimeMockInvocationMatcher'))
  106. {
  107. $file .= 'mock/matcher/';
  108. }
  109. else if (0 === strpos($class, 'LimeMock'))
  110. {
  111. $file .= 'mock/';
  112. }
  113. else if (0 === strpos($class, 'LimeTester'))
  114. {
  115. $file .= 'tester/';
  116. }
  117. else if (0 === strpos($class, 'LimeShell'))
  118. {
  119. $file .= 'shell/';
  120. }
  121. else if (0 === strpos($class, 'LimeConstraint'))
  122. {
  123. $file .= 'constraint/';
  124. }
  125. $file .= $class.'.php';
  126. if (file_exists($file))
  127. {
  128. require_once $file;
  129. return true;
  130. }
  131. }
  132. return false;
  133. }
  134. }
  135. /**
  136. * Prints the given value to the error stream in a nicely formatted way.
  137. *
  138. * @param mixed $value
  139. */
  140. function lime_debug($value)
  141. {
  142. $result = "";
  143. if (is_object($value) || is_array($value))
  144. {
  145. $result = is_object($value) ? sprintf("object(%s) (\n", get_class($value)) : "array (";
  146. if (is_object($value))
  147. {
  148. $value = LimeTesterObject::toArray($value);
  149. }
  150. foreach ($value as $key => $val)
  151. {
  152. if (is_object($val) || is_array($val))
  153. {
  154. $output = is_object($val) ? sprintf("object(%s) (", get_class($val)) : "array (";
  155. if (is_object($val))
  156. {
  157. $val = LimeTesterObject::toArray($val);
  158. }
  159. if (count($val) > 0)
  160. {
  161. $output .= "\n ...\n ";
  162. }
  163. $output .= ")";
  164. }
  165. else
  166. {
  167. if (is_string($val) && strlen($val) > 60)
  168. {
  169. $val = substr($val, 0, 57).'...';
  170. }
  171. $output = lime_colorize($val);
  172. }
  173. $result .= sprintf(" %s => %s,\n", var_export($key, true), $output);
  174. }
  175. $result .= ")";
  176. }
  177. else
  178. {
  179. $result = lime_colorize($value);
  180. }
  181. fwrite(STDERR, $result."\n");
  182. }
  183. /**
  184. * Returns a colorized export of the given value depending on its type.
  185. *
  186. * @param mixed $value
  187. * @return string
  188. */
  189. function lime_colorize($value)
  190. {
  191. static $colorizer = null;
  192. if (is_null($colorizer) && LimeColorizer::isSupported())
  193. {
  194. $colorizer = new LimeColorizer();
  195. $colorizer->setStyle('string', array('fg' => 'cyan'));
  196. $colorizer->setStyle('integer', array('fg' => 'green'));
  197. $colorizer->setStyle('double', array('fg' => 'green'));
  198. $colorizer->setStyle('boolean', array('fg' => 'red'));
  199. }
  200. $type = gettype($value);
  201. $value = var_export($value, true);
  202. if (!is_null($colorizer) && in_array($type, array('string', 'integer', 'double', 'boolean')))
  203. {
  204. $value = $colorizer->colorize($value, $type);
  205. }
  206. return $value;
  207. }