-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5 from HavokInspiration/view-mode
Add a View mode
- Loading branch information
Showing
13 changed files
with
456 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
<?php | ||
/** | ||
* Copyright (c) Yves Piquel (http://www.havokinspiration.fr) | ||
* | ||
* Licensed under The MIT License | ||
* Redistributions of files must retain the above copyright notice. | ||
* | ||
* @copyright Copyright (c) Yves Piquel (http://www.havokinspiration.fr) | ||
* @link http://github.com/HavokInspiration/wrench | ||
* @license http://www.opensource.org/licenses/mit-license.php MIT License | ||
*/ | ||
namespace Wrench\Mode; | ||
|
||
use Cake\Core\Configure; | ||
use Cake\Network\Request; | ||
use Cake\Network\Response; | ||
|
||
/** | ||
* `Output` Maintenance Mode. | ||
* When used, it will send the content of the configured file as a response | ||
*/ | ||
class View extends Mode | ||
{ | ||
|
||
/** | ||
* Default config | ||
* | ||
* - `code` : The status code to be sent along with the response. | ||
* - `view` : Array of parameters to pass to the View class constructor. Only the following options are supported : | ||
* - `className` : Fully qualified class name of the View class to use. Default to AppView | ||
* - `templatePath` : Path to the template you wish to display (relative to your ``src/Template`` directory). | ||
* You can use plugin dot notation. | ||
* - `template` : Template name to use. Default to "template". | ||
* - `plugin` : Theme where to find the layout and template | ||
* - `theme` : Same thing than plugin | ||
* - `layout` : Layout name to use. Default to "default" | ||
* - `layoutPath` : Path to the layout you wish to display (relative to your ``src/Template/Layout``directory). | ||
* You can use plugin dot notation. Default to "Layout" | ||
* All other options are not supported (they might work though) | ||
* - `headers` : Additional headers to be set with the response | ||
* | ||
* @var array | ||
*/ | ||
protected $_defaultConfig = [ | ||
'code' => 503, | ||
'view' => [ | ||
'className' => 'App\View\AppView', | ||
'templatePath' => null, | ||
'template' => 'maintenance', | ||
'plugin' => null, | ||
'theme' => null, | ||
'layout' => null, | ||
'layoutPath' => null | ||
], | ||
'headers' => [] | ||
]; | ||
|
||
/** | ||
* {@inheritDoc} | ||
* | ||
* Will set the location where to redirect the request with the specified code | ||
* and optional additional headers. | ||
*/ | ||
public function process(Request $request, Response $response) | ||
{ | ||
$this->_backwardCompatibility(); | ||
|
||
$className = $this->config('view.className'); | ||
if (empty($className)) { | ||
$className = 'App\View\AppView'; | ||
} | ||
|
||
$viewConfig = $this->config('view') ?: []; | ||
$view = new $className($request, $response, null, $viewConfig); | ||
$response->body($view->render()); | ||
$response->statusCode($this->config('code')); | ||
|
||
$headers = $this->config('headers'); | ||
if (!empty($headers)) { | ||
$response->header($headers); | ||
} | ||
return $response; | ||
} | ||
|
||
/** | ||
* Generate correct View constructor parameter key if CakePHP version | ||
* is below 3.1 where important changes were introduced regarding naming | ||
* Also compensate for a fix that is not existant prior to 3.0.5 in the InstanceConfigTrait | ||
* | ||
* @return void | ||
*/ | ||
protected function _backwardCompatibility() | ||
{ | ||
if ($this->config('view') === null) { | ||
$this->config('view', $this->_defaultConfig['view']); | ||
} | ||
|
||
if (version_compare(Configure::version(), '3.1.0', '<')) { | ||
$config = $this->config('view'); | ||
$config['view'] = $this->config('view.template'); | ||
$config['viewPath'] = $this->config('view.templatePath'); | ||
$this->config('view', $config); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,185 @@ | ||
<?php | ||
/** | ||
* Copyright (c) Yves Piquel (http://www.havokinspiration.fr) | ||
* | ||
* Licensed under The MIT License | ||
* Redistributions of files must retain the above copyright notice. | ||
* | ||
* @copyright Copyright (c) Yves Piquel (http://www.havokinspiration.fr) | ||
* @link http://github.com/HavokInspiration/wrench | ||
* @license http://www.opensource.org/licenses/mit-license.php MIT License | ||
*/ | ||
namespace Wrench\Test\TestCase\Mode; | ||
|
||
use Cake\Core\Configure; | ||
use Cake\Core\Plugin; | ||
use Cake\Event\Event; | ||
use Cake\Network\Request; | ||
use Cake\TestSuite\TestCase; | ||
use Wrench\Routing\Filter\MaintenanceModeFilter; | ||
|
||
class ViewTest extends TestCase | ||
{ | ||
|
||
/** | ||
* @inheritDoc | ||
*/ | ||
public function setup() | ||
{ | ||
Plugin::load(['TestPlugin']); | ||
} | ||
|
||
/** | ||
* @inheritDoc | ||
*/ | ||
public function tearDown() | ||
{ | ||
parent::tearDown(); | ||
Plugin::unload('TestPlugin'); | ||
Configure::write('Wrench.enable', false); | ||
} | ||
|
||
/** | ||
* Test the View filter mode without params | ||
* @return void | ||
*/ | ||
public function testViewModeNoParams() | ||
{ | ||
Configure::write('Wrench.enable', true); | ||
|
||
$filter = new MaintenanceModeFilter([ | ||
'mode' => [ | ||
'className' => 'Wrench\Mode\View' | ||
] | ||
]); | ||
|
||
$request = new Request(); | ||
$response = $this->getMock('Cake\Network\Response', ['statusCode']); | ||
$response->expects($this->once()) | ||
->method('statusCode') | ||
->with(503); | ||
|
||
$result = $filter->beforeDispatch(new Event('name', null, ['request' => $request, 'response' => $response])); | ||
$expected = "Layout Header\nThis is an element<div>test</div>This app is undergoing maintenanceLayout Footer"; | ||
$this->assertEquals($expected, $result->body()); | ||
} | ||
|
||
/** | ||
* Test the View with custom params | ||
* @return void | ||
*/ | ||
public function testViewModeCustomParams() | ||
{ | ||
Configure::write('Wrench.enable', true); | ||
|
||
$filter = new MaintenanceModeFilter([ | ||
'mode' => [ | ||
'className' => 'Wrench\Mode\View', | ||
'config' => [ | ||
'code' => 404, | ||
'view' => [ | ||
'templatePath' => 'Maintenance', | ||
'layout' => 'maintenance', | ||
'layoutPath' => 'Maintenance' | ||
] | ||
] | ||
] | ||
]); | ||
|
||
$request = new Request(); | ||
$response = $this->getMock('Cake\Network\Response', ['statusCode']); | ||
$response->expects($this->once()) | ||
->method('statusCode') | ||
->with(404); | ||
|
||
$result = $filter->beforeDispatch(new Event('name', null, ['request' => $request, 'response' => $response])); | ||
$expected = "Maintenance Header\nI'm in a sub-directoryMaintenance Footer"; | ||
$this->assertEquals($expected, $result->body()); | ||
} | ||
|
||
/** | ||
* Test the View with custom params and plugins | ||
* @return void | ||
*/ | ||
public function testViewModeCustomParamsPlugin() | ||
{ | ||
Configure::write('Wrench.enable', true); | ||
|
||
$filter = new MaintenanceModeFilter([ | ||
'mode' => [ | ||
'className' => 'Wrench\Mode\View', | ||
'config' => [ | ||
'code' => 404, | ||
'view' => [ | ||
'template' => 'maintenance', | ||
'templatePath' => 'Maintenance', | ||
'layout' => 'maintenance', | ||
'plugin' => 'TestPlugin', | ||
'layoutPath' => 'Maintenance', | ||
] | ||
] | ||
] | ||
]); | ||
|
||
$request = new Request(); | ||
$response = $this->getMock('Cake\Network\Response', ['statusCode']); | ||
$response->expects($this->once()) | ||
->method('statusCode') | ||
->with(404); | ||
|
||
$result = $filter->beforeDispatch(new Event('name', null, ['request' => $request, 'response' => $response])); | ||
$expected = "Plugin Maintenance Header\nI'm in a plugin sub-directoryPlugin Maintenance Footer"; | ||
$this->assertEquals($expected, $result->body()); | ||
|
||
$filter = new MaintenanceModeFilter([ | ||
'mode' => [ | ||
'className' => 'Wrench\Mode\View', | ||
'config' => [ | ||
'code' => 404, | ||
'view' => [ | ||
'template' => 'maintenance', | ||
'templatePath' => 'Maintenance', | ||
'layout' => 'maintenance', | ||
'theme' => 'TestPlugin', | ||
'layoutPath' => 'Maintenance', | ||
] | ||
] | ||
] | ||
]); | ||
|
||
$request = new Request(); | ||
$response = $this->getMock('Cake\Network\Response', ['statusCode']); | ||
$response->expects($this->once()) | ||
->method('statusCode') | ||
->with(404); | ||
|
||
$result = $filter->beforeDispatch(new Event('name', null, ['request' => $request, 'response' => $response])); | ||
$expected = "Plugin Maintenance Header\nI'm in a plugin sub-directoryPlugin Maintenance Footer"; | ||
$this->assertEquals($expected, $result->body()); | ||
|
||
$filter = new MaintenanceModeFilter([ | ||
'mode' => [ | ||
'className' => 'Wrench\Mode\View', | ||
'config' => [ | ||
'code' => 404, | ||
'view' => [ | ||
'template' => 'TestPlugin.maintenance', | ||
'templatePath' => 'Maintenance', | ||
'layout' => 'TestPlugin.maintenance', | ||
'layoutPath' => 'Maintenance', | ||
] | ||
] | ||
] | ||
]); | ||
|
||
$request = new Request(); | ||
$response = $this->getMock('Cake\Network\Response', ['statusCode']); | ||
$response->expects($this->once()) | ||
->method('statusCode') | ||
->with(404); | ||
|
||
$result = $filter->beforeDispatch(new Event('name', null, ['request' => $request, 'response' => $response])); | ||
$expected = "Plugin Maintenance Header\nI'm in a plugin sub-directoryPlugin Maintenance Footer"; | ||
$this->assertEquals($expected, $result->body()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 15 additions & 0 deletions
15
tests/test_app/App/Plugin/TestPlugin/src/Template/Layout/Maintenance/maintenance.ctp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<?php | ||
/** | ||
* Copyright (c) Yves Piquel (http://www.havokinspiration.fr) | ||
* | ||
* Licensed under The MIT License | ||
* Redistributions of files must retain the above copyright notice. | ||
* | ||
* @copyright Copyright (c) Yves Piquel (http://www.havokinspiration.fr) | ||
* @link http://github.com/HavokInspiration/wrench | ||
* @license http://www.opensource.org/licenses/mit-license.php MIT License | ||
*/ | ||
?> | ||
Plugin Maintenance Header | ||
<?= $this->fetch('content'); ?> | ||
Plugin Maintenance Footer |
Oops, something went wrong.