Source of file MaintenanceMode.php
Size: 2,058 Bytes - Last Modified: 2021-12-23T10:51:48+00:00
/var/www/docs.ssmods.com/process/src/src/MaintenanceMode.php
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586 | <?php /** * Ability to easily toggle maintenance mode via CLI. To run this command: * * sake dev/tasks/MaintenanceMode [on|off] * * * @package maintenancemode * * @author Patrick Nelson <pat@catchyour.com> * * @since 2015-10-08 */ namespace dljoseph\MaintenanceMode; use SilverStripe\Control\Director; use SilverStripe\Dev\BuildTask; use SilverStripe\SiteConfig\SiteConfig; class MaintenanceMode extends BuildTask { protected $title = 'Maintance Mode Task'; protected $description = 'Ability to easily toggle maintenance mode via CLI.'; protected $enabled = true; /** * @param \SilverStripe\Control\HTTPRequest $request */ public function run($request) { // Only allow execution from the command line (for simplicity). if (!Director::is_cli()) { echo '<p>Sorry, but this can only be run from the command line.</p>'; return; } try { // Get and validate desired maintenance mode setting. $get = $request->getVars(); if (empty($get['args'])) { throw new \Exception("Please provide an argument (e.g. 'on' or 'off').", 1); } $arg = strtolower(current($get['args'])); if ($arg != 'on' && $arg != 'off') { throw new \Exception("Invalid argument: '$arg' (expected 'on' or 'off')", 2); } // Get and write site configuration now. $config = SiteConfig::current_site_config(); $previous = (!empty($config->MaintenanceMode) ? 'on' : 'off'); $config->MaintenanceMode = ($arg == 'on'); $config->write(); // Output status and exit. if ($arg != $previous) { $this->output("Maintenance mode is now '$arg'."); } else { $this->output("NOTE: Maintenance mode was already '$arg' (nothing has changed)."); } } catch (\Exception $e) { $this->output('ERROR: '.$e->getMessage()); if ($e->getCode() <= 2) { $this->output('Usage: sake dev/tasks/MaintenanceMode [on|off]'); } } } #################### ## HELPER METHODS ## #################### /** * Output helper. * * @param $text */ protected function output($text) { echo "$text\n"; } } |