Skip to content

Commit

Permalink
Merge pull request #30 from creative-commoners/pulls/2.0/middleware
Browse files Browse the repository at this point in the history
 API Replace RequestFilter with HTTPMiddleware
  • Loading branch information
robbieaverill authored Dec 12, 2017
2 parents 2550692 + 22c573d commit 329a6a0
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 21 deletions.
1 change: 0 additions & 1 deletion .upgrade.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ mappings:
SpellProvider: SilverStripe\SpellCheck\Data\SpellProvider
SpellController: SilverStripe\SpellCheck\Handling\SpellController
SpellException: SilverStripe\SpellCheck\Handling\SpellException
SpellRequestFilter: SilverStripe\SpellCheck\Handling\SpellRequestFilter
HunSpellProvider: SilverStripe\SpellCheck\Providers\HunSpellProvider
HunSpellProviderTest: SilverStripe\SpellCheck\Tests\HunSpellProviderTest
SpellControllerTest: SilverStripe\SpellCheck\Tests\SpellControllerTest
6 changes: 3 additions & 3 deletions _config/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ SilverStripe\Core\Injector\Injector:
Name: spellcheckprocessors
---
SilverStripe\Core\Injector\Injector:
SilverStripe\Control\RequestProcessor:
SilverStripe\Control\Director:
properties:
filters:
- '%$SilverStripe\SpellCheck\Handling\SpellRequestFilter'
Middlewares:
SpellCheckMiddleware: '%$SilverStripe\SpellCheck\Handling\SpellCheckMiddleware'
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,16 @@
namespace SilverStripe\SpellCheck\Handling;

use SilverStripe\Control\HTTPRequest;
use SilverStripe\Control\HTTPResponse;
use SilverStripe\Control\RequestFilter;
use SilverStripe\Core\Config\Config;
use SilverStripe\Control\Middleware\HTTPMiddleware;
use SilverStripe\Core\Config\Configurable;
use SilverStripe\Forms\HTMLEditor\HTMLEditorConfig;
use SilverStripe\i18n\i18n;
use SilverStripe\Security\SecurityToken;

class SpellRequestFilter implements RequestFilter
class SpellCheckMiddleware implements HTTPMiddleware
{
use Configurable;

/**
* HTMLEditorConfig name to use
*
Expand All @@ -20,28 +21,34 @@ class SpellRequestFilter implements RequestFilter
*/
private static $editor = 'cms';

public function preRequest(HTTPRequest $request)
public function process(HTTPRequest $request, callable $delegate)
{
// Check languages to set
$languages = [];
foreach (SpellController::get_locales() as $locale) {
$languages[] = i18n::getData()->localeName($locale) . '=' . $locale;
}


// Set settings
$editor = Config::inst()->get(__CLASS__, 'editor');
$editor = static::config()->get('editor');
HTMLEditorConfig::get($editor)->enablePlugins('spellchecker');
HTMLEditorConfig::get($editor)->addButtonsToLine(2, 'spellchecker');
$token = SecurityToken::inst();
HTMLEditorConfig::get($editor)->setOption('spellchecker_rpc_url', $token->addToUrl('spellcheck/'));
HTMLEditorConfig::get($editor)->setOption('browser_spellcheck', false);
HTMLEditorConfig::get($editor)->setOption('spellchecker_languages', '+'.implode(', ', $languages));
return true;
HTMLEditorConfig::get($editor)->setOption(
'spellchecker_languages',
'+'.implode(', ', $this->getLanguages())
);

return $delegate($request);
}

public function postRequest(HTTPRequest $request, HTTPResponse $response)
/**
* Check languages to set
*
* @return array
*/
public function getLanguages()
{
return true;
$languages = [];
foreach (SpellController::get_locales() as $locale) {
$languages[] = i18n::getData()->localeName($locale) . '=' . $locale;
}
return $languages;
}
}

0 comments on commit 329a6a0

Please sign in to comment.