Skip to content
This repository has been archived by the owner on Jan 10, 2019. It is now read-only.

Commit

Permalink
Update
Browse files Browse the repository at this point in the history
  • Loading branch information
oanhnn committed Jun 22, 2017
1 parent 1289c4e commit 286b62a
Show file tree
Hide file tree
Showing 14 changed files with 472 additions and 153 deletions.
3 changes: 0 additions & 3 deletions .gitattributes
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,6 @@
/.gitattributes export-ignore
/.gitignore export-ignore
/.travis.yml export-ignore
/.php_cs export-ignore
/phpunit.xml.dist export-ignore
/phpunit.xml export-ignore
/phpcs.xml.dist export-ignore
/phpcs.xml export-ignore
/CHANGELOG.md export-ignore
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,13 @@ All Notable changes to project will be documented in this file
### Security
- Nothing

## 0.2.0 - 2016-07-15

### Changed

- [x] Changed to use config instead of content maintenance's lock file
- [x] Changed command shell to enable and disable maintenance mode

## 0.1.0 - 2016-04-28

First release with features:
Expand Down
24 changes: 17 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,18 +42,28 @@ Add this line to `config/bootstrap.php` file
Plugin::load('Lemon/CakePlugin/MaintenanceMode', ['bootstrap' => true]);
```

### Configure plugin

Modified config:

```
Cake\Core\Configure::write('maintenance_mode', [
'lockFile' => TMP . 'maintenance.php',
'viewClass' => 'App\View\AppView',
'templatePath' => 'Pages',
'templateFile' => 'maintenance',
'templateLayout' => 'default',
'viewVars' => [],
]);
```

### Enable maintenance mode

```
$ bin/cake maintenance_mode enable
```

Using option `--force` to enable maintenance mode with default config:

- View class: `\App\View\AppView`
- Templatce: `Pages/maintenance.ctp`
- Layout: `default`
- Time: a hour from now
Using option `--force` to enable maintenance mode without confirmation prompt:

### Disable maintenance mode

Expand All @@ -78,7 +88,7 @@ If you would like to help take a look at the [list of issues][issues].

License
---
This project is released under the MIT License.
This project is released under the MIT License.
Copyright © 2015-2016 LemonPHP Team.


Expand Down
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
"cakephp/cakephp": "~3.0"
},
"require-dev": {
"phpunit/phpunit": "~4.8"
"phpunit/phpunit": "~4.8",
"squizlabs/php_codesniffer": "^2.6"
}
}
16 changes: 14 additions & 2 deletions config/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,21 @@
* with this source code in the file LICENSE.
*/

use Cake\Core\Configure;
use Cake\Routing\DispatcherFactory;
use Lemon\CakePlugin\MaintenanceMode\Routing\Filter\MaintenanceModeFilter;

defined('MAINTENANCE_CONFIG_FILE') || define('MAINTENANCE_CONFIG_FILE', TMP . 'maintenance.php');

DispatcherFactory::add(MaintenanceModeFilter::class);

$defaults = [
'lockFile' => TMP . 'maintenance.php',
'viewClass' => 'App\View\AppView',
'templatePath' => 'Pages',
'templateFile' => 'maintenance',
'templateLayout' => 'default',
'viewVars' => [],
];

Configure::write('maintenance_mode', array_merge($defaults, (array) Configure::read('maintenance_mode')));

unset($defaults);
14 changes: 9 additions & 5 deletions src/Routing/Filter/MaintenanceModeFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

namespace Lemon\CakePlugin\MaintenanceMode\Routing\Filter;

use Cake\Core\Configure;
use Cake\Event\Event;
use Cake\Routing\DispatcherFilter;

Expand Down Expand Up @@ -52,27 +53,30 @@ public function implementedEvents()
*/
public function handle(Event $event)
{
if (is_file(MAINTENANCE_CONFIG_FILE) && is_readable(MAINTENANCE_CONFIG_FILE)) {
$config = Configure::read('maintenance_mode');
if (is_file($config['lockFile'])) {
// stop event
$event->stopPropagation();

$request = $event->data['request'];
$response = $event->data['response'];
$config = require MAINTENANCE_CONFIG_FILE;

$viewClass = $config['viewClass'];

/*@var $view \Cake\View\View */
$view = new $viewClass($request, $response);
$view->templatePath($config['templatePath']);
$view->template($config['templateFile']);
$view->layout($config['templateLayout']);

$view->set('startAt', \Cake\I18n\Time::createFromFormat('YmdHis', $config['startAt']));
$view->set('endAt', \Cake\I18n\Time::createFromFormat('YmdHis', $config['endAt']));
// Set view vars
foreach ($config['viewVars'] as $key => $value) {
$view->set($key, $value);
}

$response->body($view->render());

return $response;
}
}
}
}
76 changes: 76 additions & 0 deletions src/Shell/DownShell.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<?php
/**
* This file is part of `lemonphp/cakeplugin-maintenance-mode` project.
*
* (c) 2015-2016 LemonPHP Team
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/

namespace Lemon\CakePlugin\MaintenanceMode\Shell;

use Cake\Core\Configure;
use Cake\Console\Shell;

/**
* Down Shell
*
* Enable maintenance mode
*
* @author Oanh Nguyen <oanhnn.bk@gmail.com>
*/
class DownShell extends Shell
{
/**
* Gets the option parser instance and configures it.
*
* @return \Cake\Console\ConsoleOptionParser
*/
public function getOptionParser()
{
$parser = parent::getOptionParser();
$parser
->description(__d('cake_console', 'Enable maintenance mode'))
->addOption('force', [
'short' => 'f',
'help' => __d('cake_console', 'Run without confirmation prompt.'),
'boolean' => true,
'default' => false,
])
;

return $parser;
}

/**
* Enable maintenance mode
*/
public function main()
{
$config = Configure::read('maintenance_mode');

// check maintenance mode is enabled
if (is_file($config['lockFile'])) {
$this->success(__d('cake_console', 'Maintenance mode is already enabled'));
return $this->_stop(0);
}

// confirm: Do you want disable maintenance mode?
if (!$this->param('force')) {
$confirm = $this->in(__d('cake_console', 'Do you want enable maintenance mode?'), ['y', 'n'], 'n');
if ('y' !== $confirm) {
return $this->_stop(0);
}
}

// Save to file
if (false !== file_put_contents($config['lockFile'], date('YmdHis'))) {
$this->success(__d('cake_console', 'Maintenance mode is enabled'));
$this->_stop(0);
} else {
$this->success(__d('cake_consle', 'Maintenance mode can\'t enable'));
$this->_stop(1);
}
}
}
127 changes: 0 additions & 127 deletions src/Shell/MaintenanceModeShell.php

This file was deleted.

0 comments on commit 286b62a

Please sign in to comment.