123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166 |
- <?php
- class sfBrowser extends sfBrowserBase
- {
- protected
- $listeners = array(),
- $context = null,
- $currentException = null;
-
- protected function doCall()
- {
-
- $this->context = $this->getContext(true);
- sfConfig::set('sf_test', true);
-
- sfConfig::set('sf_rendering_filter', array('sfFakeRenderingFilter', null));
- $this->resetCurrentException();
-
- ob_start();
- $this->context->getController()->dispatch();
- $retval = ob_get_clean();
-
- $this->context->getResponse()->setContent($retval);
-
- if ($this->context->getUser())
- {
- $this->context->getUser()->shutdown();
- $this->context->getStorage()->shutdown();
- }
- }
-
- public function getContext($forceReload = false)
- {
- if (is_null($this->context) || $forceReload)
- {
- $isContextEmpty = is_null($this->context);
- $context = $isContextEmpty ? sfContext::getInstance() : $this->context;
- $currentConfiguration = $context->getConfiguration();
- $configuration = ProjectConfiguration::getApplicationConfiguration($currentConfiguration->getApplication(), $currentConfiguration->getEnvironment(), $currentConfiguration->isDebug());
- $this->context = sfContext::createInstance($configuration);
- unset($currentConfiguration);
- if (!$isContextEmpty)
- {
- sfConfig::clear();
- sfConfig::add($this->rawConfiguration);
- }
- else
- {
- $this->rawConfiguration = sfConfig::getAll();
- }
- $this->context->getEventDispatcher()->connect('application.throw_exception', array($this, 'ListenToException'));
- foreach ($this->listeners as $name => $listener)
- {
- $this->context->getEventDispatcher()->connect($name, $listener);
- }
- }
- return $this->context;
- }
- public function addListener($name, $listener)
- {
- $this->listeners[$name] = $listener;
- }
-
- public function getResponse()
- {
- return $this->context->getResponse();
- }
-
- public function getRequest()
- {
- return $this->context->getRequest();
- }
-
- public function getUser()
- {
- return $this->context->getUser();
- }
-
- public function shutdown()
- {
- parent::shutdown();
-
- sfToolkit::clearDirectory(sfConfig::get('sf_test_cache_dir').'/sessions');
- }
-
- public function listenToException(sfEvent $event)
- {
- $this->setCurrentException($event->getSubject());
- }
- }
- class sfFakeRenderingFilter extends sfFilter
- {
- public function execute($filterChain)
- {
- $filterChain->execute();
- $this->context->getResponse()->sendContent();
- }
- }
|