Skip to content
Draft
Show file tree
Hide file tree
Changes from 7 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 @@ -144,8 +144,32 @@ public function addFormatMap($contentHeader, $format)
*/
protected function render()
{
// Trigger the onBeforeRender event.
PluginHelper::importPlugin('system');
$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');

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

/**
Expand Down
22 changes: 22 additions & 0 deletions libraries/src/Application/Exception/OfflineWebsiteException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?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;

use Throwable;

\defined('JPATH_PLATFORM') or die;

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

namespace Joomla\CMS\Error\JsonApi;

\defined('JPATH_PLATFORM') or die;

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 4.0.0
*/
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 4.0.0
*/
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 4.0.0
*/
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 @@ -17,6 +17,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 @@ -55,6 +56,7 @@ public function render(\Throwable $error): string
{
$errors = new ErrorHandler;

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