function registry($key, $object = null) { if ($object !== null) { return Registry::set($object, $key); } return Registry::get($key); }
/** * Set the view rendering layer. */ public function initialize() { parent::initialize(); /** @type \Titon\View\View $view */ $view = Registry::get('titon.view'); $view->addPath($this->getModule()->getViewPath()); $this->setView($view); }
/** * Test that register() and get() lazy load callbacks. */ public function testRegisterAndGet() { Registry::register('base', function () { return new Base(['key' => 'registry']); }); $object = Registry::get('base'); $this->assertInstanceOf('Titon\\Common\\Base', $object); $this->assertEquals('registry', $object->getConfig('key')); $this->assertInstanceOf('Titon\\Common\\Base', Registry::get('base')); try { Registry::get('missingKey'); $this->assertTrue(false); } catch (Exception $e) { $this->assertTrue(true); } }
/** * Default mechanism for handling uncaught exceptions. * Will fetch the current controller instance or instantiate an ErrorController. * The error view template will be rendered. * * @uses Titon\Debug\Debugger * * @param \Exception $exception */ public function handleError(Exception $exception) { if (class_exists('Titon\\Debug\\Debugger')) { Debugger::logException($exception); } // Get the controller try { $controller = Registry::get('titon.controller'); if (!$controller instanceof Controller) { throw new MissingControllerException(); } } catch (Exception $e) { $controller = new ErrorController(); $controller->initialize(); } // And the view try { $view = Registry::get('titon.view'); if (!$view instanceof ViewInterface) { throw new MissingViewException(); } } catch (Exception $e) { $view = new View(); $view->addHelper('html', new HtmlHelper()); $view->addHelper('block', new BlockHelper()); $view->addHelper('asset', new AssetHelper(['webroot' => $this->getWebroot()])); } $controller->setView($view); $controller->setRequest($this->getRequest()); $controller->setResponse($this->getResponse()); $this->emit('mvc.preError', [$this, $controller, $exception]); $response = $controller->renderError($exception); $this->emit('mvc.postError', [$this, $controller, $exception, &$response]); $this->getResponse()->body($response)->respond(); $this->emit('mvc.onShutdown', [$this]); exit; }