Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[5.3] Add offline mode support in the API #34151

Draft
wants to merge 24 commits into
base: 5.3-dev
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
d2b7d88
Add offline mode support in the API
wilsonge May 23, 2021
efcedb3
Ensure API is not cached
wilsonge May 23, 2021
4ba043b
Fix year
wilsonge May 25, 2021
e25c35f
Fix year
wilsonge May 25, 2021
13ebabc
Remove my useless code
wilsonge May 25, 2021
8501b40
Fix handler
wilsonge May 25, 2021
9586572
Fix exception path
wilsonge May 29, 2021
dc9beb1
Update libraries/src/Error/JsonApi/OfflineWebsiteExceptionHandler.php
zero-24 May 30, 2021
e0727da
Merge branch '4.0-dev' into feature/offline-mode-api
zero-24 May 30, 2021
e1f17c8
remove InstallLanguageExceptionHandler
alikon Jun 4, 2021
487f726
Merge pull request #66 from alikon/patch-81
wilsonge Jun 4, 2021
c968a2d
Remove unused use
wilsonge Jun 4, 2021
12b9267
Update libraries/src/Error/JsonApi/OfflineWebsiteExceptionHandler.php
wilsonge Aug 19, 2021
dc95d45
Update libraries/src/Error/JsonApi/OfflineWebsiteExceptionHandler.php
wilsonge Aug 19, 2021
c17055e
Update libraries/src/Error/JsonApi/OfflineWebsiteExceptionHandler.php
wilsonge Aug 19, 2021
c8f755e
Update libraries/src/Application/ApiApplication.php
wilsonge Aug 19, 2021
fb97862
Merge branch '4.1-dev' into feature/offline-mode-api
chmst Jan 31, 2022
4c13992
Merge tag 'psr12anchor' into psr12/merge/34151
joomla-bot Jun 27, 2022
aeb1f9c
Phase 1 convert BRANCH to PSR-12
joomla-bot Jun 27, 2022
71ea1cd
Phase 2 convert BRANCH to PSR-12
joomla-bot Jun 27, 2022
5733d95
Merge tag 'psr12final' into psr12/merge/34151
joomla-bot Jun 27, 2022
0700bec
Merge branch '4.2-dev' into feature/offline-mode-api
laoneo Oct 21, 2022
d46dbf1
Merge branch '4.3-dev' into feature/offline-mode-api
laoneo Oct 21, 2022
f51a4b7
Merge branch '4.3-dev' into feature/offline-mode-api
laoneo Oct 22, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions libraries/src/Application/ApiApplication.php
Original file line number Diff line number Diff line change
Expand Up @@ -146,8 +146,32 @@ public function addFormatMap($contentHeader, $format)
*/
protected function render()
{
// Trigger the onBeforeRender event.
PluginHelper::importPlugin('system');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you remove this event triggering, so we can merge it in 4.3 and make a new pr on 5.0. I fear these events will have some undesired effect, so it is better to add it in 5.0 and document it properly.

$this->triggerEvent('onBeforeRender');

/**
* Check we aren't in offline mode. In which case for users who can't access the site the API is disabled
* and we won't show anything!
*/
if ($this->get('offline') && !$this->getIdentity()->authorise('core.login.offline')) {
$offlineMessage = '';

if ($this->get('display_offline_message', true) == true) {
$offlineMessage = $this->get('offline_message');
}

throw new Exception\OfflineWebsiteException($offlineMessage);
}

// Render the document
$this->setBody($this->document->render($this->allowCache()));

// Trigger the onAfterRender event.
$this->triggerEvent('onAfterRender');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as comment above.


// Mark afterRender in the profiler.
JDEBUG ? $this->profiler->mark('afterRender') : null;
}

/**
Expand Down
19 changes: 19 additions & 0 deletions libraries/src/Application/Exception/OfflineWebsiteException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

/**
* Joomla! Content Management System
*
* @copyright (C) 2021 Open Source Matters, Inc. <https://www.joomla.org>
* @license GNU General Public License version 2 or later; see LICENSE.txt
*/

namespace Joomla\CMS\Application\Exception;

/**
* Exception class for a website that is in offline mode!
*
* @since __DEPLOY_VERSION__
*/
class OfflineWebsiteException extends \RuntimeException
{
}
61 changes: 61 additions & 0 deletions libraries/src/Error/JsonApi/OfflineWebsiteExceptionHandler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php

/**
* Joomla! Content Management System
*
* @copyright (C) 2021 Open Source Matters, Inc. <https://www.joomla.org>
* @license GNU General Public License version 2 or later; see LICENSE.txt
*/

namespace Joomla\CMS\Error\JsonApi;

use Exception;
use Joomla\CMS\Application\Exception\OfflineWebsiteException;
use Tobscure\JsonApi\Exception\Handler\ExceptionHandlerInterface;
use Tobscure\JsonApi\Exception\Handler\ResponseBag;

/**
* Handler for the site being in offline mode
*
* @since __DEPLOY_VERSION__
*/
class OfflineWebsiteExceptionHandler implements ExceptionHandlerInterface
{
/**
* If the exception handler is able to format a response for the provided exception,
* then the implementation should return true.
*
* @param \Exception $e The exception to be handled
*
* @return boolean
*
* @since __DEPLOY_VERSION__
*/
public function manages(Exception $e)
{
return $e instanceof OfflineWebsiteException;
}

/**
* Handle the provided exception.
*
* @param Exception $e The exception being handled
*
* @return \Tobscure\JsonApi\Exception\Handler\ResponseBag
*
* @since __DEPLOY_VERSION__
*/
public function handle(Exception $e)
{
$status = 503;
$error = ['title' => $e->getMessage()];

$code = $e->getCode();

if ($code) {
$error['code'] = $code;
}

return new ResponseBag($status, [$error]);
}
}
2 changes: 2 additions & 0 deletions libraries/src/Error/Renderer/JsonapiRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use Joomla\CMS\Error\JsonApi\InvalidRouteExceptionHandler;
use Joomla\CMS\Error\JsonApi\NotAcceptableExceptionHandler;
use Joomla\CMS\Error\JsonApi\NotAllowedExceptionHandler;
use Joomla\CMS\Error\JsonApi\OfflineWebsiteExceptionHandler;
use Joomla\CMS\Error\JsonApi\ResourceNotFoundExceptionHandler;
use Joomla\CMS\Error\JsonApi\SaveExceptionHandler;
use Joomla\CMS\Error\JsonApi\SendEmailExceptionHandler;
Expand Down Expand Up @@ -57,6 +58,7 @@ public function render(\Throwable $error): string
if ($error instanceof \Exception) {
$errors = new ErrorHandler();

$errors->registerHandler(new OfflineWebsiteExceptionHandler());
$errors->registerHandler(new InvalidRouteExceptionHandler());
$errors->registerHandler(new AuthenticationFailedExceptionHandler());
$errors->registerHandler(new NotAcceptableExceptionHandler());
Expand Down