PHP code example of ennacx / cake-middlewares

1. Go to this page and download the library: Download ennacx/cake-middlewares 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/ */

    

ennacx / cake-middlewares example snippets



// in src/Application.php

use Ennacx\CakeMiddlewares\Enum\MaintenanceCheckMethod;
use Ennacx\CakeMiddlewares\Middleware\MaintenanceMiddleware;

class Application extends BaseApplication
{
    public function middleware(MiddlewareQueue $middlewareQueue): MiddlewareQueue
    {
        $middleware
            // Catch any exceptions in the lower layers,
            // and make an error page/response
            ->add(new ErrorHandlerMiddleware(Configure::read('Error'), $this))

            // Handle plugin/theme assets like CakePHP normally does.
            ->add(new AssetMiddleware([
                'cacheTime' => Configure::read('Asset.cacheTime'),
            ]))

            // Add routing middleware.
            // If you have a large number of routes connected, turning on routes
            // caching in production could improve performance.
            // See https://github.com/CakeDC/cakephp-cached-routing
            ->add(new RoutingMiddleware($this))

            // Add Maintenance middleware
            ->add(new MaintenanceMiddleware([
                'thru_ip_list' => ['127.0.0.1/32'],

                'check_method' => \Ennacx\CakeMiddlewares\Enum\MaintenanceCheckMethod::FILE,

                'check_file_path' => TMP . 'maintenance',

                'is_maintenance' => true,

                'check_date_from'   => null,
                'check_date_to'     => null,
                'check_date_format' => 'Y-m-d H:i:s',

                'maintenance_message' => 'Sorry, we are currently performing server maintenance.',

                'use_proxy' => true
            ]))

            // Parse various types of encoded request bodies so that they are
            // available as array through $request->getData()
            // https://book.cakephp.org/4/en/controllers/middleware.html#body-parser-middleware
            ->add(new BodyParserMiddleware())

            // Cross Site Request Forgery (CSRF) Protection Middleware
            // https://book.cakephp.org/4/en/security/csrf.html#cross-site-request-forgery-csrf-middleware
            ->add(new CsrfProtectionMiddleware([
                'httponly' => true,
            ]));

        return $middleware;
    }
}


/*
 * Specifies the path of the flag file.
 * If not specified or null, the path of "TMP . 'maintenance'" will be referenced.
 */
$flagFilePath = TMP . 'maintenance_flag_file';

// Get instance
$sw = \Ennacx\CakeMiddlewares\Utils\MaintenanceSwitch::getInstance($flagFilePath);

/*
 * To maintenance mode
 * 
 * If it returns false, it is already in maintenance mode.
 * If the file creation fails, throws \RuntimeException.
 */
$result = $sw->on();

/*
 * To Operation mode
 * 
 * If it returns false, it is already in operation mode.
 * If the file remove fails, throws \RuntimeException.
 */
$result = $sw->off();

/*
 * Switching between Maintenance and Operation
 * 
 * Executes one of the on() and off() methods depending on the existence of a maintenance file.
 * If the file creation/remove fails, throws \RuntimeException.
 */
$result = $sw->toggle();

/*
 * Check for the existence of the maintenance flag file.
 */
$maintenance = $sw->isMaintenance();