PageRenderTime 44ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/application/libraries/Engine/Sanity.php

https://github.com/shopaholiccompany/shopaholic
PHP | 221 lines | 155 code | 44 blank | 22 comment | 17 complexity | 7706111baf9674ca90cc0b0ef40d933e MD5 | raw file
Possible License(s): BSD-3-Clause, GPL-3.0, LGPL-2.1
  1. <?php
  2. /**
  3. * SocialEngine
  4. *
  5. * @category Engine
  6. * @package Engine_Sanity
  7. * @copyright Copyright 2006-2010 Webligo Developments
  8. * @license http://www.socialengine.net/license/
  9. * @version $Id: Sanity.php 7244 2010-09-01 01:49:53Z john $
  10. * @author John Boehr <j@webligo.com>
  11. */
  12. /**
  13. * @category Engine
  14. * @package Engine_Sanity
  15. * @copyright Copyright 2006-2010 Webligo Developments
  16. * @license http://www.socialengine.net/license/
  17. * @author John Boehr <j@webligo.com>
  18. */
  19. class Engine_Sanity implements Engine_Sanity_Test_Interface
  20. {
  21. const ERROR_NONE = 0;
  22. const ERROR_NOTICE = 1;
  23. const ERROR_WARNING = 2;
  24. const ERROR_ERROR = 4;
  25. static protected $_defaultTranslator;
  26. static protected $_defaultDbAdapter;
  27. protected $_tests;
  28. // General
  29. public function __construct($options = null)
  30. {
  31. if( is_array($options) ) {
  32. $this->setOptions($options);
  33. }
  34. }
  35. public function setOptions(array $options)
  36. {
  37. foreach( $options as $key => $value)
  38. {
  39. $method = 'set' . ucfirst($key);
  40. if( method_exists($this, $method) ) {
  41. $this->$method($value);
  42. }
  43. }
  44. return $this;
  45. }
  46. public function getType()
  47. {
  48. return 'collection';
  49. }
  50. public function setName($name)
  51. {
  52. $this->_name = $name;
  53. return $this;
  54. }
  55. public function getName()
  56. {
  57. if( null === $this->_name ) {
  58. $translate = Engine_Sanity::getDefaultTranslator();
  59. if( $translate ) {
  60. $this->_name = $translate->_(strtolower(get_class($this)) . '_name');
  61. } else {
  62. $this->_name = $this->getType();
  63. }
  64. }
  65. return $this->_name;
  66. }
  67. // Run stuff
  68. public function run()
  69. {
  70. foreach( (array) $this->getTests() as $test )
  71. {
  72. $test->execute();
  73. }
  74. return $this;
  75. }
  76. public function execute()
  77. {
  78. $this->run();
  79. return $this;
  80. }
  81. // Tests
  82. public function addTest($spec, $options = array())
  83. {
  84. if( $spec instanceof Engine_Sanity_Test_Interface ) {
  85. $test = $spec;
  86. } else if( is_string($spec) ) {
  87. $class = 'Engine_Sanity_Test_' . ucfirst($spec);
  88. Engine_Loader::loadClass($class);
  89. $test = new $class($options);
  90. } else if( is_array($spec) ) {
  91. if( !empty($spec['type']) ) {
  92. $class = 'Engine_Sanity_Test_' . ucfirst($spec['type']);
  93. unset($spec['type']);
  94. } else if( !empty($spec['class']) ) {
  95. $class = $spec['class'];
  96. unset($spec['class']);
  97. } else {
  98. throw new Engine_Sanity_Exception('No type or class specified for test');
  99. }
  100. Engine_Loader::loadClass($class);
  101. $options = array_merge($spec, $options);
  102. $test = new $class($options);
  103. }
  104. if( !($test instanceof Engine_Sanity_Test_Interface ) ) {
  105. throw new Engine_Sanity_Exception('Test must be an instance of Engine_Sanity_Test_Abstract');
  106. }
  107. $this->_tests[] = $test;
  108. return $this;
  109. }
  110. public function addTests(array $tests)
  111. {
  112. foreach( $tests as $key => $value ) {
  113. if( is_numeric($key) ) {
  114. $this->addTest($value);
  115. } else {
  116. $this->addTest($key, $value);
  117. }
  118. }
  119. return $this;
  120. }
  121. public function clearTests()
  122. {
  123. $this->_tests = array();
  124. return $this;
  125. }
  126. public function getTests()
  127. {
  128. return $this->_tests;
  129. }
  130. public function setTests(array $tests)
  131. {
  132. $this->addTests($tests);
  133. return $this;
  134. }
  135. // Messages
  136. public function getMaxErrorLevel()
  137. {
  138. $maxErrorLevel = Engine_Sanity::ERROR_NONE;
  139. foreach( (array) $this->getTests() as $test ) {
  140. $maxErrorLevel = max($maxErrorLevel, $test->getMaxErrorLevel());
  141. }
  142. return $maxErrorLevel;
  143. }
  144. public function getMessages()
  145. {
  146. $messages = array();
  147. foreach( (array) $this->getTests() as $test ) {
  148. $testMessages = array();
  149. foreach( (array) $test->getMessages() as $testMessage ) {
  150. $testMessages[] = $testMessage->toString();
  151. }
  152. $messages[$test->getName()] = $testMessages;
  153. }
  154. return $messages;
  155. }
  156. public function hasMessages()
  157. {
  158. return count($this->getMessages() > 0);
  159. }
  160. // Translation
  161. static public function setDefaultTranslator(Zend_Translate $translate)
  162. {
  163. self::$_defaultTranslator = $translate;
  164. }
  165. static public function getDefaultTranslator()
  166. {
  167. return self::$_defaultTranslator;
  168. }
  169. static public function setDefaultDbAdapter(Zend_Db_Adapter_Abstract $dbAdapter)
  170. {
  171. self::$_defaultDbAdapter = $dbAdapter;
  172. }
  173. static public function getDefaultDbAdapter()
  174. {
  175. return self::$_defaultDbAdapter;
  176. }
  177. }