作者:niallmccrudde
项目:zf
/**
* Encode data as JSON, disable layouts, and set response header
*
* If $keepLayouts is true, does not disable layouts.
*
* @param mixed $data
* @param bool $keepLayouts
* NOTE: if boolean, establish $keepLayouts to true|false
* if array, admit params for Zend_Json::encode as enableJsonExprFinder=>true|false
* this array can contains a 'keepLayout'=>true|false
* that will not be passed to Zend_Json::encode method but will be used here
* @return string|void
*/
public function direct($data = null, $keepLayouts = false)
{
if ($data == null) {
throw new \InvalidArgumentException('JSON: missing argument. $data is required in json($data, $keepLayouts = false)');
}
$options = array();
if (is_array($keepLayouts))
{
$options = $keepLayouts;
$keepLayouts = (array_key_exists('keepLayouts', $keepLayouts))
? $keepLayouts['keepLayouts']
: false;
unset($options['keepLayouts']);
}
$data = \Zend\Json\Json::encode($data, null, $options);
if (!$keepLayouts) {
$layout = LayoutManager::getMvcInstance();
if ($layout instanceof LayoutManager) {
$layout->disableLayout();
}
}
$response = \Zend\Controller\Front::getInstance()->getResponse();
$response->setHeader('Content-Type', 'application/json');
return $data;
}
作者:alab100110
项目:zf
/**
* Retrieve layout object
*
* @return \Zend\Layout\Layout
*/
public function getLayout()
{
if (null === $this->_layout) {
$this->_layout = \Zend\Layout\Layout::startMvc($this->getOptions());
}
return $this->_layout;
}
作者:niallmccrudde
项目:zf
/**
* Sets up the fixture, for example, open a network connection.
* This method is called before a test is executed.
*
* @return void
*/
public function setUp()
{
\Zend\Layout\Layout::resetMvcInstance();
$this->front = Controller\Front::getInstance();
$this->front->resetInstance();
$this->front->addModuleDirectory(__DIR__ . '/../../_files/modules');
$this->broker = $this->front->getHelperBroker();
$this->layout = Layout\Layout::startMvc();
$this->helper = new Helper\ContextSwitch();
$this->broker->register('contextswitch', $this->helper);
$this->request = new \Zend\Controller\Request\Http();
$this->response = new \Zend\Controller\Response\Cli();
$this->front->setRequest($this->request)
->setResponse($this->response)
->addControllerDirectory(__DIR__);
$this->view = new \Zend\View\PhpRenderer();
$this->viewRenderer = $this->broker->load('viewRenderer');
$this->viewRenderer->setView($this->view);
$this->controller = new ContextSwitchTestController(
$this->request,
$this->response,
array()
);
$this->controller->setHelperBroker($this->broker);
$this->controller->setupContexts();
$this->helper->setActionController($this->controller);
}
作者:heiglandrea
项目:zf
/**
* Disable layouts and view renderer
*
* @return \Zend\Controller\Action\Helper\AutoComplete\AbstractAutoComplete Provides a fluent interface
*/
public function disableLayouts()
{
if (null !== ($layout = Layout::getMvcInstance())) {
$layout->disableLayout();
}
HelperBroker::getStaticHelper('viewRenderer')->setNoRender(true);
return $this;
}
作者:bradley-hol
项目:zf
/**
* Disable layouts and view renderer
*
* @return \Zend\Controller\Action\Helper\AutoComplete\AbstractAutoComplete Provides a fluent interface
*/
public function disableLayouts()
{
if (null !== ($layout = Layout::getMvcInstance())) {
$layout->disableLayout();
}
$this->getBroker()->load('viewRenderer')->setNoRender(true);
return $this;
}
作者:niallmccrudde
项目:zf
/**
* Sends mail to recipient(s) if log entries are present. Note that both
* plaintext and HTML portions of email are handled here.
*
* @return void
*/
public function shutdown()
{
// If there are events to mail, use them as message body. Otherwise,
// there is no mail to be sent.
if (empty($this->_eventsToMail)) {
return;
}
if ($this->_subjectPrependText !== null) {
// Tack on the summary of entries per-priority to the subject
// line and set it on the Zend_Mail object.
$numEntries = $this->_getFormattedNumEntriesPerPriority();
$this->_mail->setSubject(
"{$this->_subjectPrependText} ({$numEntries})");
}
// Always provide events to mail as plaintext.
$this->_mail->setBodyText(implode('', $this->_eventsToMail));
// If a Zend_Layout instance is being used, set its "events"
// value to the lines formatted for use with the layout.
if ($this->_layout) {
// Set the required "messages" value for the layout. Here we
// are assuming that the layout is for use with HTML.
$this->_layout->events =
implode('', $this->_layoutEventsToMail);
// If an exception occurs during rendering, convert it to a notice
// so we can avoid an exception thrown without a stack frame.
try {
$this->_mail->setBodyHtml($this->_layout->render());
} catch (\Exception $e) {
trigger_error(
"exception occurred when rendering layout; " .
"unable to set html body for message; " .
"message = {$e->getMessage()}; " .
"code = {$e->getCode()}; " .
"exception class = " . get_class($e),
E_USER_NOTICE);
}
}
// Finally, send the mail. If an exception occurs, convert it into a
// warning-level message so we can avoid an exception thrown without a
// stack frame.
try {
$this->_mail->send();
} catch (\Exception $e) {
trigger_error(
"unable to send log entries via email; " .
"message = {$e->getMessage()}; " .
"code = {$e->getCode()}; " .
"exception class = " . get_class($e),
E_USER_WARNING);
}
}
作者:alab100110
项目:zf
public function appBootstrap()
{
$this->frontController->setControllerDirectory(__DIR__ . '/_files/functional-test-app/controllers/');
// create an instance of the ErrorHandler so we can make sure it will point to our specially named ErrorController
$plugin = new \Zend\Controller\Plugin\ErrorHandler();
$plugin->setErrorHandlerController('zend-layout-functional-test-error')->setErrorHandlerAction('error');
$this->frontController->registerPlugin($plugin, 100);
\Zend\Layout\Layout::startMvc(__DIR__ . '/_files/functional-test-app/layouts/');
}
作者:rexma
项目:zf
/**
* Get layout object
*
* @return \Zend\Layout\Layout
*/
public function getLayout()
{
if (null === $this->_layout) {
$this->_layout = \Zend\Layout\Layout::getMvcInstance();
if (null === $this->_layout) {
// Implicitly creates layout object
$this->_layout = new \Zend\Layout\Layout();
}
}
return $this->_layout;
}
作者:rexma
项目:zf
/**
* Sets up the fixture, for example, open a network connection.
* This method is called before a test is executed.
*
* @return void
*/
public function setUp()
{
Layout::resetMvcInstance();
$this->request = new HTTPRequest();
$this->response = new CLIResponse();
$this->front = FrontController::getInstance();
$this->front->resetInstance();
$this->front->setRequest($this->request)->setResponse($this->response);
$this->broker = $this->front->getHelperBroker();
$this->viewRenderer = $this->broker->load('viewRenderer');
$this->layout = Layout::startMvc();
}
作者:heiglandrea
项目:zf
/**
* Sets up the fixture, for example, open a network connection.
* This method is called before a test is executed.
*
* @return void
*/
public function setUp()
{
Layout::resetMvcInstance();
HelperBroker::resetHelpers();
HelperBroker::setPluginLoader(null);
$this->request = new HTTPRequest();
$this->response = new CLIResponse();
$this->front = FrontController::getInstance();
$this->front->resetInstance();
$this->front->setRequest($this->request)->setResponse($this->response);
$this->viewRenderer = HelperBroker::getStaticHelper('viewRenderer');
$this->layout = Layout::startMvc();
}
作者:stunt
项目:zf
/**
* Sets up the fixture, for example, open a network connection.
* This method is called before a test is executed.
*
* @return void
*/
public function setUp()
{
$this->markTestSkipped('Skipped until DOJO is converted to Zend\\Dojo');
\Zend\Layout\Layout::resetMvcInstance();
HelperBroker\HelperBroker::resetHelpers();
HelperBroker\HelperBroker::setPluginLoader(null);
$this->request = new \Zend\Controller\Request\HTTP();
$this->response = new \Zend\Controller\Response\Cli();
$this->front = \Zend\Controller\Front::getInstance();
$this->front->resetInstance();
$this->front->setRequest($this->request)->setResponse($this->response);
$this->viewRenderer = HelperBroker\HelperBroker::getStaticHelper('viewRenderer');
$this->layout = Layout\Layout::startMvc();
}
作者:rafalwrzeszc
项目:zf
/**
* Encode data as JSON, disable layouts, and set response header
*
* If $keepLayouts is true, does not disable layouts.
*
* @param mixed $data
* @param bool $keepLayouts
* NOTE: if boolean, establish $keepLayouts to true|false
* if array, admit params for Zend_Json::encode as enableJsonExprFinder=>true|false
* this array can contains a 'keepLayout'=>true|false
* that will not be passed to Zend_Json::encode method but will be used here
* @return string|void
*/
public function __invoke($data, $keepLayouts = false)
{
$options = array();
if (is_array($keepLayouts)) {
$options = $keepLayouts;
$keepLayouts = array_key_exists('keepLayouts', $keepLayouts) ? $keepLayouts['keepLayouts'] : false;
unset($options['keepLayouts']);
}
$data = JsonFormatter::encode($data, null, $options);
if (!$keepLayouts && $this->layout instanceof Layout) {
$this->layout->disableLayout();
}
if ($this->response instanceof Response) {
$headers = $this->response->headers();
$headers->addHeaderLine('Content-Type', 'application/json');
}
return $data;
}
作者:bradley-hol
项目:zf
/**
* Encode data as JSON, disable layouts, and set response header
*
* If $keepLayouts is true, does not disable layouts.
*
* @param mixed $data
* @param bool $keepLayouts
* NOTE: if boolean, establish $keepLayouts to true|false
* if array, admit params for Zend_Json::encode as enableJsonExprFinder=>true|false
* this array can contains a 'keepLayout'=>true|false
* that will not be passed to Zend_Json::encode method but will be used here
* @return string|void
*/
public function __invoke($data, $keepLayouts = false)
{
$options = array();
if (is_array($keepLayouts)) {
$options = $keepLayouts;
$keepLayouts = array_key_exists('keepLayouts', $keepLayouts) ? $keepLayouts['keepLayouts'] : false;
unset($options['keepLayouts']);
}
$data = \Zend\Json\Json::encode($data, null, $options);
if (!$keepLayouts) {
$layout = LayoutManager::getMvcInstance();
if ($layout instanceof LayoutManager) {
$layout->disableLayout();
}
}
$response = \Zend\Controller\Front::getInstance()->getResponse();
$response->setHeader('Content-Type', 'application/json');
return $data;
}
作者:stunt
项目:zf
/**
* Sets up the fixture, for example, open a network connection.
* This method is called before a test is executed.
*
* @return void
*/
public function setUp()
{
\Zend\Layout\Layout::resetMvcInstance();
HelperBroker\HelperBroker::resetHelpers();
$this->front = Controller\Front::getInstance();
$this->front->resetInstance();
$this->front->addModuleDirectory(dirname(__FILE__) . '/../../_files/modules');
$this->layout = Layout\Layout::startMvc();
$this->helper = new Helper\ContextSwitch();
HelperBroker\HelperBroker::addHelper($this->helper);
$this->request = new \Zend\Controller\Request\HTTP();
$this->response = new \Zend\Controller\Response\Cli();
$this->front->setRequest($this->request)->setResponse($this->response)->addControllerDirectory(dirname(__FILE__));
$this->view = new \Zend\View\View();
$this->viewRenderer = HelperBroker\HelperBroker::getStaticHelper('viewRenderer');
$this->viewRenderer->setView($this->view);
$this->controller = new ContextSwitchTestController($this->request, $this->response, array());
$this->controller->setupContexts();
$this->helper->setActionController($this->controller);
}
作者:bradley-hol
项目:zf
/**
* Prepare data for autocompletion
*
* @param mixed $data
* @param boolean $keepLayouts
* @return string
*/
public function prepareAutoCompletion($data, $keepLayouts = false)
{
if (!$data instanceof DojoData) {
$items = array();
foreach ($data as $key => $value) {
$items[] = array('label' => $value, 'name' => $value);
}
$data = new DojoData('name', $items);
}
if (!$keepLayouts) {
$this->getBroker()->load('viewRenderer')->setNoRender(true);
$layout = Layout::getMvcInstance();
if ($layout instanceof Layout) {
$layout->disableLayout();
}
}
$response = FrontController::getInstance()->getResponse();
$response->setHeader('Content-Type', 'application/json');
return $data->toJson();
}
作者:stunt
项目:zf
/**
* Sets up the fixture, for example, open a network connection.
* This method is called before a test is executed.
*
* @return void
*/
public function setUp()
{
if (isset($_SERVER['HTTP_X_REQUESTED_WITH'])) {
unset($_SERVER['HTTP_X_REQUESTED_WITH']);
}
\Zend\Layout\Layout::resetMvcInstance();
HelperBroker\HelperBroker::resetHelpers();
HelperBroker\HelperBroker::addPrefix('Zend\\Controller\\Action\\Helper');
$this->front = \Zend\Controller\Front::getInstance();
$this->front->resetInstance();
$this->front->addModuleDirectory(dirname(__FILE__) . '/../../_files/modules');
$this->layout = Layout\Layout::startMvc();
$this->helper = new \Zend\Controller\Action\Helper\AjaxContext();
$this->request = new \Zend\Controller\Request\HTTP();
$this->response = new \Zend\Controller\Response\Cli();
$this->front->setRequest($this->request)->setResponse($this->response);
$this->view = new \Zend\View\View();
$this->view->addHelperPath(dirname(__FILE__) . '/../../../../../library/Zend/View/Helper/');
$this->viewRenderer = HelperBroker\HelperBroker::getStaticHelper('viewRenderer');
$this->viewRenderer->setView($this->view);
$this->controller = new AjaxContextTestController($this->request, $this->response, array());
$this->helper->setActionController($this->controller);
}
作者:stunt
项目:zf
/**
* Initialize context detection and switching
*
* @param mixed $format
* @throws \Zend\Controller\Action\Exception
* @return void
*/
public function initContext($format = null)
{
$this->_currentContext = null;
$controller = $this->getActionController();
$request = $this->getRequest();
$action = $request->getActionName();
// Return if no context switching enabled, or no context switching
// enabled for this action
$contexts = $this->getActionContexts($action);
if (empty($contexts)) {
return;
}
// Return if no context parameter provided
if (!($context = $request->getParam($this->getContextParam()))) {
if ($format === null) {
return;
}
$context = $format;
$format = null;
}
// Check if context allowed by action controller
if (!$this->hasActionContext($action, $context)) {
return;
}
// Return if invalid context parameter provided and no format or invalid
// format provided
if (!$this->hasContext($context)) {
if (empty($format) || !$this->hasContext($format)) {
return;
}
}
// Use provided format if passed
if (!empty($format) && $this->hasContext($format)) {
$context = $format;
}
$suffix = $this->getSuffix($context);
$this->_getViewRenderer()->setViewSuffix($suffix);
$headers = $this->getHeaders($context);
if (!empty($headers)) {
$response = $this->getResponse();
foreach ($headers as $header => $content) {
$response->setHeader($header, $content);
}
}
if ($this->getAutoDisableLayout()) {
$layout = \Zend\Layout\Layout::getMvcInstance();
if (null !== $layout) {
$layout->disableLayout();
}
}
if (null !== ($callback = $this->getCallback($context, self::TRIGGER_INIT))) {
if (is_string($callback) && method_exists($this, $callback)) {
$this->{$callback}();
} elseif (is_string($callback) && function_exists($callback)) {
$callback();
} elseif (is_array($callback)) {
call_user_func($callback);
} else {
throw new Action\Exception(sprintf('Invalid context callback registered for context "%s"', $context));
}
}
$this->_currentContext = $context;
}
作者:alab100110
项目:zf
public function testJsonHelperDoesNotDisableLayoutsWhenKeepLayoutFlagTrue()
{
$layout = Layout\Layout::startMvc();
$this->assertTrue($layout->isEnabled());
$data = $this->helper->direct(array('foobar'), true);
$this->assertTrue($layout->isEnabled());
}
作者:hjr
项目:zf
/**
* Static method for initialization with MVC support
*
* @param string|array|\Zend\Config\Config $options
* @return \Zend\Layout\Layout
*/
public static function startMvc($options = null)
{
if (null === self::$_mvcInstance) {
self::$_mvcInstance = new self($options, true);
}
if (is_string($options)) {
self::$_mvcInstance->setLayoutPath($options);
} elseif (is_array($options) || $options instanceof Config\Config) {
self::$_mvcInstance->setOptions($options);
}
return self::$_mvcInstance;
}
作者:bradley-hol
项目:zf
/**
* Get layout object
*
* @return \Zend\Layout\Layout
*/
public function getLayoutInstance()
{
if (null === $this->_layout) {
if (null === ($this->_layout = \Zend\Layout\Layout::getMvcInstance())) {
$this->_layout = new \Zend\Layout\Layout();
}
}
return $this->_layout;
}