Skip to content

carbontwelve/maintenance-screen

 
 

Repository files navigation

maintenance-screen

Latest Version on Packagist Software License Build Status Code Coverage Scrutinizer Code Quality Total Downloads

The "Maintenance mode" screen library

Install

Via Composer

$ composer require progminer/maintenance-screen

For using some included classes you also need to install more requrements:

  • For MaintenanceScreen\FileLoader\YamlFileLoader - Symfony Yaml:
$ composer require symfony/yaml ^4.0
  • For ProgMinerUtils\TemplateRenderer\TwigTemplateRenderer - Twig:
$ composer require twig/twig ^2.4

Usage

An instance of MaintenanceScreen\MaintenanceScreen consists of configurations array, MaintenanceScreen\TranslatorProvider\TranslatorProviderInterface instance and ProgMinerUtils\TemplateRenderer\TemplateRendererInterface instance.

This example step by step illustrates how works with the MaintenanceScreen\MaintenanceScreen.

Example

In first order you have to write uses, include a vendor/autoload.php (ommited), etc. Also you could make configuration array for MaintenanceScreen\MaintenanceScreen.

use MaintenanceScreen\ConfigurationLoader;
use MaintenanceScreen\MaintenanceScreen;

use MaintenanceScreen\TranslatorProvider\ArrayTranslatorProvider;

use ProgMinerUtils\TemplateRenderer\CallableTemplateRenderer;

$config = [
    'template_name'    => 'Default', // template name, not required
    'default_language' => 'en',      // uses if Accept-Language is not provided, not required
    'charset'          => 'utf-8'    // not required, charset for Response and TemplateRenderer
];

Here you have to make MaintenanceScreen\TranslatorProvider\TranslatorProviderInterface instance and you have two included methods:

  • Use translations from array (MaintenanceScreen\TranslatorProvider\ArrayTranslatorProvider class)
  • Use translations from config files (MaintenanceScreen\TranslatorProvider\FilesystemTranslatorProvider class)

A simple example for first method here:

$translatorsProvider = new ArrayTranslatorProvider([
    'en' => ['title' => 'Site in maintenance mode', 'text' => 'Site in maintenance mode'],
    'ru' => ['title' => 'Сайт в режиме техобслуживания', 'text' => 'Сайт в режиме техобслуживания']
]);

Also you can create class that implements MaintenanceScreen\TranslatorProvider\ITranslatorProvider.

Now you have to make a ProgMinerUtils\TemplateRenderer\TemplateRendererInterface instance, for example, ProgMinerUtils\TemplateRenderer\CallableTemplateRenderer:

$templateRenderer = new CallableTemplateRenderer([
    'Default' => function($vars) { ?>

<html lang="<?=$vars['lang']?>">
    <head><title><?=$vars['title']?></title></head>
    <body><h1><center><?=$vars['text']?></center></h1></body>
</html>

    <?php }
]);

And, finally, MaintenanceScreen\MaintenanceScreen instance:

$maintenanceScreen = new MaintenanceScreen($config, $translatorProvider, $templateRenderer);

When you have an instance of MaintenanceScreen\MaintenanceScreen you can render and/or send rendered Symfony\Component\HttpFoundation\Response:

  • Rendering:
$response = $maintenanceScreen->render();
  • Sending:
$maintenanceScreen->send();

Both methods have not required argument $request - instance of class Symfony\Component\HttpFoundation\Request. If it is not provided this methods calls a Symfony\Component\HttpFoundation\Request::createFromGlobals method for getting current request.

Todo

  • Add more file loaders

Contributing

Please see CONTRIBUTING for details.

Security

If you discover any security related issues, please email eridan200@mail.ru instead of using the issue tracker.

Credits

License

The MIT License (MIT). Please see License File for more information.

About

The "Maintenance mode" screen for your site

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • PHP 100.0%