예제 #1
0
/**
* Tests the body method
*
*/
	public function testBody() {
		$response = new CakeResponse();
		$this->assertNull($response->body());
		$response->body('Response body');
		$this->assertEquals($response->body(), 'Response body');
		$this->assertEquals($response->body('Changed Body'), 'Changed Body');
	}
예제 #2
0
 /**
  * Test that beforeDispatcher replaces run id
  *
  * @return void
  */
 public function testReplaceRunId()
 {
     $filter = new XHProfDispatcher();
     $response = new CakeResponse();
     $response->body('Run id: %XHProfRunId%.');
     $event = new CakeEvent('DispatcherTest', $this, compact('response'));
     $filter->beforeDispatch($event);
     $this->assertSame($response, $filter->afterDispatch($event));
     $this->assertRegExp('/^Run id: [0-9a-f]{13}\\.$/', $response->body());
 }
예제 #3
0
/**
 * Handles (fakes) redirects for Ajax requests using requestAction()
 *
 * @param Controller $controller A reference to the controller
 * @param string|array $url A string or array containing the redirect location
 * @param mixed $status HTTP Status for redirect
 * @param boolean $exit
 * @return void
 */
	public function beforeRedirect($controller, $url, $status = null, $exit = true) {
		if (!$this->request->is('ajax')) {
			return;
		}
		foreach ($_POST as $key => $val) {
			unset($_POST[$key]);
		}
		if (is_array($url)) {
			$url = Router::url($url + array('base' => false));
		}
		if (!empty($status)) {
			$statusCode = $this->response->httpCodes($status);
			$code = key($statusCode);
			$this->response->statusCode($code);
		}
		$this->response->body($this->requestAction($url, array('return', 'bare' => false)));
		$this->response->send();
		$this->_stop();
	}
 /**
  * Handles (fakes) redirects for Ajax requests using requestAction()
  * Modifies the $_POST and $_SERVER['REQUEST_METHOD'] to simulate a new GET request.
  *
  * @param Controller   $controller A reference to the controller
  * @param string|array $url        A string or array containing the redirect location
  * @param int|array    $status     HTTP Status for redirect
  * @param bool         $exit       Whether to exit script, defaults to `true`.
  *
  * @return void
  */
 public function beforeRedirect(Controller $controller, $url, $status = NULL, $exit = TRUE)
 {
     if (!$this->request->is('ajax')) {
         return;
     }
     if (empty($url)) {
         return;
     }
     $_SERVER['REQUEST_METHOD'] = 'GET';
     foreach ($_POST as $key => $val) {
         unset($_POST[$key]);
     }
     if (is_array($url)) {
         $url = Router::url($url + array('base' => FALSE));
     }
     if (!empty($status)) {
         $statusCode = $this->response->httpCodes($status);
         $code = key($statusCode);
         $this->response->statusCode($code);
     }
     $this->response->body($this->requestAction($url, array('return', 'bare' => FALSE)));
     $this->response->send();
     $this->_stop();
 }
예제 #5
0
 /**
  * Instantiates the correct view class, hands it its data, and uses it to render the view output.
  *
  * @param string $view View to use for rendering
  * @param string $layout Layout to use
  * @return CakeResponse A response object containing the rendered view.
  * @triggers Controller.beforeRender $this
  * @link http://book.cakephp.org/2.0/en/controllers.html#Controller::render
  */
 public function render($view = null, $layout = null)
 {
     $event = new CakeEvent('Controller.beforeRender', $this);
     $this->getEventManager()->dispatch($event);
     if ($event->isStopped()) {
         $this->autoRender = false;
         return $this->response;
     }
     if (!empty($this->uses) && is_array($this->uses)) {
         foreach ($this->uses as $model) {
             list($plugin, $className) = pluginSplit($model);
             $this->request->params['models'][$className] = compact('plugin', 'className');
         }
     }
     $this->View = $this->_getViewObject();
     $models = ClassRegistry::keys();
     foreach ($models as $currentModel) {
         $currentObject = ClassRegistry::getObject($currentModel);
         if ($currentObject instanceof Model) {
             $className = get_class($currentObject);
             list($plugin) = pluginSplit(App::location($className));
             $this->request->params['models'][$currentObject->alias] = compact('plugin', 'className');
             $this->View->validationErrors[$currentObject->alias] =& $currentObject->validationErrors;
         }
     }
     $this->autoRender = false;
     $this->response->body($this->View->render($view, $layout));
     return $this->response;
 }
 /**
  * Dispatches and invokes given Request, handing over control to the involved controller. If the controller is set
  * to autoRender, via Controller::$autoRender, then Dispatcher will render the view.
  *
  * Actions in CakePHP can be any public method on a controller, that is not declared in Controller.  If you
  * want controller methods to be public and in-accessible by URL, then prefix them with a `_`.
  * For example `public function _loadPosts() { }` would not be accessible via URL.  Private and protected methods
  * are also not accessible via URL.
  *
  * If no controller of given name can be found, invoke() will throw an exception.
  * If the controller is found, and the action is not found an exception will be thrown.
  *
  * @param CakeRequest $request Request object to dispatch.
  * @param CakeResponse $response Response object to put the results of the dispatch into.
  * @param array $additionalParams Settings array ("bare", "return") which is melded with the GET and POST params
  * @return string|void if `$request['return']` is set then it returns response body, null otherwise
  * @throws MissingControllerException When the controller is missing.
  */
 public function dispatch(CakeRequest $request, CakeResponse $response, $additionalParams = array())
 {
     $beforeEvent = new CakeEvent('Dispatcher.beforeDispatch', $this, compact('request', 'response', 'additionalParams'));
     $this->getEventManager()->dispatch($beforeEvent);
     $request = $beforeEvent->data['request'];
     if ($beforeEvent->result instanceof CakeResponse) {
         if (isset($request->params['return'])) {
             return $response->body();
         }
         $response->send();
         return;
     }
     $controller = $this->_getController($request, $response);
     if (!$controller instanceof Controller) {
         throw new MissingControllerException(array('class' => Inflector::camelize($request->params['controller']) . 'Controller', 'plugin' => empty($request->params['plugin']) ? null : Inflector::camelize($request->params['plugin'])));
     }
     $response = $this->_invoke($controller, $request, $response);
     if (isset($request->params['return'])) {
         return $response->body();
     }
     $afterEvent = new CakeEvent('Dispatcher.afterDispatch', $this, compact('request', 'response'));
     $this->getEventManager()->dispatch($afterEvent);
     $afterEvent->data['response']->send();
 }
예제 #7
0
 /**
  * Processes the feed and rebuilds an array based on the feeds type (RSS, RDF, Atom).
  *
  * @access protected
  * @param CakeResponse $response
  * @param array $query
  * @param string $source
  * @return boolean
  */
 protected function _process($response, $query, $source)
 {
     $feed = TypeConverter::toArray($response->body());
     $clean = array();
     if (!empty($query['root']) && !empty($feed[$query['feed']['root']])) {
         $items = $feed[$query['feed']['root']];
     } else {
         // Rss
         if (isset($feed['channel']) && isset($feed['channel']['item'])) {
             $items = $feed['channel']['item'];
             // Rdf
         } else {
             if (isset($feed['item'])) {
                 $items = $feed['item'];
                 // Atom
             } else {
                 if (isset($feed['entry'])) {
                     $items = $feed['entry'];
                     // Xml
                 } else {
                     $items = $feed;
                 }
             }
         }
     }
     if (empty($items) || !is_array($items)) {
         return $clean;
     }
     // Gather elements
     $elements = array('title', 'guid' => array('guid', 'id'), 'date' => array('date', 'pubDate', 'published', 'updated'), 'link' => array('link', 'origLink'), 'image' => array('image', 'thumbnail'), 'author' => array('author', 'writer', 'editor', 'user'), 'source' => array('source'), 'description' => array('description', 'desc', 'summary', 'content', 'text'));
     if (is_array($query['fields'])) {
         $elements = array_merge_recursive($elements, $query['fields']);
     }
     // Loop the feed
     foreach ($items as $item) {
         $data = array();
         foreach ($elements as $element => $keys) {
             if (is_numeric($element)) {
                 $element = $keys;
                 $keys = array($keys);
             }
             if (isset($keys['attributes'])) {
                 $attributes = $keys['attributes'];
                 unset($keys['attributes']);
             } else {
                 $attributes = array('value', 'href', 'src', 'name', 'label');
             }
             if (isset($keys['keys'])) {
                 $keys = $keys['keys'];
             }
             foreach ($keys as $key) {
                 if (isset($item[$key]) && empty($data[$element])) {
                     $value = $this->_extract($item[$key], $attributes);
                     if (!empty($value)) {
                         $data[$element] = $value;
                         break;
                     }
                 }
             }
         }
         if (empty($data['link'])) {
             trigger_error(sprintf('Feed %s does not have a valid link element.', $source), E_USER_NOTICE);
             continue;
         }
         if (empty($data['source']) && $source) {
             $data['source'] = (string) $source;
         }
         $sort = null;
         if (isset($data[$query['feed']['sort']])) {
             $sort = $data[$query['feed']['sort']];
         }
         if (!$sort) {
             if ($query['feed']['sort'] == 'date' && isset($data['date'])) {
                 $sort = strtotime($data['date']);
             } else {
                 $sort = microtime();
             }
         }
         if (!empty($data)) {
             $clean[$sort] = $data;
         }
     }
     return $clean;
 }
예제 #8
0
/**
 * Instantiates the correct view class, hands it its data, and uses it to render the view output.
 *
 * @param string $view View to use for rendering
 * @param string $layout Layout to use
 * @return CakeResponse A response object containing the rendered view.
 * @link http://book.cakephp.org/2.0/en/controllers.html#Controller::render
 */
	public function render($view = null, $layout = null) {
		$this->beforeRender();
		$this->Components->trigger('beforeRender', array(&$this));

		$viewClass = $this->viewClass;
		if ($this->viewClass != 'View') {
			list($plugin, $viewClass) = pluginSplit($viewClass, true);
			$viewClass = $viewClass . 'View';
			App::uses($viewClass, $plugin . 'View');
		}

		$View = new $viewClass($this);

		if (!empty($this->uses)) {
			foreach ($this->uses as $model) {
				list($plugin, $className) = pluginSplit($model);
				$this->request->params['models'][$className] = compact('plugin', 'className');
			}
		} if (!empty($this->modelClass) && ($this->uses === false || $this->uses === array())) {
			$this->request->params['models'][$this->modelClass] = array('plugin' => $this->plugin, 'className' => $this->modelClass);
		}

		$models = ClassRegistry::keys();
		foreach ($models as $currentModel) {
			$currentObject = ClassRegistry::getObject($currentModel);
			if (is_a($currentObject, 'Model')) {
				$className = get_class($currentObject);
				list($plugin) = pluginSplit(App::location($className));
				$this->request->params['models'][$currentObject->alias] = compact('plugin', 'className');
				$View->validationErrors[$currentObject->alias] =& $currentObject->validationErrors;
			}
		}

		$this->autoRender = false;
		$this->View = $View;
		$this->response->body($View->render($view, $layout));
		return $this->response;
	}
 public function render($view = null, $layout = null)
 {
     $response = new CakeResponse();
     $response->body('');
     return $response;
 }
예제 #10
0
 /**
  * Shows a message to the user for $pause seconds, then redirects to $url.
  * Uses flash.ctp as the default layout for the message.
  * Does not work if the current debug level is higher than 0.
  *
  * @param string $message Message to display to the user
  * @param mixed $url Relative string or array-based URL to redirect to after the time expires
  * @param integer $pause Time to show the message
  * @param string $layout Layout you want to use, defaults to 'flash'
  * @return void Renders flash layout
  * @link http://book.cakephp.org/view/983/flash
  */
 public function flash($message, $url, $pause = 1, $layout = 'flash')
 {
     $this->autoRender = false;
     $this->set('url', Router::url($url));
     $this->set('message', $message);
     $this->set('pause', $pause);
     $this->set('page_title', $message);
     $this->response->body($this->render(false, $layout));
 }