* it may be easier to work directly with `getCtrl()->run(...)` which
* handles inputs/outputs in a more abstract fashion.
*
- * @param object $ctrl
+ * @param \Civi\Setup\UI\SetupController $ctrl
* A web controller.
*/
public static function run($ctrl) {
$method = $_SERVER['REQUEST_METHOD'];
- list ($headers, $body) = $ctrl->run($method, ($method === 'GET' ? $_GET : $_POST));
- foreach ($headers as $k => $v) {
+
+ /** @var \Civi\Setup\UI\SetupResponse $response */
+ $response = $ctrl->run($method, ($method === 'GET' ? $_GET : $_POST));
+
+ self::send($ctrl, $response);
+ }
+
+ /**
+ * @param \Civi\Setup\UI\SetupController $ctrl
+ * @param \Civi\Setup\UI\SetupResponse $response
+ */
+ public static function send($ctrl, $response) {
+ http_response_code($response->code);
+ foreach ($response->headers as $k => $v) {
header("$k: $v");
}
- echo $body;
+
+ /** @var \Civi\Setup\Model $model */
+ $model = \Civi\Setup::instance()->getModel();
+
+ if ($response->isComplete) {
+ echo $response->body;
+ }
+ else {
+ $pageVars = [
+ 'pageAssets' => $response->assets,
+ 'pageTitle' => $response->title,
+ 'pageBody' => $response->body,
+ 'shortLangCode' => \CRM_Core_I18n_PseudoConstant::shortForLong($model->lang),
+ 'textDirection' => (\CRM_Core_I18n::isLanguageRTL($model->lang) ? 'rtl' : 'ltr'),
+ ];
+
+ echo $ctrl->render($ctrl->getResourcePath('page.tpl.php'), $pageVars);
+ }
}
}
* Ex: 'GET' or 'POST'.
* @param array $fields
* List of any HTTP GET/POST fields.
- * @return array
- * The HTTP headers and response text.
- * [0 => array $headers, 1 => string $body].
+ * @return SetupResponse
*/
public function run($method, $fields = array()) {
$this->setup->getDispatcher()->dispatch('civi.setupui.run', new UIBootEvent($this, $method, $fields));
* Ex: 'GET' or 'POST'.
* @param array $fields
* List of any HTTP GET/POST fields.
- * @return array
- * The HTTP headers and response text.
- * [0 => array $headers, 1 => string $body].
+ * @return SetupResponse
*/
public function runStart($method, $fields) {
$checkInstalled = $this->setup->checkInstalled();
];
// $body = "<pre>" . htmlentities(print_r(['method' => $method, 'urls' => $this->urls, 'data' => $fields], 1)) . "</pre>";
- $body = $this->renderPage(ts('CiviCRM Installer'), $this->render($tplFile, $tplVars));
-
- return array(array(), $body);
+ return $this->createPage(ts('CiviCRM Installer'), $this->render($tplFile, $tplVars));
}
/**
* Ex: 'GET' or 'POST'.
* @param array $fields
* List of any HTTP GET/POST fields.
- * @return array
- * The HTTP headers and response text.
- * [0 => array $headers, 1 => string $body].
+ * @return SetupResponse
*/
public function runInstall($method, $fields) {
$checkInstalled = $this->setup->checkInstalled();
$this->setup->getDispatcher()->dispatch('civi.setupui.boot', new UIBootEvent($this, $method, $fields));
}
+ /**
+ * @param string $message
+ * @param string $title
+ * @return SetupResponse
+ */
public function createError($message, $title = 'Error') {
- return [
- [],
- $this->renderPage($title, sprintf('<h1>%s</h1>\n%s', htmlentities($title), htmlentities($message))),
- ];
+ return $this->createPage($title, sprintf('<h1>%s</h1>\n%s', htmlentities($title), htmlentities($message)));
}
/**
* @param string $title
* @param string $body
- * @return string
+ * @return SetupResponse
*/
- public function renderPage($title, $body) {
+ public function createPage($title, $body) {
/** @var \Civi\Setup\Model $model */
$model = $this->setup->getModel();
- $pageAssets = [
+ $r = new SetupResponse();
+ $r->code = 200;
+ $r->headers = [];
+ $r->isComplete = FALSE;
+ $r->title = $title;
+ $r->body = $body;
+ $r->assets = [
['type' => 'script-url', 'url' => $this->getUrl('jquery.js')],
['type' => 'script-code', 'code' => 'window.csj$ = jQuery.noConflict();'],
['type' => 'style-url', 'url' => $this->urls['res'] . "template.css"],
['type' => 'style-url', 'url' => $this->getUrl('font-awesome.css')],
];
+
if (\CRM_Core_I18n::isLanguageRTL($model->lang)) {
- $pageAssets[] = ['type' => 'style-url', 'url' => $this->urls['res'] . "template-rtl.css"];
+ $r->assets[] = ['type' => 'style-url', 'url' => $this->urls['res'] . "template-rtl.css"];
}
- $pageVars = [
- 'pageAssets' => $pageAssets,
- 'pageTitle' => $title,
- 'pageBody' => $body,
- 'shortLangCode' => \CRM_Core_I18n_PseudoConstant::shortForLong($model->lang),
- 'textDirection' => (\CRM_Core_I18n::isLanguageRTL($model->lang) ? 'rtl' : 'ltr'),
- ];
- return $this->render($this->getResourcePath('page.tpl.php'), $pageVars);
+ return $r;
}
/**
}
/**
- * @return array
+ * @return SetupResponse
*/
private function renderFinished() {
$m = $this->setup->getModel();
$tplFile = $this->getResourcePath('finished.' . $m->cms . '.php');
if (file_exists($tplFile)) {
- return [[], $this->renderPage(ts('CiviCRM Installed'), $this->render($tplFile))];
+ return $this->createPage(ts('CiviCRM Installed'), $this->render($tplFile));
}
else {
return $this->createError("Installation succeeded. However, the final page ($tplFile) was not available.");
--- /dev/null
+<?php
+namespace Civi\Setup\UI;
+
+/**
+ * This represents a response from the Setup UI.
+ *
+ * Previously, responses where an array of the form:
+ * [0 => array $headers, 1 => string $body].
+ *
+ * This implements \ArrayAccess for backward compatibility.
+ */
+class SetupResponse implements \ArrayAccess {
+
+ /**
+ * @var bool
+ *
+ * TRUE if the body represents a fully formed HTML page.
+ * FALSE if the body is a fragment of an HTML page.
+ */
+ public $isComplete = TRUE;
+
+ /**
+ * @var array
+ * Ex: ['Content-Type': 'text/html']
+ */
+ public $headers = [];
+
+ /**
+ * @var array
+ * Ex: $assets[0] = ['type' => 'script-url', 'url' => 'http://foobar'];
+ */
+ public $assets = [];
+
+ /**
+ * @var string
+ * Ex: '<h1>Hello world</h1>'
+ */
+ public $body = '';
+
+ /**
+ * @var string|null
+ * The title of the response page (if it's an HTML response).
+ */
+ public $title = NULL;
+
+ /**
+ * @var int
+ */
+ public $code = 200;
+
+ /**
+ * @var array
+ * Array(int $oldPos => string $newName).
+ */
+ protected $oldFieldMap;
+
+ /**
+ * SetupResponse constructor.
+ */
+ public function __construct() {
+ $this->oldFieldMap = [
+ 0 => 'headers',
+ 1 => 'body',
+ ];
+ }
+
+ public function offsetExists($offset) {
+ return isset($this->oldFieldMap[$offset]);
+ }
+
+ public function &offsetGet($offset) {
+ if (isset($this->oldFieldMap[$offset])) {
+ $field = $this->oldFieldMap[$offset];
+ return $this->{$field};
+ }
+ else {
+ return NULL;
+ }
+ }
+
+ public function offsetSet($offset, $value) {
+ if (isset($this->oldFieldMap[$offset])) {
+ $field = $this->oldFieldMap[$offset];
+ $this->{$field} = $value;
+ }
+ }
+
+ public function offsetUnset($offset) {
+ if (isset($this->oldFieldMap[$offset])) {
+ $field = $this->oldFieldMap[$offset];
+ unset($this->{$field});
+ }
+ }
+
+}