| 1 |
<?php |
|---|
| 2 |
|
|---|
| 3 |
|
|---|
| 4 |
|
|---|
| 5 |
|
|---|
| 6 |
|
|---|
| 7 |
|
|---|
| 8 |
|
|---|
| 9 |
|
|---|
| 10 |
|
|---|
| 11 |
|
|---|
| 12 |
|
|---|
| 13 |
|
|---|
| 14 |
|
|---|
| 15 |
|
|---|
| 16 |
|
|---|
| 17 |
|
|---|
| 18 |
|
|---|
| 19 |
class sfBrowser extends sfBrowserBase |
|---|
| 20 |
{ |
|---|
| 21 |
protected |
|---|
| 22 |
$listeners = array(), |
|---|
| 23 |
$context = null, |
|---|
| 24 |
$currentException = null; |
|---|
| 25 |
|
|---|
| 26 |
|
|---|
| 27 |
* Calls a request to a uri. |
|---|
| 28 |
*/ |
|---|
| 29 |
protected function doCall() |
|---|
| 30 |
{ |
|---|
| 31 |
|
|---|
| 32 |
$this->context = $this->getContext(true); |
|---|
| 33 |
|
|---|
| 34 |
sfConfig::set('sf_test', true); |
|---|
| 35 |
|
|---|
| 36 |
|
|---|
| 37 |
sfConfig::set('sf_rendering_filter', array('sfFakeRenderingFilter', null)); |
|---|
| 38 |
|
|---|
| 39 |
$this->resetCurrentException(); |
|---|
| 40 |
|
|---|
| 41 |
|
|---|
| 42 |
ob_start(); |
|---|
| 43 |
$this->context->getController()->dispatch(); |
|---|
| 44 |
$retval = ob_get_clean(); |
|---|
| 45 |
|
|---|
| 46 |
|
|---|
| 47 |
$this->context->getResponse()->setContent($retval); |
|---|
| 48 |
|
|---|
| 49 |
|
|---|
| 50 |
if ($this->context->getUser()) |
|---|
| 51 |
{ |
|---|
| 52 |
$this->context->getUser()->shutdown(); |
|---|
| 53 |
$this->context->getStorage()->shutdown(); |
|---|
| 54 |
} |
|---|
| 55 |
} |
|---|
| 56 |
|
|---|
| 57 |
|
|---|
| 58 |
* Returns the current application context. |
|---|
| 59 |
* |
|---|
| 60 |
* @param bool $forceReload true to force context reload, false otherwise |
|---|
| 61 |
* |
|---|
| 62 |
* @return sfContext |
|---|
| 63 |
*/ |
|---|
| 64 |
public function getContext($forceReload = false) |
|---|
| 65 |
{ |
|---|
| 66 |
if (null === $this->context || $forceReload) |
|---|
| 67 |
{ |
|---|
| 68 |
$isContextEmpty = null === $this->context; |
|---|
| 69 |
$context = $isContextEmpty ? sfContext::getInstance() : $this->context; |
|---|
| 70 |
|
|---|
| 71 |
|
|---|
| 72 |
$currentConfiguration = $context->getConfiguration(); |
|---|
| 73 |
$configuration = ProjectConfiguration::getApplicationConfiguration($currentConfiguration->getApplication(), $currentConfiguration->getEnvironment(), $currentConfiguration->isDebug()); |
|---|
| 74 |
|
|---|
| 75 |
|
|---|
| 76 |
$configuration->getEventDispatcher()->connect('application.throw_exception', array($this, 'listenToException')); |
|---|
| 77 |
foreach ($this->listeners as $name => $listener) |
|---|
| 78 |
{ |
|---|
| 79 |
$configuration->getEventDispatcher()->connect($name, $listener); |
|---|
| 80 |
} |
|---|
| 81 |
|
|---|
| 82 |
|
|---|
| 83 |
$this->context = sfContext::createInstance($configuration); |
|---|
| 84 |
unset($currentConfiguration); |
|---|
| 85 |
|
|---|
| 86 |
if (!$isContextEmpty) |
|---|
| 87 |
{ |
|---|
| 88 |
sfConfig::clear(); |
|---|
| 89 |
sfConfig::add($this->rawConfiguration); |
|---|
| 90 |
} |
|---|
| 91 |
else |
|---|
| 92 |
{ |
|---|
| 93 |
$this->rawConfiguration = sfConfig::getAll(); |
|---|
| 94 |
} |
|---|
| 95 |
} |
|---|
| 96 |
|
|---|
| 97 |
return $this->context; |
|---|
| 98 |
} |
|---|
| 99 |
|
|---|
| 100 |
public function addListener($name, $listener) |
|---|
| 101 |
{ |
|---|
| 102 |
$this->listeners[$name] = $listener; |
|---|
| 103 |
} |
|---|
| 104 |
|
|---|
| 105 |
|
|---|
| 106 |
* Gets response. |
|---|
| 107 |
* |
|---|
| 108 |
* @return sfWebResponse |
|---|
| 109 |
*/ |
|---|
| 110 |
public function getResponse() |
|---|
| 111 |
{ |
|---|
| 112 |
return $this->context->getResponse(); |
|---|
| 113 |
} |
|---|
| 114 |
|
|---|
| 115 |
|
|---|
| 116 |
* Gets request. |
|---|
| 117 |
* |
|---|
| 118 |
* @return sfWebRequest |
|---|
| 119 |
*/ |
|---|
| 120 |
public function getRequest() |
|---|
| 121 |
{ |
|---|
| 122 |
return $this->context->getRequest(); |
|---|
| 123 |
} |
|---|
| 124 |
|
|---|
| 125 |
|
|---|
| 126 |
* Gets user. |
|---|
| 127 |
* |
|---|
| 128 |
* @return sfUser |
|---|
| 129 |
*/ |
|---|
| 130 |
public function getUser() |
|---|
| 131 |
{ |
|---|
| 132 |
return $this->context->getUser(); |
|---|
| 133 |
} |
|---|
| 134 |
|
|---|
| 135 |
|
|---|
| 136 |
* Shutdown function to clean up and remove sessions |
|---|
| 137 |
* |
|---|
| 138 |
* @return void |
|---|
| 139 |
*/ |
|---|
| 140 |
public function shutdown() |
|---|
| 141 |
{ |
|---|
| 142 |
parent::shutdown(); |
|---|
| 143 |
|
|---|
| 144 |
|
|---|
| 145 |
sfToolkit::clearDirectory(sfConfig::get('sf_test_cache_dir').'/sessions'); |
|---|
| 146 |
} |
|---|
| 147 |
|
|---|
| 148 |
|
|---|
| 149 |
* Listener for exceptions |
|---|
| 150 |
* |
|---|
| 151 |
* @param sfEvent $event The event to handle |
|---|
| 152 |
* |
|---|
| 153 |
* @return void |
|---|
| 154 |
*/ |
|---|
| 155 |
public function listenToException(sfEvent $event) |
|---|
| 156 |
{ |
|---|
| 157 |
$this->setCurrentException($event->getSubject()); |
|---|
| 158 |
} |
|---|
| 159 |
} |
|---|
| 160 |
|
|---|
| 161 |
class sfFakeRenderingFilter extends sfFilter |
|---|
| 162 |
{ |
|---|
| 163 |
public function execute($filterChain) |
|---|
| 164 |
{ |
|---|
| 165 |
$filterChain->execute(); |
|---|
| 166 |
|
|---|
| 167 |
$this->context->getResponse()->sendContent(); |
|---|
| 168 |
} |
|---|
| 169 |
} |
|---|
| 170 |
|
|---|