From 3435ffa6706f97ebd84224574778818bf75c9e22 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Zyme=C5=82a?= Date: Thu, 21 May 2020 08:39:08 +0200 Subject: [PATCH] module init --- Error/Maintenance.php | 84 +++++++++++++++++++++++++++++++++++++++++++ Plugin/HttpPlugin.php | 56 +++++++++++++++++++++++++++++ composer.json | 26 ++++++++++++++ etc/di.xml | 15 ++++++++ etc/module.xml | 13 +++++++ registration.php | 13 +++++++ 6 files changed, 207 insertions(+) create mode 100644 Error/Maintenance.php create mode 100644 Plugin/HttpPlugin.php create mode 100644 composer.json create mode 100644 etc/di.xml create mode 100644 etc/module.xml create mode 100644 registration.php diff --git a/Error/Maintenance.php b/Error/Maintenance.php new file mode 100644 index 0000000..f471726 --- /dev/null +++ b/Error/Maintenance.php @@ -0,0 +1,84 @@ +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; + } +} diff --git a/Plugin/HttpPlugin.php b/Plugin/HttpPlugin.php new file mode 100644 index 0000000..505eef1 --- /dev/null +++ b/Plugin/HttpPlugin.php @@ -0,0 +1,56 @@ +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); + } +} diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..2611bae --- /dev/null +++ b/composer.json @@ -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": "sales@alpineinc.com" + } + ], + "autoload": { + "files": [ + "registration.php" + ], + "psr-4": { + "Alpine\\Maintenance\\\\": "" + } + } +} diff --git a/etc/di.xml b/etc/di.xml new file mode 100644 index 0000000..7e64448 --- /dev/null +++ b/etc/di.xml @@ -0,0 +1,15 @@ + + + + + + + diff --git a/etc/module.xml b/etc/module.xml new file mode 100644 index 0000000..e3361cb --- /dev/null +++ b/etc/module.xml @@ -0,0 +1,13 @@ + + + + + diff --git a/registration.php b/registration.php new file mode 100644 index 0000000..4022876 --- /dev/null +++ b/registration.php @@ -0,0 +1,13 @@ +