-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
207 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
<?php | ||
/** | ||
* Alpine_Maintenance | ||
* | ||
* @copyright Copyright (c) 2020 Alpine Consulting, Inc (www.alpineinc.com) | ||
* @author Michal Zymela ([email protected]) | ||
*/ | ||
|
||
namespace Alpine\Maintenance\Error; | ||
|
||
use Magento\Framework\App\Bootstrap; | ||
use Magento\Framework\App\Filesystem\DirectoryList; | ||
use Magento\Framework\App\Response\Http; | ||
use Magento\Framework\Filesystem; | ||
use Magento\Framework\Filesystem\Io\File as FileReader; | ||
use Magento\Framework\ObjectManagerInterface; | ||
use Magento\Framework\Phrase; | ||
|
||
class Maintenance | ||
{ | ||
/** @var int */ | ||
const RESPONSE_CODE = 503; | ||
|
||
/** | ||
* @var ObjectManagerInterface | ||
*/ | ||
private $objectManager; | ||
/** | ||
* @var Filesystem | ||
*/ | ||
private $filesystem; | ||
/** | ||
* @var FileReader | ||
*/ | ||
private $fileReader; | ||
|
||
/** | ||
* Maintenance constructor. | ||
* | ||
* @param Filesystem $filesystem | ||
* @param FileReader $fileReader | ||
*/ | ||
public function __construct( | ||
Filesystem $filesystem, | ||
FileReader $fileReader | ||
) { | ||
$this->filesystem = $filesystem; | ||
$this->fileReader = $fileReader; | ||
$objectManagerFactory = Bootstrap::createObjectManagerFactory(BP, $_SERVER); | ||
$this->objectManager = $objectManagerFactory->create($_SERVER); | ||
} | ||
|
||
/** | ||
* Render maintenance page | ||
*/ | ||
public function renderPage() | ||
{ | ||
$response = $this->objectManager->create(Http::class); | ||
$response->setHttpResponseCode(self::RESPONSE_CODE); | ||
$response->setBody( | ||
$this->setTemplate() | ||
); | ||
|
||
$response->sendResponse(); | ||
} | ||
|
||
/** | ||
* @return bool|Phrase|string | ||
*/ | ||
private function setTemplate() | ||
{ | ||
$templatePath = $this->filesystem | ||
->getDirectoryRead(DirectoryList::PUB) | ||
->getAbsolutePath('maintenance/index.html'); | ||
|
||
$template = $this->fileReader->read($templatePath); | ||
|
||
if (!$template) { | ||
return __('Maintenance html file not found. Upload your index.html file under pub/maintenance directory.'); | ||
} | ||
|
||
return $template; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
<?php | ||
/** | ||
* Alpine_Maintenance | ||
* | ||
* @copyright Copyright (c) 2020 Alpine Consulting, Inc (www.alpineinc.com) | ||
* @author Michal Zymela ([email protected]) | ||
*/ | ||
|
||
namespace Alpine\Maintenance\Plugin; | ||
|
||
use Alpine\Maintenance\Error\Maintenance; | ||
use Exception; | ||
use Magento\Framework\App\Bootstrap; | ||
use Magento\Framework\App\Http; | ||
|
||
class HttpPlugin | ||
{ | ||
|
||
/** | ||
* @var Maintenance | ||
*/ | ||
private $maintenance; | ||
|
||
/** | ||
* HttpPlugin constructor. | ||
* | ||
* @param Maintenance $maintenance | ||
*/ | ||
public function __construct(Maintenance $maintenance) | ||
{ | ||
$this->maintenance = $maintenance; | ||
} | ||
|
||
/** | ||
* @param Http $subject | ||
* @param callable $proceed | ||
* @param Bootstrap $bootstrap | ||
* @param Exception $exception | ||
* | ||
* @return bool | ||
*/ | ||
public function aroundCatchException( | ||
Http $subject, | ||
callable $proceed, | ||
Bootstrap $bootstrap, | ||
Exception $exception | ||
) { | ||
if (!$bootstrap->isDeveloperMode() && $bootstrap->getErrorCode() === Bootstrap::ERR_MAINTENANCE) { | ||
$this->maintenance->renderPage(); | ||
|
||
return true; | ||
} | ||
|
||
return $proceed($bootstrap, $exception); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
{ | ||
"name": "alpine/maintenance", | ||
"description": "Custom maintenance page from html file placed in pub/maintenance directory", | ||
"require": { | ||
"php": "~7.1.3||~7.2.0||~7.3.0", | ||
"magento/magento-composer-installer": "*", | ||
"magento/framework": "102.0.*" | ||
}, | ||
"type": "magento2-module", | ||
"version": "1.0.0", | ||
"license": "OSL-3.0", | ||
"authors": [ | ||
{ | ||
"name": "Alpine Consulting, Inc", | ||
"email": "[email protected]" | ||
} | ||
], | ||
"autoload": { | ||
"files": [ | ||
"registration.php" | ||
], | ||
"psr-4": { | ||
"Alpine\\Maintenance\\\\": "" | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<?xml version="1.0"?> | ||
<!-- | ||
/** | ||
* Alpine_Maintenance | ||
* | ||
* @copyright Copyright (c) 2020 Alpine Consulting, Inc (www.alpineinc.com) | ||
* @author Michal Zymela ([email protected]) | ||
*/ | ||
--> | ||
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd"> | ||
<type name="Magento\Framework\App\Http"> | ||
<plugin name="alpine_maintenance_http_plugin" type="Alpine\Maintenance\Plugin\HttpPlugin" sortOrder="99" /> | ||
</type> | ||
</config> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<?xml version="1.0"?> | ||
<!-- | ||
/** | ||
* Alpine_Maintenance | ||
* | ||
* @copyright Copyright (c) 2020 Alpine Consulting, Inc (www.alpineinc.com) | ||
* @author Michal Zymela ([email protected]) | ||
*/ | ||
--> | ||
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd"> | ||
<module name="Alpine_Maintenance" setup_version="1.0.0"/> | ||
</config> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<?php | ||
/** | ||
* Alpine_Maintenance | ||
* | ||
* @copyright Copyright (c) 2020 Alpine Consulting, Inc (www.alpineinc.com) | ||
* @author Michal Zymela ([email protected]) | ||
*/ | ||
|
||
\Magento\Framework\Component\ComponentRegistrar::register( | ||
\Magento\Framework\Component\ComponentRegistrar::MODULE, | ||
'Alpine_Maintenance', | ||
__DIR__ | ||
); |