Extension.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. <?php
  2. namespace Codeception;
  3. use Codeception\Configuration as Config;
  4. use Codeception\Event\SuiteEvent;
  5. use Codeception\Exception\ModuleRequireException;
  6. use Codeception\Lib\Console\Output;
  7. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  8. /**
  9. * A base class for all Codeception Extensions and GroupObjects
  10. *
  11. * Available Properties:
  12. *
  13. * * config: current extension configuration
  14. * * options: passed running options
  15. *
  16. */
  17. abstract class Extension implements EventSubscriberInterface
  18. {
  19. protected $config = [];
  20. protected $options;
  21. protected $output;
  22. protected $globalConfig;
  23. private $modules = [];
  24. public function __construct($config, $options)
  25. {
  26. $this->config = array_merge($this->config, $config);
  27. $this->options = $options;
  28. $this->output = new Output($options);
  29. $this->_initialize();
  30. }
  31. public static function getSubscribedEvents()
  32. {
  33. if (!isset(static::$events)) {
  34. return [Events::SUITE_INIT => 'receiveModuleContainer'];
  35. }
  36. if (isset(static::$events[Events::SUITE_INIT])) {
  37. if (!is_array(static::$events[Events::SUITE_INIT])) {
  38. static::$events[Events::SUITE_INIT] = [[static::$events[Events::SUITE_INIT]]];
  39. }
  40. static::$events[Events::SUITE_INIT][] = ['receiveModuleContainer'];
  41. } else {
  42. static::$events[Events::SUITE_INIT] = 'receiveModuleContainer';
  43. }
  44. return static::$events;
  45. }
  46. public function receiveModuleContainer(SuiteEvent $e)
  47. {
  48. $this->modules = $e->getSuite()->getModules();
  49. }
  50. /**
  51. * Pass config variables that should be injected into global config.
  52. *
  53. * @param array $config
  54. */
  55. public function _reconfigure($config = [])
  56. {
  57. if (is_array($config)) {
  58. Config::append($config);
  59. }
  60. }
  61. /**
  62. * You can do all preparations here. No need to override constructor.
  63. * Also you can skip calling `_reconfigure` if you don't need to.
  64. */
  65. public function _initialize()
  66. {
  67. $this->_reconfigure(); // hook for BC only.
  68. }
  69. protected function write($message)
  70. {
  71. if (!$this->options['silent']) {
  72. $this->output->write($message);
  73. }
  74. }
  75. protected function writeln($message)
  76. {
  77. if (!$this->options['silent']) {
  78. $this->output->writeln($message);
  79. }
  80. }
  81. public function hasModule($name)
  82. {
  83. return isset($this->modules[$name]);
  84. }
  85. public function getCurrentModuleNames()
  86. {
  87. return array_keys($this->modules);
  88. }
  89. public function getModule($name)
  90. {
  91. if (!$this->hasModule($name)) {
  92. throw new ModuleRequireException($name, "module is not enabled");
  93. }
  94. return $this->modules[$name];
  95. }
  96. public function getTestsDir()
  97. {
  98. return Config::testsDir();
  99. }
  100. public function getLogDir()
  101. {
  102. return Config::outputDir();
  103. }
  104. public function getDataDir()
  105. {
  106. return Config::dataDir();
  107. }
  108. public function getRootDir()
  109. {
  110. return Config::projectDir();
  111. }
  112. public function getGlobalConfig()
  113. {
  114. return Config::config();
  115. }
  116. }