Skip to content

Commit

Permalink
module init
Browse files Browse the repository at this point in the history
  • Loading branch information
mzymela committed May 21, 2020
1 parent d2fc7b5 commit 3435ffa
Show file tree
Hide file tree
Showing 6 changed files with 207 additions and 0 deletions.
84 changes: 84 additions & 0 deletions Error/Maintenance.php
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;
}
}
56 changes: 56 additions & 0 deletions Plugin/HttpPlugin.php
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);
}
}
26 changes: 26 additions & 0 deletions composer.json
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\\\\": ""
}
}
}
15 changes: 15 additions & 0 deletions etc/di.xml
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>
13 changes: 13 additions & 0 deletions etc/module.xml
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>
13 changes: 13 additions & 0 deletions registration.php
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__
);

0 comments on commit 3435ffa

Please sign in to comment.