PHP code example of juliangut / zf-maintenance

1. Go to this page and download the library: Download juliangut/zf-maintenance library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

juliangut / zf-maintenance example snippets


use DateTime;

return array(
    'zf-maintenance' => array(
        // Strategy service to be used on maintenance
        'strategy' => 'ZfMaintenanceStrategy',

        // Template for the maintenance strategy
        'template' => 'zf-maintenance/maintenance',

        // Maintenance blocks access to application
        'block' => true,

        /*
         * Maintenance providers
         * Different means to activate maintenance mode
         */
        'providers' => array(
            'ZfMaintenanceConfigProvider' => array(
                'active' => false,
            ),
            'ZfMaintenanceConfigScheduledProvider' => array(
                'start'   => '2020-01-01 00:00:00',
                'end'     => new DateTime('2020-01-02 05:00:00'),
                'message' => 'Undergoing scheduled maintenance tasks',
            ),
            'ZfMaintenanceEnvironmentProvider' => array(
                'variable' => 'zf-maintenance',
                'value'    => 'On',
            ),
            'ZfMaintenanceFileProvider' => array(
                'file'    => __DIR__ . '/maintenance',
                'message' => 'We are currently running maintenance proccesses',
            ),
            'ZfMaintenanceCrontabProvider' => array(
                'expression' => '0 0 1 * *', // @monthly
                'interval'   => 'PT1H', // 1 hour
                'message'    => 'We are currently running maintenance proccesses',
            ),
        ),

        /*
         * Exceptions to maintenance mode
         * Provides a way to bypass maintenance mode by fulfilling at least one of the conditions
         */
        'exclusions' => array(
            'ZfMaintenanceIpExclusion' => array(
                '127.0.0.1',    // Localhost
                '192.168.1.10', // Private network
            ),
            'ZfMaintenanceRouteExclusion' => array(
                'home',
                'admin',
            ),
        ),
    ),
);

$provider = new ConfigProvider();
$provider->setMessage('custom maintenance mode message');

use Jgut\Zf\Maintenance\Provider\ConfigProvider;

$provider = new ConfigProvider();
$provider->setActive(true);

use Jgut\Zf\Maintenance\Provider\EnvironmentProvider;

putenv('zf-maintenance=On');

$provider = new EnvironmentProvider();
$provider->setVar('zf-maintenance');
$provider->setValue('On');

use Jgut\Zf\Maintenance\Provider\FileProvider;

$provider = new FileProvider();
$provider->setFile(__DIR__ . '/maintenance_file');

use Jgut\Zf\Maintenance\Provider\ConfigScheduledProvider;
use DateTime;

$provider = new ConfigScheduledProvider();
$provider->setStart('2020-01-01 00:00:00');
$provider->setEnd(new DateTime('2020-01-01 05:00:00')),

use Jgut\Zf\Maintenance\Provider\CrontabProvider;

$provider = new CrontabProvider();
$provider->setExpression('@monthly'); // 0 0 1 * *
$provider->setInterval('PT1H'), // 1 hour

// Maintenance will be ON the 1st of every month at 0:00 during 1 hour

use Jgut\Zf\Maintenance\Exclusion\IpExclusion;
use Zend\Http\PhpEnvironment\RemoteAddress;

$excludedIps = array(
    '127.0.0.1',
    '192.168.1.10',
);

$exclusion = new IpExclusion($excludedIps, new RemoteAddress);

use Jgut\Zf\Maintenance\Exclusion\RouteExclusion;
use Zend\Mvc\Router\RouteMatch;

$excludedRoutes = array(
    'routeName',
    array(
        'controller' => 'controllerName',
    ),
    array(
        'controller' => 'controllerName',
        'action'     => 'actionName',
    ),
);

$exclusion = new RouteExclusion($excludedRoutes, new RouteMatch);

$maintenanceMessage = $this->maintenanceMessage();
if ($maintenanceMessage !== '') {
    sprintf('<div class="alert alert-info">%s</div>', $maintenanceMessage);
}

$maintenance = $this->scheduledMaintenance();
// Start or end can be null if not provided

/*
array(
    'start' => \DateTime,
    'end'   => \DateTime,
);
*/

php composer.phar 

php composer.phar